Databases & caching · Trade-off defence
Add a cache, or fix the query?
A read-heavy endpoint is slow. When does a cache in front of the database help, and when is it needless risk?
Trade-off defence · a cache in front of PostgreSQLIllustrative scenario, not a real incident
The order history endpoint has a p99 of 1.8 seconds against a target of 300 ms
A shop's GET /customers/{id}/orders returns a customer's 20 most recent orders from PostgreSQL. It takes 300 requests a second, and 19 in 20 requests to the service are reads. The team is four engineers and runs no cache today.
The confirmation page links to this endpoint, so a new order must appear in it at once. A colleague proposes putting Redis in front. Another says the query should be fixed first.
What the dashboards and logs show
- p99 latency
- 1.8 s, target 300 ms
- Query plan
- sequential scan of `orders`: 40 million rows read, 20 returned
- Database CPU
- 70%, most of it this one query
- Freshness
- an order placed a second ago must be listed
Take a side · neither is wrong, and your defence is what is scored
Before you decide, argue one side. Which will you defend?
Your rubric score, the model answer, the follow-up questions and the related lessons appear once you have answered the last step.