Caching: A Beginners Explanation to Better Performance

Caching: A Beginners Explanation to Better Performance

Optimising performance with different types of caching

We all understand that milliseconds are important when it comes to performance. Users will engage with our websites, apps, or products more pleasantly if we can eliminate a few milliseconds here and there, that's why we cache. In order to avoid repeatedly returning to the original source or dataset, caching enables us to save frequently visited content in memory. Caching speeds up and optimises common tasks like database lookups or calculations by storing them locally. Every Backend engineer should have "caching" in their toolbox. This blog post explains the few types of caching, how to optimise performance with each, and when to employ each type.

What is Caching?

Storage of frequently utilised resources, such as calculation results or data, is referred to as caching. If a piece of data is cached, subsequent requests for it can be fulfilled from the cache rather than having to be recalculated or requested from the original source. If the data is accessed frequently, this can considerably cut the time needed to complete activities. Caching has a straightforward premise. When a piece of information is likely to be needed again soon, it makes sense to store it so that it may be easily retrieved and used when required.

The Goal of Caching

Caching aims to deliver frequently accessed content and computations as rapidly as feasible from a nearby location. The database, the server's memory, or the user's PC are all examples of nearby locations. In order to serve frequently accessed content from a nearby location, we must first store it there. Caching is useful in this situation. It enables us to save frequently used info close by for easier retrieval.

When to Cache

Caching is useful whenever you have frequently accessed data or computations that you can’t afford to have run each time they are requested. You might have data that takes a long time to compute or that you need to fetch from a database. Typically, caching is used in these situations:

  • When data is static

    Server-side caching is the ideal choice if the data you need to cache is static and doesn't change frequently. This is due to the fact that the data will always be in the same location, negating the requirement for client-side caching that must be refreshed whenever it changes.

  • When data is dynamic

    Use client-side caching whenever possible if the data you are caching is dynamic. The ability to refresh the cached data as necessary is made possible via client-side caching, which is significant when working with user input.

Types of Caches

There are many types of caching that all have their own uses and benefits. To understand when to use each type of cache, it’s helpful to know their differences and similarities.

Database Level Caching

It might be challenging to cache your database, therefore some advance preparation is necessary. However, if done right, it may be quite profitable. All the difficulties associated with storing and retrieving data are taken care of by this kind of caching. In other words, the information (or output) is saved to a different database table or partition within the same database. In this manner, all it takes to get the data again is a query on the partitioned database. When your application has a large number of readers for a particular dataset and a low number of writes, database-level caching is fantastic. This indicates that it's a good idea to save the information in a location that makes it simple to go to when you need it again. If your dataset is mainly read-only, this is quite helpful.

Browser Cache

The browser cache is one of the simplest and oldest forms of caching. A browser cache is a directory on a computer where frequently used or static content is stored for quick retrieval. It is commonly used to store images, videos, or web pages that are visited often so that they do not have to be downloaded again. The browser cache can be used for almost every type of content. This includes HTML pages, CSS, JavaScript, images, and fonts.

HTTP Cache

The HTTP cache stores data on the server side. The data is stored in a format that the client can understand and process while being rewritten as static content that can be served to clients without any interaction with the server. It is commonly used along with a Content Delivery Network (CDN) to decrease the load on the server. The HTTP cache is the most robust form of caching. It can store information in a variety of ways, including the browser cache, the reverse proxy, or even the database. It is used for both static and dynamic data. However, it is best used for static data since it would be difficult to manage how a client is viewing that data.

Server Side Storage (SSS) / Static Content Cache

The most common type of cache is a static content or static language content cache. Static content is any asset like images, CSS, JavaScript, fonts, or any other static asset that you don’t want to change. This type of cache is an SSS, which stands for server-side storage. This means that the files are stored on an external server. This server will be a CDN (more on that shortly) or the origin server. This type of caching is often used to reduce load time and decrease your website’s bandwidth costs. Because static content isn’t changing frequently, it makes sense to store it on the server so it doesn’t have to be pulled from the original source each time.

Cache Invalidation

An important aspect of caching is ensuring that the data you are caching is accurate. If you are caching data that is incorrect, it can have serious consequences. To avoid these consequences, you must invalidate your caches when the data they are caching is updated. There are two ways to invalidate your caches:

  • Expiration

    When you set an expiration date on a cache, it will automatically invalidate itself when that date passes. This works well for caches that don’t change very often, but it doesn’t work as well for caches that change often.
  • Invalidation Trigger

    An invalidation trigger is a piece of code that is triggered when the data you are caching is updated. This works well for caches that are updated often.

Cache Hierarchy

The concept of caching has a hierarchy, with each subsequent layer of caching being slower than the layer before it. The lower in the hierarchy a piece of data is, the faster it will be accessed. The higher up in the hierarchy, the slower it will be accessed since it has to go through all the layers below it.

Cache Hierachy


Caching is an essential tool in any backend engineer’s toolkit. There are many types of caches, each used for different purposes. Server-side static content caching is the most common type of cache. Caching is a great way to speed up your application, but there is a risk that you might have out of date or even incorrect data in your cache. Make sure to invalidate your cache when you need to update data or change an algorithm.