What is Caching?

What is Caching?

Written by Dev Dojo on Apr 11th, 2018 Views Report Post

If you've ever heard the term cache or caching, and you're not too familiar with what it means. Fear not! Keep on reading to get the simplest answer any idiot could comprehend.

Let's start things off with a quick example of caching and how it works.


A quick example

When you type in a URL in your browser somewebsite.com and press ENTER. Your computer (referred to as the client computer) makes a request to the server. The server is responsible for sending back a response which displays in your browser.

We can pretend that the server does a simple calculation:

5+5

And returns the result (response) back to you:

10

Now, say that someone else requests the same webpage from a different computer. The server will need to do the calculation again:

5+5

And return the response:

10

As you can see, every time this web page is accessed it needs to run the calculation and return the result.

Obviously, this calculation is not going to cause the server to act slow, but imagine if the calculation was large and needed to retrieve a lot of information. Well, if you were to get a large amount of traffic to that page it could make your server slow or even make it crash.


The server can save the response

What if instead of running this calculation every time, we could store the response in a temporary location perhaps a folder called 'cache', well then every time we get any kind of request, we can just return the result:

10

And we don't have to run the calculation.


What happens when our program or our code changes

What if our application logic changed and is now suppose to run the following functionality?

5+7

Well, if someone were to access that page, they would still get:

10

This is of course until we clear the cache (delete the contents of the cache folder)

After clearing our cache, our application logic will run the 5+7 functionality and store a new cache file containing the new response:

12

And that's the basics of cache or caching.

Need a visual representation of this example. Check out the following video: What is Caching

This was an example of server caching there are also other types of caching, but if you understand this concept all the other types of caching will work the same way.

Comments (0)