Mastering OpenResty: Essential Lua Coding Standards for High‑Performance Backend Development
This guide introduces OpenResty—a high‑performance web platform built on Nginx and Lua—and provides detailed Lua coding conventions, including indentation, spacing, line length, variable naming, array handling, string concatenation, function design, module imports, and error handling to write clean, efficient backend code.
OpenResty
OpenResty® is a high‑performance web platform based on Nginx and Lua that bundles many well‑crafted Lua libraries, third‑party modules, and most dependencies, enabling developers to build dynamic web applications, services, and gateways capable of handling tens of thousands to millions of concurrent connections.
By aggregating carefully designed Nginx modules—mostly developed by the OpenResty team—Nginx becomes a powerful general‑purpose web application platform. Developers can use Lua scripts to invoke C and Lua modules supported by Nginx, rapidly constructing systems that scale to 10K or even 1,000K concurrent connections on a single machine.
Lua Overview
Lua is a lightweight, embeddable scripting language known for its simplicity, high performance, and extensibility. Designed to be efficient, flexible, and easy to extend, Lua’s name—meaning “moon” in Portuguese—reflects its small size yet powerful capabilities.
Its clear syntax and gentle learning curve make it ideal for rapid development and prototyping, especially in game development, network programming, and configuration parsing. Lua runs cross‑platform on Windows, Linux, macOS, and many other operating systems.
Lua Coding Standards
Indentation
Use four spaces for indentation in OpenResty code, even though Lua does not enforce this.
-- No
if a then
ngx.say("hello Tinywan")
end
-- Yes
if a then
ngx.say("hello Tinywan")
endSpaces
Place a single space on both sides of operators.
-- No
local i=1
local s = "Tinywan"
-- Yes
local i = 1
local s = "Tinywan"Blank Lines
Avoid adding semicolons at line ends and keep functions separated by two blank lines. Separate multiple if elseif branches with a blank line.
-- No
if a then
ngx.say("hello Tinywan");
end;
-- Yes
if a then
ngx.say("hello Tinywan")
end -- No
local function foo()
end
local function bar()
end
-- Yes
local function foo()
end
local function bar()
end -- No
if a == 1 then
foo()
elseif a== 2 then
bar()
elseif a == 3 then
run()
else
error()
end
-- Yes
if a == 1 then
foo()
elseif a== 2 then
bar()
elseif a == 3 then
run()
else
error()
endMaximum Line Length
Limit lines to 80 characters; break longer lines and align continuation lines with the opening parenthesis.
-- No
return limit_conn_new("plugin-limit-conn", conf.conn, conf.burst, conf.default_conn_delay)
-- Yes
return limit_conn_new("plugin-limit-conn", conf.conn, conf.burst,
conf.default_conn_delay)When aligning string concatenation, place the .. operator at the start of the continuation line.
-- No
return limit_conn_new("plugin-limit-conn" .. "plugin-limit-conn" ..
"plugin-limit-conn")
-- Yes
return limit_conn_new("plugin-limit-conn" .. "plugin-limit-conn"
.. "plugin-limit-conn")Variables
Always use local variables; avoid globals. Adopt snake_case for naming, and uppercase for constants.
-- No
i = 1
s = "Tinywan"
-- Yes
local i = 1
local s = "Tinywan" -- No
local IndexArr = 1
local str_Name = "Tinywan"
-- Yes
local index_arr = 1
local str_name = "Tinywan" -- No
local max_int = 65535
local server_name = "Tinywan"
-- Yes
local MAX_INT = 65535
local SERVER_NAME = "Tinywan"Arrays
Pre‑allocate arrays with table.new and avoid inserting nil. Use ngx.null when a placeholder is required.
-- No
local t = {}
for i = 1, 100 do
t[i] = i
end
-- Yes
local new_tab = require "table.new"
local t = new_tab(100, 0)
for i = 1, 100 do
t[i] = i
end -- No
local t = {1, 2, nil, 3}
-- Yes
local t = {1, 2, ngx.null, 3}Strings
Do not concatenate strings in hot code paths; build a table of fragments and use table.concat instead.
-- No
local s = ""
for i = 1, 100000 do
s = s .. "a"
end
-- Yes
local t = {}
for i = 1, 100000 do
t[i] = "a"
end
local s = table.concat(t, "")Functions
Function names should follow snake_case. Return early to reduce nesting.
-- No
local function testNginx()
end
-- Yes
local function test_nginx()
end -- No
local function check(age, name)
local ret = true
if age < 20 then
ret = false
end
if name == "a" then
ret = false
end
return ret
end
-- Yes
local function check(age, name)
if age < 20 then
return false
end
if name == "a" then
return false
end
return true
endModules
Localize all require results and frequently used globals such as ngx to improve readability and performance.
-- No
local function foo()
local ok, err = ngx.timer.at(delay, handler)
end
-- Yes
local timer_at = ngx.timer.at
local function foo()
local ok, err = timer_at(delay, handler)
end -- No
local core = require "apisix.core"
local timer_at = ngx.timer.at
local function foo()
local ok, err = timer_at(delay, handler)
end
-- Yes
local ngx = ngx
local require = require
local core = require "apisix.core"
local timer_at = ngx.timer.at
local function foo()
local ok, err = timer_at(delay, handler)
endError Handling
Always check the return value of functions that may produce errors and return error messages as a second string parameter.
-- No
local sock = ngx.socket.tcp()
local ok = sock:connect("www.google.com", 80)
ngx.say("successfully connected to google!")
-- Yes
local sock = ngx.socket.tcp()
local ok, err = sock:connect("www.google.com", 80)
if not ok then
ngx.say("failed to connect to google: ", err)
return
end
ngx.say("successfully connected to google!") -- No
local function foo()
local ok, err = func()
if not ok then
return false
end
return true
end
-- Yes
local function foo()
local ok, err = func()
if not ok then
return false, "failed to call func(): " .. err
end
return true
endSigned-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.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
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.
