Skip to content

Interview PrepCase studies · Advanced

Classic Designs, Part 1 ​

15. How would you design a scalable REST API? ​

The short answer

Start with a stateless API service behind a load balancer, so any instance can serve any request and you scale by adding instances. Design clear resource URLs, consistent errors, pagination with cursors, idempotency keys on creates, versioning, and validation at the edge. Put an API gateway in front for TLS, authentication, rate limits and request IDs. Keep data in PostgreSQL with indexes and a connection pool, add Redis for hot reads and read replicas when reads grow, move slow work to queues, and add observability, timeouts and graceful shutdown from day one.

clientsCDN + gatewayTLS, authrate limitsAPI instancesstatelessautoscaledRedis: hot readsPostgreSQL primary+ read replicasqueue → workerslogs, metrics, traces
A stateless middle, shared state behind it. Everything that makes the API scale sits outside the API instances: cache, database, queue. The instances can be added or replaced at any time.

API design choices to state:

ConcernChoice
Resources/orders, /orders/{id}, /orders/{id}/items
PaginationCursor-based: ?limit=20&cursor=… (pagination)
RetriesIdempotency-Key header on POST
ErrorsOne JSON error shape with a code and the request ID
Versioning/v1 in the path or a header; additive changes within a version
ConcurrencyETag and If-Match for updates

Scaling path, in order: indexes and query fixes, then caching, then more instances, then read replicas, then queues for heavy work, then partitioning only if writes outgrow one primary.

16. How would you design a URL shortener? ​

The short answer

Two operations: create a short code for a long URL, and redirect from the code. It is extremely read-heavy, so the redirect path must be a cache hit almost always. Generate codes of 7 Base62 characters, which gives 3.5 trillion combinations, using a counter or ID generator encoded in Base62, or random codes with a uniqueness check. Store code → long URL in a key-value store or PostgreSQL partitioned by code, cache hot codes in Redis, return 302 if you need click analytics or 301 to let browsers cache, and send click events through a queue to analytics.

Requirements and estimates:

  1. 100 million new links a month ≈ 40 writes per second.
  2. 100 redirects per new link ≈ 4,000 reads per second; peaks about 40,000.
  3. 5 years × 1.2 billion a year = 6 billion links × 500 bytes ≈ 3 TB.
  4. Redirect latency target: under 20 ms at p99.
POST /linkscreate serviceID → Base62 codeGET /aZ3k9Q1redirect service302 Location: …Redis, 95% hitslinks storepartitioned by codeclick events → queue → analyticsmiss
Writes are rare, reads are hot. Creating a link touches the store once. A redirect is almost always answered from Redis. Clicks are recorded asynchronously, so analytics never slows the redirect down.

Generating codes, the main deep dive:

ApproachHowTrade-off
Counter plus Base62A unique ID, from a Snowflake generator or ranges handed to each server, encoded in Base62No collisions; codes are guessable in sequence
Random plus check7 random Base62 characters; insert with a unique constraint; retry on conflictUnguessable; rare retries
Hash of the URLFirst 7 characters of a hashSame URL gives same code; collisions must be handled

Why 7 characters: 62 to the power 7 is about 3.5 trillion, far more than 6 billion links. Even 6 characters, 57 billion, would do, but 7 leaves room.

Other points: 301 is cached by browsers, so you lose click counts and cannot change the target; 302 keeps every click visible. Check new URLs against malware lists, rate limit creation per user, and support expiry with a TTL or a nightly cleanup.

17. How would you design a notification service? ​

The short answer

Other services publish notification requests, such as "order shipped for user 42", to a queue. A notification service looks up the user's preferences and quiet hours, renders the message from a template per channel and language, and hands it to per-channel workers for push, email, SMS and in-app, each calling a provider such as APNs, FCM, an email service or an SMS gateway. It must be reliable, with retries, dead letter queues and deduplication; respect rate limits per user and per provider; record delivery status; and keep slow channels from blocking fast ones.

orderspaymentsmarketingqueuenotification svcpreferencesdedupe, rate limitrender templatepush queue → APNs, FCMemail queue → email providerSMS queue → SMS gatewayin-app storedelivery status log
One brain, separate pipes per channel. The service decides whether and what to send. Each channel has its own queue and workers, so a slow SMS provider never delays push notifications.

Deep dives to offer:

TopicDesign
DuplicatesEach request carries an ID; a short-lived Redis key notified:{id} blocks a second send
PrioritiesSeparate queues for transactional, such as password resets, and marketing
User fatiguePer-user limits, such as 3 marketing pushes a day; quiet hours by time zone
Provider failureRetries with backoff, a second provider for SMS and email, a DLQ
Device tokensStore per device; delete tokens the provider reports as invalid
Scale10 million notifications in a campaign: queue them, workers sized to provider rate limits

18. How would you design a chat application? ​

