Scaling Writes
A write path is limited by the slowest sustained stage, not by the length of its queue.
What, why, when and how
What is it?
Scaling writes increases sustainable mutation throughput through less work per write, batching, partitioning and controlled admission.
Why is it needed?
Bursts can exceed storage capacity and make latency, retries and queue age grow without bound.
When should I use it?
Use it when measured write pressure or burst absorption requirements exceed the baseline.
When should I choose something else?
A queue does not fix a permanent capacity deficit. Avoid acknowledging completion when only durable acceptance has happened.
How do I use it?
Define the acknowledgement boundary, count index and replication work, partition independent updates, and enforce queue, concurrency and retry limits.
Can you show a concrete example?
If arrivals are 1,000 jobs per second and workers finish 600, backlog grows by 400 per second. A 24,000-job buffer fills in 60 seconds, so the system needs admission control, more capacity or a weaker completion target.
Read the diagram
Arrivals 1,000/s; completions 600/s; Backlog grows 400/s; buffer holds 24,000 jobs; Buffer fills in 60 s: bound admission
Follow the detailed diagrams below to trace the additional state changes and failure cases behind these answers.
A write path is limited by the slowest sustained stage, not by the length of its queue. Scaling it requires understanding write amplification, partition balance, durability, and overload behavior. A queue can smooth bursts and decouple callers, but it cannot make a database or external provider process more work per second indefinitely.
Learning goals
Write amplification; batching; append logs; partitioning; hot keys; queues; backpressure; admission control; bounded backlog; replay.
Measure the queue's promise
A burst arrives at 12,000 jobs/s while the sink completes 8,000/s. For 900 seconds, backlog grows by (12,000-8,000)*900 = 3,600,000 jobs. If arrivals later fall to 4,000/s, the 8,000/s sink drains at 4,000/s and needs another 900 seconds, ignoring retries and variability. If arrivals stay at 12,000/s, no finite queue fixes the deficit.
Batching 100 records per write reduces per-request overhead, but not necessarily payload bytes or every index update. Bound batch bytes, item count and linger time. At low arrival rates, waiting for 100 items can violate latency; flush on a deadline as well. Durable acknowledgement happens at the stated boundary, not when a process appends to an unprotected in-memory array.
aggregate key: video7 / minute12:00
bucket 0: +30; bucket 1: +20
derived total: 50 at the declared merge watermarkRead the diagram
aggregate key: video7 / minute12:00; bucket 0: +30; bucket 1: +20; derived total: 50 at the declared merge watermark
Salting a hot counter spreads increments and makes reads a merge. It is appropriate for a count under a declared freshness/error contract; it does not safely split one exclusive inventory balance into independently spendable copies. Vertical partitioning separates workloads or large payloads, while horizontal sharding splits owner subsets. Both require a routing and recovery story.
Show answer and explanation
Answer: A queue acceptance SLO and a completed-write SLO are different. Monitor oldest age, byte budget, retries and drain rate. Shed or delay lower-priority work before durable storage or the sink exceeds its safe budget.
Step-by-step reasoning
1. Count the real write work
One incoming event may update several indexes, replicas, counters, and projections. Measure bytes and storage operations, not just API requests. Remove unnecessary synchronous work and use append-oriented records when the access pattern permits. Batching amortizes overhead but adds waiting time and makes failure units larger.
2. Acknowledge at a defined boundary
If the API returns success before durable acceptance, a crash can lose accepted work. Persist to a durable log or transactionally store intent before acknowledging according to the stated contract. Consumers then apply derived work asynchronously. Define retention and replay so a downstream outage does not outlast recoverable history.
3. Partition for independent progress
Choose a key that preserves only the ordering actually needed. Ordering every event globally unnecessarily serializes the system. Per-account or per-campaign ordering may be sufficient. Watch skew: a single popular campaign can overload one partition even with hundreds of idle partitions elsewhere.
4. Apply backpressure before collapse
Measure oldest-message age, not just queue depth. Bound producers, consumer concurrency, and retries. Reject, defer, sample, or prioritize work according to its importance. Billing events may require durable rejection and retry; optional telemetry may permit sampling. Make this a product contract rather than a silent data-loss policy.
Contracts and state
The following sketch makes the decision boundary concrete. Field names and capacity assumptions are illustrative; adapt them to the stated product contract.
Arrival = 12,000 events/s
Sustainable service = 8,000 events/s
Backlog growth = 4,000 events/s
After 1 hour = 14.4 million queued events
Scaling workers helps only if the bottleneck can process more.Worked example
A telemetry service has short bursts at 12,000/s but normally receives 3,000/s. An 8,000/s consumer tier can absorb a bounded burst and drain later. If 12,000/s lasts all day, the same design is unstable. Estimate burst duration and available retention. Add capacity at the actual bottleneck, reduce work per event, or throttle admission; do not label the queue “infinite.”
Read the diagram
Arrivals are 12000 events per second and service is 8000. Backlog grows linearly by 4 million per 1000 seconds, reaching 14.4 million after an hour.
Failure walkthrough
Retries during a storage slowdown multiply write pressure. Use exponential backoff with jitter, per-tenant limits, and a bounded retry budget. Poison events need an inspectable quarantine path so they do not block a partition forever. Replay must preserve original event identities; generating new IDs turns a repair into duplicate business activity.
Read the diagram
Traffic exceeds service → Queue age increases → Admission limit engages → Useful work remains stable → Spare capacity drains backlog
Decisions and trade-offs
| Technique | Gain | Cost |
|---|---|---|
| Batching | Less overhead per event | Latency and larger retry units |
| Partitioning | Parallel owners | Cross-key coordination |
| Async projection | Shorter acceptance path | Lag and replay operations |
| Admission control | Protects stable capacity | Explicit rejection or delay |
Check your understanding
An operator adds ten consumers but throughput stays fixed. What should they inspect next?
Show answer and explanation
Answer: Inspect partition parallelism, key skew, database write capacity, provider quotas, and lock contention. Extra consumers cannot exceed a fixed downstream budget or split one ordered hot partition automatically.
Transfer to a new scenario
Handle a burst of telemetry without claiming the queue increases downstream capacity.
At 12k incoming and 8k processed events/s, what happens during a sustained hour?
Continue the connection
Study Kafka and explain which guarantee from this lesson carries into that topic.
Distinguish admission from completion
A queue lets the API acknowledge durable acceptance before expensive processing, but the durability claim depends on what was committed before the acknowledgement. Store a job plus outbox or append to a durable log under an explicit contract. The worker pool then consumes within downstream concurrency and throughput limits.
Batching amortizes fixed overhead but trades added wait time and larger retry units. Flush on item count, byte count and time, and keep a stable batch or item identity. Partition by the ordering key; random partitioning spreads load but loses per-entity order unless another layer restores it.
Monitor oldest-event age alongside throughput. A queue can look stable in length while one low-priority tenant starves. Bound per-tenant buffers and apply a documented fairness policy. When sustained arrival exceeds service, choose backpressure, rejection, delayed completion or reduced work; buffering alone cannot settle the deficit.
Read the diagram
Technique / Gain / Cost; Batching / Less overhead per event / Latency and larger retry units; Partitioning / Parallel owners / Cross-key coordination; Async projection / Shorter acceptance path / Lag and replay operations; Admission control / Protects stable capacity / Explicit rejection or delay
Operational sketch
accept -> durable log -> partition workers -> sink
flush if count >= 100 OR bytes >= limit OR age >= 20ms
commit sink effect before safe acknowledgement
retry using original event identityA tempting mistake
Increasing workers against an exhausted connection pool can lower throughput through contention and timeout retries. Scale after identifying the bottleneck and reserve capacity for recovery traffic.
Transfer exercise
Why might larger batches worsen p99 latency?
Show answer and explanation
Answer: Items wait for a batch to fill and one slow item or retry can delay a larger group. Use a time cap and measure both throughput and end-to-end age.