Learn Steps

Working on scale: What is caching and where to use which one?

This is the second article in series Working on scale. You can find the first one below. It was about replication and sharding and where to use which one.

Working on Scale: What is replication and sharding and where to use which one?

In this article we will be looking into caching, its different types, tools and where to use which one. So lets start with what is caching and where to use which one.

Caching:

Caching is saving repetitive data in faster accessible space rather than getting data from slow space again and again. In caching we save information which does not change very frequently thus saving resources which may be used to calculate that information. Caches are generally temporary and you may loose it any time.

Important things to know when you are using caching. 

You must be aware of the scenario what will happen in case of cache miss. You must handle these scenarios properly.

The cache invalidation is one the important topics that people usually don’t put focus on. You must understand that expiring cache is as important as creating a cache.

Caches are generally temporary memory and you may loose it any time. In that case what is your failover strategy and how much time it will take to warmup the cache again.

 

In todays computer architecture there are many types of caching that we can talk about below are few listed ones.

1. Web Browser Caching

2. Web Server Caching

3. CDN caching

4. Database caching

5. Object or Application Caching

6. Hardware caching in Form of L1, L2 and L3 caches

 

Lets look into these one by one and see which one is applied where.

Web Browser Caching:

Here we cache the scripts or libraries that rarely change on browser side. In this case browser don’t need to load them again and again over the internet, thus makes the web pages load faster. Few of the examples that we save in browser cache are libraries like Jquery, Angular, Bootstrap.

 

Web Server Caching:

In web server caching the information is cached on web servers. The web server is when asked by the client sends the information that it saves in cache. These can cache the response of a particular request and can serve the further such requests from the cache.

Example: Varnish cache.

 

CDN caching:

A few percentage considers CDN’s as also caches but they are actually servers which are optimized for serving content in different regions of the world from the nearest locations which made them fast. What actually happens with CDN is you made the content available to CDN and then the CDN makes it available in each of the region specific server. Now when client asks for the resource it is being served from the nearest locations.

Example: Akamai, Cloudflare

 

Database Caching: 

Database also tries to cache data to serve it faster. They try to cache it in memory instead of reading it every time from the disk to serve it. Reads from SSD is 4 times slower than Memory.

Example: Query caching in mysql.

 

Application caching: 

In application caching the caching is actually handled by caching and make use of in memory databases like redis or memcached. Cache logic is implemented in application code and thus require code changes if you are planing to implement it.

Example: Redis, Memcached

 

CPU caches: 

Now a days there are three to four levels of caching that are implemented as hardware named as L1, L2, L3, L4 caches. L1 is the fastest and L4 contains the biggest memory space. You can see the inverse relationship between speed and space. You can read more about them here.

 

These are the types of caches that i know of if you know few more please mention in comments and those will be included here.
Also if you find any mistake feel free to mention in comment I will get it fixed as early as possible.