This document provides guidance for AI agents working with Redis, including key concepts, common patterns, and best practices.
Redis is an open-source, in-memory data structure store used as a database, cache, message broker, and streaming engine. It supports various data structures such as strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, geospatial indexes, and streams.
- Strings: Binary-safe strings up to 512 MB
- Lists: Collections of strings ordered by insertion order
- Sets: Unordered collections of unique strings
- Sorted Sets: Sets ordered by a score
- Hashes: Maps of field-value pairs
- Streams: Append-only log data structure
- Bitmaps: Bit-level operations on strings
- HyperLogLogs: Probabilistic data structure for cardinality estimation
- Geospatial: Latitude/longitude coordinate storage and queries
- JSON: Native JSON document storage (Redis Stack)
Redis offers multiple persistence options:
- RDB (Redis Database): Point-in-time snapshots
- AOF (Append Only File): Log of every write operation
- Hybrid: Combination of RDB and AOF
- Replication: Master-replica architecture for read scaling and data redundancy
- Sentinel: Automatic failover and monitoring
- Cluster: Horizontal scaling with automatic sharding
- Caching: Store frequently accessed data to reduce database load
- Session Management: Store user session data
- Real-time Analytics: Count, rank, and aggregate data in real-time
- Message Queues: Pub/Sub messaging and stream processing
- Leaderboards: Sorted sets for ranking systems
- Rate Limiting: Track and limit API requests
- Geospatial Applications: Location-based queries
- Vector Search: Similarity search with Redis Stack
SET key value [EX seconds] [NX|XX]
GET key
INCR key
DECR key
MGET key1 key2 ...
MSET key1 value1 key2 value2 ...
HSET key field value
HGET key field
HMGET key field1 field2 ...
HGETALL key
HINCRBY key field increment
LPUSH key value1 [value2 ...]
RPUSH key value1 [value2 ...]
LPOP key
RPOP key
LRANGE key start stop
SADD key member1 [member2 ...]
SMEMBERS key
SISMEMBER key member
SINTER key1 key2 ...
SUNION key1 key2 ...
ZADD key score1 member1 [score2 member2 ...]
ZRANGE key start stop [WITHSCORES]
ZRANK key member
ZINCRBY key increment member
DEL key1 [key2 ...]
EXISTS key1 [key2 ...]
EXPIRE key seconds
TTL key
KEYS pattern (use with caution in production)
SCAN cursor [MATCH pattern] [COUNT count]
- Use connection pooling for better performance
- Close connections properly when done
- Handle connection errors gracefully
- Use descriptive, hierarchical names:
user:1000:profile - Use colons (
:) as separators - Keep keys reasonably short but readable
- Include version numbers if schema changes:
user:v2:1000
- Use pipelining for multiple commands
- Prefer
SCANoverKEYSin production - Use appropriate data structures for your use case
- Set expiration times on temporary data
- Use
MGET/MSETfor batch operations
- Denormalize data for read performance
- Use hashes for objects with multiple fields
- Use sorted sets for rankings and time-series data
- Consider memory usage when choosing data structures
- Always check command return values
- Handle
nilresponses appropriately - Implement retry logic for transient failures
- Use transactions (MULTI/EXEC) for atomic operations
- Never expose Redis directly to the internet
- Use ACLs (Access Control Lists) for authentication
- Enable TLS for encrypted connections
- Rename or disable dangerous commands in production
GET cache_key
if nil:
data = fetch_from_database()
SET cache_key data EX 3600
return data
else:
return cached_data
key = "rate_limit:user:" + user_id + ":" + current_minute
count = INCR key
if count == 1:
EXPIRE key 60
if count > limit:
return "rate limit exceeded"
SET lock_key unique_value NX EX 10
if success:
# perform critical section
# release lock only if we own it
DEL lock_key (with Lua script to verify ownership)
/content/commands/: Complete command reference/content/develop/: Development guides and tutorials/content/operate/: Operational guides for deployment and management/content/integrate/: Integration guides for various languages and frameworks
Redis evolves with each version. Check the version-specific command references:
- Redis 8.6: Latest features including HOTKEYS command
- Redis 8.4: Previous stable release
- Redis 8.2, 8.0: Earlier 8.x releases
- Redis 7.4, 7.2: 7.x series
- Redis 6.2: Long-term support release
When working with Redis commands:
- Check the command documentation in
/content/commands/ - Look for code examples in
/content/develop/ - Review integration guides for your programming language
- Consider the Redis version you're targeting
- Redis is single-threaded for command execution (though I/O is multi-threaded in recent versions)
- All data is stored in memory (with optional persistence to disk)
- Commands are atomic
- Use Lua scripts for complex atomic operations
- Redis Cluster has some limitations (e.g., multi-key operations must be on the same hash slot)