Boost Web Performance with OpenResty: Caching, Compression, and Dynamic Updates

This article explains how to use OpenResty with Lua to build a high‑performance caching layer that directly accesses Redis, compresses large responses, schedules periodic updates, forwards requests intelligently, and provides configurable URL caching, thereby improving concurrency, reducing latency, and optimizing bandwidth usage.

Open Source Linux
Open Source Linux
Open Source Linux
Boost Web Performance with OpenResty: Caching, Compression, and Dynamic Updates

1. OpenResty

OpenResty is a high‑performance web platform based on Nginx and Lua. It bundles many Lua libraries, third‑party modules and dependencies, making it easy to build dynamic web applications, services, and gateways that handle extremely high concurrency and are highly extensible.

The access‑layer cache is implemented by developing with OpenResty using Lua.

2. Nginx + Redis

The typical architecture sends HTTP requests to Nginx, which load‑balances to Tomcat; Tomcat then reads data from Redis, a serial chain that fails if Tomcat crashes or its threads are exhausted.

Using OpenResty’s lua‑resty‑redis module lets Nginx access Redis directly, avoiding Tomcat threads; even if Tomcat is down, requests are still served, reducing latency and increasing concurrency.

3. Compression to Reduce Bandwidth

For data larger than 1 KB, compress it with Nginx before storing in Redis:

Improves Redis read speed

Reduces bandwidth usage

Compression consumes CPU; data smaller than 1 KB is left uncompressed for higher TPS.

OpenResty does not provide a Redis connection‑pool implementation; you must implement one in Lua or use existing examples such as

http://wiki.jikexueyuan.com/project/openresty/redis/out_package.html

.

Store Redis values as JSON, e.g. {length:xxx,content:yyy}, where content is the compressed page and length is the original size, used to decide whether decompression is needed.

Compression is performed with the lua‑zlib library.

4. Timed Updates

Configure a timer (steps 1 and 2 in the diagram) so that an Nginx Lua timer periodically requests the Tomcat page URL and saves the returned HTML into Redis.

Cache TTL can be long (e.g., one hour) to keep service available if Tomcat fails, while the update interval can be short (e.g., one minute) to keep the cache fresh.

5. Request Forwarding

When a browser requests a page:

Nginx first tries to fetch the HTML from Redis.

If Redis has no data, Nginx retrieves the page from Tomcat and updates Redis.

The HTML is then returned to the browser.

6. Single‑Process Timed Update

All Nginx worker processes can forward requests to Redis, but only worker 0 runs the timed task that updates Redis. The Lua script obtains the worker ID with ngx.worker.id().

7. Configurability

Through a management backend you can configure cacheable URLs, cache TTL, and update intervals, e.g.

modify?url=index&&expire=3600000&&intervaltime=300000&&sign=xxxx

. The sign is generated by signing the request parameters with a secret key; Nginx validates the signature using the same secret key before applying the configuration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend-developmentrediscachingLuacompressionOpenResty
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.