talira

A rate-limiter for Elixir with pluggable storage backends

open sourceMITself-hostablefree

Hammer is a rate limiter for Elixir: you define a module with use Hammer, backend: ... and call hit(key, scale, limit), which allows or denies an action and says how long to wait, per user, API key or any key you compose. It ships ETS (the default) and Atomic backends that count in the node's memory, and the ExHammer project publishes a Redis backend (hammer_backend_redis) and a Mnesia backend (hammer_backend_mnesia, labelled beta) as separate packages, so several nodes can share one set of counters. Its docs also show a distributed, eventually consistent setup that keeps ETS counters on each node and broadcasts every hit over Phoenix.PubSub, the pattern hex.pm's own rate limiter uses. It supports fixed window, fixed window per key, leaky bucket, token bucket and (on ETS) sliding window algorithms, and works as a plug in Phoenix. Version 7 (February 2025) replaced the older Hammer.check_rate API.

Problems it solves

  • throttle each customer's API token separately in a Phoenix API
  • each BEAM node keeps its own request counts, so our effective limit grows with every node we add
  • share request counters across all the nodes of an Elixir cluster
  • limit how many files a user can upload per minute in an Elixir app
  • return a retry-after time when a client goes over its request limit in Elixir
  • keep rate limit counters in Redis so every BEAM node sees the same count
  • cluster-wide request throttling in Elixir without running Redis
  • allow short bursts but cap the average request rate with a token bucket in Elixir
  • enforce one SMS per minute and six per hour for the same user in one atomic check
  • throttle login attempts per IP address in a Phoenix endpoint
  • upgrade old Hammer.check_rate code to the current rate limiter API

Features

  • hit/3 returns {:allow, count} or {:deny, retry_after}
  • ETS backend (default) and Atomic backend built in
  • Redis backend (hammer_backend_redis, Hammer.Redis) for counters shared by every node
  • Mnesia backend (hammer_backend_mnesia, beta)
  • distributed ETS guide: local counters plus Phoenix.PubSub broadcasts of each hit
  • fixed window, fixed window per key, leaky bucket and token bucket algorithms; sliding window on ETS
  • custom increments per hit
  • Redis backend: hit_many/1 checks several limits in one atomic round trip
  • Redis backend: SSL connections and Redis Cluster hash tags
  • used as a function plug in Phoenix controllers or endpoints
View 1 more
  • Igniter installer (mix igniter.install hammer) that generates a rate limit module

Limitations

  • Doesn't share counts between nodes with its built-in backends alone: Atomic is single-node, and ETS needs PubSub broadcasts or the Redis or Mnesia backend12
  • Doesn't keep distributed ETS counters exactly in sync: the PubSub setup is eventually consistent and new nodes start with empty counters2
  • Doesn't have a stable Mnesia backend yet: hammer_backend_mnesia is labelled beta3
  • Doesn't support Redis versions before 7.0 in its Redis backend: on older servers counter keys never expire4

Capabilities

Takes pluginsplan not known