{"id":2230,"library":"pyrate-limiter","title":"PyrateLimiter","description":"PyrateLimiter is a Python library implementing the Leaky-Bucket Algorithm for rate limiting. It supports both synchronous and asynchronous workflows and offers various backends like in-memory, SQLite, Redis, and PostgreSQL for persistent limit tracking. Currently at version 4.1.0, it maintains an active development and release cadence, with several minor and patch releases in the last year.","status":"active","version":"4.1.0","language":"en","source_language":"en","source_url":"https://github.com/vutran1710/PyrateLimiter","tags":["rate-limiting","leaky-bucket","async","redis","postgresql","sqlite","concurrency"],"install":[{"cmd":"pip install pyrate-limiter","lang":"bash","label":"Core library"},{"cmd":"pip install pyrate-limiter[redis]","lang":"bash","label":"Redis backend"},{"cmd":"pip install pyrate-limiter[postgres]","lang":"bash","label":"PostgreSQL backend"},{"cmd":"pip install pyrate-limiter[all]","lang":"bash","label":"All optional backends"}],"dependencies":[{"reason":"Required for MultiProcessBucket with SQLite file locking.","package":"filelock","optional":true},{"reason":"Required for the Redis backend.","package":"redis","optional":true},{"reason":"Required for the PostgreSQL backend.","package":"psycopg","optional":true}],"imports":[{"note":"While functional, importing directly from the top-level package is the recommended and cleaner approach.","wrong":"from pyrate_limiter.limiter import Limiter","symbol":"Limiter","correct":"from pyrate_limiter import Limiter"},{"note":"`RequestRate` was renamed to `Rate` in v4.0.0.","wrong":"from pyrate_limiter import RequestRate","symbol":"Rate","correct":"from pyrate_limiter import Duration, Rate"}],"quickstart":{"code":"from pyrate_limiter import Duration, Rate, Limiter\n\n# Limit 5 requests within 2 seconds\nlimiter = Limiter(Rate(5, Duration.SECOND * 2))\n\n# Blocking mode (default) - waits until permit available\nfor i in range(6):\n    limiter.try_acquire(str(i))\n    print(f\"Acquired permit {i}\")\n\n# Non-blocking mode - returns False if bucket full\nfor i in range(6):\n    success = limiter.try_acquire(str(i), blocking=False)\n    if not success:\n        print(f\"Rate limited at {i}\")","lang":"python","description":"This quickstart demonstrates how to set up a basic in-memory rate limiter with a single rate. It shows both blocking and non-blocking acquisition attempts."},"warnings":[{"fix":"Update `Limiter` initialization: `limiter = Limiter(Rate(X, Duration.Y))` for single rates or use `limiter = Limiter(BucketFactory(...))` for complex scenarios. Replace `RequestRate` with `Rate`.","message":"The `Limiter` constructor API changed significantly in v4.0.0. It now expects a single `Bucket` instance or a `BucketFactory` instead of a list of `Rate` objects directly. The `RequestRate` class was also renamed to `Rate`.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Review the documentation for the specific bucket type or `Limiter` methods to configure delay behavior in v4.x, as `max_delay` is no longer a direct constructor argument.","message":"The `max_delay` argument for configuring rate limit delays in the `Limiter` constructor was removed in v4.0.0. Delay behavior is now integrated or configured differently at the bucket level.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Install with the appropriate extra: `pip install pyrate-limiter[redis]` for Redis, or `pip install pyrate-limiter[postgres]` for PostgreSQL.","message":"For Redis or PostgreSQL backends, you must install the optional dependencies (e.g., `pyrate-limiter[redis]` or `pyrate-limiter[postgres]`). Without them, these backends will not be available.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure `Rate` objects passed to a bucket or factory are sorted according to the specified rules.","message":"When defining multiple rates for a single limiter, they must be ordered correctly: by interval length (least to greatest) and by the ratio of limit/interval (greatest to least). Incorrect ordering can lead to unexpected rate limiting behavior.","severity":"gotcha","affected_versions":"All"},{"fix":"Switch to a persistent backend (`SQLiteBucket`, `RedisBucket`, `PostgresBucket`) and configure it appropriately for your distributed setup.","message":"The default `InMemoryBucket` does not share state across multiple processes or distributed environments. For multiprocessing or distributed applications, use persistent backends like `SQLiteBucket` (with `use_file_lock=True`) or `RedisBucket`/`PostgresBucket`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}