Skip to main content

High-performance Python rate limiter for Django, FastAPI, and Flask using Redis and Lua

Project description

flux-limiter

Flux is a high-performance framework-agnostic rate limiter for Python that just works. It's built on a C++ core to respect your latency budget, and it plugs straight into Django, FastAPI, and Flask.

Whether you're protecting a public API or just trying to stop a single user from spamming your login form, Flux handles it without the boilerplate.

Installation

pip install flux-limiter

or

uv add flux-limiter

(You'll need a Redis server running, but you probably already have one of those.)

How to use it

The easiest way is the decorator. It figures out what framework you're using and sends the right 429 response automatically.

from flux import rate_limit

# Allow 100 requests every minute
@rate_limit(requests=100, period=60)
def my_api_endpoint(request):
    return {"data": "This is protected"}

That's it. No middleware to configure, no exception handlers to write.

Framework Examples

FastAPI

from fastapi import FastAPI, Request
from flux import rate_limit

app = FastAPI()

@app.get("/")
@rate_limit(requests=5, period=10)
async def root(request: Request):
    return {"message": "Hello World"}

Django

from django.http import JsonResponse
from flux import rate_limit

@rate_limit(requests=20, period=60)
def my_view(request):
    return JsonResponse({"ok": True})

Flask

from flask import Flask
from flux import rate_limit

app = Flask(__name__)

@app.route("/login")
@rate_limit(requests=5, period=300) # 5 tries every 5 minutes
def login():
    return "Login Page"

Configuration

You don't need a config file to get started, but when you're ready to tweak things, run:

python -m flux.cli init

This generates a flux.toml file where you can adjust everything:

flux.toml Reference

The flux.toml file is where you configure the behavior of the rate limiter.

Redis Settings [redis]

Option Default Description
host "127.0.0.1" Redis server hostname.
port 6379 Redis server port.
pool_size 5 Number of connections in the pool.
timeout_ms 200 Connection timeout in milliseconds.

Flux Core Settings [flux]

Option Default Description
key_prefix "flux:" Prefix for all keys stored in Redis.
fail_silently true If true, allows requests to proceed if Redis is down (Fail Open).
console_logging false Enable debug logging to stdout.
jitter_enabled true Adds random variance to Retry-After to prevent thundering herds.
jitter_max_ms 500 Max jitter in milliseconds (if enabled).

Analytics Settings [analytics]

Option Default Description
enabled false Enable the background analytics server.
port 4444 Port for the metrics server (/metrics).
stream "flux:events" Redis stream key for events.
retention 100000 Max stream length (events).
sample_rate 1.0 Sampling rate (0.0 to 1.0) for logging.

Rate Limit Defaults [rate_limit]

Option Default Description
policy "gcra" Algorithm: "gcra", "token_bucket", "leaky_bucket", "fixed_window".
requests 100 Number of requests allowed per period.
period 60 Time period in seconds.
burst requests Burst capacity (defaults to requests count).

Complete Configuration Example

Here is a complete flux.toml file with all options explained:

# =============================================================================
# Flux Configuration
# =============================================================================

# -----------------------------------------------------------------------------
# Redis Connection Settings
# -----------------------------------------------------------------------------
[redis]
host = "127.0.0.1"
port = 6379
pool_size = 5
timeout_ms = 200

# -----------------------------------------------------------------------------
# Flux Core Settings
# -----------------------------------------------------------------------------
[flux]
key_prefix = "flux:"
log_file = "flux_debug.log"
fail_silently = true        # Fail Open: Allow requests if Redis is down
console_logging = false     # Enable for dev debugging

# Jitter prevents Thundering Herd
jitter_enabled = true
jitter_max_ms = 500

# -----------------------------------------------------------------------------
# Analytics & Monitoring
# -----------------------------------------------------------------------------
[analytics]
enabled = true
port = 4444
stream = "flux:events"
retention = 100000
sample_rate = 1.0           # 1.0 = Log 100% of requests

# -----------------------------------------------------------------------------
# Default Rate Limiting Settings
# -----------------------------------------------------------------------------
[rate_limit]
policy = "gcra"             # Recommended: Smooths out traffic
requests = 100              # 100 requests...
period = 60                 # ...per 60 seconds

# -----------------------------------------------------------------------------
# Named Rate Limits
# -----------------------------------------------------------------------------
[rate_limits.api_tier_1]
requests = 1000
period = 3600
policy = "gcra"

[rate_limits.strict_login]
requests = 5
period = 300
policy = "token_bucket"

Named Limits

Instead of hardcoding numbers in your code, you can define them in flux.toml (as seen above) and use them by name:

@rate_limit(name="api_tier_1")
def expensive_call():
    pass

Analytics & Monitoring

Flux comes with a built-in lightweight analytics server that exposes metrics.

To enable it, add this to your flux.toml:

[analytics]
enabled = true
port = 4444          # Default port
sample_rate = 1.0    # Log 100% of requests

You can view the metrics by running this:

python -m flux monitor

You can also hit /metrics to get the follwing data:

  • Total requests
  • Rate limit hits (allowed)
  • Rate limit blocks (429s)

Why isn't this just Python?

Because speed matters. The core logic of Flux is written in C++ and communicates directly with Redis using optimized Lua scripts.

It supports multiple algorithms depending on your needs:

  • GCRA: The default. Smooth, leaky-bucket style limiting. Great for APIs.
  • Token Bucket: Allows for bursts (e.g., let users traverse a paginated list quickly) but enforces a long-term average.
  • Fixed Window: Simple counters. Good for "N actions per day".

Read More

👉 Check out blog for more details

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

flux_limiter-0.1.19-cp313-cp313-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.13Windows x86-64

flux_limiter-0.1.19-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (432.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.19-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl (465.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ i686

flux_limiter-0.1.19-cp313-cp313-macosx_11_0_arm64.whl (327.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

flux_limiter-0.1.19-cp312-cp312-win_amd64.whl (831.1 kB view details)

Uploaded CPython 3.12Windows x86-64

flux_limiter-0.1.19-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (432.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.19-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (465.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ i686

flux_limiter-0.1.19-cp312-cp312-macosx_11_0_arm64.whl (327.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

flux_limiter-0.1.19-cp311-cp311-win_amd64.whl (638.4 kB view details)

Uploaded CPython 3.11Windows x86-64

flux_limiter-0.1.19-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (433.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.19-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (465.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

flux_limiter-0.1.19-cp311-cp311-macosx_11_0_arm64.whl (327.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file flux_limiter-0.1.19-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 82d2fb1c4f859dbf13a3146582405ff4a002ab55b980bf92c61651449c6d0dd4
MD5 2eacdedd7165de5eafcf6858b7ea57ef
BLAKE2b-256 6080a61873dc23c19c762b0a97d3ca43155bc41803542461341abcde39d1d9b4

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5e1c40ff2b5759d4789d62e337fbb9fef2721201e2927ead6ada3a0e792e5d43
MD5 0a2add05698e1f776c54d6d62f9ffea0
BLAKE2b-256 e9f42dd8ddddb3ca0d547c239a5c518e0b3b1233e4f556c2785c29fc1cb93214

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 27c6a9f490e8a5621410a629bda2b7f36cb1e92a4c2d28239a05fb0097b338f5
MD5 3e3e4d56843e0f007d9516b51e5f00c9
BLAKE2b-256 e67c36aa7adb6d845c8e7a494567ad43249882a215906101520db50043d944ab

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f9543a3242a56b1e2b06e038aed6691890fa2e05a27bdd6c3720938a5a70251b
MD5 0009f9715013ef1b32a5b974a65144ab
BLAKE2b-256 f62955bad97aaf33296b2567cb72d235c63cf96ae4e545616b09d30078dfe309

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 da7476a29f6d24a5a9c36bb948c558118e234f45b58b4654c3b48a8242871748
MD5 1cc33c0f405963d41f20820e632291a4
BLAKE2b-256 b495f21f19d61d1589320f5ea549672f08f082713b3326feb0ad293a7a7bedb1

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c1d7db24b4fc1276051e4f275d847260842990f9bc64bb06c894e8a6d5767b1
MD5 aa39bec581b54b42d66d1760354b31b5
BLAKE2b-256 d087f55b540d89ad52bf5f15cf67dd0853bca9d6fb981a771d54ae2c8ccd23df

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 1afa78b55892813b01fba14de4cd7a4d6aeca7ca2f8881f964c7ab7430335251
MD5 65f652273e20dac9b0c6125966461bd9
BLAKE2b-256 858230e0f0715980810d0633e0bf43c1dc73d5fc1df2a4296e8762615f7b0ccb

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 66b5a12ce74694b4e4533f804d6d64e3fc8d1683eeaf537a38014d17979f3e08
MD5 d03901ca1e788fa951fe7621d4399860
BLAKE2b-256 3e89e3c9588db07b6eae7f740540e3cff544d72fa864eed75b736ef98a64ad1e

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 83d5582fa9e7b60ef465f64d6041c6df3b41833923de4d5f959455e2ef0f4526
MD5 127f730c91165df0e399112d99db95d6
BLAKE2b-256 60f328ef37e2121ca4f5c2def747be4bac5e34f1bb1c76aca2fa0ae6c7fa8dfe

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b8ac624248630ac325b3d9503364695e01378cb6e540598431a4499f8f4c7dd8
MD5 f8d01a2db5811b16383d412fadbe61d1
BLAKE2b-256 b79b929626d198709dac3afa01e2256f9b8707add7b1710bbfaabf10701bff3b

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 11d7af82c413a7a481f6efd44cb2f0c53a7dea6d034d07afb171b59b54dcd977
MD5 3f66a29280b4e9095322c38fb6bc9935
BLAKE2b-256 7d2fdd6664c947d374174738fafcb49018727f60f947cbfdba8bcfaff3405ded

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.19-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.19-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 21054cd54365823fddb9966fa3d7d3d657799ca548d7c04c332e5ff296ba74c5
MD5 3c4da87658fc005121125d4210385d7b
BLAKE2b-256 9467fbc283a72bda27886187e757085777e42e8496f737a7a0173ce8e1ec045f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page