Lua-nginx-module integrates Lua into NginX, and provides high-concurrent & non-blocking request processing ability without forcing developers to explicitly divide business logics into state machines. Developers can write programs in the plain-old sequential way, while lua-nginx-module will automatically interrupt the program logic at blockable I/O operations, saving the context and delegating those I/O operations to NginX’s event mechanism. When the I/O operations are done, lua-nginx-module will restore the context and resume the program logic. User program itself will feel everything as usual as never being interrupted.
Lua-nginx-module uses one-coroutine-per-request request handling model, i.e. for each incoming request, lua-nginx-module creates a coroutine to run user code to process the request, and the coroutine will be destroyed when the request handling process is done. Every coroutine has its own independent global environment, which inherits shared read-only common data. So any global values injected by the user code won’t affect other request’s processing, and will be freed when request handling is done. You can imagine that user code is running in a ‘sandbox’, which shares the same lifecycle with the request itself. By this way, lua-nginx-module can prevent memory-leak caused by abusing uses of global values in user code, hence improve robustness.
Benefit from Lua’s excellent coroutine support, lua-nginx-module can handle tens of thousands of concurrent requests with very low memory overhead. According to our tests, the memory overhead for each request in lua-nginx-module is only 2 KB or even half smaller if LuaJIT is used. So obviously lua-nginx-module is a good candidate for implementing extensive concurrent services.