Caching is everywhere.
It’s one of the few techniques in software development that can be applied at every layer of your application.
Things start right from the front-end layer and end at the database layer. The only thing that changes is the overall usage pattern of the cache.
The diagram below shows the utilization of cache across a typical application stack.
Let’s look at each stage in a little more detail.
1 - Front End App
Browsers cache stuff all the time - HTTP responses, asset files, HTML pages, and so on.
Developers can set the caching directives in response headers to control the caching behavior for specific assets.
2 - Content Delivery Networks
CDNs can cache static content like images, stylesheets, and JS files.
Their advantage is caching stuff closer to the users, reducing latency in the process.
3 - Load Balancers
Some load balancers can also cache frequently accessed data.
Responses can be served without calling the backend server. This improves the response times and also reduces the load on the server or database.
4 - Service Instance Cache
Service instances can employ their caching solutions to boost performance.
This can be in the form of in-memory caches that are checked before fetching from the database or even the distributed cache.
5 - Distributed Cache
A more robust caching solution can use distributed caches like Redis.
They help with faster reads and writes than going to a traditional relational database. Also, you can scale the cache across multiple nodes for greater resilience.
6 - Full-text Search Tools
Searching through tons of text information is a time-consuming task.
Tools like ElasticSearch help index the data for faster text search, effectively acting as a cache.
7 - Databases
You might think that databases always fetch from the disk.
But databases also use special caching mechanisms. For example, a Bufferpool is like a cache within the database that holds data pages to support faster reads.
Also, Materialized Views store results of pre-computed results of expensive queries for faster access
Lastly, many new-age databases have their built-in cache solutions.
So - for how many layers have you used caching?
Shoutout
Here are a few interesting articles that I read last week:
That’s it for today! ☀️
Enjoyed this issue of the newsletter?
Share with your friends and colleagues.
See you later with another edition — Saurabh
Excellent overview on where caching is utilized, Saurabh! I have used it in all of these layers. At my first job, we did some computation-heavy DB operations, and I remember using views to get to data faster, but I haven't used it since. All the other layers are pretty much part of our everyday lives 😃
Thanks for the shoutout! 🙌
Great summary. Worth mentioning that in distributed systems you also have to pay attention to cache keys, we recently wrote about that - https://packagemain.tech/p/unified-namespaced-cache-keys