Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

waywardson's avatar

API Structure: Pre-Calculate Data or Calculate it on Demand?

My API returns data that requires several calls to the database and a lot of calculations. It's time-sensitive data and needs to be updated and recalculated regularly.

When doing the DB calls and calculations on-demand, as requests come in, the page load is quite slow. And gets slower the more complex the data becomes.

The solution I came up with is to do all of that on scheduled tasks and save the results to the DB in json. Then when an API request comes in - the controller just fetches the json from the DB and returns it.

The results are far better in terms of page responsiveness. I have a snappy SPA experience with Vue, even with lots of data being returned.

It feels like that is probably a dirty hack though, that I shouldn't be doing?

What are best practices in this situation?

0 likes
1 reply
sr57's avatar

The solution I came up with is to do all of that on scheduled tasks and save the results to the DB

It's a good choice (when the original data does not changed to frequently and users don't need realtime calculations)

Please or to participate in this conversation.