Microcaching with Nginx. A 5 minute setup with code

Microcaching is a very useful startegy when we want to setup a simple cache without complicated expiration logic. With Nginx you can set it up in 5 minutes for any website

What is microcaching?

Microcaching is a caching strategy where the expire time is set to a very low value like 1-10 seconds. In some cases it can be extended to as far as 1 minute, but the prefix micro refers to a small amount of time. Where is is applicable you might ask? Since the caching purpose is to save time on repeated tasks that have te same output, microcaching cames best handy where we do not know how to invalidate the cache. For example, imagine that you have a website where the main page called index is hit very often by the users (like 100 times per second). The main page can aggregate things like weather, news list, main article, a bunch of photos. Each of those modules can have their own invalidation logic. Instead of caching each module and invalidating it separately we can cache the wole main page and invalidate it every 10 seconds. This way we are sure that if any module content changes it will be reflected in at most 10 seconds. On the other hand we save a great amount of time in engeneering a caching solution.

In the above example, the page is hit about 100 times per second. After applying microcaching technique we can lower it to as low as 0.1 per second (which is 1 per every ten second). To achieve this solution we will use Nginx. Look at the configuration below to see how it's done.

proxy_cache_path /tmp/cache keys_zone=cache:10m levels=1:2 inactive=600s max_size=100m;
server {
    proxy_cache cache;
    proxy_cache_lock on; # Allow only one request to populate the cache
    proxy_cache_valid 200 10s; # Invalidate the cache every 10 seconds
    proxy_cache_use_stale updating; # Command Nginx to use currently cached content while cached entry is updated

    listen external-ip:80;  # External IP address
    location / {
        proxy_http_version 1.1; # Always upgrade to HTTP/1.1 
        proxy_set_header Connection ""; # Enable keepalives
        proxy_pass http://website-upstream; # Setup a proxy server

upstream website-upstream {
    zone website 128k;
    keepalive 20; # Keepalive pool to upstream
    server localhost:80; # Bind to local address

Similar searches: nginx caching / nginx microcache / nginx microcaching / microcaching

These posts might be interesting for you:

  1. Setup Nginx with free SSL A+ Cert from Let's Encrypt on Ubuntu 18.04
  2. Can you proxy MongoDB connection with Nginx? Sure you can!
Author: Peter

I'm a backend programmer for over 10 years now, have hands on experience with Golang and Node.js as well as other technologies, DevOps and Architecture. I share my thoughts and knowledge on this blog.