02. Pattern Choosers

Goal: When stuck between options, follow a short decision tree — then say the trade-off out loud.

These are defaults for beginners, not laws. If two answers both fit, pick the simpler one and explain when you’d switch.


How to use this page

  1. Hit a fork in your design (“Do I need a queue?”).
  2. Walk the matching tree below.
  3. State: choice + one reason + one risk.

Example:
“I’d add a queue for thumbnail generation because upload ACK shouldn’t wait on CPU work. Risk: users briefly see a placeholder until the worker finishes.”


Chooser 1 — Do I need a queue?

Signal you need a queueExample
Work takes seconds–minutesTranscoding, PDF, ML batch
Spiky writes, smooth consumersAnalytics ingest
Multiple independent side effectsEmail + push + webhook
Want retries without blocking userPayment notification
Signal you do not need a queue yetExample
Simple CRUD under moderate QPSBlog CMS
User needs the result in the same response“Save and show the new total”
You’re avoiding learning transactionsDon’t hide consistency bugs behind Kafka

Beginner default: No queue until you can name the async job.


Chooser 2 — Cache vs DB vs CDN?

Ask: What are we speeding up, and is it OK if it’s slightly stale?

LayerStoresGood forBad for
CDNEdge copies of filesImages, video segments, JS/CSSPer-user private JSON (unless careful)
App cacheHot keys in memorySessions, product pages, redirect mapsSource of truth forever
DBDurable truthMoney, bookings, identityServing every viral read alone

Phrase: “Cache is a speed layer, not the system of record.”


Chooser 3 — SQL vs NoSQL?

Start with SQL. Switch when you have a concrete reason.

Choose SQL whenChoose NoSQL-style when
Strong relationships, joinsMostly key lookups
Transactions matterYou accept eventual consistency
Ad-hoc admin queriesSchema evolves constantly (documents)
Team knows SQLYou already know the access patterns

Interview tip: “I’d start with Postgres. If redirect QPS explodes, the mapping can move to a KV/cache tier; payments stay on SQL.”


Chooser 4 — Sync vs async?

If the user…Prefer
Must see success/failure nowSync (or sync for core + async for extras)
Can wait for email / notificationAsync
Uploads a large fileSync ACK of upload + async processing
Books a seatSync hold; async email ticket

Split brain pattern (very common):

Sync:  validate → write critical truth → return 200
Async: notify, analytics, search index, thumbnails

Chooser 5 — Fan-out on write vs on read?

Used for feeds, notifications, timelines.

Fan-out on writeFan-out on read
Fast reads (timeline ready)Cheap writes
Expensive for celebritiesSlower/more work at read time
Great for average usersGreat for mega-popular accounts

Phrase: “Hybrid — because celebrity fan-out would melt the write path.”

See News Feed after you try this chooser yourself.


Chooser 6 — Push (WebSocket) vs pull (HTTP poll)?

NeedPrefer
Chat, presence, live collabWebSocket / SSE (push)
Occasional updates, simple clientsShort polling or version checks
One-way server events, simpler than WSServer-Sent Events

Beginner default: HTTP for request/response; add WebSockets only when latency/push is a real requirement.


Quick reference card (print this mentally)

QuestionBeginner default
Queue?Only if you named an async job
Cache?Yes if read-heavy + stale OK briefly
CDN?Yes if media/static
DB?Postgres first
Sync vs async?Sync critical truth; async side effects
Fan-out?Write for normal users; hybrid for celebs
Realtime?WebSocket only if product needs push

Practice: pick without a case study

For each prompt, write choice + reason (2 minutes each):

  1. “Users upload 4K video; others stream it.” — queue? CDN?
  2. “Like button on posts, 100:1 read:write.” — cache? exact counts?
  3. “Seat booking for a hot concert.” — SQL transaction? Redis hold?
  4. “Celebrity with 50M followers posts.” — fan-out strategy?
Sample answers (not the only right ones)
  1. Queue for transcoding; CDN + object storage for playback segments. Upload ACK sync; encode async.
  2. Cache post pages / counts; counts can be approximate or updated async — don’t take a write lock on every like if scale is huge.
  3. Short-lived hold in Redis or locked row; confirm in SQL with idempotent payment — correctness on inventory matters.
  4. Hybrid fan-out — don’t write to 50M timelines synchronously; celebs pulled at read time or processed with special workers.

Check your understanding

  1. When is a queue the wrong tool?
  2. Why is “cache as source of truth” dangerous?
  3. Why start with SQL in interviews even if you know Cassandra?
Answers
  1. When the user needs the result in the same request and the work is light; or when you’re using a queue to avoid designing transactions.
  2. Cache can be flushed, evicted, or stale; money/inventory must live in a durable store with clear rules.
  3. SQL fits most CRUD + transaction stories; Cassandra is a scaling choice you justify with access patterns and team cost — not a default flex.

Next: First 10 minutes — narrated walkthroughs for five classic prompts.