The same report, before and after
Tallowbrook’s sales report is served two ways by the same API. The legacy version loads each order, then its customer, then each product, one database query at a time. The optimized version asks for everything in three batched queries and caches the result until the data changes. Both return the same report.
Your sandbox
Not created yet.
Measure it from your browser
Seven requests to the live API: the legacy report three times, the optimized report three times, then once more with the ETag from the last answer. The times below are measured here, in your browser, and include the network.
What changed, and what is simulated
- Fewer round trips. The legacy code asks the database a question for every order, every customer and every product line (the “N+1” pattern). The optimized code asks three questions in total and joins the answers in memory.
- A cache that knows when it is stale. The finished report is kept until an order, customer or product changes. Clients that send back the ETag get
304 Not Modifiedand no body at all. - What is simulated. This demo keeps its data in memory, so each “query” waits a fixed few milliseconds to stand in for a database round trip. The number of queries is real; the size of the gap on a real system depends on its database and network.