When one database is the bottleneck
After this: Tell reads from writes, see that most apps read far more, and see why one database runs out.
On this page · 5 sections · 2 checks
A shop's website runs on one app server and one database. Every page asks the database something: show this product, list these reviews, save this order. The shop gets popular. More servers in front can share the app's work, as Scaling out: load balancers and stateless servers showed. But every one of those servers still asks the same database. What happens when that one database cannot keep up?
Reads and writes
The requests that reach a database come in two kinds.
- A looks data up and changes nothing: a SELECT. Showing a product page is a read.
- A changes data: an INSERT, an UPDATE or a DELETE. Placing an order is a write.
Think about a shop. A customer opens twenty product pages, reads the reviews, and then buys one thing. That is dozens of reads for a single write. Most apps work like this: a news story is written once and read many times, and a profile is changed rarely and viewed often. An app whose requests are mostly reads is called .
A worked example
Here are the numbers this tutorial uses throughout.
- The shop's app sends its database 9,000 reads and 1,000 writes every second.
- That is 10,000 requests per second, and 9,000 of them, 90%, are reads.
- One database can answer about 3,000 reads per second.
- So the reads alone are three times what one database can serve.
When requests arrive faster than they are answered, the extra ones wait in a line. Here 6,000 more reads arrive each second than leave, so the line grows every second and pages get slower and slower.
The numbers in this learning path are round teaching numbers: a database query takes 10 ms, and one database answers 3,000 reads per second. Real values depend on the machine and the query, and can be several times higher or lower. The size of each number compared with the others is what matters.
One database in the picture
Every request of the app ends at the same machine. Run it and follow one read.
- Follow one requestPress Run it and step through. Follow the request from the users to the app server and on to the database, and read where the time is spent.
Press Run it, then step through and watch the requests move.
The app server only passes the request on. The time, and the work, are in the database.
Three ways forward
- Make each query cheaper. A search that reads every row can often become a quick lookup with an index, as in Indexes: why queries are fast or slow. This is the first thing to try, because it costs no new machines.
- Buy a bigger machine. This works for a while, but the biggest machine has a limit, and it costs much more than several small ones.
- Use more machines. That is the rest of this tutorial: copies of the database to share the reading (lessons 2 and 3), and splitting the data to share the storing and the writing (lesson 4). Lesson 5 shows what goes wrong when the network between the machines breaks.
Check yourself
A recipe site: cooks open recipes all day, and a few new recipes are added each hour. Is its database read-heavy or write-heavy?
One database can answer 3,000 reads per second. The app now sends it 9,000 reads per second. What happens?
- Tell a read (a SELECT, which changes nothing) from a write (an INSERT, UPDATE or DELETE).
- Say why most apps are read-heavy: a thing is written once and read many times.
- Work the shop's numbers: 9,000 reads and 1,000 writes per second, 90% reads, three times what one database serves.
- Name the three ways forward: cheaper queries with an index, a bigger machine, or more machines.