8000 Slabs: waste after light churn, defrag RSS spikes, and values near 4 KiB (after 1.2.0) · Issue #240 · kividbio/kividb · GitHub
[go: up one dir, main page]

Skip to content

Slabs: waste after light churn, defrag RSS spikes, and values near 4 KiB (after 1.2.0) #240

Done

Description

@murtazayusuf

Measured on Linux (Azure D16s_v5, glibc) against 1.2.0-rc3 (c0f49b0) and 1.1.3, following Parikalp Bhardwaj's review in #221 (findings 2 and 5). Not a regression after churn: rc3's RSS after delete/refill churn is the same as or lower than 1.1.3's in every shape measured (100 B values 23-40% lower, 1 KB 0-28% lower, mixed 100 B-16 KB within ±3%). Three things to improve after 1.2.0:

1. Waste in full slabs after light churn (efficiency). At 10-20% churn, RSS settles 13-16% above load (100 B: 158 -> 183 MB, of which 24 MB is free blocks in slabs that are 75-100% live; 1 KB: about 150 MB of 1.2 GB). It doesn't grow past cycle 2. Without maxmemory, a slab is reused only when a quarter of it is free (#208 lowered that only near the limit), and active defrag can't help: plan_evacuation only takes slabs from the partial lists, never a FULL one, and the waste stays under the default trigger (64 MB / 20%). Proposed: let defrag evacuate the sparsest FULL slabs (FULL -> EVACUATING under the class lock), and trigger on slab waste itself (e.g. > 10% of slab bytes and > 16 MB). The hot path and the 1/4 rule stay as they are (they exist to avoid lock ping-pong on small overwrites).

2. Active defrag (on by default) briefly raises RSS during churn. With 1 KB values, a delete phase pushes waste over 20%, defrag starts, the slabs it evacuates leave the lists, and the refill takes fresh slabs: peak RSS up to +26% over load (rc3 defaults 1308-1339 MB vs 1191-1207 with defrag off at 10-20% churn), settling within a cycle or two. Peaks stay below 1.1.3's except one sample (+3%). Smaller evacuation sets and not starting while a large delete is being refilled would cut it.

3. Values near 4 KiB (small regression vs 1.1.3).

  • Memory: values of about 3830-4020 B use up to +202 B per key (+5.2%) more RSS than 1.1.3: their class fits only 16 blocks in a 64 KiB slab and leaves an unused tail (e.g. a 3832 B value goes in the 3904 class, 3072 B left per slab). used_memory doesn't show it. Smaller tails exist in other classes above 2 KB.
  • Throughput: above 4088 B (8 B header + value no longer fits the largest slab block, MAX_BLOCK) values go to the system allocator: SET at pipeline 16 drops about 17% across the boundary and is about 8% below 1.1.3 above it (p99 0.93 -> 1.2 ms). Below it rc3 is 5-10% faster than 1.1.3. GET unchanged.
  • Directions: class sizes of 65536/k above 2 KB, or larger slabs for the big classes; raising MAX_BLOCK past 4 KiB would also help mixed-size workloads (values above 4 KiB live in glibc today, where Redis/jemalloc keeps fragmentation at 2-4%).

Evidence and scripts are kept with the measurement (churn.py, run_churn.sh, spike.sh, run_f5.sh).

