Lua in Databases and Infrastructure
44.1 Redis Scripting
Redis runs scripts atomically with Lua 5.1 (plus cjson, cmsgpack, bit, struct). Redis 7 added Functions (FUNCTION LOAD, FCALL). Keep scripts short: they block the server.
-- rate_limit.lua: atomic sliding-window rate limiter
-- KEYS[1] = rate limit key, ARGV[1] = limit, ARGV[2] = window in seconds
local current = redis.call("INCR", KEYS[1])
if current == 1 then
redis.call("EXPIRE", KEYS[1], tonumber(ARGV[2]))
end
if current > tonumber(ARGV[1]) then
return 0
end
return 1redis-cli EVAL "$(cat rate_limit.lua)" 1 rate:user:42 10 60
redis-cli SCRIPT LOAD "$(cat rate_limit.lua)" # then EVALSHA <sha> ...44.2 Other Infrastructure Uses
Nginx / OpenResty / Kong / APISIX Request handling, auth, routing, plugins
lua-load actions, fetches, converters, services Lua HTTP filters Apache httpd Protocol dissectors and post-dissectors
In-memory database and app server with LuaJIT stored procedures
Aerospike, Couchbase (older versions) NSE scripts (Nmap Scripting Engine) Snort 3 / Suricata Lua filters for document conversion LuaTeX / LuaLaTeX Scripting inside TeX
mpv, VLC, Hammerspoon, AwesomeWM, WezTerm, conky Configuration and extensions
-- Wireshark dissector sketch
local proto = Proto("myproto", "My Protocol")
local f_type = ProtoField.uint8("myproto.type", "Type", base.DEC)
local f_len = ProtoField.uint16("myproto.len", "Length", base.DEC)
proto.fields = { f_type, f_len }
function proto.dissector(buffer, pinfo, tree)
pinfo.cols.protocol = "MYPROTO"
local subtree = tree:add(proto, buffer(), "My Protocol Data")
subtree:add(f_type, buffer(0, 1))
subtree:add(f_len, buffer(1, 2))
end
DissectorTable.get("udp.port"):add(9999, proto)