Josue Kouka

Josue Kouka

Code, Linux, and open source — one post at a time.

Interview Prep — Part 4: System Design & Distributed Systems

Part 4 of 8 · Interview Prep · ← Previous: Part 3 — Databases & SQL · Next: Part 5 — System Design Case Studies →

System design is the round people dread, and mostly for the wrong reason. It isn’t a memory test. It’s a test of whether you can say what breaks first and what you’d do about it. These 36 questions build that up from the fundamentals. The full case studies live in Part 5.

What this assumes: you’ve written an API that talks to a database, and you know what a query, an index, and an HTTP request are. Sharding, quorums, and Raft all get explained here.

What you should be able to do after: sketch a system on a whiteboard and defend the three decisions the interviewer will actually push on.

Every answer opens with The gist, one or two plain sentences. If the gist is all you have time for, that’s still worth more than a half-remembered detail. The full answer underneath is what you say when they ask you to go deeper.

Many answers end with Ask:, the clarifying question that tells you this concept is in play. Knowing what a quorum is doesn’t help if you never ask the question that calls for one. Those questions are what you take into step 1 of Part 5.

On this page
In quiz mode, click a question to reveal its answer.

Key Concepts: What to Reach For

Not questions, a map. Skim it to see how the pieces relate, then come back to it later to find the question you need.

Each row is a concept and the situation that should make you reach for it, so you can orient before drilling into the “why” in the questions. The numbers point to where each one is covered in depth.

Scaling & data distribution

ConceptWhen to reach for it
SQL vs. NoSQLChoosing the storage model itself, before sharding or caching even enter the picture (Q11, Q12).
Horizontal scalingYou’ve hit a ceiling on one box and the workload is stateless enough to split across many (Q1).
Sharding / horizontal partitioningA single database’s write throughput or storage has become the bottleneck (Q3).
Consistent hashingNodes get added and removed, and you need only a fraction of keys to remap rather than almost all of them (Q27).
Caching: cache-aside / write-through / write-behindReads vastly outnumber writes and some staleness is tolerable. Pick the variant by how fresh reads need to be versus how fast writes need to be (Q10).
Hot keys and skewOne user, tenant, or item gets far more traffic than the rest, and adding shards doesn’t help (Q36).
CDN and edge cachingUsers are far from your servers and the content is the same for everyone (Q34).
L4 vs. L7 load balancingL4 when you just need fast, protocol-agnostic distribution; L7 when routing needs to look at path, host, or cookies (Q9).

Consistency & coordination

ConceptWhen to reach for it
CAP theoremFraming any “what happens during a network partition” design decision (Q8).
PACELCSame question, but you also need to reason about the latency/consistency trade-off when there’s no partition (Q22).
Consistency spectrum (strong / causal / read-your-writes / eventual)Picking the cheapest guarantee that’s still correct for the feature: a like count needs less than a balance check (Q2, Q29).
Quorum reads/writes (N/R/W)You’re replicating across N nodes and want a tunable guarantee that reads see the latest write (Q23).
Distributed locksCoordinating mutual exclusion across processes, not just goroutines in one process (Q13).
Leader election / RaftA cluster of nodes needs to agree on exactly one coordinator, and that agreement must survive crashes (Q14, Q28).
Clock skew / logical clocksYou need to order events across machines and wall-clock timestamps aren’t reliable enough (Q16).

Messaging & delivery

ConceptWhen to reach for it
Sync vs. async communicationSync when you need an immediate answer and can accept coupling to the callee’s availability; async when you can decouple in time (Q6).
BackpressureA fast producer would otherwise overwhelm a slow consumer’s buffer (Q7).
Idempotency & idempotency keysA client might retry a request and you need retries to be safe (Q5, Q24).
Delivery semantics (at-most / at-least / exactly-once)Deciding what a message queue actually guarantees you, and what your own code still has to guarantee on top (Q15).
Outbox patternYou need a DB write and a message publish to succeed or fail together (Q25).
Saga vs. two-phase commitA transaction spans multiple services and holding locks across all of them (2PC) is too heavyweight (Q26).
Polling vs. long polling vs. SSE vs. WebSocketThe server has news for the client. Pick by how fast it must arrive and whether the client talks back (Q33).
Kafka vs. RabbitMQ vs. NATSKafka for a durable, replayable event log; RabbitMQ for flexible routing and per-message ack/retry; NATS for low-latency, lightweight pub/sub (Q4).
Gossip protocolsDetecting node failure across a large cluster without a central bottleneck (Q21).

Resilience & failure handling

ConceptWhen to reach for it
p99 and availability ninesTurning “fast” and “always up” into numbers you can design against (Q31).
Single point of failure, redundancy, failoverAny box on the diagram that there’s only one of (Q32).
Rate limiting algorithmsProtecting an endpoint from abuse or overload, and choosing between a smooth rate and an allowed burst (Q35).
Thundering herd mitigation (jitter, coalescing)Many clients would otherwise retry or wake up at the same instant against a recovering resource (Q17).
Bulkhead vs. circuit breakerBulkhead to stop one dependency’s failure from starving resources shared with everything else; circuit breaker to stop calling a dependency that’s clearly already failing (Q30).
Byzantine vs. crash fault toleranceByzantine only matters in adversarial or untrusted settings; infrastructure you control just needs crash-fault tolerance (Raft/Paxos) (Q18).
Two Generals’ ProblemReasoning about why perfect agreement over an unreliable channel is impossible, and why we settle for retries plus idempotency instead (Q19).
CRDTsReplicas need to accept writes independently (offline-first, multi-region) and eventual convergence is good enough (Q20).

