Cache Types & Winning Strategies for Optimal Performance

Fatma Erkan
SDTR
Published in
5 min readJun 28, 2024

Cache Types

Caching is commonly referred to as the technique of storing frequently used data. The storing process can be performed in the application host server’s RAM (In-Memory Caching) or an external caching server (Distributed Caching).

In Memory Caching (Local Caching)

This kind of caching includes storing data in the RAM instead of in a database or a disk. It is useful when the application requires high-speed data access. This is because the caching reduces the disk reads and database queries to retrieve data. On the other hand, in-memory caching is volatile and data is lost when the system restarts or shuts down because the data is stored in RAM.

The size of the cache we can hold is directly proportional to the RAM size of the application server. If the application has only one instance, it works seamlessly. However, if the application has multiple instances and these instances are directed through a load balancer, the same user can be directed to different instances at different times. In this case, since users may access different caches at different times, they may see different data with each request. To resolve this inconsistency, the load balancer can be configured to route requests from the same user to the same instance using the sticky session feature (based on the sticky session cookie or IP address).

If an application is running on multiple servers, using a Distributed Cache rather than an In-Memory Cache is a more robust solution.

An example of this cache type is an e-commerce app that has a list of products and the data frequently needs to be retrieved from the database. In this scenario, the product list can be stored in the memory after the first retrieval, so other requests for this data can be served from memory directly instead of getting from the primary database each time.

Client-Side Caching

Client-side caching stores data on the client’s device as browsers, so this is useful for web applications that require access to static files such as images, and js files. With this, it improves the performance and reduces the network traffic. However, it is important to avoid stale data because the cached data may not always be up to date.

For example, a web application can store images that do not change often. So this data can be stored in client-side caching because the request to the same content can be served directly from the cache instead of the server again.

Distributed Caching (Global Caching)

This kind of caching stores data across multiple servers. Useful when the app requires availability and scalability. It is available to use when high volume and extremely low latency are expected. It allows multiple servers to share the workload of storing and retrieving data, this improves the performance of the app and reduces the risk of data loss. Multiple instances of cache are created in different clusters separated by geo-location or data centers. However, it is complex to manage a distributed cache system, and ensuring consistency across multiple nodes can be challenging.

If an application is large-scale as e-commerce and has clients all over the world, the app could use a distributed cache solution. It reduces latency and improves performance. Cache data is not stored in the RAM of the operating system where the application is deployed. In this method, cache data is stored in a separate cache service.

Pros & Cons of Distributed Caching

Cache Strategies

On-Demand Caching: Data is cached after receiving a request from the user.

Pre-populate Caching: This refers to caching data when the application starts up. This is commonly preferred for static data, such as lists of cities, districts, vehicle brands, and models, which are rarely modified and can be cached directly for use within the application.

Cache Aside

In this principle, the application or service is responsible for managing the cache. When the data is requested the service checks the cache first. If the requested data is not stored in the cache, it is retrieved from the database and stored in the cache for future requests. It is important to the management of the cache to ensure that it remains up to date. It is best for apps that read data that won’t update frequently.

Write-Through

In principle, data is written to both the cache and primary database at the same time. When data is updated, it is written to the cache and primary database simultaneously. This ensures that the cache always contains up-to-date information but it can slow down write operations.

Write-Back/Behind

In this strategy, data is written to the cache first, and then to the database later. This allows write operations to be faster but it can lead to data inconsistency if the cache is not properly managed. There is a possibility of data loss if the cache fails for any reason.

Read-Through

In this principle, the cache is used as the primary data source. When a client requests data, the cache is checked first. If data is not stored in the cache, it is retrieved from the primary database and stored in the cache for future use. This is useful when the primary database is slow or when the data is read but rarely updated.

Write Around

Data is written directly into the data store without writing to the cache. Reading from the database, the same will be placed into the cache. Suited for the app that won’t frequently re-read recently written data into the db.

Refresh-Ahead

The cached data was refreshed before it expired. This reduces latency since the data is updated before it is used. In later stages, the same is used during fetch.

--

--