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.

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.


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": "/**", # This rule will include all requests with different methods on all endpoints and it's not suggested really! Just for demonstration purposes!
            "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.3.tar.gz (13.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.3-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: limifyrate-0.1.3.tar.gz
  • Upload date:
  • Size: 13.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.3.tar.gz
Algorithm Hash digest
SHA256 10f5aa90e9b9b1f3a71a44790e46e0cfa8b547c9b845a958f90c342d8df68a72
MD5 93ac2b4e5d3064073e8c42d2ead24357
BLAKE2b-256 11a939de939fef738d5b036b419940ade612ac5989065a266b1f6089a360f5ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for limifyrate-0.1.3.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.3-py3-none-any.whl.

File metadata

  • Download URL: limifyrate-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 16.0 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7b7ab82ede91926ed0a01f0571f3da67e8e249948ad2c0aebf1251ce9112ad51
MD5 4e6a275e61e1d88900345d57e8c13f3e
BLAKE2b-256 b565798f71f756d9c2aebcef11c31177761ab01086b69c27ea08379cae750b2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for limifyrate-0.1.3-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