8000 Review of the storage redesign (#196, #205, #206) · Issue #221 · kividbio/kividb · GitHub
[go: up one dir, main page]

Skip to content

Review of the storage redesign (#196, #205, #206) #221

Closed

Description

@Parikalp-Bhardwaj

Summary

I reviewed the memory-per-key work in #196 and #205, with #206 as related correctness context, and the storage changes in #197, #200, #208 and #209.

I reproduced the memory measurements against public master at e2d9415, identified one LFU access-metadata regression, and measured three remaining memory-efficiency opportunities. The other allocation boundary observation is a hypothesis requiring a benchmark, not a confirmed performance regression.

I did not find a matching public issue or PR for the findings below during this review. #202 and #208 concern slab behavior near maxmemory; finding 2 examines churn without a memory limit.

Environment and measurement method

  • Apple M3 Pro, 12 cores, 128 shards; macOS 26.5; Rust 1.97.1; release build with fat LTO, default features, Tokio I/O.
  • Compared b386ea3 (before Memory per key, step 1: keyspace shards behind ShardMap #196) with e2d9415 (public master at verification time).
  • Fresh server for each measurement; 16-byte padded keys where stated; independent random, incompressible values loaded with pipelined SET.
  • Per-key memory: (INFO memory used_memory after load − used_memory before load) / number of keys.
  • RSS: macOS physical footprint measured with vmmap -summary; this is indicative, not equivalent to a Linux/glibc RSS measurement.
  • For comparisons with the PR's 512-shard measurements, I matched the number of keys per shard on this 128-shard machine.

Memory per key — reproduced measurements

All units below are bytes per key unless otherwise noted.

Workload Before: b386ea3 used_memory Master: e2d9415 used_memory Master RSS/key
16 B key, 100 B value 198.0 146.9 158
Same, with TTL 284.0 171.3 191
Same, allkeys-lru / allkeys-lfu 250.5 146.9 ~170
Same, at equivalent 10M/512-shard load (2.5M keys here) 180.8 143.1 149
8 B key, 16 B value 118.0 66.9 77
64 B key, 100 B value 262.0 194.9 177
16 B key, 1000 B value 1094.0 1074.9 1068
16 B key, 1000 B value, TTL 1180.0 1083.3 1068

A storage-layout model matches each master row to approximately 0.1 B/key:

B/key = slab_class(12 + 12*has_ttl + key_length + value_length)
      + 9 * bucket_count / keys_per_shard
      + 8 * ttl_vector_capacity / ttl_key_count  [for TTL keys]

Relevant code at e2d9415: entry header src/store/entry.rs:75, TTL_ROOM at :101, Slot at :796, size classes src/value_slab.rs:148, TTL list src/store/shard_map.rs:288.

For a 16-byte key and 100-byte value, 146.9 B/key consists of 116 B payload, a 12 B header, and about 18.9 B of hash-table overhead.

Finding 1 — GETDEL on WRONGTYPE resets LFU frequency

Status: Confirmed behavioral regression introduced by #205. Classification: Correctness / LFU eviction metadata.

Source at e2d9415: src/commands/strings.rs:762 removes the key; the wrong-type path reinserts it at :776. The vacant insertion path in src/store/shard_map.rs:561 creates a new entry without retaining the old access word, unlike the replacement path at :554.

Reproduction: Run on an isolated instance configured with --maxmemory 1gb --maxmemory-policy allkeys-lfu:

HSET h f v
HGET h f
# Repeat HGET roughly 200 times to build a higher LFU frequency.
OBJECT FREQ h
GETDEL h
OBJECT FREQ h

Observed: GETDEL h returns WRONGTYPE, yet the LFU frequency drops from approximately 10 to 5. The pre-redesign implementation preserved the count; Redis does not reset it in this case. Since LFU access counters are probabilistic, the exact pre-error count can vary.

Expected: A failed type check should not recreate the entry or reset its accumulated LFU history. The attempted lookup may still count as an access.

Potential fix: Check the type before removing the entry, or explicitly preserve the removed entry's access metadata when restoring it.

I checked LRU separately: OBJECT IDLETIME becomes zero after this access, matching Redis's lookup behavior. This finding is LFU-specific, not a claim that all LRU behavior is incorrect.

Finding 2 — Slab reuse under delete/refill churn increases RSS without maxmemory

Status: Confirmed performance observation; underlying reuse policy predates the redesign.

Source at e2d9415: src/value_slab.rs:225–230 (listable), :321 (free), :430 (settle), :591 (fill). Defragmentation defaults are in src/config.rs:546–548 and the trigger in src/compaction.rs:117.

Reproduction: Insert 1 million keys with 16-byte keys and 100-byte values, randomly delete X% of the keys, insert X% new keys, then compare memory at the same final key count.

Churn used_memory before → after Physical footprint before → after
20% delete/refill 141.4 → 141.4 MB 155.9 → 180.9 MB (about +16%)
20%, active defrag enabled, 40 s later ~141.4 MB 180.4 MB
50% delete/refill 141.4 → 141.4 MB 155.9 → 158.9 MB

A 20% churn workload with 1 KB values showed about 20% additional RSS.

Likely mechanism: Without a memory limit, a mostly live slab isn't relisted for reuse until at most 3/4 of it remains live. Random 20% deletion leaves many slabs above that threshold, so new writes allocate fresh slabs. Near maxmemory, #208 uses a different threshold, and should not be conflated with this case. The wasted space is bounded; this is not a claim of an unbounded memory leak.

Ideas to test, not yet validated: Dynamic relisting based on aggregate idle slab space; reuse the most-free eligible slab before allocating a new slab; or trigger compaction based on absolute waste in the largest affected size classes. Any alternative must measure partial-slab-list contention so it does not undo #208's gains.

Finding 3 — Hash-table occupancy causes variable per-key overhead

Status: Confirmed memory-efficiency characteristic, not a correctness bug.

Source: src/store/entry.rs (Slot, around :796 and Slot::new around :814); src/store/shard_map.rs (rehash and shrink, around :518 and :905).

A bucket costs approximately 9 B (8-byte slot plus control byte). Tables grow in power-of-two steps and allow up to 7/8 occupancy; the per-key table cost therefore varies with how full each shard's table is.

Comparable dataset shape Approximate hash-table overhead
1M-key benchmark point (about 48% occupancy) 18.9 B/key
Equivalent 10M/512-shard benchmark point (about 60%) 15.1 B/key
Near 7/8 occupancy 10.3 B/key

In the measured range, this overhead varied from about 10.3–20.5 B/key. It explains the 146.9 versus 143.1 B/key difference for the two equivalent workload shapes above.

Potential direction: Sweep key counts around table growth thresholds and evaluate alternative capacity or slot representations. A 32-bit slot handle is a possible architecture experiment, not an established improvement: it would lose the current 12-bit hash fragment and prefetch hint, affect address layout, and require correctness/performance validation.

Finding 4 — TTL metadata costs 24.4 B/key in the 100-byte-value workload

Status: Confirmed memory-efficiency observation.

Source at e2d9415: src/store/entry.rs:101 (TTL_ROOM); src/store/shard_map.rs:288 (TTL vector) and :302 (ttl_push).

Workload: 16 B key, 100 B value used_memory
No TTL 146.9 B/key
With TTL 171.3 B/key
Additional cost 24.4 B/key
  • Approximately 16 B comes from the entry crossing from a 128 B to 144 B slab class when 12 B of TTL metadata is added.
  • Approximately 8.4 B comes from per-shard TTL vector capacity.

At 1 KB values, the extra metadata doesn't cross the same size-class boundary, and the observed TTL overhead is only about 8.4 B/key.

Ideas for benchmarking: More compact TTL position/expiry representation with a fallback for far-future expiry; finer 8-byte classes in the smaller-entry range; or smaller handles in the TTL index. Evaluate expiration correctness, eviction, iteration costs, and allocation contention before proposing a change.

8000 Finding 5 — Values near 4 KiB may move outside slab allocation

Status: Source-level observation; performance impact not yet measured.

Source at e2d9415: src/store/entry.rs:119 (INLINE_MAX) and :153 (fits_inline); src/value_slab.rs:56 (MAX_BLOCK).

With a 16 B key, a 4096 B value cannot fit inline with the entry header; the implementation uses an additional value allocation, above the slab's maximum block size. This suggests a potential second-allocation and compaction boundary worth benchmarking.

A size sweep around 4 KiB should determine whether this materially affects RSS, allocation frequency or latency before treating it as a priority.

Throughput caveat

Three rounds of redis-benchmark -c 50 -d 100 for SET/GET produced 39–57k ops/s and p99 latency of 1.0–1.6 ms, with roughly ±20% run-to-run spread. This laptop benchmark cannot establish a throughput regression or improvement. The repository's Callgrind instruction-count workflow and a controlled Linux benchmark would be more informative.

Proposed next experiments

  1. Slab churn: 10/20/30/50% deletion and refill, 1/5/10 cycles, 100 B/1 KB/mixed 100 B–16 KB values, on Linux/glibc. Measure RSS minus used_memory and memtier p99/p99.9 at 8–32 writer threads.
  2. Table occupancy: Sweep approximately 0.5M–10M keys across growth thresholds; plot bucket capacity and B/key. If evaluating slot changes, include collision-heavy keys and scripts/perf/run.sh with WORKLOADS=full.
  3. TTL footprint: Compare TTL and TTL+LRU at key lengths 8/16/32 and value lengths 16/100/1000, including expiry throughput with 1M keys expiring in 1–10 seconds.

No production code was modified for this review. The memory measurements above are reproducible on the specified macOS setup, but I'd want the candidate optimizations validated on Linux before proposing implementation changes.

I'd appreciate feedback on which of these opportunities is worth exploring first.

Activity

  1. murtazayusuf commented on Oct 31, 2026

    @murtazayusuf
    Contributor

    Thanks Parikalp, this is a careful review.

    • Finding 1 is confirmed in the code: the vacant-insert path doesn't carry the access word over. We'll fix it for 1.2.0 by checking the type before removing the key, and look for other remove-and-reinsert paths.
    • Finding 2 is the one we'd like to explore first. Could you run your churn experiment on Linux with glibc (RSS minus used_memory, 10–50% churn, 100 B and 1 KB values)?
    • Findings 3–5 are noted. We'll revisit the TTL size classes after 1.2.0.
  2. murtazayusuf commented on Nov 1, 2026

    @murtazayusuf
    Contributor

    Follow-up on findings 2 and 5, measured on Linux (Azure D16s_v5, glibc) against the 1.2.0 candidate and 1.1.3. Thanks again; both are real.

    • Finding 2 reproduces: at 10-20% delete/refill churn without maxmemory, RSS settles 13-16% above load and stays there (free blocks in slabs that are 75-100% live). It is not a regression against 1.1.3, whose RSS after the same churn is higher in every shape we tried (100 B values: 1.1.3 grows 218 -> 230-262 MB, the candidate 158 -> 158-183 MB). Active defrag can't reach those slabs today (it only evacuates partial ones), which is where we'd fix it.
    • Finding 5: at 4 KiB the change in memory per key is small (+49 B, same as 1.1.3). The real effects are a size-class tail at 3830-4020 B (up to +5% RSS per key) and a SET throughput step above 4088 B, where values leave the slabs.

    Both are tracked, with numbers and proposed fixes, in #240, after 1.2.0. Finding 1 (GETDEL) is being fixed for 1.2.0.

  3. Parikalp-Bhardwaj commented on Nov 1, 2026

    @Parikalp-Bhardwaj
    CollaboratorAuthor

    Thanks @murtazayusuf , for following up with the Linux benchmarks!

    Good to see that the slab reuse behavior reproduces on Linux, and that the 1.2.0 candidate still uses less memory than 1.1.3 under churn.

    The size-class overhead around 3830–4020 B and the throughput change above 4088 B are interesting findings too.

    I saw your earlier request for an independent Linux/glibc churn test. Since you've now reproduced it and opened #240, would another run from my side still be useful, or would it be better to explore the proposed optimizations after 1.2.0?

  4. added
    memory
    on Nov 1, 2026
  5. murtazayusuf commented on Nov 1, 2026

    @murtazayusuf
    Contributor

    Closing this review, with every finding accounted for. Thanks again, @Parikalp-Bhardwaj:

    1. GETDEL resetting the LFU count: fixed in Five small correctness fixes for 1.2.0: GETDEL/MOVE LFU, noscript commands, lazyfree-lazy-expire config, dead-key access word, expired ordering #248, together with MOVE and RENAME k k, which had the same pattern.
    2. Slab waste under churn: confirmed on Linux, tracked in Slabs: waste after light churn, defrag RSS spikes, and values near 4 KiB (after 1.2.0) #240 (yours).
    3. Table occupancy: expected behaviour, no change planned.
    4. TTL crossing a size class (+16 B/key at 100 B values): tracked with the size-class work in Slabs: waste after light churn, defrag RSS spikes, and values near 4 KiB (after 1.2.0) #240.
    5. Values near 4 KiB: measured, tracked in Slabs: waste after light churn, defrag RSS spikes, and values near 4 KiB (after 1.2.0) #240.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    memory

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0