Skip to content

Interview PrepThe method · Core

Patterns That Solve Most Questions ​

5. Which patterns solve most system design questions? ​

The short answer

Most designs are combinations of about ten patterns. Cache hot reads. Queue slow or bursty work. Fan out feeds on write or on read. Partition data by a key so it spreads across machines. Replicate for reads and failover. Put media in object storage behind a CDN. Make money and state changes idempotent and publish events with an outbox. Feed a search index from database changes. Protect the system with rate limits, timeouts and circuit breakers. Use one writer per resource, with leases or locks, when order matters. Learn when each applies, and you can assemble almost any design.

Problem shapeReach forExample questions
Reads far exceed writesCache, CDN, read replicasURL shortener, product catalogue, profiles
Work can happen laterQueue and workersNotifications, video encoding, emails
Each user sees others' contentFan-out on write or readNews feed, timeline, inbox
Too much data for one machinePartition by keyChat history, metrics, logs
Large filesObject storage, CDN, presigned uploadsFile storage, video, photos
Must not happen twiceIdempotency keys, outbox, ledgerPayments, orders, ticketing
Search by textSearch index fed by change dataProduct search, typeahead
Spiky or abusive trafficRate limits, queues, load sheddingFlash sale, public APIs
One thing must be decided onceSingle writer, lock, atomic updateSeat booking, job scheduler, leader
Real-time updatesWebSockets or SSE plus pub/subChat, live scores, ride tracking

6. When and how should you add caching to a design? ​

The short answer

Add caching when reads dominate, the same data is read many times, and slight staleness is acceptable. Put it in layers: CDN for public content, Redis for shared hot data with cache-aside and TTLs, and small in-process caches for the very hottest keys. Say what is cached, the key, the TTL, how it is invalidated, the expected hit rate, and what happens on a miss storm or cache outage. Do not cache data that must always be exact, such as balances during a payment.

The sentence to say: "Redirect lookups are 99% reads and the same links are hit repeatedly, so I put Redis in front with cache-aside, key link:{code}, a 24-hour TTL, and delete on update. At a 95% hit rate the database sees 5% of 40,000 reads per second, about 2,000, which one primary with a replica handles."

The mechanics are covered in cache-aside, stampedes and distributed caching.

7. When should a design use a queue? ​

The short answer

Use a queue when work does not need to finish before you reply to the user, when work is slow, such as sending email, encoding video or calling a flaky partner, when traffic is bursty and you want to smooth it, or when several services must react to one event. The API writes a job or event and returns 202 Accepted; workers process it at their own pace, scale independently and retry on failure. State that delivery is at least once, so workers are idempotent, and that failed messages go to a dead letter queue.

spike5,000 / sAPIenqueue, 202queueabsorbs the burstworkerssteady 800 / sautoscale workers on queue depth; the database sees a steady load
A shock absorber. The API only records the work, which is fast. The queue holds the spike. Workers process at the rate the database can take, and more workers start when the queue grows.

Details: message queues, dead letter queues and idempotency.

8. What is fan-out on write versus fan-out on read? ​

The short answer

Both answer "how does each user see posts from everyone they follow?". Fan-out on write, or push: when someone posts, copy the post ID into a precomputed feed list for each follower. Reads are instant, a single list fetch, but a celebrity with 50 million followers creates 50 million writes. Fan-out on read, or pull: store posts only once, and when a user opens their feed, fetch recent posts from everyone they follow and merge them. Writes are cheap, but reads are slow for users who follow many accounts. Real systems use a hybrid: push for normal accounts, pull for celebrities.

Fan-out on writeFan-out on read
Work happensWhen postingWhen reading
Feed readOne list lookup, fastMany lookups and a merge, slower
Celebrity postMillions of writesOne write
StorageA feed list per userPosts only
Inactive usersWasted writes to feeds nobody readsNo waste
Best forNormal accounts, read-heavy appsCelebrities, rarely read feeds

The full design is in design a news feed.

9. How and when do you partition data? ​

The short answer

