A high-performance rate limiter written in C++ that speaks Redis protocol. See our blog post to learn more.
- Ensure Bazel is installed
- Check out the smyte-db repo
- Ensure your submodules are up-to-date:
git submodule update
- Build the project:
bazel build -c opt ratelimit
Once compiled (as above), the binary lives at ./bazel-bin/ratelimit/ratelimit
.
It takes a few options:
--port
: the TCP port to listen on (default 9049)--rocksdb_db_path
: path where ratelimit should persist its state--rocksdb_create_if_missing
: pass this flag to create the database if it does not exist
Example: ./bazel-bin/ratelimit/ratelimit --rocksdb_db_path ratelimit-data --rocksdb_create_if_missing
You can also run it inside docker: docker run -p 9049:9049 smyte/ratelimit
RL.REDUCE key max refilltime [REFILL refillamount] [TAKE tokens] [AT timestamp]
: create a bucket identified bykey
if it does not exist that can hold up tomax
tokens and return the number of tokens remaining. Everyrefilltime
secondsrefillamount
tokens will be added to the bucket up tomax
. Ifrefillamount
is not provided it defaults tomax
. If there are at leasttokens
remaining in the bucket it will return true and reduce the amount bytokens
. Iftokens
is not provided it defaults to1
. If you want to provide your own current timestamp for refills rather than use the server's clock you can pass atimestamp
.RL.GET key max refilltime [REFILL refillamount] [AT timestamp]
: same asRL.REDUCE
, exceptRL.GET
does not reduce the number of tokens in the bucket.RL.PREDUCE
: same asRL.REDUCE
, but uses milliseconds instead of seconds.RL.PGET
: same asRL.GET
, but uses milliseconds instead of seconds.
Start the compiled server (see section on running it)
Send it some traffic:
$ redis-cli -p 9049 RL.REDUCE twoPerMin 2 60
(integer) 2
$ redis-cli -p 9049 RL.REDUCE twoPerMin 2 60
(integer) 1
$ redis-cli -p 9049 RL.REDUCE twoPerMin 2 60
(integer) 0