Implementing OpenResty‑Lua Caching with Nginx, Redis, Compression and Timed Updates
The article explains how a senior architect uses OpenResty and Lua to let Nginx directly access Redis for caching, compresses large responses, schedules periodic updates, and provides a configurable, signature‑verified caching layer to improve web performance and resilience.
Hi, I am a senior architect. This article shares a practical solution built on OpenResty, a high‑performance web platform based on Nginx and Lua.
1. OpenResty
OpenResty integrates many Lua libraries, third‑party modules and dependencies, enabling developers to build highly concurrent, extensible dynamic web applications, services, and gateways.
2. Nginx + Redis
Typical architecture routes HTTP requests through Nginx to Tomcat, which then reads data from Redis; this chain is serial and blocks when Tomcat fails. By using the lua‑resty‑redis module, Nginx can access Redis directly, avoiding Tomcat threads, keeping the service alive when Tomcat is down, and reducing response time.
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 under 1 KB is left uncompressed for higher TPS.
OpenResty does not provide a Redis connection‑pool implementation; you can use existing Lua examples such as http://wiki.jikexueyuan.com/project/openresty/redis/out_package.html . Store Redis values as JSON {length:xxx,content:yyy} , where content is the compressed page and length is the original size, allowing the consumer to decide whether decompression is needed.
Compression is performed with the lua‑zlib library.
4. Timed Updates
A Lua timer in Nginx periodically fetches the Tomcat page URL, saves the returned HTML into Redis, and can be configured with a long cache TTL (e.g., 1 hour) while updating the cache more frequently (e.g., every minute) to keep data fresh.
5. Request Forwarding
Nginx first tries to get the page HTML from Redis.
If the data is missing, it fetches the page from Tomcat, stores it in Redis, and returns it to the browser.
6. Single‑Process Timed Update
All Nginx worker processes handle normal requests, but only worker 0 runs the periodic update. The Lua script obtains the worker ID with ngx.worker.id() .
7. Configurable Caching
The backend management interface allows configuring cache URLs, TTL, and update intervals, e.g., modify?url=index&&expire=3600000&&intervaltime=300000&&sign=xxxx . The sign is a signature generated by the backend secret key over the query string; Nginx verifies the signature with the same secret key before applying the configuration.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.