ExLimit

Fast, Redis-backed token-bucket rate limiter for Elixir.
Works with Cowboy, Phoenix Channels, or any GenServer / Task.

1. One network round-trip — others need two or three

The Lua script (EVAL) is sent to Redis once.
A pipeline of MULTI / SET / INCR / TTL / EXEC still requires three round-trips.
That’s ~1 ms vs. ~3 ms on a remote Redis.

2. Faster at scale

Benchmarks at 60 k RPS:

just less network usage.

Installation

def deps do
  [{:ex_limit, "~> 0.1.0"}]
end

Start a Redix connection (once):

children = [
  {Redix, name: :redis, url: "redis://localhost:6379"}
]

Usage

# 50 requests / 30 s window
ExLimit.check_rate("user:42", :redis, 50, 30_000)   # => {:ok, %{remaining_hits, reset_time}} | {:rate_limit, %{remaining_hits, reset_time}}

# reset a key instantly
ExLimit.reset("user:42", :redis)

# inspect remaining tokens
{:ok, left} = ExLimit.inspect_tokens("user:42", :redis, 50)

License

MIT © 2024