Applied patterns (case studies in Part 5)

For the problem-level patterns behind these, like scaling reads or dealing with contention, and when to reach for each, see the patterns in Part 5.

PatternUse case it solves
Read-heavy key lookup + cache in front of DBURL shortener (Q1).
Shared atomic counter (Redis + Lua)Rate limiting across multiple gateway instances (Q2).
Bounded parallel fan-out with a deadlineAggregating many signals under a tight latency SLA (Q3).
Queue-based fan-out per channelNotifying millions of users across push/email/SMS (Q4).
Idempotency key + unique constraintPayment processing that survives retries without double-charging (Q5).
Atomic claim + lease/heartbeatA job scheduler where each job runs exactly once, even across crashes (Q6).
Consistent hashing + pub/subA sharded, horizontally-scalable chat backend (Q7).
Partition by natural key + idempotent upsertHigh-throughput event ingestion without double-counting on reprocess (Q8).
Strangler figMigrating a monolith to microservices incrementally, without a big-bang rewrite (Q9).
Hash-chained append-only logTamper-evident, queryable audit logging (Q10).

System Design Fundamentals

1 to 7 are vocabulary you’ll use in every design conversation. 8 to 10 are the ones you should be able to draw from memory. 11 and 12 are the decision that comes before any of it: what kind of database this even is.

1. Difference between vertical and horizontal scaling, and when does horizontal stop being trivial?

The gist: vertical is a bigger machine, horizontal is more machines. Horizontal is easy right up until state enters the picture, and then state is the whole problem.

Vertical scaling means a bigger machine: simple, but it has a hard ceiling. Horizontal scaling means more machines sharing load, and it stops being trivial the moment state enters the picture. Stateless request handling scales linearly, but a stateful component needs partitioning, replication, or coordination logic. Ask: “How many requests a second at peak, and does any server keep state between requests?”

2. Explain eventual consistency with a good-fit and bad-fit example.

The gist: replicas agree eventually, not instantly. Fine for a like count, dangerous for a balance check right before a withdrawal.

Eventual consistency means replicas will converge given enough time without new writes, but reads shortly after a write might see stale data.

Good fit: a like count. Bad fit: an account balance check right before authorizing a large withdrawal. Ask: “Can this read be stale, and for how long?”

3. Difference between horizontal and vertical partitioning (sharding) of a database?

The gist: vertical splits a table by columns, horizontal (sharding) splits it by rows across machines. Same word, two completely different problems.

Vertical partitioning splits a table by columns: same rows, different tables. Horizontal partitioning (sharding) splits a table by rows across multiple database instances based on a shard key, so each shard holds a subset of rows but the full schema.

What they’re testing: whether you keep the two straight under pressure. Part 3 covers partitioning versus sharding from the single-instance side, and interviewers like asking both. Ask: “How big does the data get, and which key shows up in every query?”

4. When would you choose Kafka vs RabbitMQ vs NATS?

The gist: Kafka is a replayable log, RabbitMQ is a smart work queue, NATS is fast and light. Pick by whether you need history, routing, or raw speed.

Kafka: high-throughput, durable, replayable event log, ideal for event sourcing and stream processing. RabbitMQ: flexible routing, strong work-queue semantics with per-message ack and retry. NATS: extremely low latency, lightweight pub/sub. Ask: “Do we need to replay old events, or just hand each job to one worker?”

5. Explain idempotency and why it matters in distributed systems.

The gist: doing it twice gives the same result as doing it once. You need it because a client whose request times out genuinely can’t tell whether it worked.

An idempotent operation produces the same end result no matter how many times it’s applied. That’s critical because network failures mean clients can’t always tell if a request succeeded, so they have to be able to retry safely.

What they’re testing: whether you connect it to retries. Idempotency isn’t a nice property in the abstract, it’s what makes a retry safe, and retries are unavoidable. Ask: “Can the client retry this, and what happens if it runs twice?”

6. Synchronous vs asynchronous communication between services: what are the tradeoffs?

The gist: sync gives you an answer now and ties your uptime to theirs. Async decouples you, and hands you retries, ordering, and eventual consistency to deal with instead.

Synchronous is simpler and gives immediate feedback, but it couples the caller’s availability to the callee’s. Asynchronous decouples services in time, at the cost of eventual consistency and more complex failure, retry, and ordering handling. Ask: “Does the caller need the answer now, or is “accepted, we’ll get to it” enough?”

7. Explain backpressure and why a fast producer plus a slow consumer needs it.

The gist: a way for a slow consumer to say “slow down” instead of silently buffering until something runs out of memory.

Backpressure is a mechanism for a slow consumer to signal a fast producer to slow down, preventing unbounded buffering that leads to memory exhaustion or unbounded latency growth.

// A bounded channel is backpressure. Once 100 items are queued,
// the producer blocks on send until the consumer catches up.
jobs := make(chan Job, 100)

// Unbounded would be make(chan Job) fed by an ever-growing slice:
// the producer never blocks, and memory grows until the process dies.

