Skip to content

πŸš€ An advanced exploration of high-performance rate limiting using Python's FastAPI and leveraging Redis for precision control and scalability.πŸ”₯

License

Notifications You must be signed in to change notification settings

gia-lexa/fast-api-rate-limiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

30 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Advanced Rate Limiting Strategies in FastAPI πŸš€

Overview

An advanced exploration of high-performance rate limiting, leveraging Redis for precision control and scalability.

System Design

The system is designed for high-throughput APIs handling millions of requests while preventing abuse. It efficiently tracks request counts using Redis with O(log n) time complexity for rate checks. Optimized expiry policies ensure minimal memory overhead.

Architecture Diagram

   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚     Client Requests API       β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
   β”‚ Rate Limiter (FastAPI + Redis) β”‚
   β”‚  β”œβ”€β”€ Fixed Window              β”‚
   β”‚  β”œβ”€β”€ Sliding Window            β”‚
   β”‚  β”œβ”€β”€ Token Bucket              β”‚
   β”‚  β”œβ”€β”€ Leaky Bucket              β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
   β”‚         API Service Layer      β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Algorithm Choices

Each algorithm is optimized for different use cases. The current main.py implements Leaky Bucket, but earlier commits of that file show the implementations of Fixed Window and Sliding Window.

  • Fixed Window – Simple but allows bursts at window edges
  • Sliding Window – More accurate, avoids bursts
  • Leaky Bucket – Ensures a smooth request flow
  • Token Bucket – Allows short bursts while enforcing long-term limits

Performance Benchmarks

Rate Limiter Performance (Benchmark @ 10,000 requests):
-------------------------------------------------------
βœ” Fixed Window    β†’  120ΞΌs avg latency, O(1) Redis ops
βœ” Sliding Window  β†’  150ΞΌs avg latency, O(log n) Redis ops
βœ” Leaky Bucket    β†’  130ΞΌs avg latency, O(1) Redis ops

πŸ“Œ Designed for low-latency enforcement and high scalability.

Trade-offs & Considerations

Distributed Systems Considerations:

  • Race conditions are handled via atomic Redis operations (INCR, ZADD).
  • Can be extended for multi-node rate limiting using Redis Cluster or consistent hashing.
  • Supports dynamic rate limits (e.g., per-user tiers, API keys).

Future Enhancements Currently In-Progress

βœ… JWT-Based API Rate Limits – Per-user quotas instead of IP-based limits

βœ… Rate Limit Adaptation – Machine learning to dynamically adjust limits

βœ… Prometheus + Grafana Monitoring – Real-time dashboards

Disclaimer

This project is an in-progress technical demonstration of advanced rate-limiting strategies. While provided under the MIT License, it is not optimized for production use, and no guarantees are made regarding security, reliability, or real-world performance. It serves as an exploration of best practices in scalable rate limiting.


πŸš€ This project showcases production-level rate limiting strategies, balancing speed, fairness, and resilience for high-scale APIs.

About

πŸš€ An advanced exploration of high-performance rate limiting using Python's FastAPI and leveraging Redis for precision control and scalability.πŸ”₯

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages