Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

redis-throttle

> 编程语言
Open source

Rack middleware for rate-limiting incoming HTTP requests configured to be used with Redis.

273 stars0 likes0 views
WebsiteGitHub

About

Rack middleware for rate-limiting incoming HTTP requests configured to be used with Redis.

Redis Throttle Middleware

This is a fork of the Rack Throttle middleware that provides logic for rate-limiting incoming HTTP requests to Rack applications using Redis as storage system. You can use Rack::RedisThrottle with any Ruby web framework based on Rack, including Ruby on Rails 3.0 and Sinatra. This gem was designed to experiment rate limit with Rails 3.x and Doorkeeper.

Thanks to Open Source

Redis Throttle Middleware come to life thanks to the work I've made in Lelylan, an open source microservices architecture for the Internet of Things. If this project helped you in any way, think about giving us a star on Github.

Features

  • Works only with Redis.
  • Automatically deploy by setting ENV['REDIS_RATE_LIMIT_URL'].
  • When the Redis connection is not available redis throttle skips the rate limit check (it does not blow up).
  • Automatically adds X-RateLimit-Limit and X-RateLimit-Remaining headers.
  • Set MockRedis while running your tests

Requirements

Redis Throttle is tested against MRI 1.9.3, 2.0, and 2.1.x.

Installation

Update your gem file and run bundle

ruby
gem 'redis-throttle', git: 'git://github.com/lelylan/redis-throttle.git'

Rails Example

ruby
# At the top of config/application.rb
require 'rack/redis_throttle'

# Inside the class of config/application.rb
class Application  'text/plain'}, "Hello, world!\n"] }

Customizations

You can fully customize the implementation details of any of these strategies by simply subclassing one of the default implementations.

In our example we want to reach these goals:

  • We want to use Doorkeper as authorization system (OAuth2)
  • The number of daily requests are based on the user id and not the IP address (default in Rack::RedisThrottle)
  • The number of daily requests is dynamically set per user by the user#rate_limit field.

Now subclass Rack::RedisThrottle::Daily, create your own rules and use it in your Rails app

ruby
# /lib/middlewares/daily_rate_limit
require 'rack/redis_throttle'

class DailyRateLimit  'application/json' }.merge(headers), [body(request).to_json] ]
end

def body(request)
  {
    status: 403,
    method: request.env['REQUEST_METHOD'],
    request: "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['PATH_INFO']}",
    description: 'Rate limit exceeded',
    daily_rate_limit: max_per_window(request)
  }
end

Notes

Testing coverage

Only Rack::RedisThrottle::Daily has a test suite. We will cover all the gem whenever I'll find more time and I'll see it being used widely.

Contributing

Fork the repo on github and send a pull requests with topic branches. Do not forget to provide specs to your contribution.

Running specs

  • Fork and clone the repository.
  • Run gem install bundler to get the latest for the gemset.
  • Run bundle install for dependencies.
  • Run bundle exec guard and press enter to execute all specs.

Spec guidelines

Follow betterspecs.org guidelines.

Coding guidelines

Follow github guidelines.

Feedback

Use the issue tracker for bugs. Mail or Tweet us for any idea that can improve the project.

Links

  • GIT Repository
  • Initial inspiration from Martinciu's dev blog

Authors

Andrea Reginato Thanks to Lelylan for letting me share the code.

Contributors

Special thanks to the following people for submitting patches.

Changelog

See CHANGELOG

Copyright

Redis Throttle is free and unencumbered public domain software. See LICENCE

Issues· 4 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言