Try it: run the bounded-channel snippet with a producer faster than the consumer, and watch len(jobs) climb to the buffer size and stay there instead of growing without bound. Ask: “When work arrives faster than we can handle it, do we drop it, queue it, or reject it?”

8. Explain CAP theorem with a concrete example.

The gist: when the network splits, you pick one: keep answering with possibly-stale data, or refuse to answer rather than risk being wrong. You don’t get both.

Under a network partition, a distributed system must choose between Consistency (every read sees the latest write) and Availability (every request gets a response, even if possibly stale).

Choosing AP: a shopping cart service that keeps accepting writes during a partition and reconciles later. Choosing CP: a bank ledger balance check that would rather return an error than risk showing a stale balance.

graph TD
    Client --> Router
    Router --> A[Replica A]
    Router --> B[Replica B]
    A -. network partition .- B
    CP["CP choice: reject write, return error until healed"]
    AP["AP choice: accept write, reconcile later"]

What they’re testing: that you know “CA” isn’t a real option. Partitions aren’t a design choice, they’re weather, so the only question is what you do when one happens. Ask: “During a network split, do we show an error or old data?”

9. Explain L4 vs L7 load balancing.

The gist: L4 forwards packets by IP and port without opening them. L7 reads the HTTP request and can route on path or host. Speed versus smarts.

L4 (transport layer) balances based on IP, port, and TCP/UDP-level info without inspecting request content, which makes it fast and protocol-agnostic. L7 (application layer) understands HTTP and can route based on path, host header, or cookies: more flexible, but it adds processing overhead.

graph TD
    subgraph "L4 Load Balancer: transport layer"
    C1[Client] --> LB1["LB: IP / port only"]
    LB1 --> S1[Server 1]
    LB1 --> S2[Server 2]
    end
    subgraph "L7 Load Balancer: application layer"
    C2[Client] --> LB2["LB: inspects path / host / headers"]
    LB2 -->|"/api/v1/*"| SA[Service A]
    LB2 -->|"/api/v2/*"| SB[Service B]
    end

Ask: “Do we route by path or host, or just spread connections?”

10. Explain caching strategies: cache-aside, write-through, write-behind.

The gist: three answers to “when does the database get the write?” Cache-aside fills on a miss, write-through writes both at once, write-behind writes later and hopes nothing crashes.

Cache-aside: the app checks the cache first, and on a miss reads from the database and populates the cache. Write-through: writes go to the cache and database synchronously together, so reads are always fresh but writes are slower. Write-behind: writes go to the cache immediately and are asynchronously flushed to the database later, which is fastest for writes but risks data loss if the cache fails before flushing.

graph TD
    subgraph "Cache-aside"
    App1[App] -->|"1: read, miss"| Cache1[(Cache)]
    App1 -->|"2: read"| DB1[(DB)]
    App1 -->|"3: populate"| Cache1
    end
    subgraph "Write-through"
    App2[App] -->|write| Cache2[(Cache)]
    Cache2 -->|sync write| DB2[(DB)]
    end
    subgraph "Write-behind"
    App3[App] -->|write| Cache3[(Cache)]
    Cache3 -. async flush .-> DB3[(DB)]
    end

Try it: implement cache-aside against redis-cli: GET a key, on a miss read from your database and SETEX it with a short TTL, then GET it again and confirm the second read never touches the database. Ask: “How fresh must a read be, and can we ever lose a write?”

11. SQL vs. NoSQL: what’s actually different, and when would you choose each?

The gist: SQL enforces a schema up front and gives you joins plus multi-row ACID transactions. NoSQL relaxes some of that on purpose, in exchange for horizontal scale and a data model that’s cheap to change.

A relational (SQL) database enforces a schema before you write anything, supports arbitrary joins across normalized tables, and gives strong ACID transactions across multiple rows and tables at once. That structure is also the constraint: scaling writes horizontally means sharding, and sharding is what breaks the cross-shard joins and transactions the model was built around.

A NoSQL database relaxes one or more of those guarantees deliberately, usually the schema, the joins, or the multi-row transaction, to get horizontal scalability and a data model that’s cheaper to change as the product changes. Most trade toward BASE (Basically Available, Soft state, Eventually consistent) instead of ACID, the same trade Q2 covers from the consistency side.

Choose SQL when the data has real relationships you’ll query across, and correctness under concurrent writes matters more than raw write throughput: orders, payments, inventory. Choose NoSQL when the access pattern is simple and known ahead of time (fetch by key, append an event), the schema keeps changing, or a single relational instance can’t hold the write volume or connection count.

What they’re testing: whether “NoSQL is for scale” is the whole answer you have, or whether you’ll also say what you give up to get it. Plenty of NoSQL migrations get quietly undone once someone needs a join that used to be free.

Try it: take one table from a project you know well and write down every join it currently makes. Each one becomes application-code work, or duplicated data to avoid it, the moment that table moves to a non-relational store. Ask: “What are the queries, and do they join across entities?”

12. What are the main types of NoSQL databases, and what does each one actually fit?

The gist: “NoSQL” isn’t one data model, it’s four unrelated ones that all happen to skip a fixed relational schema. The type decides what it’s good at, not the “NoSQL” label.

Key-value (Redis, DynamoDB in its simplest mode): a hash map with a network in front of it. Fast and simple, and only good for lookups by a known key: sessions, caches, feature flags.

Document (MongoDB): stores semi-structured JSON-like documents, each one self-contained. Fits data that’s naturally a single nested object, read and written as a whole, whose shape varies row to row: a product catalog, a user profile.

