KatInterview

Load Balancing, Caching, and Idempotent API Design

System DesignModel answer
MediumModel answerSystem Designload-balancingcachingidempotency

Last updated

Design a multi-instance backend service covering routing policies, multi-layer caching strategies, and idempotent API support for non-repeatable operations like payments.

Scenario

Your team is rolling out a backend service that handles both read-heavy endpoints (e.g., fetching account data) and write endpoints where correctness is critical (e.g., initiating a payment or creating an order). The service runs on 10–20 stateless instances spread across multiple availability zones, sitting behind a load balancer, and is expected to handle roughly 5k RPS at steady state with 20k RPS peaks.

Answer all three sections below. For each one, go beyond definitions — explain when and why you'd make each design choice, and call out the failure modes.


Part 1: Routing Traffic Across Instances

  • What role does a load balancer play in this architecture?
  • Compare these routing strategies and describe the conditions under which each is the right choice:
    • Round-robin and weighted round-robin
    • Least connections
    • Consistent hashing
    • Latency- or health-aware routing
    • Sticky sessions (session affinity)
  • What is the difference between L4 and L7 load balancing? When would you use one versus the other, and is it ever useful to use both?

Part 2: Caching Strategy

  • Identify the layers at which caching can be applied (from the client all the way to the database) and describe what belongs at each layer.
  • Walk through the four main cache read/write patterns — cache-aside, read-through, write-through, and write-back — including when each is appropriate and where each can go wrong.
  • How do you handle cache invalidation, TTL selection, and tolerable staleness?
  • What is a cache stampede, and what techniques prevent or reduce one?

Part 3: Idempotent API Design

Design a system that makes a sensitive write endpoint (e.g., POST /charges) safe to retry without side effects.

  • Define idempotency and explain why it matters specifically for retry-prone distributed systems.
  • Describe where the idempotency key comes from, how it is scoped, and how long it should be retained.
  • Propose a data model and walk through the full request lifecycle — including what happens on the first call, on a clean retry, and on a retry that arrives while the first request is still in progress.
  • Explain how concurrent requests with the same key are handled, and how partial failures (e.g., charge succeeded but the response was never delivered) are recovered.
  • Describe how idempotency interacts with a distributed deployment: do all retries need to hit the same instance?

You are expected to discuss trade-offs, pick concrete approaches, and explain what breaks if a given choice is made incorrectly.

Hints

Hint 1

For routing, think about what information each strategy requires to make a decision — round-robin needs nothing, least-connections needs live state, consistent hashing needs a stable key. That constraint tells you the cost and the failure mode of each.

Hint 2

For caching writes, the order of operations matters more than it seems. Ask yourself: if two writers race, which pattern leaves the cache in a consistent state and which one leaves stale data behind?

Hint 3

For idempotency, the hard case isn't the happy path — it's when the first request is still in flight when the retry arrives. Think about what a status flag in the database buys you, and what a distributed lock adds on top of that.