| name | redis-master |
| description | Leverage Redis for caching, sessions, pub/sub, queues, rate limiting, and real-time features. Design Redis data structures for optimal performance. |
| category | php-frameworks |
| tags | ["redis","cache","pub-sub","session","queue"] |
| complexity | advanced |
| risk | low |
| compatibility | ["claude-code","antigravity","gemini-cli","cursor","copilot","codex-cli","autohand","kiro"] |
| source | antigravity-official |
| version | 1.0.0 |
| date_added | 2026-07-10 |
| last_updated | 2026-07-10 |
Redis Master
Purpose
Use Redis effectively as a cache, message broker, session store, queue, and real-time data structure server.
Operating Mode
You are a Redis expert who selects appropriate data structures, implements patterns correctly, and avoids common pitfalls like memory bloat and cache stampedes.
The Process
1️⃣ Data Structure Selection
| Structure | Best For | Example Use Case |
|---|
| String | Simple values, counters | Cache, rate limiting |
| Hash | Objects, records | User sessions, config |
| List | Queues, activity feeds | Job queues, recent items |
| Set | Unique collections | Tags, permissions |
| Sorted Set | Ranked data | Leaderboards, rate windows |
| Stream | Event log | Activity stream, audit log |
| Bitmap | Boolean flags at scale | Feature flags, presence |
| HyperLogLog | Cardinality estimates | Unique visitor counts |
2️⃣ Caching Patterns
$user = Cache::remember("user:{$id}", 3600, function () use ($id) {
return User::find($id);
});
public function updateUser(User $user, array $data): User
{
$user->update($data);
Cache::put("user:{$user->id}", $user, 3600);
return $user;
}
$value = Cache::lock("lock:user:{$id}", 10)->block(5, function () use ($id) {
return Cache::remember("user:{$id}", 3600, fn() => User::find($id));
});
Cache::tags(['posts', 'user:123'])->put("post:{$postId}", $post, 3600);
Cache::tags(['user:123'])->flush();
3️⃣ Rate Limiting with Redis
function checkRateLimit(string $key, int $limit, int $windowSeconds): bool
{
$now = microtime(true);
$windowStart = $now - $windowSeconds;
$redis = Redis::connection();
$redis->zRemRangeByScore($key, '-inf', $windowStart);
$count = $redis->zCard($key);
if ($count >= $limit) {
return false;
}
$redis->zAdd($key, $now, $now);
$redis->expire($key, $windowSeconds);
return true;
}
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
4️⃣ Pub/Sub for Real-time
Redis::publish('notifications', json_encode([
'user_id' => $userId,
'type' => 'order_ready',
'message' => 'Your order #123 is ready!',
]));
Redis::subscribe(['notifications'], function ($message, $channel) {
$data = json_decode($message, true);
WebSocket::send($data['user_id'], $data);
});
5️⃣ Leaderboard with Sorted Sets
Redis::zAdd('leaderboard:weekly', $score, $userId);
$leaderboard = Redis::zRevRangeWithScores('leaderboard:weekly', 0, 9);
$rank = Redis::zRevRank('leaderboard:weekly', $userId);
$score = Redis::zScore('leaderboard:weekly', $userId);
6️⃣ Session Storage
SESSION_DRIVER=redis
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=null
7️⃣ Redis Best Practices
- Always set TTL on cached keys — never use
SET key value without expiry
- Use namespaced keys:
app:users:123 not just user
- Monitor memory:
redis-cli info memory
- Set
maxmemory-policy allkeys-lru for cache-only Redis instances
- Use pipelining for bulk operations
- Enable persistence (RDB + AOF) for non-cache data
- Use Redis Cluster or Sentinel for HA
Outputs
- Caching layer implementation
- Rate limiting configuration
- Session storage setup
- Pub/sub patterns
- Leaderboard/ranking implementation
- Redis configuration for production