Appearance
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 shape | Reach for | Example questions |
|---|---|---|
| Reads far exceed writes | Cache, CDN, read replicas | URL shortener, product catalogue, profiles |
| Work can happen later | Queue and workers | Notifications, video encoding, emails |
| Each user sees others' content | Fan-out on write or read | News feed, timeline, inbox |
| Too much data for one machine | Partition by key | Chat history, metrics, logs |
| Large files | Object storage, CDN, presigned uploads | File storage, video, photos |
| Must not happen twice | Idempotency keys, outbox, ledger | Payments, orders, ticketing |
| Search by text | Search index fed by change data | Product search, typeahead |
| Spiky or abusive traffic | Rate limits, queues, load shedding | Flash sale, public APIs |
| One thing must be decided once | Single writer, lock, atomic update | Seat booking, job scheduler, leader |
| Real-time updates | WebSockets or SSE plus pub/sub | Chat, 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.
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 write | Fan-out on read | |
|---|---|---|
| Work happens | When posting | When reading |
| Feed read | One list lookup, fast | Many lookups and a merge, slower |
| Celebrity post | Millions of writes | One write |
| Storage | A feed list per user | Posts only |
| Inactive users | Wasted writes to feeds nobody reads | No waste |
| Best for | Normal accounts, read-heavy apps | Celebrities, 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.
| Partitioning | How | Good | Watch out |
|---|---|---|---|
| Hash of key | hash(user_id) % N, or a hash ring | Even spread | Range queries hit every shard |
| Range of key | Time ranges, alphabetical ranges | Fast range scans | New data all lands on the newest range |
| By tenant | Each tenant on a shard | Isolation, simple queries | Very large tenants |
| Compound key | (chat_id, message_time) | Locality plus order | Very 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.
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.
| Risk | Pattern |
|---|---|
| Client retries a charge | Idempotency key (idempotency keys) |
| Saved the order but the event was lost | Outbox (outbox) |
| Event processed twice | Processed-message table (idempotency) |
| Step 3 of 4 fails across services | Saga with compensations (sagas) |
| Two buyers, one last item | Atomic conditional update or row lock (locking) |
| Our records drift from the provider's | Daily 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:
| Word | Products containing it |
|---|---|
| running | 12, 40, 77 |
| shoes | 12, 40, 91 |
| waterproof | 40, 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.