The short answer

Clients hold a persistent WebSocket to a fleet of gateway servers. When Asha sends a message, her gateway passes it to a chat service, which assigns an ordered ID per conversation, stores it in a write-optimised database partitioned by conversation, such as Cassandra, and routes it: it looks up which gateway each recipient is connected to, through a presence store in Redis, and pushes it there. Offline recipients get a push notification and fetch missed messages on reconnect by asking for everything after their last seen ID. Add read receipts, typing indicators, group fan-out and media through object storage.

Ashagateway 1chat serviceorder, store, routemessages, by conversationpresence: Ben → gateway 3gateway 3Benoffline? push notification①② store③
Store first, then deliver. ① The message arrives over Asha's open connection. ② It is saved with an ordered ID, so it is never lost. ③ The chat service finds which gateway Ben is connected to and sends it there; if he is offline, he gets a push and catches up later.

Estimates: 50 million daily users, 40 messages each a day = 2 billion messages ≈ 23,000 per second, peaks 100,000; at 200 bytes, about 400 GB a day before replication.

Deep diveDesign
OrderingA sequence number per conversation, assigned by the chat service; clients sort by it
Delivery guaranteeClient sends with a client message ID; server acknowledges; client retries until acknowledged; server deduplicates
Catching upClient sends "last seen sequence"; server returns everything after it
StoragePartition by conversation_id, cluster by sequence; old messages to cold storage
GroupsSmall groups fan out per member; huge channels let members pull
PresenceGateways write user → gateway to Redis with a TTL, refreshed by heartbeats
MediaUpload to object storage; the message holds the link
End-to-end encryptionKeys on devices; the server only routes ciphertext

19. How would you design a search system? ​

The short answer

Keep the primary database as the source of truth. Build an indexing pipeline that turns each record into a search document, the fields to match, filter, sort and rank on, and writes it to a search engine that maintains an inverted index. Feed it from database change events, using the outbox or change data capture, so it stays seconds behind. The query service parses the query, adds filters such as category, price and the user's tenant, asks the engine, ranks results, and loads anything sensitive or exact, such as live stock, from the database. Add typo tolerance, synonyms, facets and autocomplete.

PostgreSQLchange eventsCDC or outboxindexersearch engineinverted indexreplicas, shardsuser queryquery serviceparse, filters, tenantre-rank, enrichlive stock, price
Two paths that meet at the index. Changes flow from the database into the index a moment later. Queries read the index for matching and ranking, then the database for anything that must be exact right now.
Deep diveDesign
RelevanceText match score plus business signals: popularity, conversion, freshness
Typos and synonymsFuzzy matching; synonym lists such as "trainers = sneakers"
FacetsCounts per brand and price range, computed by the engine
FreshnessSeconds of lag; reindex everything from the database when the mapping changes, using an alias swap
SecurityFilter by tenant and permission inside the query, never after
ScaleShards for size, replicas for query throughput

20. How would you design a payment system? ​

The short answer

Correctness beats everything. Clients call a payment service with an idempotency key. It creates a payment record in state created, calls the payment provider, such as Stripe or Adyen, to authorise, and moves through explicit states, authorised, captured, refunded, failed, each change recorded in an append-only double-entry ledger. Provider results also arrive by webhooks, which are verified, deduplicated and applied idempotently. Events go out through an outbox. A daily reconciliation compares the ledger with the provider's reports. Card data never touches your servers; the provider's hosted fields or SDK tokenise it.

checkoutIdempotency-Keypayment servicestate machineledger entriesoutboxproviderStripe, Adyenwebhook handlerverify, dedupeordersvia eventsnightly reconciliation: ledger versus provider settlement reportauthorisewebhook
Every step recorded, every message expected twice. The payment's state moves only forward through recorded steps. The provider's answer can arrive directly or by webhook, and both paths are idempotent. Reconciliation catches anything the rest missed.

A double-entry ledger, the heart of it. Every movement of money writes two entries that sum to zero, so money is never created or lost:

EntryAccountDebitCredit
Customer pays £85.50Customer receivable85.50
Provider clearing85.50
Provider settles to the bank, minus a £1.20 feeProvider clearing85.50
Bank84.30
Fees expense1.20

Deep dives:

RiskDesign
Double charge on retryIdempotency key with a unique constraint; return the stored result
Timeout from the provider, unknown outcomeMark pending; query the provider by our reference; never blindly retry the charge
Webhook delivered twice or out of orderStore event IDs; apply state changes only if they move forward
Lost event to ordersOutbox in the same transaction
DriftReconciliation, with alerts on mismatches
Card dataTokenised by the provider; you store tokens, never card numbers, which keeps PCI scope small
Money as numbersIntegers in minor units, such as pence, never floating point

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
System Design
The method
Case studies
Auth designs
Scenarios
Other topics
Nothing picked yet