Interview Q&A / System Design
System Design interview questions
Caching, scaling, databases and classic design problems for beginners.
Prefer rapid revision? Practise System Design short interview answers →
- How would you design a URL shortener like bit.ly?
Store a mapping from a short code to the long URL. Generate codes with a counter encoded in base62 (or random codes with a collision check), keep the mapping in a key-value store, put a cache in front for fast redirects, and return a 301 or 302 redirect. Add rate limits, expiry and analytics as extras.
IntermediateIn-depth3 min read - What is a message queue and why would you use one?
A message queue lets one service drop a message and move on, while another service picks it up and processes it later. It decouples services, absorbs traffic spikes, makes slow work asynchronous and retries failures. Examples are RabbitMQ, Amazon SQS and Kafka (a log-based alternative).
IntermediateIn-depth4 min read - What is the CAP theorem, in simple words?
The CAP theorem says that when a network partition splits a distributed system, it must choose between consistency (every read sees the latest write or gets an error) and availability (every request gets a non-error response, possibly stale). Partitions can't be ruled out, so the real choice is CP or AP during a partition.
IntermediateIn-depth4 min read - Can you give a simple example of when Layer 7 load balancing is necessary?
Layer 7 is needed when routing depends on request content, such as the URL path, headers or cookies. For example, sending /api traffic to API servers and /images traffic to a media cluster.
IntermediateQuick answer1 min read - How do Cache-Control and ETag headers work together?
Cache-Control says how long a response may be reused without asking. ETag is a fingerprint of the content; after expiry the browser sends it back, and the server replies 304 Not Modified if nothing changed, saving the download.
IntermediateQuick answer1 min read - How do polling, long polling, SSE and WebSockets differ for real-time updates?
Polling asks repeatedly; long polling holds each request open until there is news; SSE streams server-to-client over one HTTP connection; WebSockets give a two-way channel. Choose by direction of data and scale.
IntermediateQuick answer1 min read - How do you choose a good shard key?
A good shard key has high cardinality, spreads data and traffic evenly, and matches the most common query so requests hit one shard. Avoid keys that grow monotonically or are skewed, like timestamps or country.
IntermediateQuick answer1 min read - How do you make a message consumer idempotent?
Record the id of each processed message in the same transaction as its effect, and skip ids already seen. Or design the operation to be naturally repeatable, such as setting a value rather than incrementing it.
IntermediateQuick answer1 min read - How do you update a file that is already cached on a CDN?
Either purge the URL on the CDN, which is slow and rate limited, or better, put a version or content hash in the filename, such as app.3f9a1c.js. A new name is a new cache entry, so there is nothing to invalidate.
IntermediateQuick answer1 min read - How do you version a public API without breaking existing clients?
Put the version in the URL (/v1/) or a header, never change the meaning of existing fields, add new fields as optional, and announce deprecations with a long sunset period. Prefer additive changes so versions are rarely needed.
IntermediateQuick answer1 min read - How does an idempotency key stop duplicate payments?
The client sends a unique key with each payment request. The server stores the key with the result; if the same key arrives again, it returns the saved result instead of charging again.
IntermediateQuick answer1 min read - How does DNS-based global load balancing route users to the nearest data centre?
The DNS service answers with a different IP depending on where the user is, which server is healthy, or latency measurements. Users in Mumbai get the Mumbai address, users in London get London's.
IntermediateQuick answer1 min read - How does Kafka keep messages in order, and what role do partitions play?
Kafka guarantees order only within a single partition. Messages with the same key go to the same partition, so ordering holds per key. More partitions give more parallelism but no global order.
IntermediateQuick answer1 min read - In CAP, why is partition tolerance not really a choice?
Network partitions happen in any real distributed system, so you cannot opt out of P. The real decision is what to do during a partition: refuse some requests to stay consistent (CP) or answer with possibly stale data (AP).
IntermediateQuick answer1 min read - What does at-least-once delivery mean, and can you get exactly-once?
At-least-once means a message is never lost but may be delivered more than once. True exactly-once delivery is not practical across a network, so systems use at-least-once plus idempotent consumers to get exactly-once effects.
IntermediateQuick answer1 min read - What is a cache stampede and how do you prevent it?
A cache stampede happens when a popular key expires and many requests miss at once, all hitting the database together. Prevent it with request coalescing or locks, jittered TTLs, and refreshing entries before they expire.
IntermediateQuick answer1 min read - What is a hot partition and how do you fix it?
A hot partition is a shard or partition that receives far more traffic than the rest, so it becomes the bottleneck. Fix it with better keys, key salting, splitting the hot shard, or caching hot items.
IntermediateQuick answer1 min read - What is a stale closure in a React effect, and how do you fix it?
A closure captures the values from the render where it was created. An effect with a missing dependency keeps using old values. Fix it by listing the dependency, using the updater form of setState, or storing the latest value in a ref.
IntermediateQuick answer1 min read - What is backpressure and how do you handle a queue that keeps growing?
Backpressure means slowing or limiting producers when consumers cannot keep up. For a growing queue, add consumers, make processing faster, reject or throttle new work, and set limits so the queue cannot grow without bound.
IntermediateQuick answer1 min read - What is lazy initialisation in useState, and when should you use it?
Pass a function to useState, as in useState(() => compute()). React runs it only on the first render. Without the function form, the expensive call runs on every render, even though its result is ignored after the first.
IntermediateQuick answer1 min read - What is the difference between synchronous and asynchronous replication?
Synchronous replication makes the leader wait for replicas to confirm before acknowledging a write, so no acknowledged write is lost but writes are slower. Asynchronous replies immediately and replicates later, which is faster but can lose recent writes.
IntermediateQuick answer1 min read - What is the difference between write-through and write-back caching?
Write-through writes to the cache and the database together before replying, so data is safe but writes are slower. Write-back writes to the cache first and flushes to the database later, which is faster but can lose data on a crash.
IntermediateQuick answer1 min read - What is the N+1 problem in GraphQL and how is it solved?
N+1 happens when fetching a list triggers one query for the list and then one extra query per item for a nested field. DataLoader batches those per-item lookups into a single query.
IntermediateQuick answer1 min read - When would you choose useReducer over useState?
Choose useReducer when state has several related fields, or the next state depends on complex rules and actions. It centralises update logic in one pure function, which is easier to test and reason about than many scattered setters.
IntermediateQuick answer1 min read - When would you pick Redis over Memcached for a cache?
Memcached is a simple, fast key-value cache. Redis adds data structures (lists, sets, sorted sets), optional persistence, replication and pub/sub, so choose Redis when you need more than plain get and set.
IntermediateQuick answer1 min read - When would you use gRPC instead of REST?
Use gRPC for fast, strongly typed communication between internal services and for streaming. It uses HTTP/2 and Protocol Buffers, which are compact and quick, but browsers and public clients find REST easier.
IntermediateQuick answer1 min read - Why is returning a total count with every page expensive, and what are the alternatives?
COUNT(*) over a large filtered table scans many rows on every request. Alternatives are returning only has_next, using approximate counts, caching the count, or showing 'more than 1,000' instead of an exact total.
IntermediateQuick answer1 min read - Why must a reducer function be pure, and what breaks if it is not?
React may call a reducer more than once with the same inputs, for example in Strict Mode. A pure reducer returns a new state from state and action only. Mutating state or running side effects inside it gives wrong or duplicated results.
IntermediateQuick answer1 min read - Why should retries use exponential backoff with jitter?
Backoff spaces out retries (1s, 2s, 4s...) so a struggling service gets room to recover. Jitter adds randomness so thousands of clients do not all retry at the same instant and cause a retry storm.
IntermediateQuick answer1 min read