Redis Lua scripts
Lua scripts run inside Redis and execute atomically — no other client can interleave commands between the script's lines. This is how you build compound operations that need to be race-free: "check the counter, if under limit increment and return new value, else return current and TTL" (rate limiting); "if the lock value matches mine, delete it" (safe distributed lock release); "decrement balance only if balance ≥ amount". You could do the same with WATCH and retry, but Lua is cleaner — one round-trip, guaranteed atomicity, the logic lives server-side. Two gotchas: scripts block the whole Redis server while running (keep them microsecond-short), and you can't call Lua from within Lua. Use EVAL to run inline, SCRIPT LOAD + EVALSHA to cache by hash (avoids resending the source). Modern Redis adds "functions" (similar idea, declared once, called by name).