Caching Demystified: A Beginner's Guide to Optimizing System Performance
Let's take an overview of caching.
When it comes to performance, caching is an important technique to be used in systems.
So, what is caching?
Caching is a process that helps to store some data in order to retrieve it more quickly whenever a user wants it, by avoiding expensive operations such as disk I/O, database queries like joins, heavy computations, etc.
Populating the cache:
There are several ways to populate the cache:
1. Lazy Population:
When a user makes a request to get the data, it fetches the data from the DB, and after creating the result, it is stored in the cache and then sent to the user.
2. Preloaded/Warming Population:
It is a technique to populate the cache by analyzing the access pattern of the user to data. Let's say, for example, YouTube training videos. The videos are accessed by lots of users, so frequently accessed ones are put into the cache.
3. Write-through caching:
While storing the data in the DB, we also write the data in the Cache. In this case, we know this data will be accessed in the coming time for sure. For example, live cricket scores. Whenever any changes regarding the score of the live cricket match changes, we update the DB and cache. Because we know users will see the score in a few minutes. So we should serve the score as soon as possible, like Crickbuzz.
Scaling of Cache:
As the cache is similar to DB, the scaling of the cache is the same as DB scaling:
1. Vertical Scaling
2. Horizontal Scaling
Locations that can be used as Cache:
1. Client-Side Caching:
Using browsers, and mobile devices for caching. We can use this type of cache when the data is mostly constant across time. Let's say, for example, an auth token that we can store in the local storage of the browser, also we can store images, JS files, etc.
2. CDN - Content Delivery Network:
CDN is a set of distributed servers located across different places in the world. Let's say we have CDN servers in SA, London, and Mumbai, and our main/origin server is in SF. So if someone from India makes a request to the server, it will first go to the Mumbai server to check if the data is available to serve; otherwise, it will go to the origin server. By checking the nearest CDN server, we reduce the response delay.
3. Remote Caching:
The most popular place to cache the data. It is a centralized place to cache the data. For example, Redis.
4. Database Caching:
Let's understand it by taking an example. Let's say we have two tables in SQL: one is the Post and the other is the Likes table. Now let's say we want to know the total number of likes of a particular post. For that, we need to run a query: SELECT COUNT(*) FROM likes WHERE post_id=xyz. Now, each time to know the number of likes, performing this operation is very costly. So for that, we can store the total number of likes in the Post table by creating a column called total_likes. We will update the total_likes by one when a user likes the post. By doing this, we can avoid the expensive DB query.