Rohit
Rohit

Reputation: 1194

PHP, MySQL - Storing data locally vs Fetching from remote every time

I have a dashboard application that has PHP backend and javascript frontend. Data is read from multiple sources and I have access to databases of all the sources. While designing the application, is it a good idea to store remote data locally instead of hitting the remote database everytime the application has a request?

  1. Store locally? Reason being the data is not live. I can write a cron to run in the background to update the data every 5 min and the application always will read the data from local DB thereby giving faster load times.

  2. Read from remote every time? Since I have direct database access to all these remote DB's, I do not notice any performance gain of storing data locally over fetching from remote everytime.

Which approach scales better?

Upvotes: 0

Views: 144

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562388

What you're describing is called "caching." It's a common optimization.

Fetching data remotely is much more expensive than getting it out of a local cache.

You should learn the Latency Numbers Every Programmer Should Know.

The tricky part of caching is knowing when you need to discard the local cached copy of data and re-fetch it from the remote database. This is a hard problem with no single answer.

There's an old joke attributed to Phil Karlton:

“There are only two hard things in Computer Science: cache invalidation and naming things.”

Upvotes: 1

Related Questions