Redis Server Requirements and Setup
Relevant source files
The following files were used as context for generating this wiki page:
Redis serves as the critical high-performance storage backend for FastLimit, enabling atomic rate-limiting operations across distributed Python applications. It acts as the "source of truth" for request counters and token buckets, ensuring zero race conditions through the execution of server-side Lua scripts.
The system is designed to work with Redis 7.0+ and leverages advanced features such as script caching (EVALSHA), epoch-aligned expiration (EXPIREAT), and server-side time synchronization to maintain accuracy across multiple application nodes.
Sources: README.md:9-13, fastlimit/backends/redis.py:27-35
Server Requirements
FastLimit requires a specific Redis environment to ensure performance and data integrity.
Versioning and Persistence
- Redis Version: 7.0 or higher is required for compatibility with modern command sets and performance optimizations.
- Persistence: For production environments,
appendonly yes(AOF) is recommended to prevent the loss of rate-limiting state during server restarts. - Memory Management: A suggested
maxmemoryof 256MB is typically sufficient for high-throughput environments, coupled with anallkeys-lrupolicy to automatically evict the least recently used rate-limit keys if memory limits are reached.
Sources: README.md:6, docker-compose.yml:10-15, CONTRIBUTING.md:465-470
Performance Metrics
FastLimit is optimized for high-throughput scenarios, with the following benchmarks achieved on a standard Redis setup:
| Metric | Performance Target |
|---|---|
| Latency (p50) | 0.8ms - 1.2ms |
| Latency (p99) | 2.0ms - 2.8ms |
| Throughput | 10,000 - 15,000+ req/s |
| Memory per Key | ~100 to 200 bytes |
Sources: README.md:195-205
Infrastructure Setup
The project provides multiple ways to deploy and configure the Redis backend depending on the environment.
Development Environment
Developers can use Docker to spin up a pre-configured Redis instance with debugging enabled.
services:
redis:
image: redis:7-alpine
container_name: fastlimit-redis-dev
ports:
- "6379:6379"
command: redis-server --appendonly yes --loglevel debug
volumes:
- ./data/redis:/data
Sources: docker-compose.dev.yml:8-16
Production-Ready Configuration
The production configuration includes health checks and network isolation within a bridge network.
The diagram above illustrates the relationship between the application, the Redis server, and the Docker health check mechanism.
Sources: docker-compose.yml:5-20
Connection Configuration
The RateLimiter and RedisBackend classes handle connection pooling and script registration automatically.
Connection Parameters
FastLimit uses the redis-py async client to manage connections. The following parameters are configurable through the RateLimitConfig model:
| Parameter | Default | Description |
|---|---|---|
redis_url | "redis://localhost:6379" | Connection string (supports passwords) |
max_connections | (Client Default) | Maximum size of the connection pool (defaulted to 50 in docs) |
socket_timeout | (Client Default) | Timeout for socket operations |
connection_timeout | (Client Default) | Timeout for establishing a connection |
Sources: fastlimit/backends/redis.py:107-115, README.md:209
Security and Redaction
To prevent accidental exposure of sensitive information, the backend includes a redaction utility for logs. Passwords in Redis URLs are automatically replaced with [REDACTED] before being logged during the connection phase.
Sources: fastlimit/backends/redis.py:126-127, fastlimit/backends/redis.py:441-470
Atomic Logic and Lua Integration
The core rate-limiting logic resides in Lua scripts that are registered on the Redis server during the application's connection phase.
Script Registration Flow
Clock Synchronization
To prevent issues caused by clock skew between multiple application servers, FastLimit retrieves the current time directly from the Redis server using the TIME command. This ensures that window boundaries for Fixed Window and Sliding Window algorithms remain consistent across the entire cluster.
Sources: fastlimit/backends/redis.py:399-415, fastlimit/limiter.py:157
Conclusion
Setting up the Redis backend for FastLimit involves ensuring a version-compatible server is available and configuring appropriate persistence and memory policies. By offloading atomic counters and time synchronization to Redis through Lua scripts, FastLimit achieves high-performance rate limiting while maintaining strict consistency across distributed nodes.
Sources: README.md:195-215, fastlimit/scripts/fixed_window.lua:1-10