Community Blog Saying Goodbye to Write Stall: How Tair Serverless KV Keeps Latency Stable Under Extreme Write Pressure

Saying Goodbye to Write Stall: How Tair Serverless KV Keeps Latency Stable Under Extreme Write Pressure

This article introduces how Tair Serverless KV eliminates LSM tree write stalls to ensure stable latency under heavy write pressure.

By Alibaba Cloud Tair Team

Since its inception, the Log-Structured Merge tree (LSM tree) has been plagued by write stalls and write stops — under heavy write pressure, latency and throughput swing wildly, and write traffic can even drop to zero. To this day, neither academia nor industry has found a general solution to this problem.

This article introduces an innovative solution in Alibaba Cloud ApsaraDB's Tair Serverless KV storage engine (FlexEngine): the first to model the LSM tree as a producer-consumer system, eliminating write stall at the mechanism level. The solution has been granted a patent.

1. What is a write stall: a pain point common to LSM-based engines

The LSM tree (with RocksDB and LevelDB as typical open-source implementations) is widely used across database products for its standout strengths: excellent write performance, sequential data layout, high compression ratio, and low space cost. But it has one problem it was born with — the write stall/stop.

The root cause lies in how the LSM tree writes: a user write first lands in the in-memory memtable and returns immediately (this is why the LSM tree writes so fast), then background flush and compaction threads organize the data level by level and persist it to disk. When the user write rate consistently exceeds the processing rate of the background threads, data keeps piling up at a given level, and the engine proactively "hits the brakes":

• Accumulation exceeds the soft limit → enters a stall state, and writes are throttled.

• Accumulation exceeds the hard limit → triggers a write stop, and the service becomes unavailable.

There are three trigger conditions: the number of immutable memtables, the number of level 0 files, and the pending compaction bytes.

1_

Making matters worse, the native engine's throttling logic is very crude: once it enters a write stall, it keeps cutting tokens by fixed coefficients (0.8/0.6), then restores them on exit by another fixed set (1.25/1.4). It does not sense the engine's true processing capacity — it just "guesses". The result: burst writes are completely unconstrained, the throttling start point is disconnected from real capacity, and multiple column families (CFs) interfere with one another. In the end, throughput oscillates violently in a sawtooth pattern and latency spikes reach hundreds of seconds. Existing industry solutions — whether they throttle inside the engine or at a layer above it — can only ease the severity of the oscillation, and cannot prevent the write stall at its root.

2

For a cloud product that commits to a stable SLA, this unpredictable performance cliff is unacceptable.

2. Core idea: modeling the LSM tree as a producer-consumer system

We approached the problem from a different angle: the LSM tree is essentially a producer-consumer system — users are the producers, writing data into the memtable, while background flush and compaction threads are the consumers, organizing the data level by level into a steady state. A write stall is, in essence, the "warehouse overflow" that results when the production rate exceeds the consumption rate for a sustained period.

Past optimizations mostly tried to "increase consumption capacity" (reduce write amplification, adopt new hardware) or "enlarge the buffer queue" (delay the trigger conditions). But consumption capacity always has a ceiling, so accumulation happens sooner or later. Our approach turns this around: we compute the engine's true real-time consumption capacity and smoothly throttle the production rate to stay within it.

How do we quantify consumption capacity? The key insight is that for every byte of user data written to the LSM tree, the background must move a number of bytes equal to the read-write amplification (RWA) factor. So the engine's real-time consumption capacity can be modeled as:

Engine write capacity = Available disk bandwidth / (Real-time RWA(t) + 1)

Here, RWA(t) changes dynamically with data volume and the number of levels, and the "+1" accounts for the WAL write overhead. As long as throttling keeps pace with this dynamic value, data does not accumulate, and the write stall trigger conditions are never met.

3. Architecture: a two-level token-based control loop

The solution forms a control loop around two token-management components:

