Lesson 6 of 9 · 28m
Write Paths: Partitioning, Queues, and Consistency
Reason through write amplification, partitioning keys, idempotent consumers, ordering, and the consistency guarantees a system can actually keep.
Write paths are where optimism goes to die. Reads can be cached, replicated, and approximated. Writes mutate truth. Once a system accepts money, messages, inventory, permissions, or irreversible actions, the design must answer hard questions about ordering, duplication, contention, and what the user believes happened.
Follow one write all the way down
Take a simple action:
POST /orders
validate input -> reserve inventory -> charge payment -> create order
-> write outbox -> publish event -> update search/read models -> notify userEach arrow can fail independently. If the charge succeeds and order creation times out, did the order exist? If the event publishes twice, does inventory decrement twice? Senior design is mostly deciding where transactions end and compensating actions begin.
Partition for locality of contention
A partition key decides which writes fight each other:
user_id good for per-user documents, poor for celebrity fan-out
workspace_id good tenant locality, poor if one workspace dominates
post_id good for post counters, poor for global timeline scans
time bucket good for ingestion, dangerous hot shard at current timeHot partitions are not theoretical. A celebrity post, viral product launch, or breaking-news room concentrates writes exactly where the system is least flexible. Sometimes the answer is write sharding of counters, sometimes queue smoothing, sometimes accepting approximate aggregation.
Queues decouple, but do not create correctness
Kafka, SQS, Pub/Sub, or a database outbox can absorb bursts and isolate consumers. They do not guarantee business correctness by themselves. Consumers need idempotency:
def handle(event):
if processed_events.exists(event.id):
return
with transaction():
apply_business_effect(event)
processed_events.insert(event.id, consumer="inventory")At-least-once delivery is normal. Ordering is usually scoped to a key, not global. Poison messages need retry limits and dead-letter handling. A queue without these answers is just a place where outages wait patiently.
Understand write amplification
One logical write often becomes many physical writes: primary storage, replicas, WAL, indexes, audit log, outbox, cache invalidation, search update, analytics event, backups. Estimate amplification before promising throughput. Ten thousand user writes per second can become one hundred thousand disk or network mutations downstream.
Consistency is a user promise
Do not recite CAP like a spell. Specify behavior:
after comment creation returns 201, author can immediately read it
other viewers may see it within 2 seconds
inventory reservation is strongly consistent within one region
analytics counts are approximate and eventually consistentThen choose mechanisms accordingly. Read-your-writes may require primary reads, quorum behavior, tokens, or caching the write response. Cross-region strong consistency costs latency and availability; sometimes it is necessary, often it is not.
Use the outbox pattern when events must not vanish
Writing a database row and publishing a queue event is not atomic across two systems. The outbox pattern writes both the state change and event in one local transaction, then a relay publishes the event. It is boring, reliable infrastructure — exactly what you want near money and permissions.
Field notes
- Unique constraints are your last line of defense against duplicate retries; use them for natural idempotency boundaries.
- Distributed locks are not a substitute for correct transaction design.
- Backpressure is a feature. If ingestion cannot keep up, shed low-priority work before corrupting core writes.
- When a candidate says "eventually consistent" without naming what users observe, the design is still unfinished.
Was this lesson helpful?