Read-heavy → cache
Most apps are 80%+ reads. Push reads to Redis; let DB handle writes.
# Cache aside, with TTL
val = redis.get(key)
if val is None:
val = db.fetch(key)
redis.set(key, val, ex=300)Patterns from the System Design track condensed. Each entry is something an interviewer can probe for 3 minutes — perfect cue cards for a mock interview.
Most apps are 80%+ reads. Push reads to Redis; let DB handle writes.
# Cache aside, with TTL
val = redis.get(key)
if val is None:
val = db.fetch(key)
redis.set(key, val, ex=300)Group writes into a single transaction to amortise overhead.
# Aggregate then flush every 100 events or 1s, whichever first.
Pin reads to the primary for N seconds after a user writes — avoids stale-read UX.
# After write: session.set("primary_until", now + 5s)Horizontal scale via N identical pods behind a load balancer. WebSockets pin via cookie.
# Round-robin for HTTP; consistent-hash for WS
Spread a single hot table across N shards by hash(user_id) % N.
shard = hash(user_id) % SHARDS conn = pools[shard]
Reads to N replicas; writes go to primary only. Lag is your enemy.
# Replica monitoring: SHOW REPLICA STATUS; alert if Seconds_Behind > 30
Two hard things in CS. Pick one: TTL (simple, stale OK) or write-through (fresh, slower writes).
# Pattern: write-through
def set(k, v):
db.write(k, v)
redis.set(k, v)All clients miss at once when a hot key expires. Use lock-and-fetch or probabilistic early refresh.
lock = redis.set(f"{key}:lock", 1, nx=True, ex=10)
if lock: refresh()Safe-to-retry jobs need an idempotency key the worker checks.
def process(event):
if seen(event.idempotency_key):
return
do_the_work(event)
mark_seen(event.idempotency_key)Retries with exponential backoff; after N tries, send to dead-letter for human triage.
try: process(e) except Transient: enqueue(e, delay=2**attempt) except Permanent: dlq.put(e)
Rate, Errors, Duration — the 3 metrics every service should expose.
# Prometheus counters/histograms per endpoint
Distributed traces (W3C `traceparent` header) glue together a request across services.
# OpenTelemetry SDK propagates the header automatically
JSON logs with `trace_id`, `user_id` — searchable, joinable.
logger.info({"event": "checkout", "user_id": uid, "trace_id": tid})Want to actually learn these patterns, not just paste them? Open the System design cheatsheet track — each snippet has a full lesson behind it.