• RateLimiter (disk bandwidth management): manages the bandwidth quota for all disk reads and writes — user file reads/writes and background flush/compaction — so that background tasks do not crowd out the foreground and the disk is not saturated.

• WriteController (foreground write throttling): before each write, a user must request tokens, and the requested amount is "write data volume × write_penalty(t)", where write_penalty(t) is computed from the real-time RWA. The token dispatch rate is exactly the disk bandwidth currently available to the write path.

Whenever the LSM tree structure changes — a memtable switch, or a completed flush/compaction — the system recomputes the RWA and updates write_penalty. Throttling therefore tracks the engine's true capacity in real time, rather than acting as an after-the-fact remedy.

3_

4. Key designs that make the model work in practice

A naive model still has several critical problems to solve before it is production-ready:

1. Accurate RWA measurement and smoothing. If the instantaneous RWA is measured too high, performance is lost for nothing — too low, and data still accumulates. We measure the real-time RWA of compaction level by level and apply weighted smoothing (accumulate the data involved in compaction until it exceeds 80% of the file threshold, backtracking at most 5 times). We also filter out the compaction types that do not block foreground writes, eliminating the performance dips caused by statistical noise.

2. Letting writes briefly exceed engine capacity (burst). We designed write_penalty to be segmented: when the LSM tree is healthy, user writes may run up to 150% of engine capacity; when accumulation starts to show (the original write stall trigger conditions), it falls back to 100% of the true consumption capacity. Note that this is no longer a write stall but smooth throttling that precisely matches engine capacity. This brings three benefits at once: no performance is lost when the measurement runs high, moderate accumulation is used to lower write amplification, and users' real need to absorb burst traffic is met.

4

3. Fine-grained flush and compaction scheduling. The throttling model holds only if the background consumption path itself has no bottleneck. We use the RateLimiter to give flush a higher disk-bandwidth priority than compaction. We set the memtable's soft limit to half its capacity so it aligns with true write capacity earlier, and we use concurrent subcompaction to ensure level 0 files are digested in time. In our tests, memtable accumulation never reached the hard limit.

4. Multi-CF support. Multiple CFs share the background thread pool. We introduced a global write_penalty (taking the maximum RWA across all CFs). The estimate is conservative, but paired with the burst capability, our tests showed no noticeable performance loss.

5. Measured results: maximum latency decreased by 2 to 3 orders of magnitude

Take a continuous stress test with 16 threads and 1 KB values (simulating a real product configuration) as an example. Before and after optimization:

Metric Before optimization (native engine) After optimization (FlexEngine)
Average latency 1.12ms 1.37ms
P99 latency 5.3ms 2.9ms
Maximum latency 353436ms 36ms

In the single-CF scenario, maximum latency dropped from hundreds of seconds to 36ms, an improvement of over 3 orders of magnitude; in the multi-CF scenario, maximum latency fell from 33891ms to 37.4ms, an improvement of nearly 3 orders of magnitude (the gains are even larger with bigger values). The two charts below show the write-throughput curves after optimization for the single-CF and multi-CF scenarios:

5
6

After optimization, write throughput converges smoothly to the engine's true capacity as data volume grows, with not a single write stall or write stop along the way, and average throughput stays roughly on par with the native engine — proving that stability is not traded away for performance.

6. Conclusion

Through disk bandwidth monitoring and management, real-time calculation of engine write capacity based on a producer-consumer model, precise control of foreground write traffic, and compaction scheduling tuning, Tair Serverless KV has completely eliminated the write stalls and write stops inherent to LSM engines — without sacrificing write throughput, so the product keeps write latency stable and predictable even under extreme write pressure.

For users, this means that in sustained high-write scenarios such as log collection, message buffering, and bulk import, you no longer have to reserve headroom capacity or write complex retry logic to guard against a sudden throughput cliff. Stable write performance is the product promise of Tair Serverless KV.

0 0 0
Share on

ApsaraDB

672 posts | 189 followers

Comments