What is a Cache?

A cache is a wonderful thing.

 

Firstly, we need to understand how our browsers request the webpages we visit. Every time you visit a webpage the following happens:

 

1.Your web browser sends a request to the website server for a page

 

The server then:

  • Understands what page it is you’re after
  • Finds/locates that page
  • Runs all of the code for each part of that page
  • Possibly looks in a database several times a second to retrieve odd bits of data
  • Pulls all of these bits of data together and assemble them into a final, single HTML page
  • Sends this to your computer

 

 

2. Your web browser then receives this page of HTML (static code- it doesn’t do much) which then lists a bunch of other files the browser needs like images, sounds, JavaScript, more html files etc.

 

3. Your browser then goes and requests each one of these extra pieces and assembles them into a web page. This is why you often see bits of a web page load at different times, in particular images loading one after another as you wait.

 

“But what’s a cache?!” I hear you cry.

 

If you go back to a page having visited it recently, your browser’s cache skips this whole process by assuming that it’s unlikely the page has changed much since the last time you visited. Rather than request the page again from the server, it just serves up the final result it got last time. That works great 99% of the time as it speeds up the loading process, but if the web page has changed then you’re seeing an old out dated copy- only by a few minutes, usually.

 

“So that’s why I have to clear my cache every time my web developer makes a change.”

Exactly!

However, to make things even more confusing, some servers will cache their responses too. So rather than go through the steps where the server finds the page, runs all of the code for each part of that page, and looks in a database several times a second to retrieve odd bits of data

every time a user visits the site it simply returns the same page it built last time without running the code or looking in the database.

 

That means that even though data has changed or new code has been deployed, the server isn’t bothering to lookup all that info until the cache expires and it goes through that process again. This saves a ton of time and processing power but means that sometimes it is delivering out-dated content.

 

“Sounds like a great way to save on hosting costs, I guess I’ll have to “cache” in!”

 

Indeed.