Activity

  1. Parikalp-Bhardwaj commented on Nov 1, 2026

    @Parikalp-Bhardwaj
    Collaborator

    Hi @murtazayusuf, I have been looking into #240, particularly the slab reuse issue under delete/refill workloads.

    I would like to investigate whether we can improve memory reuse through a bounded defragmentation path, without adding contention to the allocation fast path.

    I will start by reviewing the existing slab and compaction logic and comparing a few approaches against the Linux benchmark results.

    Are you already working on an implementation for this, or would this be a useful area for me to take on?

  2. murtazayusuf commented on Nov 1, 2026

    @murtazayusuf
    ContributorAuthor

    Thanks @Parikalp-Bhardwaj, nobody is on it: it's yours. It lands after 1.2.0 (we're in the release-candidate phase), so there's time to measure properly.

    What we'd look for:

    • Keep the fast path as it is. The 1/4-free rule in listable exists because relisting slabs eagerly cost a lot of lock traffic for small overwrites on many threads (7.4M -> 1.8M ops/s when we tried a lower threshold). The direction in the issue is to let active defrag evacuate the sparsest FULL slabs (FULL -> EVACUATING under the class lock; free, settle and finish_evacuation already handle EVACUATING), and to trigger on slab waste itself rather than the 64 MB / 20% default. If you find a better design, great: measure it against that one.
    • Watch the spikes (point 2). Evacuating takes fresh slabs before the old ones are given back; a smaller evacuation budget and not starting while a large delete is still being refilled should keep peak RSS at or below defrag-off.
    • Gates: on Linux/glibc, RSS for 100 B and 1 KB values at 10-20% churn within about 5% of the post-load RSS after 60 s; peak RSS (sampled every 50 ms) no higher than with defrag off; memtier SET 100 B, pipeline 16, 8 server threads, random overwrites on full slabs: ops/s and p99/p99.9 unchanged; scripts/perf/run.sh instruction counts for GET/SET within 1%; the eviction-quality and maxmemory behaviour from maxmemory bounds what the process uses; a command's keys are found through a table #208/Eviction: every key as likely to be sampled, and the runners-up looked at again #212 unchanged.

    The churn driver we used: 8 processes sending pipelined raw RESP, 16 B keys, each cycle deleting a random X% of live keys then inserting X% new ones, VmRSS and INFO sampled ~2 s after each cycle. Happy to review a design note before you write much code.

  3. Parikalp-Bhardwaj commented on Nov 1, 2026

    @Parikalp-Bhardwaj
    Collaborator

    Thanks @murtazayusuf, I'll take this on.

    1. Active defrag: I will focus on this approach first, keeping the allocation fast path and existing 1/4-free rule unchanged.
    2. Investigation: I will review the FULL → EVACUATING transition, candidate selection, and ways to limit temporary memory growth.
    3. Benchmarks: I will use the Linux churn and memtier workloads you outlined, including peak RSS, throughput, latency, and maxmemory/eviction behavior.
    4. Design note: I will prepare a proposal covering the implementation approach, trade-offs, and benchmark plan for your review before writing code.

    Thanks for the detailed guidance!

  4. added
    memory
    performance
    after-1.2.0
    on Nov 1, 2026
  5. Parikalp-Bhardwaj commented on Nov 1, 2026

    @Parikalp-Bhardwaj
    Collaborator

    Hi @murtazayusuf,

    I have completed the initial design investigation and drafted a design note. No implementation changes yet.

    The proposed approach follows your FULL → EVACUATING direction:

    1. Candidate selection: Identify sparsely occupied FULL slabs and revalidate their state, class, generation, and live count under the class lock.
    2. Fast path: Keep alloc, free, take, let_go, and the existing 1/4-free rule unchanged.
    3. Memory control: Bound evacuation work, avoid running during active refill churn, and reuse the existing evacuation lifecycle.
    4. Validation: Reproduce the Linux baseline first, then compare RSS, peak memory, throughput, latency, and instruction counts.

    One concern came up during the analysis, moving live entries requires destination capacity before source slabs can be released, so reducing steady-state RSS without increasing peak RSS may be difficult under sustained churn.

    Before implementing, I would appreciate clarification on three points:

    • Does the 5% RSS target apply 60 seconds after churn stops, or during continuous churn?
    • Is the peak-RSS gate a strict maximum against defrag-off, or is a small tolerance acceptable?
    • Would you prefer the new trigger and stabilization policy to apply only to FULL-slab evacuation initially?

    I will refine the design based on your feedback and share it for review before making code changes.

  6. Parikalp-Bhardwaj commented on Nov 1, 2026

    @Parikalp-Bhardwaj
    Collaborator

    Hi @murtazayusuf, I have made some progress on #240 and completed another round of Linux experiments.

    A few findings:

    1. Compaction pacing: The existing defrag worker sleeps after every 128 buckets. On my Linux test, a pass over 1M keys took 65.5 s at the default 10% CPU cap, with 60.8 s spent sleeping. Combining the sleeps reduced wall time to 13.3 s in a diagnostic run.
    2. Memory reclamation: Lowering the defrag trigger alone doesn't solve the peak-RSS problem, since relocating entries still requires fresh slabs.
    3. Resident-hole approach: Using free blocks in existing FULL and PARTIAL slabs looks promising. The offline analysis of measured occupancy suggests significantly less relocation work and no fresh destination slabs, although this still needs runtime validation.

    These results are from Linux/aarch64 in Docker Desktop, so I haven't treated them as final performance numbers.

    I'm planning to separate the work into a small compaction-pacing change first, followed by the resident-hole allocator and its concurrency tests.

    Would you prefer the pacing improvement as a separate PR, or as part of the eventual #240 change? I can share the design note before implementing the allocator changes.

  7. murtazayusuf commented on Nov 1, 2026

    @murtazayusuf
    ContributorAuthor

    Thanks @Parikalp-Bhardwaj, and sorry for the slow reply to your 06:10 questions. The pacing find is a good one.

    Your three questions:

    • 5% target: measured 60 s after churn stops, at each cycle's settle point. During continuous churn, please report the RSS curve, but there's no gate on it.
    • Peak-RSS gate: a small tolerance is fine. Within 2% of defrag-off counts as equal; 50 ms sampling is noisy at that level. Anything clearly above defrag-off is a fail, because the spike is what users notice.
    • Scope: yes, apply the new trigger and stabilisation policy only to FULL-slab evacuation at first. The existing partial-list path stays as it is.

    Pacing: please make it a separate small PR first. It's easy to review and measure on its own, and it helps whatever comes next. One condition: merging the sleeps must keep the same CPU share (the 10% cap). Fewer, longer sleeps shouldn't turn into bursts that show up as latency. For that PR, please show p99/p99.9 of a memtier SET/GET load running during a defrag pass, before and after, besides the wall time.

    Resident holes: this sounds promising, and it avoids fresh destination slabs, which is the peak-RSS problem you named. It does sit closer to the allocation path, though, so a design note first, please. The note should cover:

    • where the holes are found and who takes them;
    • how it stays clear of the per-thread fast path and the 1/4-free rule;
    • what the scripts/perf/run.sh instruction counts for GET/SET look like.

    Numbers: Docker Desktop on aarch64 is fine for direction. For the gate numbers we'll run your branch on a native Linux VM on our side when it's ready, so you don't need the hardware.

    All of this lands after 1.2.0.

  8. Parikalp-Bhardwaj commented on Nov 1, 2026

    @Parikalp-Bhardwaj
    Collaborator

    Okay @murtazayusuf, I will start with the compaction pacing fix as a separate small PR.

  9. Parikalp-Bhardwaj commented on Nov 2, 2026

    @Parikalp-Bhardwaj
    Collaborator

    Hi @murtazayusuf, I've opened a draft PR for the M1 compaction pacing change.

    It reduces the number of sleeps during background defrag while keeping the existing CPU-share calculation. I've also added tests, and the local checks are passing.

    The compaction-speed results look promising, but I'm leaving it as a draft because my local A/B tests showed a possible GET/SET tail-latency regression. The Docker environment was too noisy to confirm the cause.

    I've prepared a reproducible native-Linux benchmark setup to validate the change before merging.

    This is limited to pacing; the allocator changes remain separate.

  10. murtazayusuf commented on Nov 2, 2026

    @murtazayusuf
    ContributorAuthor

    Thanks, @Parikalp-Bhardwaj. Keeping #291 to the pacing alone, and as a draft until the tail latency is understood, is the right call.

    I've left review notes on #291. In short: the latency you saw is most likely the defrag thread taking a shard's lock again right after letting it go, now that it runs batches back to back for up to a millisecond; unlock_fair at the end of a batch should hand it to a waiting client. The CI failure there wasn't from your change: it was a race in hset_beside_the_field_expiry_sweep_keeps_the_new_field, now fixed on the release branch, with credit to you for spotting it.

    For the native-Linux A/B, measure GET/SET p99 and p99.9 during a pass, with and without the change. If you don't have quiet hardware for it, say so and we'll run it on an Azure VM.

  11. Parikalp-Bhardwaj commented on Nov 2, 2026

    @Parikalp-Bhardwaj
    Collaborator

    Thanks @murtazayusuf , that makes sense. I'll look into the targeted unlock_fair change at the end of compaction batches and update #291.

    I don't have a quiet native-Linux machine available, so the Azure VM would be helpful for the final A/B latency comparison. I will keep the PR in draft until we have those results.

  12. murtazayusuf commented on Nov 2, 2026

    @murtazayusuf
    ContributorAuthor

    Sounds good, @Parikalp-Bhardwaj. Push the unlock_fair change to #291 when it's ready, and we'll run the A/B on an Azure VM: GET/SET p50/p99/p99.9 during a defrag pass, master against your branch, alternating runs on the same machines. We'll post the numbers and the harness on the PR. It will be once 1.2.0 is out, since the release is taking the machines today.

  13. 23 remaining items

  14. murtazayusuf commented on Nov 21, 2026

    @murtazayusuf
    ContributorAuthor

    #990 is merged: points 1 and 2 of this issue.

    • After 10% churn of 1 KB values, RSS settles at 1.03x the loaded size (1,079 MiB; Redis 8.10.2 1,299 MiB); 100 B values settle at 1.03 to 1.05x.
    • A defrag pass peaks 1.2 to 2.1% above the same churn with active defrag off (regression suite 43 gates it at 1.03x).
    • GET p99.9 during a pass is 0.63 ms (the defrag thread now works at most 100 µs at a time).

    Left open for point 3, values near 4 KiB: a 3,832 B value costs 4,120 B of RSS per key (3,885 B at 3,800 B), and SET goes from 800k/s at 4,088 B to 681k/s at 4,100 B. A prototype with 128 KiB slabs brought the 3,832 to 3,900 B values to 3,998 B per key and left the boundary where it is, so the next step is larger slabs for the classes above 2 KiB together with a higher block limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

after-1.2.0memoryperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0