plug_rate_limit_redis

An Elixir plug that rate limiting with redis.

Overview

In Elixir, since ETS is provided by Erlang runtime, it can process rate limit count within memory, but in real world there is no guarantee that it will not restart every time it is deployed and sometimes you want to share values with multiple servers I guess.

So I decided to allow Rate Limit with Redis, even if I restarted the Erlang runtime every deployment, I also made it possible to handle from multiple servers.

Installation

  1. Add plug_rate_limit_redis to your list of dependencies in mix.exs:
  def deps do
    [{:plug_rate_limit_redis, "~> 0.1.0"}]
  end
  1. Ensure plug_rate_limit_redis is started before your application:
  def application do
    [applications: [:plug_rate_limit_redis]]
  end

Usage

config :plug_rate_limit_redis,
  host: "localhost", # Redis host
  port: 6379
plug RateLimit, interval_seconds: 60, max_requests: 30
plug RateLimit, [interval_seconds: 60, max_requests: 30] when action in [:index, :show:, :update]
pipeline :rate_limit do
  plug RateLimit, interval_seconds: 60, max_requests: 30
end

scope "/" do
  pipe_through :rate_limit
  get "/v1/foo/bar, FooController, :bar
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request