a3kov

a3kov

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 target rate and burst size (for fixed cost requests)

  • handy timeouts for retries

  • support for token “refunds” and variable cost requests

  • multiple rate limit checks in 1 atomic operation

  • compile-time validation of arguments when possible

Links

Package
Docs
Source

Showing Posts 1 to 10

Asd

Asd

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!

PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

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?

a3kov

a3kov OP

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:

PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

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:

jsm

jsm

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).

jsm

jsm

This is really cool :slight_smile:

a3kov

a3kov OP

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

a3kov

a3kov OP

Version 0.2.0 comes with new advanced features via raw_request/5:

  • Support for variable request cost for cases where multiple operations have different cost, but share the same budget.
  • Support for negative cost (“token refunds”). Can be useful where an allowed operation failed and we don’t want it to “steal” the budget.

Unless some new ideas appear the library seems feature-complete at this point.

a3kov

a3kov OP

Version 0.3 is out with benchmarking improvements and perf. optimizations:

  • 20-30% faster for buckets of normal size on 64bit
  • extremely big buckets and 32bit architectures will take a hit

Overall I think it’s a good trade-off - for now I don’t want to maintain 2 code paths and runtime checks would defeat the purpose. 64 bit and buckets typical for rate limiters is what most people use. This was a good case for me to learn about big integers on BEAM :blush:

a3kov

a3kov OP

Version 0.4 is out with a new feature.

Requires Elixir >=1.17 and Erlang >=27 (I want to use new toys :wink:)

New feature: enforce multiple rate limits in a single atomic operation via multi_request/4.
It covers cases where higher short-term rates must be allowed without making the burst instant, while enforcing lower sustained long-term rates.

Lazy refills in raw_request/5 (I figured it’s harmless so it’s now a bit faster).

Bucket cleanup should now run faster and consume less memory.

Where Next? Top

Trending in Announcing Top

restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 12366 136
New
type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
kip
I’ll shortly be launching Text, a nascent text analysis library. Current functionality In this early version (not ready for prime time) ...
New
kip
Following on from my CLDR lbraries I started work on Unicode transforms. But like everything related to CLDR there is a lot of yak-shavin...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews