open atlas
↑ Back to track
System Design Case Studies SDC · 02 · 07

Social-feed cases: reading config and code

Read real config and code from the four cases, then pick the senior fix: a retry loop missing dedup, an offset-paginated feed, a chat handler that acks before persisting, and an autocomplete that ranks on the read path.

SDC Senior ◷ 14 min
Level
FoundationsJuniorMiddleSenior

The bugs in social systems hide in a few lines: a retry without a dedup check, an OFFSET in a feed query, an ack before a write, a database call on the read path. Read each snippet, trace what happens at scale, and pick the change a senior engineer would commit.

Practise the design-review loop: locate the load-bearing line, reason about what it does under millions of events, and choose the fix that addresses the failure mode rather than papering over it.

Snippet 1 — the notification retry loop

def deliver(msg):
    for attempt in range(5):
        try:
            provider.send(msg)          # may time out / 429
            return
        except Retryable:
            sleep(2 ** attempt)         # exponential backoff
    dead_letter.put(msg)
Quiz

This has bounded retries, backoff, and a DLQ. After a provider outage recovers, users still get duplicate pushes. What is missing?

Snippet 2 — the feed query

SELECT post_id FROM feed
WHERE user_id = :uid
ORDER BY created_at DESC
LIMIT 20 OFFSET :n;     -- n grows as the user scrolls
Quiz

Deep scrolls get slow and users report posts appearing twice or being skipped. What is the cause and fix?

Snippet 3 — the chat send handler

def on_message(ws, msg):
    ack(ws, msg.client_id)              # tell sender: delivered ✓
    seq = persist(msg)                  # assign sequence, store
    route_to_recipient(msg, seq)
Quiz

What is the correctness bug, and how should the handler be reordered?

Snippet 4 — the autocomplete endpoint

def suggest(prefix):
    rows = db.query(
      "SELECT q FROM search_log WHERE q LIKE %s GROUP BY q ORDER BY count(*) DESC LIMIT 10",
      prefix + "%")            # runs on every keystroke
    return [r.q for r in rows]
Quiz

This ranks matching queries live on every keystroke. It is 600 ms slow and overloads the cluster. What is the fix?

Recall before you leave
  1. 01
    Why does a retry loop with backoff and a DLQ still duplicate, and why is OFFSET wrong for a feed?
  2. 02
    Why must a chat handler persist before it acks, and why can't autocomplete rank on the read path?
Recap

The four snippets each hide a one-line failure. The notification retry loop has backoff and a DLQ but no idempotency key, so a re-flushed backlog duplicates — retries are at-least-once, dedup must guard the send. The feed query uses OFFSET, which scans n rows (slow at depth) and shifts under inserts (skips/dupes); the fix is cursor pagination. The chat handler acks before persisting, so a crash loses the message under a confident tick; persist (and assign the sequence) first, then ack. The autocomplete endpoint ranks live per keystroke over the log, which cannot meet the budget or the load; precompute top-k per trie node offline and serve an O(1) read. The senior habit: find the load-bearing line, reason about it at scale, and apply the fix the failure mode demands.

Connected lessons

Something unclear?

Ask a question about this lesson. Questions are anonymous and go straight to the author to make the lesson better.

shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.