System design basics: 50 cards on scaling, caching and databases
System design fundamentals: load balancers, caching, CDNs, replication and sharding, CAP, consistency, queues, rate limiting and back-of-the-envelope math.
Practice now
Study without an account
Scheduled with the default Askesis timings. Progress is saved in this browser.
All 50 cards
- 1Vertical vs horizontal scalingVertical: move to a bigger machine (more CPU and RAM). Horizontal: add more machines and spread the load across themVertical scaling is simpler but hits a hardware ceiling and keeps a single point of failure; horizontal scaling needs load balancing and stateless services.
- 2Why keep application servers stateless?So any server can handle any request, and you can add, remove or replace servers freely behind a load balancerSession data moves to a shared store such as Redis or a database, or into a signed token the client holds.
- 3Latency vs throughputLatency is how long one request takes; throughput is how many requests (or how much data) the system handles per unit of timeWatch tail latency (p95, p99), not just the average: the slowest 1% of requests is often what users notice.
- 4Downtime allowed by 99.9%, 99.99% and 99.999% availabilityAbout 8.8 hours, 53 minutes and 5 minutes per year99.9% (“three nines”) is roughly 43 minutes a month.
- 5Single point of failure (SPOF)A component whose failure takes the whole system down. Remove it with redundancy: several servers, database replicas, multiple availability zones
- 6Availability of two 99.9% components in series vs in parallelSeries (both needed): . Parallel (either one is enough):Every dependency in a request’s path lowers availability; redundancy raises it.
- 7What does a load balancer do?Spreads incoming requests across a pool of servers, health-checks them, and stops sending traffic to unhealthy onesIt’s a reverse proxy, so it often terminates TLS as well. Run at least two, or use a managed one, so it isn’t a single point of failure itself.
- 8Layer 4 vs layer 7 load balancingLayer 4 routes TCP or UDP connections by IP address and port without reading the content; layer 7 understands HTTP and can route by path, host, header or cookieL4 is faster and works with any protocol; L7 allows smarter routing, such as sending
/apito one pool and/imagesto another. - 9Load-balancing algorithm for requests with very uneven durationsLeast connections: send each request to the server with the fewest active connectionsRound robin, which takes servers in turn, works well when requests are similar. Weighted variants account for servers of different sizes.
- 10Sticky sessionsThe load balancer sends all of a user’s requests to the same server, using a cookie or a hash of the client IPDownsides: uneven load, and users lose their session when that server dies. Stateless servers remove the need.
- 11Cache-aside (lazy loading)The app checks the cache first; on a miss it reads from the database and stores the result in the cachepython
def get_user(user_id): user = cache.get(f"user:{user_id}") if user is None: user = db.find_user(user_id) cache.set(f"user:{user_id}", user, ttl=300) return user - 12Write-through vs write-back cachingWrite-through: every write goes to the cache and the database together (consistent, slower writes). Write-back (write-behind): writes go to the cache and reach the database later (fast, but data is lost if the cache fails first)
- 13LRU evictionWhen the cache is full, evict the least recently used entryOther policies: LFU (least frequently used), FIFO, and expiring entries after a TTL.
- 14How do you keep cached data from going stale?Give entries a TTL so they expire, and delete (or update) the cache key whenever the underlying data changesPhil Karlton: “There are only two hard things in Computer Science: cache invalidation and naming things.”
- 15Cache stampede (thundering herd)A popular key expires and many requests miss at the same moment, all hitting the database togetherMitigations: let one request rebuild the value while the others wait (request coalescing or a lock), add random jitter to TTLs, or refresh hot keys before they expire.
- 16Where can a web system cache data?In the browser, at a CDN, in a reverse proxy, in application memory, in a distributed cache such as Redis or Memcached, and in the database’s own buffer cache
- 17What is a CDN?A content delivery network: servers in many locations (edges) that cache content close to users, cutting latency and the load on your origin servers
- 18Pull CDN vs push CDNPull: an edge fetches a file from your origin on the first request and caches it. Push: you upload content to the CDN ahead of timePull is simpler and the usual default; push suits large files that rarely change.
- 19How do you roll out a new version of a CDN-cached asset immediately?Put a content hash in the filename (
app.3f9a1c.js) and let it be cached forever; each new build gets a new URLServe fingerprinted files withCache-Control: public, max-age=31536000, immutable. Purging the CDN instead is slower and easy to get wrong. - 20Leader-follower (primary-replica) replicationAll writes go to the leader, which streams its changes to followers; reads can be served by the followersIt scales reads and allows failover, but not writes: every write still goes through the leader.
- 21Synchronous vs asynchronous replicationSynchronous: the leader waits for replicas to confirm each write (no data loss on failover, slower writes). Asynchronous: it doesn’t wait (fast, but replicas lag and the latest writes can be lost if the leader dies)
- 22A user updates their profile but still sees the old version. Likely cause and fix?The read went to a replica that hasn’t caught up (replication lag). Guarantee read-your-writes, e.g. by reading the user’s own data from the leader for a short time after they write
- 23What does the quorum condition guarantee?With replicas, writes confirmed by of them and reads querying , every read overlaps at least one replica that has the latest writeA common choice is , , .
- 24Sharding (horizontal partitioning)Splitting one dataset across several databases by a shard key, so each database holds a subset of the rowsUnlike read replicas, sharding scales writes and storage beyond one machine.
- 25Range vs hash shardingRange: each shard owns a contiguous key range (fast range scans, but hotspots, e.g. every new timestamp lands on one shard). Hash: shard by a hash of the key (even spread, but range queries must ask every shard)
- 26Consistent hashingNodes and keys sit on a hash ring and each key belongs to the next node clockwise, so adding or removing a node moves only about of the keysWith plain , changing remaps almost every key. Virtual nodes (many ring positions per server) even out the load.
- 27What makes a good shard key?Many distinct values, evenly spread traffic, and a match with how data is queried, e.g.
user_idwhen most queries are per userA bad key creates hot shards: sharding by date piles all new writes onto one shard. - 28Costs of sharding, and what to try firstCross-shard joins and transactions become hard, and rebalancing adds operational work. First try indexes, better queries, caching, read replicas and a bigger machine
- 29DenormalizationStoring redundant copies of data, such as the author’s name on every post, to avoid joins and speed up reads, at the cost of more complex writes and possible inconsistency
- 30SQL vs NoSQL: when does each fit?Relational databases suit structured data with relationships, joins and ACID transactions. NoSQL stores (key-value, document, wide-column, graph) suit flexible schemas, very large scale and simple, known access patterns
- 31CAP theoremDuring a network partition, a distributed data store must choose between consistency (every read sees the latest write) and availability (every request gets a non-error response)Partitions can’t be ruled out in practice, so “pick two of three” really means choosing CP or AP behavior when one happens.
- 32CP vs AP systemsCP systems refuse some requests during a partition to stay consistent (e.g. etcd, ZooKeeper). AP systems keep answering but may return stale data (e.g. Cassandra, or DynamoDB with eventually consistent reads)
- 33PACELCIf there is a Partition, choose Availability or Consistency; Else, in normal operation, choose Latency or ConsistencyIt extends CAP: synchronous replication buys consistency with extra latency all the time, not only during failures.
- 34Strong vs eventual consistencyStrong: once a write completes, every read sees it. Eventual: replicas converge once writes stop, but reads in between may return stale data
- 35ACID vs BASEACID: Atomicity, Consistency, Isolation, Durability, the guarantees of traditional transactions. BASE: Basically Available, Soft state, Eventually consistent, the model of many distributed NoSQL stores
- 36Why put a message queue between two services?It decouples them: the producer doesn’t wait for the consumer, traffic spikes are buffered, and failed work can be retriedTypical uses: sending emails, resizing images and processing payments in the background.
- 37Message queue vs publish-subscribeQueue: each message is processed by one of several competing consumers. Pub/sub: each message goes to every subscriber of the topic
- 38At-most-once vs at-least-once vs exactly-once deliveryAt-most-once may lose messages; at-least-once may deliver duplicates; exactly-once processing needs deduplication or transactions on topAt-least-once is the usual choice, so consumers must be idempotent: processing the same message twice must be harmless.
- 39Dead-letter queue (DLQ)Where messages go after failing processing too many times, so they don’t block the queue and can be inspected or replayed later
- 40Kafka-style log vs a classic message brokerKafka keeps an append-only, partitioned log: consumers track their own offsets, can replay old messages, and get ordering within a partition. A classic broker like RabbitMQ delivers messages and deletes them once acknowledged
- 41Back pressureA way for an overloaded consumer to make producers slow down, e.g. bounded queues that block or reject new work, instead of letting queues grow until memory runs out
- 42Token bucket rate limitingA bucket holds up to tokens and refills at rate ; each request spends a token and is rejected when the bucket is emptyIt allows bursts of up to requests while capping the long-run average at .
- 43Fixed window vs sliding window rate limitingA fixed window counts requests per calendar minute and resets, so a client can send twice the limit around a boundary. A sliding window counts over the last 60 seconds and smooths that out
- 44Idempotency keyA unique ID the client sends with a non-idempotent request, like a payment, so the server can recognize a retry and return the stored result instead of charging twiceAn operation is idempotent if doing it several times has the same effect as doing it once.
- 45Retries with exponential backoff and jitterWait longer after each failure (e.g. 1 s, 2 s, 4 s, 8 s, up to a cap) plus a random amount, so clients don’t retry in lockstep and overwhelm a recovering service
- 46Circuit breakerAfter repeated failures, stop calling the failing dependency and fail fast for a while (open), then let a few trial requests through (half-open) before resuming normal traffic (closed)
- 47Average requests per second for 1 million requests a dayAbout 12, since a day has 86,400 secondsPeak traffic is often several times the average, so size the system for the peak.
- 48Rough latency of an L1 cache hit, RAM access, SSD read, datacenter round trip and cross-continent round trip~1 ns, ~100 ns, ~100 µs, ~0.5 ms, ~150 msFrom the classic “latency numbers every programmer should know”. Fast NVMe drives beat the SSD figure, but the orders of magnitude are what matter.
- 49Powers of two for quick estimatesa thousand (KB), a million (MB), a billion (GB), a trillion (TB)
- 50Estimate the storage for 10 million photo uploads a day at 200 KB eachAbout 2 TB a day, or roughly 730 TB a year before replicationbytes bytes.
Frequently asked questions
Is this enough to prepare for a system design interview?
It gives you the vocabulary and trade-offs interviewers expect. Practice by designing real systems out loud, such as a URL shortener, a chat app or a news feed, and reach for these building blocks as you go.
What does the CAP theorem actually say?
When the network cuts nodes off from each other, a distributed data store has to either reject some requests to stay consistent or keep answering with possibly stale data. Without a partition, it can offer both.
Do I need to memorize latency numbers?
Only the orders of magnitude: nanoseconds for memory, microseconds for SSDs, milliseconds for network round trips. That is enough to see which part of a design dominates.
More in Computer science
Computer science
CS fundamentals: binary and hex, two’s complement, Big-O, stack vs heap, threads and deadlocks, CPU caches, compilers, UTF-8 and floating point.
50 cardsMorse code
Learn International Morse code: all 26 letters, the 10 digits, common punctuation and signals like SOS, each with its rhythm and a memory aid.
50 cardsDS & Algorithms
Big-O for arrays, hash maps, heaps, trees and graphs, sorting trade-offs, and the patterns behind interview problems: two pointers, sliding windows, BFS, Dijkstra and DP.
50 cardsDesign patterns
SOLID, DRY, KISS and YAGNI, plus the Gang of Four patterns used most: singleton, factory, builder, adapter, decorator, observer, strategy, command and more.
50 cardsSoftware testing
Software testing fundamentals: the test pyramid, unit, integration and E2E tests, TDD, mocks vs stubs vs fakes, coverage, mutation and property-based testing.
50 cardsAPI design
API design fundamentals: REST, HTTP methods, status codes, pagination, versioning, OAuth 2.0 and JWT, CORS, OpenAPI, GraphQL, gRPC and webhooks.
50 cardsCryptography
Cryptography flashcards: AES modes, RSA, elliptic curves, Diffie–Hellman, hashing, HMAC, signatures, password hashing, TLS 1.3 and common pitfalls.
50 cards