Designing Multi‑Level Cache Architecture for Microservices
This article explains how to design an effective multi‑level caching system for microservice environments, covering client‑side static resource caching, application‑layer CDN caching, and service‑layer Redis caching, and provides practical Nginx configuration examples to implement these caches.
Multi‑Level Cache Design in Microservice Architecture
Cache is a fundamental technique for improving performance in modern software systems. In a typical e‑commerce scenario where reads dominate writes, data is stored persistently in MySQL while read‑heavy queries are served from an in‑memory NoSQL store such as Redis, which acts as a cache.
The cache hierarchy consists of four layers from top to bottom: client, application, service, and data layers.
Client‑Side Cache
Web browsers cache static assets (images, CSS, JS, fonts) using HTTP response headers such as Expires. For example, Baidu sets a far‑future expiration date for its logo image, allowing browsers to serve the image from local disk cache without contacting the server, thereby reducing bandwidth usage.
Application‑Layer Cache
Beyond the browser, caching is configured in the CDN and Nginx layers. CDNs distribute static files to edge nodes close to users, reducing latency and bandwidth consumption. Intelligent DNS directs users to the nearest CDN node, which serves cached content if available or fetches it from the origin and stores it locally.
CDN (Content Delivery Network)
CDNs are essential for large‑scale internet applications, caching static resources such as images, videos, CSS, and JavaScript at geographically distributed points of presence. Providers like Alibaba Cloud or Tencent Cloud offer CDN services with additional features such as custom Cache‑Control headers.
Nginx Cache Management
Nginx can serve as a lightweight alternative to a CDN for smaller or internal applications. By enabling static resource caching and configuring appropriate proxy settings, Nginx stores frequently requested files locally and serves them directly, bypassing the upstream application servers.
# Set cache directory
# levels represent a two‑level directory structure for cached static resources (css, js)
# keys_zone defines the cache name and memory size (babytun‑cache, 100m shared memory)
# inactive=7d removes cache files not accessed for 7 days
# max_size=20g limits the cache directory size to 20 GB
proxy_cache_path d:/nginx-cache levels=1:2 keys_zone=babytun-cache:100m inactive=7d max_size=20g; # Define upstream server pool with weighted 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 block for handling static resources
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; # cache 200/302 responses for 1 day
proxy_cache_valid 301 5d; # cache 301 responses for 5 days
proxy_cache_valid any 5m; # default cache time
expires 90d; # browser cache expiration
}
location / {
proxy_pass http://xmall;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}After adding the above configuration, each new static file requested through Nginx will be cached locally; subsequent requests within the cache validity period will be served directly from Nginx without reaching the backend.
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.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
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.