Partition, or shard, when one database cannot hold the data or handle the writes. Choose a partition key that most queries include and that spreads load evenly: user_id for a chat app, tenant_id for B2B SaaS, short_code for a URL shortener. Use hash partitioning to spread evenly, or range partitioning when you query by ranges such as time. Say how you handle queries without the key, usually a secondary index or a separate read model, how you avoid hot partitions, and how you will rebalance, typically with consistent hashing or many small virtual partitions.

PartitioningHowGoodWatch out
Hash of keyhash(user_id) % N, or a hash ringEven spreadRange queries hit every shard
Range of keyTime ranges, alphabetical rangesFast range scansNew data all lands on the newest range
By tenantEach tenant on a shardIsolation, simple queriesVery large tenants
Compound key(chat_id, message_time)Locality plus orderVery busy chats become hot

Details: sharding and consistent hashing.

10. How do replication and read replicas fit into designs? ​

The short answer

Replicate every important data store, at least one replica in another availability zone, so a machine or zone failure does not lose data or availability; say whether replication is synchronous, no data loss but slower, or asynchronous, faster but may lose the last seconds. Add read replicas when reads overload the primary, and route reads that tolerate lag there. State the lag problem and how you handle read-your-own-writes. For global apps, mention multi-region replicas for low read latency, and that writes still go to one region unless you accept conflicts.

Details: replication and read replicas.

11. How should large files and media be handled in a design? ​

The short answer

Never push large files through your app servers or store them in the database. Put them in object storage, such as S3, and let clients upload directly with short-lived presigned URLs, or multipart uploads for very large files. Store only metadata, owner, size, type, storage key and status, in the database. Process files asynchronously: an upload event triggers workers for thumbnails, virus scans or video transcoding. Serve them to users through a CDN, with signed URLs for private content. Use lifecycle rules to move old files to cheaper storage tiers.

clientAPIobject storagequeue → workersmetadata DBCDN① URL② upload③ event④ download
Bytes never touch your servers. The API only hands out permission and records metadata. Storage holds the files, workers process them in the background, and the CDN serves them close to users.

Details: object storage uploads.

12. How do you make money and state changes correct in a distributed design? ​

The short answer

Assume every request and message can arrive twice, and every step can fail half-way. So: require an idempotency key on every create or charge, stored with a unique constraint, so a retry returns the first result; write the business change and its event in one database transaction using the outbox; make consumers idempotent; record money in an append-only double-entry ledger rather than updating balances in place; use a saga with compensations for steps across services; and run a reconciliation job against the payment provider. Say these out loud in any design that moves money or inventory.

RiskPattern
Client retries a chargeIdempotency key (idempotency keys)
Saved the order but the event was lostOutbox (outbox)
Event processed twiceProcessed-message table (idempotency)
Step 3 of 4 fails across servicesSaga with compensations (sagas)
Two buyers, one last itemAtomic conditional update or row lock (locking)
Our records drift from the provider'sDaily reconciliation

13. How do you add search to a design? ​

The short answer

Keep the database as the source of truth and copy the searchable fields into a search engine, such as Elasticsearch, OpenSearch, Meilisearch or Typesense, which builds an inverted index: a map from each word to the documents containing it, so text queries are fast and ranked by relevance. Keep the index updated asynchronously from change events, through the outbox or change data capture, and accept a second or so of lag. Searches go to the engine; loading the full record and anything that must be exact goes to the database.

An inverted index, tiny version:

WordProducts containing it
running12, 40, 77
shoes12, 40, 91
waterproof40, 91

A query for "waterproof running shoes" intersects the lists: product 40 contains all three words, so it ranks first.

The full design is in design a search system.

14. How do you protect a design from overload and abuse? ​

The short answer

Assume traffic will spike and clients will misbehave. Put rate limits per user and API key at the gateway, timeouts on every call, circuit breakers on dependencies, bulkheads between critical and optional work, queues to absorb bursts, load shedding to reject low-priority requests fast when overloaded, autoscaling for stateless parts, and a CDN or WAF in front to absorb floods. For known peaks, such as a sale, add a virtual waiting room and pre-scale. Mention these in any design that faces the public internet.

Details: rate limiting, circuit breakers, load shedding and design a flash sale.

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