Redis/Valkey + Rate Limiting learnings

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

Redis is an in-memory key-value store that delivers sub-millisecond response times and handles up to 1 million queries per second. Originally a simple key-value store, Redis now supports sets, pub/sub messaging, and atomic transactions.
The single-threaded architecture contributes to Redis speed by avoiding context switching overhead. This design limits scaling for write-heavy workloads but ensures predictable performance.
Redis supports persistence through periodic snapshots of in-memory data to disk, providing durability when needed.
Ephemeral Redis implementation where data is lost if the server crashes. Fast and cost-effective for pure caching scenarios.
Durable Redis implementation that persists data and survives server crashes. Provides Redis performance with database-level durability.
AWS's fork of Redis offering improved performance, cost-effectiveness, and scalability. Maintains full Redis API compatibility while enhancing speed and operational efficiency. Available in serverless mode for automatic scaling.
Key differences:
Valkey: 20-30% better performance than Redis
Full API compatibility for drop-in replacement
Serverless scaling eliminates manual management
Divides data across multiple instances, enabling systems to handle larger datasets than single instances support. Queries route to appropriate shards based on key ranges.
Implements master-replica architecture where writes go to master and reads can use replicas. Supports both consistent and eventually consistent reads with minimal replication lag (microseconds).
Valkey Serverless combines both strategies, automatically scaling read replicas and shards as needed.
Rate limiting uses a sliding window algorithm with core logic:
Store timestamps in an array for each request
Count requests within defined time window
Automatically remove outdated timestamps
Keys auto-expire after the time window to prevent memory growth. Real-world usage shows Redis memory fluctuating around 1 GB with effective management.
Key: user_123_rate_limit
Value: [timestamp1, timestamp2, timestamp3...]
TTL: 3600 seconds (1 hour)
System checks request count within window, adds new timestamp if under limit, removes expired entries automatically.
Throughput: Up to 1 million QPS per instance
Latency: Sub-millisecond response times
Memory: Efficient in-memory storage with persistence options
Cost: 20-30% cheaper than Redis
Performance: Better throughput and lower latency
Scaling: Automatic serverless scaling
Compatibility: Drop-in Redis replacement
Memory Usage: ~1 GB for production workloads
Efficiency: Automatic cleanup prevents memory leaks
Scalability: Handles high-volume request patterns
TTL prevents memory leaks in serverless environments
Automatic cleanup maintains performance at scale
Monitor memory usage patterns for optimization
Track request patterns and rate limit hits
Monitor Redis/Valkey memory usage
Set up alerts for performance thresholds
Start with single instance for most use cases
Add sharding when dataset exceeds single instance
Use replication for read-heavy workloads
Consider Valkey serverless for variable traffic