Boost Web Performance with OpenResty: Nginx‑Lua Caching, Compression, and Auto‑Refresh
This guide explains how to use OpenResty’s Lua modules to let Nginx read and write Redis directly, compress large payloads, schedule periodic cache updates, and configure URL‑based caching, thereby reducing latency, saving bandwidth, and improving overall backend reliability.
1. OpenResty
OpenResty is a high‑performance web platform built on Nginx and Lua, bundling many Lua libraries, third‑party modules and dependencies. It is used to build dynamic web applications, services and gateways that can handle massive concurrency.
2. Nginx + Redis
Typical architecture: HTTP request → Nginx load balancer → Tomcat → Redis. The chain is serial; if Tomcat fails or its threads are exhausted, responses stall.
By using OpenResty’s lua-resty-redis module, Nginx can read/write Redis directly, bypassing Tomcat threads, so the service remains available even when Tomcat is down, reducing latency and increasing concurrency.
3. Compression to Reduce Bandwidth
For payloads larger than 1 KB, Nginx compresses the data before storing it in Redis, which speeds up Redis reads and saves bandwidth. Compression adds CPU overhead, so data smaller than 1 KB is left uncompressed for higher TPS.
Improves Redis read speed
Reduces bandwidth usage
OpenResty does not provide a built‑in Redis connection pool; a custom Lua implementation is required. Example reference:
http://wiki.jikexueyuan.com/project/openresty/redis/out_package.htmlRedis values are stored as JSON objects, e.g. {length:xxx,content:yyy}, where content is the compressed page and length records the original size to decide whether decompression is needed on read.
Compression is performed with the lua-zlib library.
4. Timed Updates
A Lua timer in Nginx periodically fetches the original Tomcat page, stores the HTML in Redis, and refreshes the cache. Cache TTL can be set long (e.g., 1 hour) to survive Tomcat outages, while the timer interval can be short (e.g., 1 minute) for rapid updates.
5. Request Forwarding
When a browser requests a page, Nginx first tries to read the HTML from Redis. If the key is missing, Nginx fetches the page from Tomcat, stores it in Redis, and returns the content to the client.
Nginx reads page HTML from Redis
If Redis miss, fetch from Tomcat and update Redis
Return HTML to browser
6. Single‑Process Timed Update
All Nginx workers can serve front‑end requests, but only worker 0 runs the periodic update. The worker ID is obtained with ngx.worker.id().
7. Configurability
Cache URLs, TTL, and update intervals can be configured via a management backend. An example request:
modify?url=index&&expire=3600000&&intervaltime=300000&&sign=xxxxThe sign is a signature generated from the same parameters using a secret key; Nginx validates the signature before applying the configuration.
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.
