What is system design, and how do you decide what to add?
Start here before the 25 ideas. One picture of an app growing, the trade-offs behind every choice, and a guide that starts from your problem.
Most system design advice starts with patterns. This page starts with one app on one database, and adds each piece only when something runs out.
System design is deciding how the parts of an application fit together so it keeps working as more people use it. It is not a list of patterns to add. Every product starts as one app and one database, and that is the right design for longer than most people think. The work is knowing which part will run out first, and adding the smallest thing that fixes it.
Every design, small or huge, answers the same four questions:
From the user to a server, to the database, and back. Every box on that path adds time and can fail.
One database, copies of it, or pieces of it on many servers. This decides what a write costs.
A server, a disk, a whole region. Something either takes over, or people wait.
Which part runs out first, and what you add then.
The database sits under all four. App servers hold no data, so you can add ten of them in minutes. The database holds the one copy of the truth, so every answer above gets hard there first. That is why this site follows each idea down to it.
Every choice in system design buys more of one of these and pays with another. Nothing on this list comes free, and a design review is mostly asking what each box costs.
How long one request waits.
One row in a million: an index reads 4 pages, the same query without it reads 19,231. Notebook 3 →
How many requests a second the system finishes.
One connection committing durably tops out near 4,200 commits a second, set by a 0.236 ms disk flush. Napkin math →
Whether people can use it right now.
GitLab was down 18 hours in 2017 while they rebuilt from a staging copy. GitLab →
Whether every reader sees the latest write.
Two deposits at the same moment: one was lost at READ COMMITTED. Notebook 4 →
Whether "saved" means saved, even after a crash.
A Redis failover lost 2,217 writes that clients had been told were saved. Notebook 9 →
Money, and the people who run it.
Dropbox could not grow one shard, so expanding meant doubling the whole fleet for a little more data. Dropbox →
Press a choice to see what it buys and what it costs.
The same application, from its first thousand users to a million. At each step one part runs out, and you add one thing. It stops twice to ask you what comes next. The numbers of users are only an example: the order is what matters.
Read the order again: app servers, then a cache, then replicas, then shards. Each step is harder to undo than the one before. That is why the decision guide below always tries the cheap fixes first.
None of these teams were careless. Each one hit a decision that looked fine until traffic or bad luck tested it. Every one is written up on this site with its numbers and sources.
300 GB of production data deleted in seconds. Five backup methods, none of them worked. 18 hours down and 6 hours of writes lost for good.
The decision behind it: backups that were taken but never restored and checked.
When the MySQL master died, someone had to wake up and fail over by hand. They automated it, with a target of 30 seconds, and a guard that blocks a second automatic failover for 30 minutes.
The decision behind it: automate the failover, and put a limit on the automation.
Sharding by workspace put each big customer on one shard. That shard ran hot while the rest sat idle. They resharded by channel ID, live, with every row written to both copies and compared before cutting over.
The decision behind it: the shard key, picked from who owns the data instead of how it is accessed.
A release changed how often the game called the account database. Queries slowed, a 200 ms client timeout opened new connections instead of waiting, and the account service was down for almost 18 hours.
The decision behind it: retry and timeout settings that turned a slow database into an unreachable one.
Start from the problem you can measure, not from the name of a pattern. Pick what hurts:
Try these in order. Stop when the problem is gone.
Not yet: Do not shard for read load. Replicas and caches are much cheaper to add and to remove.
Measure first:
SELECT query, calls, mean_exec_time FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;
Try these in order. Stop when the problem is gone.
Not yet: Do not add replicas for write load. Every write still goes to the one primary.
Measure first:
SELECT relname, n_tup_ins + n_tup_upd + n_tup_del AS writes FROM pg_stat_user_tables ORDER BY writes DESC LIMIT 10;
Try these in order. Stop when the problem is gone.
Not yet: Do not raise max_connections into the thousands. Each PostgreSQL connection is a process with its own memory.
Measure first:
SELECT state, count(*) FROM pg_stat_activity GROUP BY state;
Try these in order. Stop when the problem is gone.
Not yet: Do not count backups you have never restored. GitLab had five.
Measure first:
SELECT now() - pg_last_xact_replay_timestamp() AS replica_lag;
Run this on the replica. On a primary it returns NULL.
Try these in order. Stop when the problem is gone.
Not yet: Do not go multi-region for writes until you can say what happens to a write when a region is lost.
Try these in order. Stop when the problem is gone.
Not yet: Do not shard to fix analytics queries. Move them out of the live database instead.
Measure first:
SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 10;
Try these in order. Stop when the problem is gone.
Not yet: Do not split the database first and work out the transactions later. Every join across the split becomes an API call.
My rule: if you cannot point at the graph that shows the problem, you do not have it yet. Every box in the animation above was added because one number crossed a line.
Pick an answer. You will see whether it is right, and why.
1. The database is at 90 percent CPU from reads. Which fix is the hardest to undo?
Sharding changes where every row lives and how every query is routed. Caches, replicas and bigger machines can be added and removed.
2. Adding read replicas helps which kind of load?
Every write still goes to the one primary, and the replicas replay all of it too.
3. Turning off synchronous_commit buys throughput. What does it cost?
The commit returns before its WAL is flushed, so a crash can lose those commits. The loss is bounded: the WAL writer flushes at most 3 times wal_writer_delay late, about 600 ms at the default 200 ms. The database stays consistent.
Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.