Wide-column (Cassandra, Bigtable): a row can have millions of columns, and each row is looked up by a partition key you choose deliberately, since that key decides which node holds the row. Fits huge write volume with a small, predictable set of query patterns: time-series data, event logs.

Graph (Neo4j): stores nodes and the relationships between them as first-class citizens, so a query that would be a five-table join in SQL, “friends of friends who also like X,” is a single traversal. Fits data where the relationships matter more than the entities: social graphs, fraud rings, recommendation engines.

Key-value:   {key -> value}                     lookup by key only
Document:    {_id: 1, name: "x", tags: [...]}   lookup + query within a document
Wide-column: (partition key, columns...)        huge write volume, known query shape
Graph:       (node)-[relationship]->(node)      relationships are the query

What they’re testing: whether you pick the type from the access pattern, not the brand name. “We use MongoDB” doesn’t answer “why document over wide-column,” and that follow-up is the real question.

Try it: take a feature you’ve built that used a relational table, and work out which of the four types, if any, actually fits its real query pattern better, and specifically why: does that query need a join, a range scan on a partition key, or a graph traversal? Ask: “What’s the one query this store must answer fast?”


Distributed Systems Concepts

The deep end, and nobody expects a junior to have all eighteen. But 15, 17 and 24 show up in ordinary backend work long before anyone calls it distributed systems, so start there.

13. Explain a distributed lock, and why plain mutexes don’t work across services.

The gist: a mutex only coordinates goroutines inside one process. Once two servers need to agree, the lock has to live somewhere they can both see, with a TTL so a crash doesn’t wedge it forever.

A sync.Mutex only coordinates goroutines within a single process’s memory space. A distributed lock uses a shared external system (Redis Redlock, or etcd and Zookeeper with consensus-backed leases) so multiple independent processes can agree on mutual exclusion, typically with a lease or TTL.

Try it: acquire a lock with SET lock:job1 owner NX EX 10 in redis-cli from two terminals. Only one returns OK; the other gets nil and knows to back off. Ask: “Can two servers do this at the same time, and what breaks if they do?”

14. What’s leader election, and how does it typically work?

The gist: getting a group of nodes to agree on exactly one boss, and to pick a new one quickly when that boss dies.

It’s a mechanism for a group of distributed nodes to agree on exactly one leader. Typically it’s implemented via a consensus protocol like Raft: nodes propose themselves, a majority quorum must agree, and if the leader fails to renew its lease, a new election triggers. Ask: “Must exactly one node do this job at a time?”

15. At-most-once vs at-least-once vs exactly-once delivery, and why is exactly-once mostly a myth?

The gist: at-most-once can lose messages, at-least-once can repeat them, exactly-once is mostly marketing. You get the effect of exactly-once by making repeats harmless.

At-most-once means a message might be lost. At-least-once means it’s guaranteed delivered, but possibly more than once. Exactly-once is extremely hard end to end. In practice, systems achieve “effectively exactly-once” by combining at-least-once delivery with idempotent processing.

What they’re testing: whether you push back on the phrase. A broker that advertises exactly-once is describing its own hop, not your handler, and your handler still has to be idempotent. Ask: “Which is worse here: a lost message or a duplicate?”

16. How do clock skew and distributed time affect ordering of events across services?

The gist: two machines’ clocks disagree, so you can’t order events by comparing timestamps. Use logical clocks, or one thing that hands out the order.

Wall-clock timestamps from different machines aren’t reliably comparable, because of clock drift. The solutions are logical clocks (Lamport or vector clocks) that capture causal ordering, or a centralized sequencer or monotonic ID source when a single global order is genuinely required. Ask: “Does order matter across the whole system, or only per user or per conversation?”

17. What is the thundering herd problem, and how do jitter and coalescing prevent it?

The gist: everyone retries at the same instant and re-kills the thing that just came back up. Jitter spreads them out, coalescing makes many callers share a single request.

Thundering herd is when a large number of clients wake up or retry simultaneously, overwhelming a resource that was just recovering.

Jittered backoff randomizes retry timing. Request coalescing (single-flight) makes sure only one actual request goes to the backend when many callers want the same resource.

What they’re testing: whether you’d catch it in your own retry code. Plain exponential backoff without jitter still synchronizes every client on the same schedule, which is the trap.

Try it: wrap a cache-refill call in Go’s singleflight.Group, fire 50 concurrent goroutines requesting the same key on a miss, and confirm only one of them actually reaches the database. Ask: “What happens when the cache is empty, or when every client reconnects at once?”

18. What’s the difference between Byzantine and crash-fault tolerance, and why do Raft and Paxos only handle the latter?

The gist: crash faults mean a node goes silent. Byzantine means it lies. Raft handles silence, and silence is all you need for servers you own.

Crash-fault tolerance assumes a failed node simply stops responding, so it never lies. Byzantine fault tolerance assumes a node can behave arbitrarily: send conflicting messages to different peers, claim false state, or act maliciously.

Raft and Paxos only tolerate crash faults, which is why they’re safe for trusted infrastructure you control (your own replica set) but insufficient for adversarial settings like blockchain consensus, which need BFT protocols such as PBFT or Tendermint instead.

19. What is the Two Generals’ Problem, and what does it actually prove about distributed consensus?

