Skip to main content

rate limiter for python with different algorithms

Project description

FastHelm

A lightweight token bucket rate limiter for FastAPI, with a Redis backend for distributed, multi-instance limiting.

Overview

Any public-facing API can be overwhelmed by too many requests in too little time, whether from a buggy client stuck in a retry loop, a scraper hammering an endpoint, an abusive actor, or simply more legitimate traffic than the backend can handle. Without a guardrail, a handful of callers can degrade the service for everyone, or run up costs that scale directly with request volume.

FastHelm sits in front of your FastAPI routes and answers one question per request: allow it, or reject it? It uses the token bucket algorithm, which enforces an average rate while still permitting short, controlled bursts. The natural shape of real API traffic.

The core logic is decoupled from the storage backend, so the same limiter runs on an in-memory backend during local development and on Redis in production without any change to the business logic or the HTTP layer. It starts simple and single process in memory. Scales up to a distributed setup where several API instances share one Redis and stay correct under concurrency through an atomic Lua script.

Simple request flow

Flow

Features

  • Token bucket with lazy refill — enforces an average rate while allowing controlled bursts; no background threads or timers, tokens are recomputed from elapsed time on each request.
  • Swappable storage backends — in-memory (lock-based, single instance) and Redis (distributed).
  • Atomic Redis limiting via a Lua script, so the read-decide-write cycle stays race-free across multiple instances.
  • FastAPI integration as middleware or a route dependency.
  • Standard HTTP semantics429 Too Many Requests with Retry-After and X-RateLimit-* headers.
  • Flexible keying — limit by client IP, API key, or a custom key function.

Tech stack

  • Python 3.14+
  • FastAPI
  • Redis
  • Poetry (dependency and environment management)
  • pytest

Project structure

The code is split into three independent layers — business logic, storage, and HTTP — each depending only on the interfaces of the layer below it.

fasthelm/                # the importable package
│
├── core/                # business logic — pure algorithm, no I/O
│   ├── limiter.py       # RateLimiter protocol + Decision result
│   └── token_bucket.py  # token bucket with lazy refill
│
├── storage/             # state backends behind a common protocol
│   ├── base.py          # Storage protocol
│   ├── memory.py        # in-process, lock-based (single instance)
│   └── redis.py         # distributed backend + atomic Lua script
│
└── http/                # FastAPI integration layer
    ├── middleware.py    # request keying + limit enforcement
    ├── dependencies.py  # per-route dependency variant
    └── responses.py     # 429 response + rate-limit headers

examples/
 └── app.py          # demo FastAPI app — wiring + a sample endpoint
   
tests/
pyproject.toml
poetry.lock
README.md

The dependency direction is one-way: http knows about core, core knows about storage only through its protocol, and core never imports anything from http. That boundary is what lets you swap the storage backend — memory for Redis — without touching the limiting logic or the HTTP layer.

Project details


Download files

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

Source Distribution

fast_helm-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

fast_helm-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file fast_helm-0.1.0.tar.gz.

File metadata

  • Download URL: fast_helm-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Linux/7.0.12-201.fc44.x86_64

File hashes

Hashes for fast_helm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 522b3e9f61bc77dc4e04e15d27272abb4a709ec81a2b34784ae2e07fea3fad22
MD5 6fccddd8ef28729a22ca23bf62a5e317
BLAKE2b-256 b5a8cc9b7a560d68b3dd904d1996ebd35700505133ed15538afc753a37e766dd

See more details on using hashes here.

File details

Details for the file fast_helm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fast_helm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Linux/7.0.12-201.fc44.x86_64

File hashes

Hashes for fast_helm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b67528cd7286d92c2a7e53e8f74787874bc8fa0f26c3ac5dba7bb31b0e4e6d14
MD5 07c933dc005a7f811db00342b7d16495
BLAKE2b-256 8c578acf6cda2942f0ffe6ea8dbdb7313438e2242da7c2e782a8efaa6c9165f1

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