Using OpenResty for a High‑Performance Logistics Gateway
This article explains how OpenResty, built on Nginx and Lua, powers JD Logistics' high‑throughput, extensible gateway by leveraging non‑blocking I/O, plugin architecture, multi‑level caching, dynamic load balancing, and custom JSON handling to achieve stable, scalable performance.
OpenResty® is a high‑performance web platform based on Nginx and Lua that bundles many Lua libraries, third‑party modules, and dependencies, making it easy to build dynamic web applications, services, and gateways capable of handling ultra‑high concurrency and extreme scalability.
Choosing OpenResty for the logistics gateway was driven by three key requirements: a non‑blocking I/O model provided by Lua coroutines, mature HTTP/HTTPS support from Nginx, and an ecosystem that enables continuous evolution through plugins such as Kong.
The gateway’s core functions—security, authentication, rate limiting, protocol conversion, and logging—are implemented as plugins that are dynamically loaded at specific Nginx request‑processing phases (Rewrite/Access, Content, and Log), allowing per‑merchant customization.
To avoid MySQL becoming a bottleneck under high concurrency, a multi‑level cache is employed: a local cache using ngx.shared.DICT, a centralized Redis cache, and an RPC layer that accesses the database. Cache consistency is maintained via a Redis‑based message queue that clears related local entries on metadata changes, with TTLs providing fault tolerance.
The load‑balancing component includes a custom Lua RPC client and adopts a dynamic, load‑aware strategy rather than static round‑robin or weight‑based algorithms. The scheduler uses a minimum‑connection algorithm, incrementing the count on request dispatch and decrementing on response or error.
Cross‑language JSON serialization posed challenges because cjson escapes the forward slash ("/" → "\/"), leading to mismatched inputs/outputs. To solve this, the gateway implements a lossless JSON serializer that operates directly on strings, eliminating conversion errors.
Performance is further optimized with OpenResty’s profiling tools that generate flame graphs for pinpointing bottlenecks. In full‑stack load tests, the gateway achieved up to 100,000 TPS, fully utilizing hardware capabilities.
In summary, the logistics gateway serves as the core service of JD Logistics' open technology platform, reliably handling all HTTPS traffic during major shopping events and demonstrating rapid feature evolution through Lua’s expressive power and a robust plugin system.
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.
JD Tech
Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.
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.
