Challenge
Report pages summed millions of transactions by date, project, source and currency on every request. As the number of donations grew, data and filters began to take more than 5 seconds to load, and the reports needed to keep up with big fundraising campaigns, when the most people are watching.
What we built
- Profiling and indexes. We found the slow queries and added composite and full-text indexes.
- Ready-made totals. A PostgreSQL materialised view with daily precision, grouped by source, project, currency and amount range, plus an hourly read model, so totals are read instead of recalculated.
- A lighter API. Dates travel in ISO 8601 (UTC), and sums and averages are calculated in the browser.
- A cache in front of the API with nginx.
- Proof, not promises: k6 load tests, charts, correct local-time day boundaries and automated tests for the numbers.
Results
- Reports opened in 0.5 s instead of more than 5 s, ten times faster, even before the cache.
- On a 5-million-record test set the table shrank from 444 MB to 23 MB, and the indexes became an order of magnitude smaller.
- With the cache, the average response time of the report API fell from 1.9 s to 0.71 ms (k6, 20 virtual users).
- The same load test served almost three times more requests.