Important Limitations and Best Practices
masterTo avoid race conditions and errors in a high-concurrency Nginx environment, follow these rules:
- Do NOT store the
resty.redisobject in a Lua module-level variable. This will cause it to be shared across all concurrent requests in a worker process, leading to "socket busy" or "bad request" errors. Always use function local variables or thengx.ctxtable. - Context Restriction: This library cannot be used in Nginx phases where the cosocket API is unavailable, such as
init_by_lua*,set_by_lua*,log_by_lua*, orheader_filter_by_lua*. - Connection Pool Sizing: A good rule of thumb for
set_keepaliveis:pool_size = (Total Redis Max Connections) / (Nginx Workers * Nginx Instances). - Error Logging: If you implement custom error handling, disable Nginx's automatic socket error logging by setting
lua_socket_log_errors off;in your Nginx configuration to avoid duplicate logs.