The gist: you can never be certain your last message arrived, no matter how many acknowledgements you trade. So real systems stop chasing certainty and use retries plus idempotency.

Two armies must attack a city simultaneously to win, but can only coordinate via messengers who might be captured, which stands in for messages that might be lost. No matter how many acknowledgments are exchanged, neither general can ever be 100% certain the other received the final ACK, so perfect agreement over an unreliable channel is provably impossible.

In practice, systems don’t solve this. They work around it with retries, timeouts, and idempotency, accepting a vanishingly small but nonzero risk instead of a mathematical guarantee.

20. What is a CRDT, and when would you reach for one instead of a distributed lock?

The gist: a data structure built so concurrent edits always merge to the same answer, with no coordination. Great for offline-first apps, useless for “balance never goes negative.”

A Conflict-free Replicated Data Type is a data structure (counter, set, map) designed so that concurrent updates from different replicas can always be merged deterministically into the same final state, without coordination or locking. A grow-only counter that merges by taking the max per-replica count is the standard example.

Reach for one when replicas need to accept writes independently (offline-first apps, multi-region writes) and eventual convergence is good enough. Skip it when you need a strict invariant a CRDT can’t express, like “balance never goes negative.”

Try it: implement a grow-only counter as a map of replica ID to count, merge two replicas’ maps by taking the max per key, and confirm the result is the same no matter which order you merge them in. Ask: “Can users write while offline, or in two regions at once?”

21. How does a gossip protocol detect node failure, and how does that differ from a centralized health check?

The gist: each node pings a few random peers and passes on what it heard. Failure news spreads like a rumour, with no central health checker to fall over.

Each node periodically pings a few random peers and forwards what it’s heard about others’ health, so failure information spreads epidemic-style across the cluster in O(log N) rounds, without a central bottleneck or single point of failure.

A centralized health-check registry is simpler to reason about, but it doesn’t scale as well and becomes a single point of failure itself. Gossip (for example SWIM) trades a small amount of detection latency for horizontal scalability and resilience.

22. What does PACELC add to CAP theorem?

The gist: CAP only covers what happens during a partition. PACELC adds the other 99.9% of the time, when you’re still trading latency against consistency.

CAP only describes the tradeoff during a network partition (P). PACELC extends it: if Partitioned, choose Availability or Consistency, as in CAP. Else, even with no partition at all, choose Latency or Consistency, since synchronously confirming a write across replicas for strong consistency always costs latency versus acknowledging locally and replicating asynchronously.

It’s a more complete lens for classifying real systems. DynamoDB is PA/EL, while a synchronously-replicated SQL cluster is PC/EC.

What they’re testing: whether you notice that CAP describes a rare event. Most of your latency budget is spent in the “else” branch, which is the half CAP says nothing about.

23. Explain quorum-based consistency (N/R/W).

The gist: write to W replicas, read from R, and if R + W is greater than N your read is guaranteed to touch at least one replica holding the newest write.

N is the total number of replicas. W is how many replicas must acknowledge a write before it’s considered successful. R is how many replicas a read must query and reconcile before returning a result. If R + W > N, every read is guaranteed to overlap with the most recent write’s replica set.

sequenceDiagram
    participant Client
    participant Coordinator
    participant R1 as Replica 1
    participant R2 as Replica 2
    participant R3 as Replica 3
    Client->>Coordinator: write
    Coordinator->>R1: write
    Coordinator->>R2: write
    Coordinator->>R3: write
    R1-->>Coordinator: ack
    R2-->>Coordinator: ack
    Note over Coordinator: N=3, W=2 acks to commit, R=2 reads, R+W>N

24. How would you design idempotency keys for a payment API?

The gist: the client sends a unique key with the request. The server claims that key atomically before charging anything, so a retry finds the existing claim and replays the old answer instead of charging twice.

Require the client to generate a unique idempotency key per logical operation and send it in the request header. The server checks a store keyed on that idempotency key: if it’s been seen before, return the previously stored result; if not, atomically claim the key before doing the actual charge.

INSERT INTO payment_requests (idempotency_key, status)
VALUES ($1, 'processing')
ON CONFLICT (idempotency_key) DO NOTHING;

-- 0 rows affected means a request with this key is already in
-- flight or finished. Look up and return its stored result
-- instead of charging again.

The unique constraint is doing the real work here. Two concurrent retries both run this statement, and exactly one of them gets a row back.

What they’re testing: that the claim happens before the charge. Claiming afterwards leaves a window where a retry lands mid-charge, which is the bug this whole design exists to prevent. Q5 in Part 5 walks the same idea through a full pipeline.

Try it: run the INSERT ... ON CONFLICT DO NOTHING statement above twice with the same key from two separate terminals at roughly the same time, and confirm only one reports a row inserted. Ask: “If the client times out and sends it again, do we charge twice?”

25. Explain the outbox pattern for reliably publishing events after a DB write.

The gist: you can’t write to the database and publish to a broker atomically. So write the event into the same database in the same transaction, and let a separate relay publish it afterwards.

Instead of writing to the database and then separately publishing to a message broker (which can fail independently), you write the business change and an outbox event row in the same database transaction. A separate poller or relay process reads unpublished outbox rows, publishes them, and marks them sent.

tx, _ := db.BeginTx(ctx, nil)
tx.Exec(`UPDATE accounts SET balance = balance - $1 WHERE id = $2`,
    amount, from)
