What is Redis?
Redis (Remote Dictionary Server) is an in-memory data structure store. All data lives in RAM — making it orders of magnitude faster than disk-based databases. Key characteristics:
- Sub-millisecond latency — Reads and writes typically <1ms
- Rich data structures — Strings, lists, sets, sorted sets, hashes, streams
- Volatile data option — TTL (time-to-live) on keys — automatic expiry
- Atomic operations — Operations are atomic, avoiding race conditions
- Pub/Sub — Real-time messaging between applications
Azure Cache for Redis Tiers
| Tier | Cache Size | HA | Clustering | Best For |
|---|---|---|---|---|
| Basic | 250 MB – 53 GB | ❌ No | ❌ No | Dev/test only — no SLA |
| Standard | 250 MB – 53 GB | ✅ Primary + replica | ❌ No | Production caching |
| Premium | 6 GB – 1.2 TB | ✅ Yes | ✅ Up to 10 shards | Large caches, clustering, VNet, persistence |
| Enterprise | 12 GB – 2 TB | ✅ Yes | ✅ Active geo-replication | Enterprise, active-active geo, RediSearch, RedisBloom |
| Enterprise Flash | 300 GB – 13 TB | ✅ Yes | ✅ Yes | Very large caches using SSD extension |
Redis Data Structures
| Type | Use Case | Example |
|---|---|---|
| String | Simple key-value, counters, cached HTML | user:1234:name = "Priya" |
| Hash | Object properties (user profiles) | user:1234 → {name, email, plan} |
| List | Queues, timelines, recent activity | notifications:user1 → [msg3, msg2, msg1] |
| Set | Unique members (tags, friends) | post:123:tags → {azure, cloud, devops} |
| Sorted Set | Leaderboards, rankings (score-ordered) | leaderboard → {alice:950, bob:900, eve:880} |
| Stream | Event logs, activity streams | Ordered event journal with consumer groups |
Caching Patterns
Cache-Aside (Lazy Loading) — Most Common
- Application requests data
- Check Redis — if found (cache hit), return it
- If not found (cache miss), query the database
- Store result in Redis with TTL
- Return data to application
Write-Through
On every database write, also write to Redis. Keeps cache always up-to-date — no stale data — but higher write latency and may cache data that's rarely read.
Read-Through
Cache sits in front of the database. All reads go through cache — if miss, cache fetches from DB and populates itself. Application only talks to the cache.
Session State Management
Redis is the standard solution for distributed session state in web applications. Instead of storing sessions on individual web servers (which breaks when you scale to multiple servers), store all sessions in Redis:
- Any web server can serve any user — fully stateless web tier
- Sessions survive web server restarts
- Built-in TTL for automatic session expiry
- Sub-millisecond session reads on every request
Pub/Sub Messaging
Redis pub/sub allows applications to publish messages to channels and subscribe to channels for real-time notifications. Use cases: real-time chat, live notifications, cache invalidation signals, event broadcasting.
# Set with TTL (expire in 3600 seconds / 1 hour)
SET user:1234:profile "{name: 'Priya'}" EX 3600
# Get
GET user:1234:profile
# Increment counter atomically
INCR page:views:homepage
# Add to sorted set (leaderboard)
ZADD leaderboard 950 "alice"
ZADD leaderboard 900 "bob"
# Get top 3 (highest scores)
ZREVRANGE leaderboard 0 2 WITHSCORES
Persistence Options
By default, Redis is in-memory only — data is lost on restart. Premium and Enterprise tiers support persistence:
- RDB (Redis Database) — Point-in-time snapshots at configurable intervals
- AOF (Append Only File) — Log every write operation — can replay to reconstruct data
Geo-Replication
Premium tier supports passive geo-replication (one primary, one secondary). Enterprise tier supports active geo-replication — write to any region, all regions stay in sync. Use for global applications needing low-latency cache access from multiple regions.