Databases 32 min read

Redis Fundamentals: Pipelines, Pub/Sub, Expiration, Transactions, Persistence, Distributed Locks and Clustering

This article provides a comprehensive overview of Redis, covering basic concepts, pipeline optimization, publish/subscribe messaging, key expiration strategies, transaction behavior, persistence mechanisms (RDB, AOF, hybrid), distributed locking techniques, Redisson and Redlock algorithms, as well as high‑availability setups using replication, Sentinel and Cluster modes.

Architect
Architect
Architect
Redis Fundamentals: Pipelines, Pub/Sub, Expiration, Transactions, Persistence, Distributed Locks and Clustering

Redis is an in‑memory data store that offers high performance for caching and real‑time data processing. The article begins with a brief introduction to Redis basics and then explains how pipelines reduce round‑trip time by batching commands, improving throughput.

It describes the publish/subscribe model, where publishers send messages to channels and multiple subscribers receive them, illustrated with a multi‑client demo.

Key expiration is covered, including the EXPIRE command, the importance of setting TTLs to avoid memory bloat, and the active and passive expiration algorithms Redis uses to clean up expired keys.

Transaction semantics are explained: MULTI/EXEC/DISCARD/WATCH provide atomic execution, but Redis does not roll back on errors, which can lead to partial updates. Examples show error handling inside transactions.

Persistence mechanisms are detailed: RDB creates point‑in‑time snapshots via forked child processes, while AOF logs every write operation. The article compares their advantages and disadvantages, introduces hybrid persistence (AOF with RDB preamble), and shows configuration snippets such as appendonly yes and appendfsync everysec .

Distributed locking strategies are explored, starting with simple SETNX, then SETNX+EXPIRE, Lua scripts for atomic lock acquisition, and finally the robust Redisson implementation that auto‑extends lock TTLs. The Redlock algorithm for multi‑node clusters is outlined step‑by‑step.

High‑availability architectures are presented: master‑slave replication (read/write separation), Sentinel monitoring and automatic failover (including quorum voting and master election), and Redis Cluster with hash slots for horizontal scaling. Configuration examples for each mode are provided, along with commands to create a cluster ( redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 --cluster-replicas 1 ).

Finally, common cache pitfalls such as cache penetration, cache breakdown, cache avalanche, and cache pollution are defined and mitigated with techniques like empty‑value caching, Bloom filters, request throttling, distributed locks, staggered TTLs, and eviction policies.

ClusteringRediscachingPersistenceTransactionsPub/SubDistributed Locks
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.