12. Case studies
One-liner. Three designs that reuse the same eight primitives.
Memorize — URL shortener
- Write: generate ID (base62 of a counter/Snowflake), store
code → urlin KV, optional custom alias. - Read: cache hit at edge/Redis; 301 vs 302 (caching vs analytics).
- Scale: read-heavy (100:1) → cache + replicas; ID generation must be coordination-free.
Memorize — news feed
- Fan-out on write (push): fast reads, expensive for celebrities.
- Fan-out on read (pull): cheap writes, slow reads.
- Hybrid: push to normal users, pull for celebrities; cache timelines; rank at read time.
Memorize — payments
- Idempotency keys, ledger (append-only double-entry), outbox pattern for events.
- Strong consistency on balances, eventual on notifications/reporting.
- Never trust "exactly-once" across services; reconcile.
Trap. Applying one pattern to all three — chat needs ordering, feeds need fan-out, payments need invariants. Do this. Name the primitive each requirement forces (cache, queue, ledger, index).
Check
A celebrity with 50M followers posts and writes to 50M timelines, stalling the write path. Best fix?
Why
Fan-out on write is O(followers); pulling celebrity posts at read time bounds the cost.