Top 40 System Design Interview Questions & Answers
Top 40 system design interview questions and answers: scalability, caching, sharding, CAP theorem, queues, microservices and classic design problems.
Scalability Fundamentals
Q1. How do you approach a system design interview?
Clarify functional and non-functional requirements, estimate scale, define the API and data model, draw a high-level design, then go deep on bottlenecks and trade-offs.
Q2. What is the difference between vertical and horizontal scaling?
Vertical scaling adds CPU or memory to one machine, which is simple but has a ceiling. Horizontal scaling adds more machines behind a load balancer, which scales further but needs stateless services.
Q3. What is a load balancer?
A component that spreads requests across servers using algorithms such as round robin or least connections. It runs health checks and removes unhealthy servers.
Q4. What is the difference between layer 4 and layer 7 load balancing?
Layer 4 routes by IP and port and is very fast. Layer 7 understands HTTP and can route by path, header or cookie, and terminate TLS.
Q5. What is latency versus throughput?
Latency is how long one request takes. Throughput is how many requests the system handles per second. Reducing one does not always improve the other.
Q6. What do availability nines mean?
99.9% availability allows about 8.8 hours of downtime a year, and 99.99% about 53 minutes. Each extra nine needs much more redundancy.
Q7. Why should services be stateless?
If no session data lives on a server, any server can handle any request. Scaling out and replacing servers becomes easy. Keep state in databases or caches.
Q8. What is a CDN?
A content delivery network caches static files and sometimes API responses at edge servers close to users, which cuts latency and load on the origin.
Q9. How do you do a back-of-the-envelope estimate?
Start from daily active users and actions per user to get requests per second, then multiply by object sizes and retention to get storage and bandwidth. Round generously.
Q10. What is a single point of failure?
A component whose failure takes down the whole system, such as one database server. Remove it with redundancy and failover.
Data Storage & Consistency
Q11. What is the CAP theorem?
During a network partition, a distributed system must choose between consistency (every read sees the latest write) and availability (every request gets a response). Partition tolerance is not optional.
Q12. What is the PACELC theorem?
It extends CAP: if there is a Partition, choose Availability or Consistency; Else, in normal operation, choose between Latency and Consistency.
Q13. What is the difference between strong and eventual consistency?
With strong consistency, reads always return the latest write. With eventual consistency, replicas may briefly return old data but converge, which allows higher availability and lower latency.
Q14. When do you choose SQL versus NoSQL?
Choose SQL for structured data, relationships and transactions. Choose NoSQL for flexible schemas, very high write volume, or simple access patterns at massive scale.
Q15. What is database replication?
Copying data to several servers. Leader-follower replication sends writes to one leader and serves reads from followers, which adds read capacity and failover.
Q16. What is sharding?
Splitting data across databases by a shard key, such as user ID, so each holds a part. It scales writes and storage, but cross-shard queries and resharding are hard.
Q17. What is consistent hashing?
A scheme that maps keys and servers onto a ring, so adding or removing a server moves only a small share of keys. Caches and distributed databases use it.
Q18. What caching strategies are common?
Cache-aside, where the app loads on a miss; write-through, which writes cache and database together; and write-behind, which writes the cache first. Each needs an expiry and invalidation plan.
Q19. What is cache invalidation, and why is it hard?
Removing or updating cached data when the source changes. It is hard because stale data, race conditions and many caches across services make it easy to serve outdated values.
Q20. What is a hot key or hot partition?
One key or shard that gets far more traffic than others, such as a celebrity's profile. Fix it with caching, replicating that key, or splitting it with a suffix.
Communication & Architecture
Q21. What is the difference between a monolith and microservices?
A monolith is one deployable app, simple to build and debug at first. Microservices split it into independently deployed services that scale separately but add network, data and operational complexity.
Q22. Why use a message queue?
Queues such as Kafka, RabbitMQ or SQS decouple producers from consumers, absorb traffic spikes, and let slow work run asynchronously with retries.
Q23. What is the difference between a queue and pub/sub?
In a queue, each message is processed by one consumer. In pub/sub, each message goes to every subscriber, so several systems can react to the same event.
Q24. What delivery guarantees do messaging systems offer?
At-most-once may lose messages, at-least-once may deliver duplicates, and exactly-once is costly. Most systems use at-least-once with idempotent consumers.
Q25. What is an API gateway?
A single entry point that routes requests to services and handles cross-cutting concerns such as authentication, rate limiting, logging and request shaping.
Q26. What is the difference between polling, long polling, WebSockets and SSE?
Polling asks repeatedly. Long polling holds the request until data arrives. WebSockets keep a two-way connection open. Server-Sent Events stream one-way from server to client over HTTP.
Q27. How do you implement rate limiting?
Common algorithms are token bucket, leaky bucket, fixed window and sliding window. Store counters in a shared store such as Redis so every server enforces the same limit.
Q28. What is the circuit breaker pattern?
After repeated failures calling a service, stop calling it for a while and return a fallback, then test it again. It stops one failing service from taking down others.
Q29. What is the saga pattern?
A way to keep data consistent across microservices without distributed transactions: each step has a compensating action that undoes it if a later step fails.
Q30. What is idempotency, and why does it matter?
Repeating an operation has the same effect as doing it once. Because networks retry, payments and order creation need idempotency keys to avoid duplicates.
Classic Design Questions
Q31. How would you design a URL shortener?
Generate a short unique code with base62 encoding of an ID or a random key, store code-to-URL mappings in a key-value store, redirect with 301 or 302, cache popular codes, and count clicks asynchronously.
Q32. How would you design a rate limiter?
Put it in the API gateway or middleware, identify clients by API key or IP, use a token bucket or sliding window in Redis with atomic scripts, and return 429 with Retry-After.
Q33. How would you design a chat system?
Clients keep WebSocket connections to chat servers, messages are stored in a database partitioned by conversation, a pub/sub layer routes messages between servers, and offline users get push notifications.
Q34. How would you design a news feed?
Fan-out on write pushes new posts into followers' cached feeds. Fan-out on read builds feeds when requested. Use a hybrid: push for most users, pull for accounts with millions of followers.
Q35. How would you design a notification system?
Services publish events to a queue, a notification service applies user preferences and templates, and workers send through email, SMS and push providers with retries and rate limits.
Q36. How would you design a file storage service like Google Drive?
Split files into chunks, deduplicate them by hash, store chunks in object storage and metadata in a database, sync changes per chunk, and use versioning for history.
Q37. How would you design a video streaming service?
Upload originals to object storage, transcode them into several resolutions and formats, serve segments through a CDN with adaptive bitrate streaming, and keep metadata and recommendations in separate services.
Q38. How would you design search autocomplete?
Keep a trie or prefix index of popular queries with precomputed top suggestions, cache results for common prefixes, and update rankings from search logs in batches.
Q39. How would you design a distributed job scheduler?
Store jobs with their next run time in a database, have workers claim due jobs with locks or leases, retry with backoff, and make jobs idempotent so a crash never runs work twice by mistake.
Q40. How would you design an e-commerce checkout?
Hold stock with reservations that expire, use idempotency keys for payment calls, coordinate order, payment and inventory with a saga, and send confirmation events asynchronously.