This backend uses the Redix library to connect to Redis.
Redis version requirement
Redis 7.0 or later is required. The :fix_window algorithm relies on EXPIREAT ... NX and
the :sliding_window algorithm relies on EXPIRETIME, both introduced in Redis 7.0. On older
Redis versions these commands fail, so counter keys never expire and the keyspace grows until
Redis runs out of memory.
defmodule MyApp.RateLimit do
# the default prefix is "MyApp.RateLimit:"
# the default timeout is :infinity
use Hammer, backend: Hammer.Redis, prefix: "MyApp.RateLimit:", timeout: :infinity
end
MyApp.RateLimit.start_link(url: "redis://localhost:6379")
# increment and timeout arguments are optional
# by default increment is 1 and timeout is as defined in the module
{:allow, _count} = MyApp.RateLimit.hit(key, scale, limit)
{:allow, _count} = MyApp.RateLimit.hit(key, scale, limit, _increment = 1, _timeout = :infinity)The Redis backend supports the following algorithms:
:fix_window- Fixed window rate limiting (default) Simple counting within fixed time windows. See Hammer.Redis.FixWindow for more details.:sliding_window- Sliding window rate limiting Simple counting within sliding time windows. See Hammer.Redis.SlidingWindow for more details.:leaky_bucket- Leaky bucket rate limiting Smooth rate limiting with a fixed rate of tokens. See Hammer.Redis.LeakyBucket for more details.:token_bucket- Token bucket rate limiting Flexible rate limiting with bursting capability. See Hammer.Redis.TokenBucket for more details.
Summary
Types
@type redis_options() :: [redis_option()]