Separate the Database
Your app and database are roommates sharing one bedroom. Under pressure, they fight over space. The first architectural decision you'll ever make is giving each of them their own room.
The roommate problem
In Chapter 1, everything lived on one server — your application code and your database running side by side on the same machine, sharing the same RAM and CPU.
That is fine when things are quiet. But under load, they start getting in each other's way. Here is exactly why.
Every database needs to keep frequently accessed data in RAM so it does not have to read from disk every time. Think of it as the database's working memory. On a 4GB server, the database ideally wants about 1GB of that for itself.
Meanwhile, your application also needs RAM — to hold the state of every active user connection, every request being processed, every response being built. Under load, it wants that same 1GB.
The fix: dedicated servers
Move the database to a dedicated server. Now the database gets 100% of that server's RAM for its own working memory, and your application gets 100% of its server's RAM for connection handling. Both run at full capacity.
The network penalty
Before the split, when your app asked the database for data, it was like asking a question to someone sitting next to you. Instant.
After the split, the app server and database server are on the same internal network, but now each request travels over a wire. That adds about 0.3–0.5 milliseconds per query.
Is that a problem? If a query used to take 5ms, it now takes 5.5ms — a 10% increase. Completely acceptable given what you gain: two machines that can be tuned, scaled, and replaced independently.
This is your first encounter with a theme you will see everywhere in system design: every architectural decision is a tradeoff. You are trading a tiny bit of latency for a massive gain in flexibility and capacity. When the tradeoff is this lopsided, it is an easy yes.
Five database types
Here is something nobody tells you when you start learning system design: there is not just one kind of database. There are five fundamentally different types, each designed for a completely different problem. Picking the wrong one for your access patterns means paying for it for years.
Interviewers will absolutely ask "which database would you use?" The right answer starts with questions — How is this data read? How is it written? Does it need to connect to other data? — before picking anything. Here are all five, with their internals, real examples, and honest gotchas:
A relational database stores data in tables — think of a spreadsheet where every row is one record and every column is a field. What makes it "relational" is that tables can reference each other using IDs. Your users table has an id column. Your orders table has a user_id column that points back to a user. The database can combine ("join") these two tables on the fly to answer questions like "give me all orders placed by Priya."
What makes this powerful is that you only store Priya's name and email once — in the users table. Ten thousand orders all just store her ID. If Priya changes her email, you update one row, and every order immediately reflects that. No duplicates. This property is called normalisation.
Using multiple databases
Large products do not pick one database for everything. Twitter uses MySQL for core entities, Redis for prebuilt timelines, and Elasticsearch for search. Not because it is elegant — managing multiple databases is extra operational work. But each type is 10–100× better at its specific job than any general-purpose alternative.
A common trap: choosing DynamoDB because it "sounds scalable," then discovering you can only look up data by primary key. Every other query becomes an expensive full scan. The database choice must match your access patterns — how you read the data matters more than how much of it you have.
When asked "which database would you choose?" — respond with questions first. "What are the read/write patterns? Does the data have relationships? Do we need transactions? Will the schema change frequently?" Pick based on the answers, then explain the tradeoff you're accepting. The reasoning is what the interviewer is listening for.