Atomic Bucket - fast single node rate limiter implementing Token Bucket algorithm

Atomic Bucket

Fast single node rate limiter implementing Token Bucket algorithm. The goal is to provide dependable solution that JustWorks™ with a focus on performance, correctness and ease of use. Bucket data is stored using :atomics module. Bucket references are stored in ETS and optionally cached as persistent terms.

Features

  • lock-free and race-free with compare-and-swap operations

  • BlazingFast™ performance, see benchmarks section in the readme. Req/s go brrrrrr

  • monotonic timer for correct calculations

  • millisecond tick supporting wider range of parameters and preventing request starvation

  • automatic calculation of bucket parameters based on average rate and burst size

  • handy timeouts for retries

  • compile-time validation of arguments when possible

  • only fixed request cost is supported for now

Links

Package
Docs
Source

14 Likes

This is a great library. The implementation is much more efficient and does not contain concurrency bugs like Hammer’s leaky bucket implementation.

Definitely gonna use it in one of my projects. One request is to leave the atomics ref management to the user!

2 Likes

TIL that :atomics exists :smiley: great library and very interesting implementation! How did you learn about atomics and how did it lead to this library?

You have much more experience in Elixir than me so that is indeed strange.
In fact I’ve learned a lot from your blog posts in my Elixir journey.

It’s used in many libraries. Perhaps it’s a new trend to build stuff on top of it :rofl:

1 Like

Haha, thank you! I must now write a little blog post about atomics and will have to name you in it! That’s what you’re getting for sharing your good work online! Take that :winking_face_with_tongue:

2 Likes

If you’re interested, this uses atomics as well: Once - Ecto type for globally unique 64-bits IDs generated by multiple Elixir nodes (to be precise, the underlying NoNoncense lib does).

5 Likes

This is really cool :slight_smile:

1 Like

v0.1.3 fixes a stupid but nonetheless serious bug. Please update if you are using the library for anything important.