Fundamentals: scaling, load balancing, CAP
Explain horizontal scaling, consistency and availability trade-offs.
By Invicly Tech, , in Interview Preparation. About 13 minutes to read.
In short
System design interviews test how you scale a service and reason about trade-offs. The core ideas are horizontal scaling behind load balancers, and the consistency versus availability choice described by CAP.
Explained simply
A system design interview is a conversation about building something big, like a photo app or a chat service, for millions of people. There is no single right answer. The interviewer wants to see how you think: what you ask, what you estimate, and how you explain the trade-offs of each choice.
Scaling means handling more users. You can buy a bigger machine (vertical scaling) or add more machines (horizontal scaling). A bigger machine is simple but has a ceiling and is one point of failure. More machines can grow almost without limit, but now you need something to share the work between them.
That something is a load balancer. It sits in front of your servers and passes each request to one of them, skipping any server that is down. For this to work, servers should be stateless: they keep no user data in their own memory, so any server can answer any request.
When data is copied across machines, a new problem appears: the copies can disagree for a moment. The CAP theorem says that when the network between machines breaks, you must choose. Either refuse some requests to stay correct, or keep answering and risk showing old data.
Press Play on a diagram to walk through it one step at a time.
- 1Clarify requirements
Functional features and non-functional goals
- 2Estimate scale
Users, requests per second, storage
- 3API and data model
Key endpoints and main tables
- 4High-level design
Clients, load balancer, services, stores
- 5Deep dive
Bottlenecks, scaling, failure handling
- 6Trade-offs
What you chose, why, and alternatives
- Bigger machine
- Simple, no code changes
- Hard upper limit
- Single point of failure
- More machines behind a load balancer
- Near-unlimited capacity
- Needs stateless services
- Survives a machine failing
- 1DNS and CDN
Send users to the nearest edge; cache static files
- 2Load balancer pair
Health checks, TLS, spread requests
- 3Stateless app servers
Autoscaled; no local sessions
- 4Cache (Redis)
Hot reads and shared sessions
- 5Database primary and replicas
Writes to primary, reads spread to replicas
- 6Object storage
Images, videos, backups
- Load balancer to Server A: GET /health1GET /health
- Server A to Load balancer: 200 OK2200 OK
- Load balancer to Server B: GET /health3GET /health
- Server B to Load balancer: No reply (timeout)4No reply (timeout)
- Load balancer to Server A: All new traffic goes to A5All new traffic goes to A
- Load balancer to Server B: Keep probing; re-add after healthy checks6Keep probing; re-add after healthy checks
In depth
Load balancers: layer 4 vs layer 7
A layer 4 load balancer works at the transport level. It sees IP addresses and ports and forwards TCP or UDP connections without reading the content, so it is very fast. A layer 7 load balancer understands HTTP. It can send /api to one group of servers and /images to another, route by host name or header, end TLS (decrypt HTTPS) so servers do not have to, and retry a failed request on another server.
Common algorithms: round robin (take turns), weighted round robin (bigger servers get more), least connections (best when requests vary a lot in length) and hashing on the client IP or a key (the same client keeps going to the same server). Health checks call each server every few seconds and remove ones that stop answering. The balancer itself must not be a single point of failure, so it runs as a redundant pair or as a managed cloud service.
- Sees IPs and ports only
- Forwards TCP or UDP connections
- Very fast, protocol-agnostic
- Example: AWS Network Load Balancer
- Reads HTTP paths, hosts, headers
- Can end TLS and retry requests
- Smarter routing, a bit more work
- Example: AWS Application Load Balancer, NGINX
How to make app servers stateless
State is anything a server remembers between requests: login sessions, shopping carts, uploaded files, in-memory counters. If it lives in one server's memory, the user loses it when the balancer sends them elsewhere or the server restarts.
Move each kind of state out. Sessions go into a shared store such as Redis, or into a signed token (like a JWT) that the client sends with every request. Uploaded files go to object storage such as Amazon S3. Counters and locks go to a database or Redis. Sticky sessions, where the balancer pins a user to one server, are a quick fix but spread load unevenly and still lose the session when that server dies. Once servers are stateless, you can add or remove them freely, which is what autoscaling needs.
- Sessions: Redis or signed tokens
- Files: object storage
- Counters and locks: database or Redis
- Sticky sessions: a crutch, not a design
CAP, PACELC and consistency models
CAP says that during a network partition (some machines cannot reach others), a replicated system must give up either consistency (every read sees the latest write) or availability (every request gets an answer). A bank balance should pick consistency: better to reject a request than show money that is not there. A like counter can pick availability: a slightly old count harms no one.
PACELC adds the everyday case: Else, when there is no partition, you still trade Latency against Consistency, because waiting for replicas to agree takes time. Between strong and eventual consistency there are useful middle levels. Read-your-writes means you always see your own changes. Monotonic reads means you never see time go backwards. In quorum systems with N copies, if each write waits for W copies and each read asks R copies with W + R > N, every read overlaps at least one copy that has the latest write.
- Reject or delay some requests
- Never show stale data
- Payments, stock levels, bookings
- Example: etcd, ZooKeeper
- Always answer, maybe stale
- Merge differences later
- Likes, feeds, shopping carts
- Example: Cassandra, DynamoDB default reads
Back-of-the-envelope numbers to remember
Estimates decide your design, so keep a few numbers in your head. A day has 86,400 seconds; round it to 100,000 to divide easily. So 1 million requests per day is about 12 per second, and 1 billion per day is about 12,000 per second. Peak traffic is often two to three times the average, and you design for the peak.
Rough latencies: reading from memory takes about 100 nanoseconds, a random read from an SSD around 100 microseconds, a round trip inside one data centre about half a millisecond, and a round trip across an ocean around 100 milliseconds or more. That is why caches sit close to the app and why global apps put servers near users. For storage, multiply items per day by bytes per item by days kept, then add room for replicas.
- 1M per day is about 12 per second
- Peak is about 2 to 3 times average
- Memory is about 1000 times faster than an SSD read
- Storage = items x size x retention x replicas
Availability: counting the nines
Availability is the share of time a system works. 99.9% (three nines) allows about 8.8 hours of downtime a year, or about 43 minutes a month. 99.99% allows about 53 minutes a year, and 99.999% about 5 minutes.
Parts in a chain multiply. If a request must pass a load balancer, an app server and a database, each 99.9%, the whole path is about 99.7%. Parts in parallel help: two independent servers that are each 99.9% available fail together only 0.1% x 0.1% of the time, giving 99.9999%. That is the maths behind removing single points of failure: every box on your diagram that has no backup caps the availability of everything behind it.
Learn it in 5 short steps
About 1h 10m each. Track them with a timer in your plan.
- 1Compare vertical and horizontal scaling~1h 10m
- 2Learn load balancers and algorithms~1h 10m
- 3Explain CAP and consistency models~1h 10m
- 4Estimate traffic and storage needs~1h 10m
- 5Use a step-by-step design framework~1h 10m
Key points
- Keep app servers stateless so any server can handle any request.
- Load balancers spread traffic with round robin, least connections or hashing.
- CAP: during a network partition you choose consistency or availability.
- Do quick estimates: about 100k seconds per day turns daily totals into per-second rates.
Key terms
Tap a card to flip it. 0/5 known
Examples
Back-of-envelope: photo app
100M daily users x 10 views = 1B reads/day
1B / ~100k seconds = ~10k reads/s, peak ~30k/s
1M uploads/day x 2 MB = 2 TB/day of storage
2 TB x 365 = ~730 TB/year -> object storage + CDNEstimating a text-only social feed with 50 million daily users
Ask first: how many posts and feed views per user? Say each user writes 2 posts and opens the feed 10 times a day. Writes: 100 million posts a day, about 1,200 per second on average and maybe 3,500 at peak. Reads: 500 million feed loads a day, about 5,800 per second, peaking near 17,000. Reads beat writes by 5 to 1, so caching feeds matters more than write speed. Storage: about 500 bytes per post, so 50 GB a day, or about 18 TB a year before replicas. That fits a sharded database comfortably.
Users: 50M daily active
Writes: 50M x 2 posts = 100M/day -> ~1,200/s (peak ~3,500/s)
Reads: 50M x 10 feed loads = 500M/day -> ~5,800/s (peak ~17,000/s)
Storage: 100M x 500 bytes = 50 GB/day -> ~18 TB/year (x3 with replicas)
Takeaway: read-heavy -> cache feeds; 18 TB/year -> shard by userSame company, two features, two consistency choices
Feature 1 is a wallet balance. If two data centres lose contact, letting both accept withdrawals could spend the same money twice. So this path is CP: writes go to one leader, and if it cannot be reached the app shows "try again later". Feature 2 is the view count on a video. If the regions disagree for a minute, nobody is hurt, and refusing to show the page would be far worse. So this path is AP: each region counts locally and the totals are merged later. Saying "it depends" is not enough in an interview; name the feature, the failure, and the choice.
Quick reference
- 1 day = 86,400 s, about 10^5; 1M requests a day is about 12 requests per second
- Peak traffic is often 2 to 3 times the daily average; design for the peak
- 99.9% allows about 8.8 hours of downtime a year; 99.99% about 53 minutes
- Parts in a chain multiply: 99.9% x 99.9% x 99.9% is about 99.7%
- Layer 4 balancers route by IP and port; layer 7 balancers route HTTP by path, host or header
- Quorum rule: with N copies, W + R > N means every read overlaps the latest write
- Rough latency: RAM 100 ns, SSD read 100 microseconds, same data centre 0.5 ms, cross-ocean 100+ ms
- Least connections suits requests of very different lengths; round robin suits similar ones
Common mistakes
- Jumping into components before clarifying requirements.
- Ignoring estimates, then choosing the wrong storage scale.
- Naming technologies without explaining the trade-off.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
A photo app has 10 million daily users. Each views 50 photos a day, and 10% of users upload one 500 KB photo a day. Estimate average and peak read QPS and the storage added per year.
- Exercise 2medium
A request passes a load balancer (99.99%), then one of two app servers in parallel (each 99.9%), then a single database (99.95%). Estimate the availability of the whole path and the yearly downtime.
- Exercise 3hard
Outline a 45-minute answer to "Design Pastebin" (users paste text and get a short link) in about ten lines, following the framework.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Do I need to name specific technologies in a system design interview?
- Naming a well-known tool (PostgreSQL, Redis, Kafka, S3) helps, but only if you say why it fits. A generic "key-value store with fast reads" plus a reason beats a list of brand names.
- How precise do my estimates need to be?
- Within a factor of about two is fine. Round aggressively, say your assumptions out loud, and use the result to make a decision, such as "this fits on one machine" or "we need to shard".
- Should I start with vertical scaling?
- Often yes. A single strong database server goes a long way, and saying "start simple, scale out when the numbers require it" shows good judgement. Then explain what you would change when the limits are reached.
- Is the CAP theorem still worth knowing?
- Yes, as a way to talk about trade-offs during network failures. Pair it with PACELC to show you know that latency versus consistency matters even when the network is fine.