Exploring Caching Patterns for Microservices Architecture
Saeed Anabtawi
Founder @ interview.ps | Mentor @GazaSkyGeeks | Content Creator @ CodeWithSaeed
Introduction
"Treat caching primarily as a performance optimization. Cache in as few places as possible to make it easier to reason about the fresh‐ness of data" -Newman, S.
In micro-services architecture, caching can be an effective technique to improve the performance and scalability of services. Caching involves storing frequently accessed data in memory, so that it can be quickly retrieved and reused without the need to repeatedly query the underlying data source.Lets start by talking about what sorts of problems caches can help with.
Where to Cache
Microservices provide caching options, with different cache locations having trade-offs. The choice of cache location depends on the optimization goal.
Client-side
With client-side caching, the data is cached outside the scope of the origin.
Client-side caching can improve latency and robustness but has downsides such as limited invalidation options and potential inconsistency between clients.
Shared client-side caching can mitigate the inconsistency problem and be more efficient but requires a round trip to the cache.
Consider the owner and implementation of the shared cache as it can blur the line between client-side and server-side caching.
Server-side
The origin microservice manages the cache and can implement advanced cache invalidation mechanisms more easily due to typical cache implementation methods. This also avoids the problem of different consumers seeing different cached values that can occur with client-side caching.
Request cache
A cached response for the initial request is stored in a request cache. Subsequent requests result in the cached result being returned. No lookups in the sales database needed, no round trips to album service this is far and away the most effective cache in terms of optimizing for speed.
The benefits here are obvious. This is super efficient, for one thing. However, we need to recognize that this form of caching is highly specific. We’ve only cached the result of this specific request. This means that other operations won’t be hitting the cache and thus won’t benefit in any way from this form of optimization
Where is my cache ?
Embedded distributed and none-distributed cache
The simplest possible caching pattern is Embedded Cache. In the diagram above, the flow is as follows:
In the case of Spring, adding a caching layer requires nothing more than adding the @Cacheable annotation to the method.
@Servic
public class BookService {
@Cacheable("books")
public String getBookNameByIsbn(String isbn) {
return findBookInSlowSource(isbn);
}
}
Embedded Distributed Cache is still the same pattern as Embedded Cache; In an embedded distributed cache, the cache is distributed across multiple nodes or servers, allowing for improved fault tolerance and scalability.
Examples of embedded distributed cache systems include Hazelcast, Ehcache, and Apache Ignite
Pros
Cons
Client-Server/Cloud Cache
This time, the flow presented on the diagram is as follows:
This architecture looks similar to the classic database architecture. We have a central server (or more precisely a cluster of servers) and applications connect to that server. If we were to compare Client-Server pattern with Embedded Cache, there are two main differences:
In terms of the architecture, Cloud is like Client-Server, with the difference being that the server part is moved outside of your organization and is managed by your cloud provider, so you don’t have to worry about all of the organizational matters.
Pros
Cons
Side-car cache
The diagram above is Kubernetes-specific, because the Sidecar pattern is mostly seen in (but not limited to) Kubernetes environments. In Kubernetes, a deployment unit is called a POD. This POD contains one or more containers which are always deployed on the same physical machine. Usually, a POD contains only one container with the application itself. However, in some cases, you can include not only the application container but some additional containers which provide additional functionalities. These containers are called sidecar containers.
This time, the flow looks as follows:
This solution is a mixture of the Embedded and Client-Server patterns. It’s similar to Embedded Cache, because:
It’s also similar to the Client-Server pattern, because:
领英推荐
Pros
Cons
Reverse proxy cache
So far, in each scenario, the application was aware that it uses a cache. This time, however, we put the caching part in front of the application, so the flow looks as follows:
Such a caching solution is based on the protocol level, so in most cases, it’s based on HTTP, which has some good and bad implications:
NGINX provides a mature reverse proxy caching solution; however, data kept in its cache is not distributed, not highly available, and the data is stored on disk.
Pros
Cons
Invalidation
There are only two hard things in Computer Science: cache invalidation and naming things.- Phil Karlton
Invalidation refers to the process of removing data from the cache. Although the concept is simple,In a microservice architecture, there are several invalidation options to consider.
Time to live (TTL)
In this technique, each item in the cache is assigned a time to live value, which specifies how long the item can remain in the cache before it is considered stale and must be invalidated. When an item is requested from the cache, the cache checks its time to live value to determine if the item is still fresh or if it has expired. If the item has expired, the cache invalidates the item and fetches a fresh copy from the origin before returning the data to the client.
Pros
Cons
Conditional GETs
In this technique, the client sends a conditional GET request to the server, including an ETag or Last-Modified header in the request. The server then checks whether the requested resource has been modified since the client's last request by comparing the ETag or Last-Modified header with the current state of the resource. If the resource has not been modified, the server returns a 304 Not Modified response, indicating that the cached copy of the resource is still valid. If the resource has been modified, the server returns a 200 OK response along with the updated resource, which the client can then use to update its cache.
Pros
Cons
Notification-based
In this mechanism, the origin server sends notifications to cache subscribers when a change occurs that might impact their cached data. Cache subscribers receive these notifications and invalidate their local cache entries, forcing them to fetch the latest data from the origin server.
Pros
Cons
Write-through
When a client requests data from the cache, if the data is not present, the cache retrieves the data from the origin server and stores it in the cache before returning the data to the client.
If the data is already present in the cache and an update is made to the data on the origin server, the cache is updated synchronously before the updated data is returned to the client.
This ensures that the cache always has the latest version of the data and reduces the likelihood of stale data being served.
Pros
Cons
Write-behind
In this technique, when data is updated on the origin server, the cache is not updated immediately. Instead, the update is cached locally and written back to the origin server asynchronously at a later time, usually in batches. When a client requests data from the cache, if the data is not present, the cache retrieves the data from the origin server and stores it in the cache before returning the data to the client.
If the data is already present in the cache and an update is made to the data on the origin server, the cache is not updated synchronously. Instead, the cache updates its local copy of the data and schedules the update to be written back to the origin server at a later time. This delay in updating the origin server can introduce the possibility of stale data being served to clients until the update is eventually written back.
Pros
Cons
References:
Manager Experience Engineering @ Publicis Groupe | Gen AI enabled Web Development
1 年Very well documented ??
mid back-end developer
2 年????