Why we need caching for better performance and availability? - Part 1
Why we need caching, the challenges it solves, what a cache is, e-commerce and mutual fund examples, and the local, distributed and hybrid cache types.
Why we need caching for better performance and availability? - Part 1
Almost all modern applications are built on data, and most of this data is read much more often than it is changed. Fund details, product catalogs, user profiles, they are requested again and again by thousands of users, and every request is going to the database if we don’t do anything. It works fine in the beginning, but as the traffic grows, the database becomes the bottleneck, the response time goes up and finally our customers are feeling it. In this series, I share the caching guidelines we derived for our teams, from the basics of caching to the server-side strategies and the hybrid caching with Redis. In this article, we are going to see why we need caching, what is a cache and what are the types of cache we can use.
Caching Guidelines series:
- Why we need caching for better performance and availability? (this article)
- How to cache the data based on the demand? Cache-aside, lazy loading and read-through
- How to choose the right server-side caching strategy? Write-through, write-behind and refresh-ahead
- How to keep local and distributed caches in sync? Hybrid caching with Pub/Sub
- How to implement hybrid caching with EasyCaching?
TL;DR Summary
- Caching keeps the frequently used data closer to the application, so we don’t read the same data from the database again and again.
- It reduces the latency and the load on the database, which improves the performance and the availability of the application.
- A local cache lives in the memory of the application instance, it is the fastest but it is private to that instance.
- A distributed cache is shared by all the instances, it is a network call but it is consistent for everybody.
- A hybrid cache uses both, to get the best of both worlds.
- Caching brings a new problem, stale data. We must always think about the expiry and the synchronization of the cache.
Common Challenges
Let us discuss the key concerns why we need caching,
High latency
Reading from a database is a network call plus the query execution time. When the same data is requested again and again, we pay this cost for every single request, though the data is not changed.
Database load
Every request which reaches the database consumes its resources. When the traffic grows, the database is slower for everybody, and it is the most expensive component to scale in most of the architectures.
Availability
When the database is slow or not available, the application is also not available. A cache in front of the database can still serve the data for the requested items, which helps us to survive the short database slowness.
Scale-out of the application
When we scale out the application instances, the new instances are starting with nothing in their memory. All their first requests are going to the database. We will see this in detail in Part 4.
The chain is simple. The more requests reach the database, the higher the latency and the load. The higher latency and load reduce the performance and availability of the application, and the customers are the first to feel it. Without caching, we will be always reactive, we add more hardware to the database when it is already hurting us.
What is a cache?
A cache is a temporary storage which keeps a copy of the frequently used data, so the next request for the same data can be served faster. In simple terms, it is the answer which we keep ready on our table, instead of walking to the library every time somebody asks the same question.
The following diagram depicts the difference between an application without a cache and with a cache,

When the requested data is available in the cache it is called as cache hit, and when it is not available it is called as cache miss. The goal of every caching strategy is to increase the number of hits, without serving the wrong data.
Why caching is important?
In order to reduce the latency, to protect the database from the repeated reads, and to improve the performance and availability of each call, caching is a must for the read-heavy applications. At the same time, it is not free. Keep in mind that every cache copy can become stale when the original data is changed, so each caching decision should have an answer for “when this data will be refreshed?”.
Where does caching help in e-commerce and mutual fund world?
Let us see two domains, to make it more practical. These are illustrative scenarios to explain the idea, not measured results.
| Domain | Data which is read again and again | Why it is a good candidate |
|---|---|---|
| E-commerce | Product catalog, product details, category pages, home page top products | Thousands of shoppers are reading the same products, but the product data changes only few times in a day |
| E-commerce | Prices and offers | Read on every page view, changes only when the business updates them |
| Mutual fund | Fund details, factsheets, fund unit details | Same fund pages are requested by many investors, and the data changes rarely |
| Mutual fund | Daily NAV (Net Asset Value) and performance data | Published once per day, but read many times during the day |
| Mutual fund | Fund search and the top performing funds list | Popular and repetitive queries which are expensive to compute |
At the same time, not everything should be cached. In e-commerce, placing an order and the payment must always go to the source of truth. In the mutual fund world, the investor’s subscription and redemption transactions must be saved in the database first. A cache is for the data which is read many times, not for the data which decides the money. We will use these two domains in the next articles to explain the strategies.
What are the types of cache?
There are three types of cache we use, depending on where the cache is located. The following diagram shows the layers between the client and the database,

- Local cache - It lives in the memory of the application instance. It is the fastest one and no network call is needed, but each instance has its own copy of the data.
- Distributed cache - It is shared by all the instances of the application (for example, Redis). It is still a network call, but all the instances are reading the same data.
- Hybrid cache - Each application instance keeps a local copy of the distributed cache in its memory. It reduces the latency and increases the performance of each call, and the distributed cache is the source of truth between the instances.
We will see how to keep the local and distributed caches in sync in Part 4.
Wrap up
In this article, we have looked at the common challenges which need caching, what is a cache, why caching is important and the three types of cache. I really appreciate that you took the time to read this article. In the next article, I will walk you through the cache-aside pattern, and the lazy loading and read-through strategies.
Related Posts
How to cache the data based on the demand? Cache-aside, lazy loading and read-through - Part 2
The cache-aside pattern with lazy loading and read-through, their pros and cons, and use cases from e-commerce and the mutual fund world.
How to choose the right server-side caching strategy? Write-through, write-behind and refresh-ahead - Part 3
How write-through, write-behind and refresh-ahead caching work, their trade-offs, and how to choose a strategy for e-commerce and mutual fund use cases.
How to implement hybrid caching with EasyCaching? - Part 5
How to configure EasyCaching’s in-memory, Redis and hybrid providers and implement cache-aside extension methods.