Skip to main content

Framework-agnostic, Redis-backed rate limiting engine for modern async Python systems.

Project description

Limify

Framework-agnostic, Redis-backed rate limiting engine for modern Python systems.

PyPI Python versions License Docs

Limify provides a clean-architecture rate limiting core with pluggable storage adapters, async and sync support, atomic Redis Lua execution, and framework adapters.

Limify is designed as infrastructure-level software for backend engineers, microservices, and API platforms.


Documentation

https://pedram-aliniaye-asli.github.io/limify


Features

  • Framework-agnostic core

  • Async and sync Redis support

  • Atomic Redis Lua token bucket implementation

  • FastAPI / Starlette middleware adapter

  • Rule-based rate limiting with wildcard matching

  • Plan-based rate limiting support

  • Multi-identity support:

    • user_id
    • org_id
    • api_key
    • client_ip
  • Clean architecture with dependency injection

  • Pluggable storage and algorithms

  • Fully testable design


Architecture

Limify uses layered clean architecture:

Application (FastAPI / Flask / etc.)
        ↓
Adapter (Middleware)
        ↓
Limiter (Orchestrator)
        ↓
Resolvers
  ├─ RuleResolver
  ├─ PlanResolver
  └─ KeyResolver
        ↓
Algorithm (Token Bucket)
        ↓
Storage Adapter (Redis)

Core is fully independent of frameworks.


Installation

Core only

pip install limifyrate

With FastAPI / Starlette adapter

pip install "limifyrate[fastapi]"

Development install

git clone https://github.com/pedram-aliniaye-asli/limify.git
cd limify
pip install -e ".[dev]"

Requirements

  • Python 3.10+
  • Redis

Quick Start (Async)

Example using async Redis and FastAPI.

import redis.asyncio as redis

from fastapi import FastAPI
from limify import Limify, LimifyConfig
from limify.adapters.fastapi.middleware import LimifyMiddleware
from limify.core.redis_adapter import RedisAsyncAdapter
from limify.core.algorithms.token_bucket import AsyncTokenBucketAlgorithm


redis_client = redis.from_url("redis://localhost:6379")

adapter = RedisAsyncAdapter(redis_client)

algorithm = AsyncTokenBucketAlgorithm(adapter)
await algorithm.initialize()

config = LimifyConfig(
    rules=[
        {
            "id": "default",
            "method": "*",
            "path": "/**", # Matches all endpoints (demo only) — not recommended.
            "rate": "5/minute",
            "priority": 0,
        },
        {
            "id": "items",
            "method": "*",
            "path": "/items",
            "rate": "10/minute",
            "priority": 10,
        },
    ]
)

limify = Limify(
    algorithm=algorithm,
    config=config,
)

app = FastAPI()

app.add_middleware(
    LimifyMiddleware,
    limiter=limify.limiter,
)


@app.get("/items")
async def items():
    return {"status": "ok"}

Sync Example

Example using synchronous Redis.

import redis

from limify import Limify, LimifyConfig
from limify.core.redis_adapter import RedisSyncAdapter
from limify.core.algorithms.token_bucket import TokenBucketAlgorithm


redis_client = redis.Redis(host="localhost", port=6379)

adapter = RedisSyncAdapter(redis_client)

algorithm = TokenBucketAlgorithm(adapter)

config = LimifyConfig(
    rules=[
        {
            "id": "default",
            "method": "*",
            "path": "/**",
            "rate": "5/minute",
        }
    ]
)

limify = Limify(
    algorithm=algorithm,
    config=config,
)

Redis Key Structure

Limify generates deterministic keys:

limify:{rule_id}:{plan_id}:{identity_type}:{identity_value}

Examples:

limify:items:default:user:42
limify:default:default:ip:127.0.0.1
limify:items:pro:apikey:abc123

Each identity and rule has an independent token bucket.


Rules

Rules define rate limits per endpoint.

Example:

{
    "id": "items",
    "method": "*",
    "path": "/items",
    "rate": "10/minute",
    "priority": 10,
}

Supported wildcards:

/items/*
/api/**

Higher priority rules override lower priority ones.


Identity Resolution Priority

Default resolution order:

  1. user_id
  2. org_id
  3. api_key
  4. client_ip
  5. anonymous

Plan Support

Limify supports plan-based rate limiting using PlanProvider.

This enables multi-tier SaaS limits.

Example plans:

free → 10/minute
pro → 100/minute
enterprise → 1000/minute

Algorithms

Currently implemented:

  • Token Bucket (async)
  • Token Bucket (sync)

Future:

  • Sliding Window
  • Fixed Window

Storage Adapters

Currently implemented:

  • RedisAsyncAdapter
  • RedisSyncAdapter

Future:

  • In-memory adapter
  • PostgreSQL adapter

FastAPI Adapter

Limify provides Starlette-compatible middleware.

limify.adapters.fastapi.middleware.LimifyMiddleware

Adds headers:

X-RateLimit-Limit
X-RateLimit-Remaining
Retry-After

Project Structure

limify/
  adapters/
    fastapi/
  core/
    algorithms/
    resolvers/
    redis_adapter.py
    limiter.py
    ...
  config.py
  defaults.py
  plan_provider.py
  ...

Status

Alpha

Core architecture is stable and production-grade, but API surface may evolve.


Roadmap

  • In-memory storage adapter
  • Flask adapter
  • Django adapter
  • Sliding window algorithm
  • Fixed window algorithm
  • Metrics support

License

MIT License


Author

Pedram Aliniaye Asli


Limify is designed as reusable infrastructure for modern backend systems.

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

limifyrate-0.1.5.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

limifyrate-0.1.5-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file limifyrate-0.1.5.tar.gz.

File metadata

  • Download URL: limifyrate-0.1.5.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for limifyrate-0.1.5.tar.gz
Algorithm Hash digest
SHA256 c8b02f443d6ad59c67a46e464ec37a12149974f50211432cdfa8669dfc47db36
MD5 448cd536210a4b5cd3b84007ad205b9d
BLAKE2b-256 438a4978796fa151db65282077bd2809f1d249a283fc59424ee14c774dec0ed8

See more details on using hashes here.

Provenance

The following attestation bundles were made for limifyrate-0.1.5.tar.gz:

Publisher: publish-pypi.yml on pedram-aliniaye-asli/limify

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file limifyrate-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: limifyrate-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for limifyrate-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d605dd42370cb7cf1e440a08035d9fbeeacf5b4ee92a317737de9c5e47cb6007
MD5 cee5df1ab3abe384f74e0e3a6ebcb9e2
BLAKE2b-256 1d8836689dadf85c27faee56af3ca68d6fde99662f184a5f85ee0616fcf0c64c

See more details on using hashes here.

Provenance

The following attestation bundles were made for limifyrate-0.1.5-py3-none-any.whl:

Publisher: publish-pypi.yml on pedram-aliniaye-asli/limify

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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