← Lessons

quiz vs the machine

Platinum1780

System Design

The Lua Script Atomicity

Bundle read, check, and write into one server side script so concurrent limiters cannot race.

5 min read · advanced · beat Platinum to climb

The race condition

A naive distributed limiter reads the counter, compares it to the limit, then writes the increment. Between the read and the write, another server can do the same. Both see a value under the limit and both allow, so the true count overshoots. This is a classic check then act race.

How Lua fixes it

Redis runs a Lua script as a single atomic step. No other command interleaves while the script executes. By placing the read, the limit check, and the increment inside one script, the entire decision becomes indivisible. Either the request is counted and allowed, or it is rejected, with no window for another caller to slip through.

Why not multiple round trips

  • Separate commands cross the network and interleave with other clients.
  • A transaction with watch can retry on conflict but adds complexity.
  • A single script is one round trip and one atomic decision, which is both correct and fast.

Key idea

A Lua script makes the read check and increment of a distributed limiter one atomic step, closing the check then act race.

Check yourself

Answer to earn rating on the learn ladder.

1. What race does a naive distributed limiter suffer?

2. How does a Lua script close the race?