Designing Multi-Level Cache Architecture for Microservices
This article explains how to design an effective multi-level caching system in a microservice environment, covering client‑side browser caching, application‑layer CDN and Nginx caching, and service‑layer in‑process and distributed caches, along with consistency strategies and practical deployment examples.
Multi-Level Cache Design in Microservice Architecture
The article introduces the concept of multi‑level caching in microservices, emphasizing its importance for high‑read, low‑write scenarios and describing how caching can improve performance by reducing database I/O and network latency.
Client Cache
Browser‑side caching of static resources such as images, CSS, JS, and fonts is explained, with an example of using the HTTP Expires header to control cache validity and reduce repeated requests.
Application Layer Cache
Two main techniques are covered: Content Delivery Network (CDN) and Nginx static‑resource caching.
CDN
CDN distributes static files to edge nodes close to users, using intelligent DNS to route requests, thereby lowering latency and bandwidth consumption for large‑scale web applications.
Nginx Cache Management
Nginx provides built‑in static‑resource caching and compression. A sample configuration is provided to enable proxy caching, set cache validity periods, and define expiration for browser caches.
# Set cache directory
# levels define two‑level directory structure for cached files (css, js)
# keys_zone defines cache name and memory usage (babytun-cache, 100m)
# inactive=7d removes files not accessed for 7 days
# max_size=20g limits total cache size
proxy_cache_path d:/nginx-cache levels=1:2 keys_zone=babytun-cache:100m inactive=7d max_size=20g;
# Backend server weight load‑balancing
upstream xmall {
server 192.168.31.181 weight=5 max_fails=1 fail_timeout=3s;
server 192.168.31.182 weight=2;
server 192.168.31.183 weight=1;
server 192.168.31.184 weight=2;
}
server {
listen 80;
location ~* \.(gif|jpg|css|png|js|woff|html)(.*){
proxy_pass http://xmall;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache xmall-cache;
proxy_cache_valid 200 302 24h;
proxy_cache_valid 301 5d;
proxy_cache_valid any 5m;
expires 90d;
}
location / {
proxy_pass http://xmall;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Service Layer Cache
Two categories are discussed: in‑process cache and distributed cache services.
In‑Process Cache
In‑process caching stores data in the application’s own memory, reducing latency; common implementations in Java include EhCache and Caffeine, and frameworks such as Hibernate, MyBatis, and Spring MVC make use of this technique.
Distributed Cache Service
Redis is presented as the typical distributed cache, centralizing cached data for multiple service instances. The article stresses the need for a hierarchical cache strategy (near‑to‑far, fast‑to‑slow) to avoid overloading Redis during traffic spikes.
To maintain cache consistency, the article suggests using a message queue (e.g., RocketMQ) to broadcast data‑change events, allowing other service instances to invalidate or update their caches promptly.
When to Use Multi‑Level Cache
Three scenarios are highlighted: stable data (e.g., postal codes), extremely high‑concurrency events (e.g., flash sales), and data that can tolerate eventual consistency (e.g., non‑critical profile information).
Conclusion
The piece summarizes the end‑to‑end cache design from client browsers through CDN/Nginx to service‑layer caches, providing practical guidance for building performant microservice architectures.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
