What in the world is caching?

Jessica Findley
5 min readApr 5, 2023

--

As a junior developer, I find it quite daunting with all the different technologies, tools and terminologies used. Which is totally normal. But where do I start with researching these things?

Well today it starts with caching. For some reason.. when I think of the word, an image of a cake pops in to my mind.. (sounds a bit like “cakeing” whatever that is..)

Literally me eating cake..

So I looked in to caching and it’s basically quick access storage and it’s used in loads of stuff. Such as web browsers, databases, computers.. its a widely used thing. Without caching, things would be ALOT slower in our tech world, so let me try to explain to you what it is.

For my example, because I think of cake with the word caching, I’m gonna use a cake shop…

Let’s say we have a cake shop, and customers walk in to our shop to buy some of our delicious cakes. In our shop you have a front counter display with ready made cakes to go, and we have your kitchen in the back of the shop where we make the cakes when you are getting low.

Now in our shop, our front counter got damaged and we can’t use it while waiting for repairs, so every time a customer comes in and requests a chocolate eclair.. what do we do? Well.. we would have to go in to our kitchen and make it! That’s not very efficient and it means our customer will be waiting a while for us to make this delicious choccy eclair. ( is it worth the wait? Abso-fricking-lutely)

This situation is what it would be like without caching. On your web browser for example, you access a website for the first time, your browser sends a request to the internet and it has to wait for it to respond with all the information such as the headers, images, text, EVERYTHING on that page. Without Caching, even if you had visited youtube before, it would still be a much slower load time.

This is also similar for our web applications, if a customer makes a request for data, your application needs to talk to your database, your database needs to find this requested data (and the database could have 10,000’s rows of data in it) and then the database will send this back to your application that you can THEN display to your customer.. It understandably can take a bit of time, which means from when the user clicks a button to request data, it could be as much as 30 seconds before they ACTUALLY see what they requested.

It’s slow because without caching, your application/ web browser would be using data stored on your HDD/SDD (hardrive). Data retrieval is much slower. Although, yes, your HDD/SDD may have some of your database data on here or data from your last youtube visit, but it’s still slow to retrieve this data.

So where does caching come in? Well.. caching is your quick access storage that is embedded in your CPU(Central Processing Unit) but it can also be used in browser caching, database caching, ram caching etc..

Let’s say we have now added a front counter display in our shop with cakes readymade on display.. when a customer walks in and they ask for an eclair, do we have to make them wait anymore for it? No! We can just give them one we made fresh this morning from the front counter.

Our front counter is like our Cache in our computer / application. It means users can access data quickly given a much better user experience.

Here is a quick little diagram just to show you the hierarchy — as you can see, cache is top level for speed compared to your HDD/SDD and RAM(Random Allocated Memory), but in terms of size, it’s much smaller :) RAM is also quick access storage, but not fast enough for your CPU(Central Processing Unit) for certain things. In these cases, caching in your CPU for example is used as a bridge between the RAM and CPU. If you want to read further the difference between RAM and Cache, this is a good article here — https://www.geeksforgeeks.org/difference-between-ram-and-cache/

As amazing as caching is in terms of quick access storage, as discussed it’s storage capacity is smaller than your HDD(Hard drive) and RAM. Now you might be asking.. what happens when you run out of space?

If you run out of space, your cache will enforce what’s called a cache eviction strategy and these are two main types.

  1. Least Recently Used — this is where data or websites that you haven’t lately are removed from your cache. ( looks at old stuff not accessed for a while and makes the judgement that you probs don’t need it cached)
  2. Random replacement — pretty much what you were thinking reading that, random items will be removed from the cache regardless of how long ago you accessed the data.

An example of Least Recently Used in our cake shop would be.. our choccy eclairs are not so delicious now :( so nobody is buying them! As a result, we stop displaying them in the front counter, but if anyone comes in and asks for one, they would have to wait while we bake some in the kitchen!

Thanks for reading, I hope it helped you :)

--

--

No responses yet