6.2 KiB
6.2 KiB
🔴 Redis
Overview
Redis is an in-memory key-value store with advanced data structures, used primarily as a cache, session store, message broker, and real-time database. Runs in RAM with optional disk persistence (RDB/AOF).
Data structures
| Structure | Description | Use case |
|---|---|---|
| String | Binary string (max 512 MB) | Cache values, session tokens, counters |
| Hash | Map field-value | User profile, cached object |
| List | Linked list (push/pop on both ends) | Queue (RPUSH/LPOP), log stream |
| Set | Unique values (unordered) | Tags, deduplication, memberships |
| Sorted Set | Unique + score (sorted) | Leaderboards, rate limiting, timeouts |
| Bitmap | Bit field | Feature flags, daily active users |
| HyperLogLog | Approximate cardinality (12 KB = 2^64) | Unique visitors (error < 1%) |
| Stream | Append-only log (Kafka-like) | Event store, messaging |
| Geospatial | Geo-indexing (GEOADD, GEOSEARCH) | Location queries, proximity search |
| JSON | JSON document (RedisJSON module) | Document structures |
Eviction policies
| Policy | Description | Use case |
|---|---|---|
| noeviction | Error on write when full | Transactional data, must not lose |
| allkeys-lru | LRU on all keys | General cache, standard |
| allkeys-lfu | LFU on all keys | Frequently accessed data |
| volatile-lru | LRU on keys with TTL | Cache with expiration |
| volatile-ttl | Closest to expiration | Short-lived data |
| allkeys-random | Random | Testing |
Redis Cluster vs Sentinel
| Feature | Redis Sentinel | Redis Cluster |
|---|---|---|
| Scaling | Read replicas (master + replica) | Data sharding (16384 hash slots) |
| Auto-failover | Yes (Sentinel) | Yes (gossip-based) |
| Multi-key ops | Yes (transactions on master) | Limited (same hash slot) |
| Client communication | Via Sentinel (deprecated) | Cluster nodes redirect (MOVED/ASK) |
| Minimum nodes | Master + Replica + 3 Sentinel | 3 masters (each with replica) |
| Use case | High availability, single shard | Multi-shard, horizontal scaling |
Persistence
| Method | Description | RTO | RPO | Use case |
|---|---|---|---|---|
| RDB (Redis Database) | Periodic snapshot to dump.rdb | Minutes | Last snapshot | Cache, loss tolerated |
| AOF (Append-Only File) | Append-only log of all write operations | Seconds | 1 s (fsync every sec) | Data must not be lost |
| RDB + AOF | Combination | Seconds | 1 s | Recommended for production |
Modules (RediSearch, RedisJSON, RedisGraph)
Redis is extensible via modules:
- RediSearch — full-text search, facets, prefix/suffix search
- RedisJSON — JSON path queries, document manipulation
- RedisGraph — graph DB (based on Cypher, deprecated since 2025)
- RedisTimeSeries — time-series with downsampling, retention policies
- RedisBloom — Bloom filters, Cuckoo filters, Top-K, Count-Min Sketch
Memcached vs Redis
| Feature | Redis | Memcached |
|---|---|---|
| Data structures | String, Hash, List, Set, Sorted Set, Stream, JSON | String only |
| Persistence | RDB + AOF | None (purely in-memory) |
| Replication | Master-replica, Cluster | None (multi-threaded) |
| Eviction | 6 policies | LRU only |
| Lua scripting | Yes (EVAL) | No |
| Transactions | Yes (MULTI/EXEC) | No |
| Pub/Sub | Yes | No |
| Streaming | Yes (Stream) | No |
Recommendations — where Redis is better
| Area | Redis | Competition | Why Redis |
|---|---|---|---|
| Cache (in-memory) | < 1 ms latency, 6 eviction policies | Memcached (LRU string only) | Richer data types, persistence, cluster |
| Session store | Hash + TTL, Cluster for HA | DynamoDB (higher latency) | Simpler, faster, native expiration |
| Rate limiting | Sorted Set (sliding window counter) | Application in DB (complex) | Atomic operations, built-in logic |
| Leaderboard / scoring | Sorted Set (ZADD, ZRANK, ZREVRANGE) | SQL (ORDER BY + COUNT = expensive) | O(1) rank, O(log N) insert |
| Message queue | List/Stream (RPUSH+BLPOP) | Kafka (heavy, JVM) | Lightweight, embedded, no broker |
| Real-time analytics | HyperLogLog + Bitmap + Stream | ClickHouse (heavy analytics) | Real-time aggregation, small RAM |
| Geolocation | GEOADD, GEOSEARCH, GEODIST | PostGIS (heavier, disk-based) | In-memory, ideal for real-time |
When to use Redis
- Cache for API — response cache, DB query cache, session cache
- Session management — distributed sessions across servers
- Rate limiting — API gateway, per-user/per-IP limits
- Leaderboards / rankings — real-time scoring
- Message broker — task queue (RQ, Celery with Redis), pub/sub notifications
- Real-time analytics — counting uniques, metrics, dashboards
- Geo-proximity — "find nearest branch" in < 1 ms
When to use something else
- Persistent data with SQL queries → PostgreSQL or MySQL
- Large volumes > RAM → Memcached (multi-threaded), Dragonfly (more RAM utilization)
- Long-term message queue → Kafka, RabbitMQ (disk-based persistence)
- Document DB → MongoDB (persistent, complex queries)
Redis licensing
Redis underwent a major license change in 2024:
| Period | License | Conditions |
|---|---|---|
| Until March 2024 | BSD 3-clause (open source) | Completely free use, including managed services |
| Since March 2024 | RSALv2 + SSPL (dual license) | SSPL: if you offer Redis as a managed service, you must release the entire stack as open source. RSALv2: restrictions on cloud operators |
| Valkey (fork, Linux Foundation) | BSD 3-clause | Fully open source fork of Redis 7.2, supported by Linux Foundation, AWS, Google, Oracle |
Impact: Managed Redis services (AWS ElastiCache, Google Memorystore, Azure Cache for Redis) cannot use Redis 7.4+ without a commercial license → they are migrating to Valkey. For self-hosted Redis, no change — RSALv2/SSPL does not restrict internal use.
Sources
References, books, and standards: sources/databases/sources.md
Last revision: 2026-06-03