Shape the data and the API
You're building a shop. With bots included, 9,000 req/s arrive, far more than your two servers can answer. Before the design, decide how the data and the API should look.
Play this levelPart of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
An API is the promise your system makes to everyone using it. Name things clearly: POST /orders creates an order, and GET /orders/42 reads one. Everything clients do later depends on this.
Choose your database by how data is used. Orders link to customers and items, and money must stay correct, so a relational database fits. NoSQL fits simple lookups by key at huge scale.
An index is like the index in a book: it finds rows without reading the whole table. It makes reads faster, and every write a little slower, because the index must be updated too.
A token bucket is the usual way to rate limit. Each user has a bucket that slowly refills. A request takes a token, and an empty bucket means wait. Short bursts pass, while steady floods don't.
What you have to achieve
9,000 requests a second at the busiest hour, 10% of them writes.
- No broken or illegal connections
- Use an API Gateway
- No component is overloaded
- At least 3k req/s get an answer
- p99 response time under 350 ms
What you learn
The boxes were only part of this design. The shape of your data and API often lasts longer than any server, because every client ends up depending on it.
Check yourself
Which API call best creates a new order?
- GET /createOrder
- POST /orders
- GET /orders/new
Show the answer
POST /orders. POST creates something, and the URL names the thing. GET must never change data, because browsers and caches repeat it freely.
Orders link to customers and items, and totals must stay correct. Which database fits?
- A relational database
- A key value store
- A cache
Show the answer
A relational database. Relational databases join related tables and use transactions, so money stays correct.
Finding a customer's orders has become slow as the table grew. What's the first fix?
- Buy a bigger machine
- Add an index on customer_id
- Delete old orders
Show the answer
Add an index on customer_id. An index finds the rows without reading the whole table. It's cheap, and it's the first thing to check.
An app shows a feed that keeps growing. How should it fetch the next page?
- Send the whole list every time
- Skip the first N rows
- Ask for items after the last one seen
Show the answer
Ask for items after the last one seen. This is cursor pagination. Skipping N rows gets slower deeper down the list, and new items shift what you see.
How does a token bucket treat a short burst from a normal user?
- It allows the burst while tokens last
- It blocks the user at once
- It removes the limit
Show the answer
It allows the burst while tokens last. Saved tokens pay for bursts, then the steady refill rate takes over. Normal users pass and floods don't.