Appearance
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.
API design choices to state:
| Concern | Choice |
|---|---|
| Resources | /orders, /orders/{id}, /orders/{id}/items |
| Pagination | Cursor-based: ?limit=20&cursor=… (pagination) |
| Retries | Idempotency-Key header on POST |
| Errors | One JSON error shape with a code and the request ID |
| Versioning | /v1 in the path or a header; additive changes within a version |
| Concurrency | ETag 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:
- 100 million new links a month ≈ 40 writes per second.
- 100 redirects per new link ≈ 4,000 reads per second; peaks about 40,000.
- 5 years × 1.2 billion a year = 6 billion links × 500 bytes ≈ 3 TB.
- Redirect latency target: under 20 ms at p99.
Generating codes, the main deep dive:
| Approach | How | Trade-off |
|---|---|---|
| Counter plus Base62 | A unique ID, from a Snowflake generator or ranges handed to each server, encoded in Base62 | No collisions; codes are guessable in sequence |
| Random plus check | 7 random Base62 characters; insert with a unique constraint; retry on conflict | Unguessable; rare retries |
| Hash of the URL | First 7 characters of a hash | Same 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.
Deep dives to offer:
| Topic | Design |
|---|---|
| Duplicates | Each request carries an ID; a short-lived Redis key notified:{id} blocks a second send |
| Priorities | Separate queues for transactional, such as password resets, and marketing |
| User fatigue | Per-user limits, such as 3 marketing pushes a day; quiet hours by time zone |
| Provider failure | Retries with backoff, a second provider for SMS and email, a DLQ |
| Device tokens | Store per device; delete tokens the provider reports as invalid |
| Scale | 10 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.
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 dive | Design |
|---|---|
| Ordering | A sequence number per conversation, assigned by the chat service; clients sort by it |
| Delivery guarantee | Client sends with a client message ID; server acknowledges; client retries until acknowledged; server deduplicates |
| Catching up | Client sends "last seen sequence"; server returns everything after it |
| Storage | Partition by conversation_id, cluster by sequence; old messages to cold storage |
| Groups | Small groups fan out per member; huge channels let members pull |
| Presence | Gateways write user → gateway to Redis with a TTL, refreshed by heartbeats |
| Media | Upload to object storage; the message holds the link |
| End-to-end encryption | Keys 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.
| Deep dive | Design |
|---|---|
| Relevance | Text match score plus business signals: popularity, conversion, freshness |
| Typos and synonyms | Fuzzy matching; synonym lists such as "trainers = sneakers" |
| Facets | Counts per brand and price range, computed by the engine |
| Freshness | Seconds of lag; reindex everything from the database when the mapping changes, using an alias swap |
| Security | Filter by tenant and permission inside the query, never after |
| Scale | Shards 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.
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:
| Entry | Account | Debit | Credit |
|---|---|---|---|
| Customer pays £85.50 | Customer receivable | 85.50 | |
| Provider clearing | 85.50 | ||
| Provider settles to the bank, minus a £1.20 fee | Provider clearing | 85.50 | |
| Bank | 84.30 | ||
| Fees expense | 1.20 |
Deep dives:
| Risk | Design |
|---|---|
| Double charge on retry | Idempotency key with a unique constraint; return the stored result |
| Timeout from the provider, unknown outcome | Mark pending; query the provider by our reference; never blindly retry the charge |
| Webhook delivered twice or out of order | Store event IDs; apply state changes only if they move forward |
| Lost event to orders | Outbox in the same transaction |
| Drift | Reconciliation, with alerts on mismatches |
| Card data | Tokenised by the provider; you store tokens, never card numbers, which keeps PCI scope small |
| Money as numbers | Integers in minor units, such as pence, never floating point |