Josselin Dionisi - Freelance developer

The web and caching, browser and server

ionicons-v5-k Josselin Dionisi Oct 5, 2022
64 reads Level: Confirmed beginner

The browser cache

You may not know how it works, but you've all cached things before. Or at least your browser has done it for you. Yes, you have?

When you first visit a website, it often takes longer to load. Nowadays it's not really noticeable as long as the site is well designed, but there is indeed a delay of a few milliseconds for a first load.

The explanation is quite simply that your browser hasn't yet been able to cache certain content on this site, such as images, CSS, Javascript, etc.

"Yes, but what is caching?"

It's the principle of putting these files in memory directly in the browser so that they can be used again later without reloading them. We'll only update them if we notice a change between the cache and the current version of a page.

The server cache

We've just looked at the browser side, and as is often the case in the Web world, there's also a server side to this principle?

In fact, we can easily cache things on a server as well. The point here is to be able to process things asynchronously, for example, or set aside data for later access.

"Wait a minute, isn't that the principle of a database?"

Yes, it is, and using a database can indeed be a way of caching items on the server side. It's not necessarily the most efficient, as performance can suffer pretty quickly, but on paper it makes sense.

"How else can we do it?"

A conventional database uses a hard disk to store its information. But for caching purposes, it's perfectly possible to use the server's RAM.

Access is much faster, and that's exactly what Redis, one of the leading systems recognized by developers, does.

A classic case

Imagine you have an online store with a large number of products. When your users arrive on your site, they're all going to end up on the same page (logically, the first one)?

If you had to load the results from the database for each visit to your site, this would be time-consuming and tedious for your server. It would have to execute the same query across the entire database.

By using a caching system such as Redis, you can execute this query only once and store the result in the server's memory. This way, the next time you visit the site, you won't need to call up the database and the query system, just retrieve the "cached" result and display it to the user.

Combining caches

Of course, it's possible - and even recommended - to combine different types of cache as required. An optimized browser cache coupled with a high-performance server cache will provide a better experience for your users, enabling them to access what they're looking for more quickly.

By now, you should have understood that the main sites you visit every day (Google, Youtube, Twitter, etc.) must have very good caches, given the volume of visits and data passing through their servers?

If you're interested in performance, I'm planning a more technical article on the subject?