Skip to content

Lesson 8 of 9 · 25m

Lua in Infrastructure: Redis, OpenResty, Neovim, and Friends

How Lua appears inside production infrastructure, what each host environment guarantees, and why the same language behaves differently inside Redis, Nginx, and an editor.

Lua shows up in infrastructure because hosts need programmable behavior without becoming programming platforms. Redis uses it for atomic server-side operations. OpenResty embeds it inside Nginx request processing. Neovim uses it as a first-class extension language. Same language name, radically different constraints.

Redis Lua: atomic, but not free

Redis executes scripts atomically relative to other commands:

-- rate limiter shape: KEYS[1] bucket, ARGV[1] limit, ARGV[2] ttl 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 1

Use EVALSHA after SCRIPT LOAD; sending the full source on every request wastes bandwidth and CPU. The operational trap is atomicity: while the script runs, Redis is not processing other commands. A loop over a large keyspace becomes a database stall, not a slow query. Keep scripts short, deterministic, and bounded; do not use them as stored procedures for application workflows.

OpenResty: Lua inside the request lifecycle

OpenResty runs Lua at Nginx phases:

location /internal/audit {
  access_by_lua_block {
    local token = ngx.var.http_x_audit_token
    if token ~= ngx.shared.audit_tokens:get("current") then
      ngx.exit(ngx.HTTP_FORBIDDEN)
    end
  }
}

The important model is asynchronous cosockets plus request phases. ngx.sleep, ngx.location.capture, and upstream sockets can yield without blocking the worker because OpenResty integrates coroutines deeply. That does not mean arbitrary Lua code is safe: CPU-bound work still blocks the worker, and blocking libraries still destroy concurrency.

Neovim: editor automation with a real API

vim.api.nvim_create_autocmd("BufWritePre", {
  pattern = "*.lua",
  callback = function()
    vim.lsp.buf.format({ async = false })
  end,
})

Neovim's Lua is not a sandbox for untrusted plugins. It is the extension API. Performance problems show up as input latency, so autocmd design and debouncing matter. The failure mode is different from Redis: not a stalled data plane, but an editor that feels broken.

Learn the host contract before the language trick

A clever metatable pattern can be fine in Neovim and dangerous in Redis. A blocking HTTP request can be tolerable in a command-line plugin and catastrophic inside Nginx. Lua is portable; operational semantics are not.

Field notes

  • In Redis, prefer EVALSHA plus fallback to EVAL on NOSCRIPT; script caches disappear after restart or flush.
  • Redis versions differ in replication semantics for effects; use explicit writes rather than relying on side effects you cannot explain.
  • In OpenResty, know which phase you are in. APIs available in content_by_lua_block may not exist in init_by_lua_block.
  • Treat Neovim plugins as trusted code. They can read files and execute shell commands because that is what editor extensions are expected to do.

Was this lesson helpful?

Sign in to rate