Sliding window counters Redis rate limiting implementation for Golang

Golang Sliding Window Counters Rate Limiter

(I tried to come up with a nicer name...)

This is a simple rate limiter built based on this blog post from Figma's engineering team.

See the post for information about the requirements and design of the actual algorithm.

Usage

The rate limiter satisfies this interface:

type Limiter interface {
    Increment(context.Context, string, int) error
}

The implementation is backed by Redis and uses the go-redis library.

client := goredis.NewClient(&goredis.Options{
    Addr: "localhost:6379",
})

ratelimiter := redis.New(client, 10, time.Minute, time.Hour)

ratelimiter.Increment(ctx, "user_id", 1)

Middleware

There's a http middleware too, for convenience. Inspired by Seth Vargo's rate limit library:

ratelimiter := redis.New(client, 10, time.Minute, time.Hour)
mw := ratelimit.Middleware(ratelimiter, ratelimit.IPKeyFunc, 1)
// use mw in your favourite HTTP library

The Middleware function has a weight parameter, which allows you to give a higher increment weight to certain routes. So for example, your base rate limit can be 1000 requests per hour and each request has a weight of 1, but a particularly computationally intensive endpoint may want to have a weight of 10, so each request increments the internal counter by 10 instead of 1.

If you use multiple middleware instances, make sure you don't add one to the global mux/router otherwise your requests will be triggering two rate limit calculations (and thus, Redis network I/O operations). This could be solved in future by a centralised middleware controller that provides a tree of limiters with order of precedence rules etc.

Owner
Barnaby Keene
multidisciplinary creator. known as southclaws everywhere.
Barnaby Keene
Similar Resources

Implementation of do255e and do255s in Go

Go Implementation of do255e and do255s This is a plain Go implementation of do255e and do255s. It is considered secure; all relevant functions should

Aug 15, 2022

Go implementation of the Heaven's Gate technique

Go implementation of the Heaven's Gate technique

gopherheaven is a Go implementation of the classic Heaven's Gate technique originally published by roy g biv on VX Heaven in 2009. gopherheaven can be used as an evasion technique to directly call 64-bit code from a 32-bit process.

Dec 20, 2022

An idiomatic Go implementation of Leaky bucket.

lbucket lbucket is an idiomatic Go leaky bucket implementation. The library make use of plain old Go stdlib; in other words, there are no third-party

Apr 17, 2022

A faster RWLock primitive in Go, 2-3 times faster than RWMutex. A Go implementation of concurrency control algorithm in paper Left-Right - A Concurrency Control Technique with Wait-Free Population Oblivious Reads

Go Left Right Concurrency A Go implementation of the left-right concurrency control algorithm in paper Left-Right - A Concurrency Control Technique w

Jan 6, 2023

Go implementation of the geodesic routines from GeographicLib

Go implementation of the geodesic routines from GeographicLib

geodesic This package is a Go implementation of the geodesic routines from GeographicLib. Features Pure Go implementation Distance calculations with n

Dec 23, 2022

go-logr implementation with pterm

go-logr implementation with pterm

plogr go-logr implementation with pterm Usage See examples Add more colors and levels By default, only level 0 (info) and level 1 (debug) are supporte

Dec 22, 2021

Reference go implementation of globaldce protocol

globaldce-go This is the reference implementation of the command line interface of globaldce coded in the go programming language. This project is sti

Nov 8, 2021

Go implementation Welford’s method for one-pass variance computation

Welford - Online method of calculating variance and standard deviation Go implementation Welford’s method for one-pass variance computation with D. H.

Jan 6, 2023

Implementation for validating the NZ COVID Pass.

NZCP validator Validates NZCP passes according to https://nzcp.covid19.health.nz. Example See example_test.go and tests for more examples. func Exampl

Dec 20, 2021
Scalable golang ratelimiter using the sliding window algorithm. Currently supports only Redis.
Scalable golang ratelimiter using the sliding window algorithm. Currently supports only Redis.

go-ratelimiter Scalable golang ratelimiter using the sliding window algorithm. Currently supports only Redis. Example usage client := redis.NewClient

Oct 19, 2021
A simple business indicator tool that uses a sliding window to detect whether the indicator exceeds the threshold

melon A simple business indicator tool that uses a sliding window to detect whether the indicator exceeds the threshold Usage //create the metric //th

Jul 11, 2021
redis-util business-friendly encapsulation of redis operations, such as the common cache set get operation

redis-util 方便业务使用的redis操作封装,比如常见的缓存set get操作, 一行代码搞定,不像开源库需要写好多行 使用方法

Oct 22, 2021
solution lock for golang, locallock and remote lock base on redis.

solution lock for golang, locallock and remote lock base on redis.

Dec 19, 2022
Small utility to cleanup entries with unexpected/erroneous ttl in redis

Small utility to cleanup entries with unexpected/erroneous ttl in redis

Apr 28, 2022
Lightweight, Simple, Quick, Thread-Safe Golang Stack Implementation

stack Lightweight, Simple, Quick, Thread-Safe Golang Stack Implementation Purpose Provide a fast, thread safe, and generic Golang Stack API with minim

May 3, 2022
Optimal implementation of ordered maps for Golang - ie maps that remember the order in which keys were inserted.

Goland Ordered Maps Same as regular maps, but also remembers the order in which keys were inserted, akin to Python's collections.OrderedDicts. It offe

Jan 3, 2023
A pure Golang implementation of Rockchip rknand vendor storage interface.

go-rkvendorstorage A pure Golang implementation of Rockchip rknand vendor storage interface. Usage package main import ( "fmt" "github.com/jamesits

Nov 8, 2022
Golang 1.18+ Generics implementation of Set methods

Golang Generics: Set A golang 1.18+ implementation of Set using Go generics Installation $ go get -u github.com/chrispappas/golang-generics-set Quick

Oct 26, 2022
go implementation of timsort

timsort timsort is a Go implementation of Tim Peters's mergesort sorting algorithm. For many input types it is 2-3 times faster than Go's built-in sor

Nov 7, 2022