DEV Community

Cover image for Caching — An overview
Kartikey Srivastava
Kartikey Srivastava

Posted on

Caching — An overview

In simple terms, caching means storing the frequently accessed data in a storage where one can quickly retrieve it without the querying the source of truth i.e. the database. This storage is generally a temporary storage. Caching helps in improving the performance of an application by loading the data somewhere near to the application so the time taken in querying the db is reduced and also the number of times a data source is accessed is also reduced.

Applications can respond to the user requests more quickly improving the overall user experience. It also helps in reducing the load on the database since the data is now being accessed from the cache and not the db. One thing to note here is, Caching is suitable for frequently accessed data.

We all know CDN servers, DNS servers, Redis cache and Apache Ignite are some of the most commonly used cache in today’s world. I have already talked about CDN and DNS in my previous posts in case you want to read.

Let’s get an overview again:

CDN(Content Delivery Network) caches the static content and serves them to the users located in the nearby region.

DNS(Domain Name System) can cache the IPs of the servers and avoid the long path from the client’s IP to the authoritative server.

Redis and Apache Ignite are the caches mostly used as databases. Redis follows a Master-slave architecture whereas Ignite follows a distributed data structure.

There are many types of caching some of which are:

Client caching
Distributed caching
Database caching
Application caching

Let’s understand them one by one with the help of examples:

DISTRIBUTED CACHING

  1. A type of caching that involves storing data across multiple servers/nodes in a network.
  2. A perfect example for this is Apache Ignite.
  3. It has 2 caching modes: PARTITIONED and REPLICATION.
  4. In case of partitioned mode, the data is stored in chunks across multiple servers so if you have 100 units of data and 3 servers…30:30:40 can be the ratio of their storage.
  5. In case of replication mode, the data is stores as a whole across all the servers so if you have 100 units of data and 3 servers… each one of them would store all 100 units.
  6. This type of caching is useful when the application requires high availability and scalability. You can always add more nodes in order to scale and also in case a node goes down, the remaining nodes can handle the user requests.
  7. However, distributed caching trades of consistency in order to achieve high availability.
  8. Read about CAP Theorem and you’ll get a better clarity of why distributed caching follows an eventual consistency pattern.

DATABASE CACHING

  1. Database caching involves storing frequently accessed data in-memory rather than fetching it from the primary database every time, which reduces database load and improves overall performance.
  2. By routing the requests to the cache, you can achieve a much lower latency and also reduce the load on the database which includes querying it every time a request comes in.
  3. Instead of querying the database for every request, your application can first check the cache. If the data is found(cache hit), the request is served from the cache. If not found(cache miss), the request is sent to the database. Now this request can be stored in cache for future queries.
  4. For example, a user requests his/her information for the first time on a server. This request is served directly from the database. With caching, now the requested data is stored closer to the client after the first request, now for every similar request it would be served from the cache instead of your primary db.
  5. Caching solutions like Redis comes into play here.

CLIENT CACHING

  1. This type of caching involves storing data directly on the client device be it a web browser or a mobile phone.
  2. It becomes useful for applications that frequently access client’s locally stored information.
  3. Obviously, the application’s performance improves since now the number of requests to the server has been reduced that also leads to less data being travelled over the network.
  4. A perfect example for the same is Password storage in Mobile applications. Applications that ask you to enter a password to log into them, they might offer you to save the password on your device with a message like: “Don’t worry, this will be saved locally on your device.”
  5. Once you give your consent, the password gets stored locally on your device in some encrypted format and the next time you log in, the password field gets automatically populated.

APPLICATION CACHING

  1. Here the caching is done within the scope of application.
  2. API responses, User serssions can be cached and served to the user requests.
  3. Do not confuse it with database caching. They tend to be similar but are different when it comes to what is being cached.
  4. Application caching can cache api responses whereas database caching can cache query results.
  5. Anything at the application level can be cached using application caching be it an api response, a token or some computed value.
  6. Application cache lies closer to the application whereas database cache lies closer to the database.
  7. For example, let’s say you have an e-commerce platform. When a user logs in, his/her user session can be stored in the application’s in memory cache and now this session dadta can be served quickly without the need to go to the database.

These are some of the caching techniques. I have kept this post short and brief as I wasn’t feeling quite well but had to keep the streak of posting on weekends alive. Will come with better and detailed posts in future.

If you liked this post do consider liking it and you can also share and follow me for more such content :).

Top comments (0)