Giter Club home page Giter Club logo

redis-rate-limiter's Introduction

A Lua script extension for Redis that provides smooth, configurable, space-efficient & blazing fast rate limiting.

Configuration

Example
key = "api-access"                                                                                 
value = {"1000", "3600", "300"}                                                                    

Each actor will be rate limited to 1000 api-access events per 3600 seconds. Once the limit
is reached, the actor will be locked out for 300 seconds. Note that the rate limit applies over
a rolling window.

Features

This rate-limiting solution -

  1. Does not use buckets & does not enumerate events. So it is space-efficient. It stores everything
    in a three-tuple for each actor,event pair.
  2. Uses a simple linear decay function to compute available quota. So it is blazing fast.
  3. Can be easily configured to rate limit over a few seconds or a few hours or a few days.

Usage

1. To check available quota without registering an event.
evalsha <SHA> 2 api-access <actor-id> <current-timestamp-seconds-since-epoch>                      

2. To check available quota while registering an event.
evalsha <SHA> 2 api-access <actor-id> <current-timestamp-seconds-since-epoch> 1                      

Included test.sh provides an example of how to configure an event type. It also shows the rate limiting in action.

The script returns available quota as a fraction. So when the rate limit is reached, it returns "0".

Performance

Processor: Intel® Core™ i7-8550U CPU @ 1.80GHz × 8
Using: benchmark.sh
Results: 142836.73 requests per second with 99% of them being served in under 0.6 ms
Note that client and server were running on the same machine. Even so, it proves that performance of this Lua script should not be an issue.

redis-rate-limiter's People

Contributors

bagashe avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.