tx.Exec(`INSERT INTO outbox (event_type, payload, published)
         VALUES ($1, $2, false)`, "FundsDebited", payload)
tx.Commit() // business change + outbox row commit atomically

// separate relay process:
// SELECT * FROM outbox WHERE published = false
// -> publish to broker -> mark published = true
graph LR
    App -->|same DB txn| Tables[("Business table +<br/>Outbox table")]
    Tables --> Relay["Relay / Poller"]
    Relay -->|publish| Broker[(Message Broker)]
    Relay -. mark published .-> Tables

What they’re testing: whether you can name the dual-write problem. Two writes to two systems can’t both be guaranteed, so the trick is to make it one write and move the second one downstream.

Try it: insert a business row and an outbox row in one transaction, then run the relay’s SELECT * FROM outbox WHERE published = false by hand and confirm your new event is sitting there waiting to be published. Ask: “Does another service need to hear about this write, and can it ever miss it?”

26. What is a saga pattern, and when would you use it instead of 2PC?

The gist: split the distributed transaction into local steps, each with an undo step. Harder to reason about than 2PC, but nobody holds locks across services while a human approves something.

A saga breaks a distributed transaction into a sequence of local transactions, each with a compensating action to undo it if a later step fails, instead of holding locks across services the way two-phase commit does.

graph TD
    subgraph "2PC: coordinator-driven"
    Coord[Coordinator] -->|"1: prepare"| PA[Participant A]
    Coord -->|"1: prepare"| PB[Participant B]
    PA -->|vote yes| Coord
    PB -->|vote yes| Coord
    Coord -->|"2: commit"| PA
    Coord -->|"2: commit"| PB
    end
    subgraph "Saga: local txns + compensations"
    S1["Step 1: Reserve Inventory"] --> S2["Step 2: Charge Payment"]
    S2 --> S3["Step 3: Ship Order"]
    S2 -. failure .-> C1["Compensate: Release Inventory"]
    S3 -. failure .-> C2["Compensate: Refund Payment"]
    end

Ask: “Does one user action change data in more than one service?”

27. Explain consistent hashing and why it’s used for sharding and caching.

The gist: put nodes and keys on a ring, so adding a node only moves the keys sitting next to it. Plain hash % N moves almost every key the moment N changes.

Consistent hashing maps both nodes and keys onto a hash ring, and a key is owned by the next node clockwise on the ring. When a node is added or removed, only the keys between it and its neighbor need to move. With naive hash(key) % N sharding, changing N remaps almost every key, which means a cache that’s suddenly all misses.

graph LR
    N1((Node 1)) --> N2((Node 2)) --> N3((Node 3)) --> N4((Node 4)) --> N1
    KeyA["key A (hash)"] -. owned by .-> N2
    KeyB["key B (hash)"] -. owned by .-> N4

Try it: hash 10 keys against hash(key) % 4 and again against % 5, and count how many land on a different node. Then put the same 4 nodes on a ring and add a fifth: far fewer keys move. Ask: “Will nodes be added or removed while it’s running?”

28. Walk through Raft leader election and log replication in more depth.

The gist: nodes vote for a leader each term, the leader replicates writes, and an entry only counts as committed once a majority has stored it. That majority rule is what makes committed writes survive any single crash.

Every node is a Follower, Candidate, or Leader, and time is divided into monotonically increasing terms. A Follower that hears no heartbeat before its election timeout becomes a Candidate, increments the term, and requests votes. It becomes Leader on receiving votes from a majority.

The Leader then replicates every write as a log entry to followers. An entry is only committed (safe to apply) once a majority of nodes have persisted it, and the Leader tracks a commit index that only advances forward.

If a Leader crashes mid-replication, the next elected Leader is guaranteed to already have every committed entry, because of the election rule that a node only votes for candidates with an equally or more up-to-date log. So no committed write is ever lost.

stateDiagram-v2
    [*] --> Follower
    Follower --> Candidate: election timeout, no heartbeat
    Candidate --> Leader: wins majority of votes
    Candidate --> Follower: sees higher term / loses election
    Leader --> Follower: discovers higher term
    Follower: Follower replicates the leader's log
    Candidate: Candidate requests votes for a new term
    Leader: Leader replicates writes, sends heartbeats

What they’re testing: the follow-up, which is always “what happens if the leader crashes mid-write.” The answer is the election restriction: a candidate missing committed entries can’t win a majority.

29. Where do strong, causal, and eventual consistency sit relative to each other, and what is “read-your-writes”?

The gist: strong means every read sees the newest write and costs the most. Eventual costs the least and may hand you stale data. Causal and read-your-writes are the useful middle ground.

Strong consistency: every read sees the latest committed write, as if there were only one copy of the data. Most expensive, since it needs coordination on every read or write.

Eventual consistency: replicas converge given enough time with no new writes, but a read shortly after a write can return stale data. Cheapest, no coordination needed.

Causal consistency sits between them: operations that are causally related (a reply to a comment) are seen by everyone in that order, but unrelated operations can be seen in different orders on different replicas.

Read-your-writes is a narrower, practical guarantee often layered on top of eventual consistency. A specific client is guaranteed to see its own writes on its next read, for example by routing that client’s reads to the replica it wrote to, or via a sticky session, even if other clients might still see stale data.

