Caching with Redis

What is Redis?

Redis (REmote DIctionary Server) is an in-memory data store, often used as a cache, message broker, or even a NoSQL database.

It’s:

  • Super fast (sub millisecond latency) (everything is in-memory)
  • Key-value based, like a giant hash table
  • Used in systems that need low-latency access to frequently used data

In this article, we explore caching with Redis from the ground up using redis-cli, and then dive into its advanced capabilities and architectural considerations.

Install Redis cli

Copy
sudo apt update
sudo apt install redis-server -y

Start Redis Server and check status

Copy
sudo service redis-server start
sudo service redis-server status

Open the Redis command line interface and test ping:

Copy
redis-cli

Stop Redis server when done

Copy
sudo service redis-server stop

Core Concepts of Redis Caching

Key-Value Storage

Redis operates as a high-speed key-value store. The value can be a simple string or a complex data structure like a hash or list. List of data types for Redis.

Copy
# Strings
SET name "Redis"
GET name
SET city "London"
INCR counter
DECR counter
APPEND city " Bridge"

# Lists
LPUSH tasks "learn redis"
LPUSH tasks "build app"
RPUSH tasks "deploy app"
LRANGE tasks 0 -1

# Hashes
HSET user:100 name "Alice" age "30"
HGET user:100 name
HGETALL user:100

# Sets (Unordered)
SADD tags "fast" "nosql" "cache"
SADD tags "fast"    # won’t duplicate
SMEMBERS tags
SISMEMBER tags "cache"

# Sorted sets
ZADD leaderboard 100 "Alice" 200 "Bob"
ZRANGE leaderboard 0 -1 WITHSCORES
ZREVRANGE leaderboard 0 0 WITHSCORES

Time-To-Live (TTL)

Redis supports automatic key expiration using TTL. This is essential for caching.

Copy
SET temp "this is temporary" EX 10
TTL temp

# You can also use
EXPIRE name 60
PERSIST name       # remove expiry

Atomic operations

All Redis commands are atomic — they finish completely or not at all. No race conditions — great for counters, locks, etc.

Copy
INCR score
DECR stock

Publish / Subscribe (Pub/Sub)

Used for messaging between parts of your system — chat apps, real-time updates, etc.

Copy
# Terminal 1:
SUBSCRIBE news

# Terminal 2:
PUBLISH news "Redis 7 released!"

Transactions (MULTI/EXEC)

Group commands together, all run as one atomic batch.

Copy
MULTI
SET balance 100
INCR balance
EXEC

Persistence Modes (RDB and AOF)

Although Redis is often used as a cache, it supports persistence. Redis can save data to disk:

  • RDB (Snapshotting): Saves the dataset at specified intervals. Good for backups.
  • AOF (Append-Only File): Logs every write operation. Safer but slower.
  • Hybrid (RDB + AOF): Combine both for balance between safety and performance.

You don’t do this via CLI often, but you can trigger a save manually:

Copy
SAVE
BGSAVE

Monitoring / Inspection

Copy
KEYS *             # List all keys (use with caution)
SCAN 0             # Safer alternative to KEYS for large DBs
TTL key            # Time to live
TYPE key           # Get the type of a key
INFO               # Redis server stats

Deleting Data

Copy
DEL name
FLUSHDB           # Deletes all keys in current DB
FLUSHALL          # Deletes all keys in all DBs

Eviction Policies

Redis can evict keys when memory is full, using policies such as:

  • volatile-lru: Least Recently Used for keys with TTL
  • allkeys-lru: LRU across all keys
  • volatile-ttl: Evict keys with nearest expiry
  • noeviction: Return errors when memory is full
  • allkeys-random: Evict random keys

Configured via maxmemory-policy.

Copy
CONFIG SET maxmemory 1mb                 # Set a low memory limit
CONFIG SET maxmemory-policy allkeys-lru

CONFIG GET maxmemory
CONFIG GET maxmemory-policy

# Reset to default
CONFIG SET maxmemory 0
CONFIG SET maxmemory-policy noeviction

Advanced Concepts

Redis Cluster

Redis Cluster allows you to shard (partition) your data across multiple Redis nodes, enabling horizontal scaling and automatic failover.

  • Data is automatically distributed across nodes (no manual sharding)
  • The Redis keyspace is split into 16,384 hash slots (0–16383). Each key maps to a slot, and each slot is handled by a node
  • Each master node can have one or more replicas; if a master goes down, a replica can be promoted automatically
  • Can survive failures of some nodes, as long as a majority of masters are reachable
  • If a key is on a different node, Redis tells the client to retry on the correct one (MOVED response)

Redis Sentinel

Redis Sentinel is a system to monitor Redis master-replica setups and provide automatic failover if the master fails.

  • Monitors master-slave setups, watches Redis masters and replicas for failures
  • Alerts admins (or systems) if something goes wrong
  • Provides automatic failover - promotes a replica to master if the master goes down
  • Clients can ask Sentinel which node is the current master
  • Good for high availability when clustering is not needed

Advanced Caching Patterns

Read-Through Caching

The application checks Redis before hitting the database. On cache miss, it loads from the DB and updates Redis.

Copy
def get_user(id):
    user = redis.get(f"user:{id}")
    if user is None:
        user = db.get_user(id)
        redis.setex(f"user:{id}", 3600, user)
    return user

Write-Through Caching

Writes go to both Redis and the database simultaneously. Ensures consistency but increases write latency.

Cache Aside / Lazy Caching

The app fetches from Redis, and on miss, loads from the DB and stores the result. Most common and flexible.

Negative Caching

Cache null or "not found" responses to avoid repeated DB hits.

Best Practices

  • Use pipelines for batching commands
  • Leverage sharding via Redis Cluster for horizontal scaling
  • Tune maxmemory, maxclients, and eviction policy for optimal memory use
  • Monitor using tools like redis-cli, MONITOR, or RedisInsight
  • Use requirepass to set authentication
  • Bind only to internal interfaces (bind 127.0.0.1)
  • Enable tls for secure in-transit data
  • Use ACLs (Redis 6+) to control access

Use Cases Beyond Caching

Apart from caching, Redis supports:

  • Pub/Sub messaging for real-time communication
  • Streams for data pipelines and log ingestion
  • Geospatial indexing for location-based queries
  • HyperLogLog, bitmaps for analytics and counting