graph LR
    Strong["Strong<br/>(linearizable)"] --> Causal["Causal<br/>(preserves cause→effect order)"] --> RYW["Read-your-writes<br/>(session-scoped)"] --> Eventual["Eventual<br/>(converges, no ordering guarantee)"]
    Strong -.->|"more coordination, higher latency"| Eventual

Ask: “Does a user need to see their own change right away, even if others see it later?”

30. Explain the bulkhead pattern, and how it differs from a circuit breaker.

The gist: a circuit breaker stops you calling something that’s already broken. A bulkhead stops that broken thing from eating the resources everything else needs.

A circuit breaker protects a single dependency by stopping calls to it once it’s clearly failing. A bulkhead protects everything else from that one dependency by isolating resources: a separate connection pool, goroutine pool, or thread pool per downstream, so a slow or exhausted dependency can only exhaust its own pool rather than starving requests to unrelated services in the same process.

The two compose. Bulkheads limit blast radius, and circuit breakers stop wasting the limited resources bulkheads gave that dependency on calls likely to fail anyway.

graph TD
    subgraph "No bulkhead: shared pool"
    ReqA1[Requests to A] --> Pool[Shared Worker Pool]
    ReqB1[Requests to B] --> Pool
    Pool --> SlowA[Slow Dependency A]
    Pool -.->|pool exhausted by A, B starves too| SlowB[Dependency B]
    end
    subgraph "Bulkhead: isolated pools"
    ReqA2[Requests to A] --> PoolA[Pool A]
    ReqB2[Requests to B] --> PoolB[Pool B]
    PoolA --> SlowA2[Slow Dependency A]
    PoolB --> FastB[Dependency B, unaffected]
    end

What they’re testing: whether you see them as complementary rather than alternatives. Answering “a bulkhead instead of a circuit breaker” is the wrong shape, you want both.

Try it: create two separate bounded worker pools in Go, saturate one with slow fake calls, and confirm requests to the other pool still complete on time. Ask: “If one dependency gets slow, what else is allowed to slow down?”


From Requirements to Design

Six concepts that turn a requirement into a design choice. Each one sits behind a question you’ll ask in step 1 of Part 5, and each one shows up in at least one case study there.

31. What do p50 and p99 mean, and what does 99.9% availability actually allow?

The gist: p99 is the latency that 99 requests out of 100 beat. Design for it, because the average hides the slow ones. And each extra nine of availability cuts your allowed downtime by ten.

Percentiles. Sort every request by how long it took. p50 is the middle one. p99 is the one slower than 99% of the rest. An average of 50ms can hide one request in a hundred taking 3 seconds.

The tail matters more than it looks. A page that makes 20 requests has an 18% chance of hitting at least one p99-slow request. So your p99 is what a lot of users feel on every page load.

Nines. Availability is the share of time the system answers:

AvailabilityDowntime a year
99%3.65 days
99.9%8.8 hours
99.99%53 minutes
99.999%5 minutes

99.9% lets you restart a box by hand. 99.99% doesn’t: nobody wakes up, logs in, and fixes anything inside 53 minutes a year, so failover has to be automatic.

Nines also multiply. A request that passes through four services in a row, each at 99.9%, gets about 99.6%. Every sync hop you add costs availability.

What they’re testing: whether you ask “p99 or average?” before you accept a latency target, and whether you know a chain is less available than its weakest link.

Ask: “Is that latency target the average or the p99, and how many nines do we need?”

32. What’s a single point of failure, and how do you remove one?

The gist: any box whose death stops the system. You remove it by running more than one, and by deciding ahead of time how the others take over.

Stateless boxes are easy. Run several app servers behind a load balancer. One dies, the others carry on.

Stateful boxes are the real work. A database needs a copy, which is replication (Part 3), and then a plan for the takeover:

  • Active-passive: one node serves, a standby follows it. On failure the standby is promoted. It’s simple, but there’s a gap during the switch, and with async replication the last few writes can be lost.
  • Active-active: every node serves. There’s no gap, but two nodes can accept conflicting writes, and now you have to resolve them.

The takeover itself needs care. If two nodes both think they’re the primary, you have split brain, which is why failover leans on leader election and a majority vote.

Don’t stop at the database. The load balancer, the one Redis, the DNS provider, and the region itself are all single points of failure until you’ve said otherwise.

What they’re testing: whether you find them without being asked. Walk the diagram box by box and ask “what if this dies?” out loud.

Ask: “If this box dies, what stops, and how long is it allowed to stay stopped?”

33. Polling, long polling, SSE, WebSocket: how does a server get news to a client?

The gist: four ways to do it, from simplest to most capable. Pick by how fast the update must arrive and whether the client needs to talk back on the same connection.

OptionHow it worksFits
PollingThe client asks every N seconds.Updates that can be N seconds late: a dashboard, a notification badge.
Long pollingThe server holds the request open until it has news or a timeout.Near real time over plain HTTP, when you can’t use anything newer.
SSEOne long-lived HTTP response, and the server pushes events down it. One way only.Live feeds, progress bars, streamed LLM output.
WebSocketOne long-lived connection, both sides send whenever they like.Chat, games, collaborative editing.

Polling is stateless, so it scales like any other HTTP endpoint. Its cost is wasted requests and a delay of up to N seconds.

The other three hold a connection open per client, and that changes your design. Servers now hold memory per connection, the load balancer has to handle long-lived connections, and a deploy or a crash drops thousands of clients at once. They all reconnect together, which is a thundering herd.

What they’re testing: that you don’t reach for WebSockets by reflex. A badge that updates every 30 seconds is a polling problem, and polling is a lot less to run.

Ask: “How fast must the update reach the user, and does the client need to send things back on the same channel?”

34. What is a CDN, and what should and shouldn’t go behind one?

The gist: a cache that sits close to the user. Great for anything many people fetch and few people change. No help for data that’s personal or changes every second.

A CDN keeps copies of your content in edge locations around the world, and sends each request to the nearest one. A 150ms round trip across a continent becomes a few milliseconds. On a miss, the edge fetches from your servers (the origin), stores the response for as long as the Cache-Control header allows, and serves the next request itself.

What fits: images, video, JS and CSS, downloads, and public API responses that are the same for everyone.

What doesn’t: per-user responses, writes, and anything that must be fresh to the second.

The hard part is the same as any cache: getting rid of stale copies. The clean trick is to never need to. Put a hash in the filename (app.3f9a.js), cache it for a year, and ship a new filename when the content changes. Purging by hand is for the things you can’t version.

A CDN also absorbs spikes. A launch that would flatten your origin is mostly cache hits at the edge.

What they’re testing: whether you know what it can’t cache. “Put a CDN in front” isn’t an answer until you’ve said which responses are the same for every user.

Ask: “Are users spread across regions, and is the content the same for everyone?”

35. Compare rate limiting algorithms: fixed window, sliding window, token bucket.

The gist: fixed window is the simplest, and it lets a double burst through at the boundary. Sliding window fixes the boundary. Token bucket allows a burst on purpose, and it’s the usual default.

  • Fixed window: one counter per client per minute. Cheap, one key. The flaw: a client sends N requests at 11:59:59 and N more at 12:00:00, so 2N in two seconds.
  • Sliding window log: store the timestamp of every request and count the ones in the last minute. Exact, but memory grows with traffic.
  • Sliding window counter: keep this window’s count and the last one’s, and weight the last one by how much of it still overlaps. Close to exact, two counters.
  • Token bucket: a bucket holds up to capacity tokens and refills at a steady rate. Each request spends one. The capacity is the burst you allow, the refill rate is the long-run limit.
  • Leaky bucket: requests wait in a queue that drains at a fixed rate. The output is perfectly smooth, and the price is added delay.
limit: 100 a minute

fixed window   |....100....|....100....|   200 can land around the boundary
token bucket   capacity 100, refill 1.67/sec: one burst of 100, then 1.67/sec

Inside one Go process, that’s golang.org/x/time/rate (Part 1, Q71). Across a fleet the counter has to be shared, which is the whole of Part 5, Q2.

What they’re testing: that you can name the boundary problem, and that you see a limit as two numbers, the burst and the sustained rate, not one.

Ask: “Is a short burst OK, or must the rate be smooth?”

36. What’s a hot key, and why doesn’t adding shards fix it?

The gist: sharding assumes every key is about as busy as the next. One celebrity, one big tenant, or one viral item breaks that, because all of its traffic lands on one node no matter how many nodes you add.

How you spot it: uneven load. One shard’s CPU is pinned, or one partition’s consumer lag keeps growing, while the rest sit idle.

What you do depends on whether it’s hot for reads or writes:

  • Hot read: cache it, and keep a short-lived copy inside each app server so most reads never leave the process.
  • Hot write: split the key with a suffix (game42#0 to game42#9) so writes spread over ten places, and merge them on read. You give up strict ordering for that key.
  • Hot counter: count in memory and flush once a second. A million likes become one write.
  • Hot tenant: give it its own shard, and keep the small tenants together.
  • A different path for the big ones: a feed that copies each post to every follower works for normal users. For a celebrity with 50M followers, you fetch their posts at read time instead.

A good shard key helps before any of this: many distinct values, spread evenly. But real traffic is rarely even, so plan for the outlier.

What they’re testing: whether you say “add more shards.” That’s the wrong answer, and the interviewer is waiting for it.

Ask: “Is the traffic spread evenly, or does one user, tenant, or item get most of it?”


What to drill first

System Design Fundamentals: 5 (idempotency) turns up in ordinary backend work every week, long before anyone asks you to design a distributed system. Practice actually drawing 8 through 10 from memory, not just describing them out loud. CAP and the three caching strategies are the two most likely to become a whiteboard request. 11 (SQL vs. NoSQL) is worth having an opinion on too, since most system design rounds ask you to pick a database before anything else.

Distributed Systems Concepts: 28 (Raft), 29 (the consistency spectrum), and 30 (bulkhead) are the ones most likely to turn into a follow-up “ok, now what if the leader crashes mid-write” question, so have those diagrams reflexive, not just recitable. 16, 17, and 23 through 27 are worth sketching too.

From Requirements to Design: 31 (p99 and nines) and 32 (single points of failure) come up in every design round, usually in the first five minutes. 36 (hot keys) is the follow-up to almost any sharding answer.

The Ask lines: read down them once without the answers. If you can say what a yes and a no would change in your design, you own the concept. If you can’t, reread that answer.

System Design Case Studies: they have their own part now. Start with Part 5, and draw each design on paper before reading the answer.


Part 4 of 8 · Interview Prep · ← Previous: Part 3 — Databases & SQL · Next: Part 5 — System Design Case Studies →