Databases 15 min read

Why Redis Is More Than a Cache: From Basics to Clustering

Redis, an open‑source in‑memory data store, serves as a database, cache, and message broker, offering rich data types, persistence, replication, Sentinel, and clustering; this article walks through its evolution from simple caching to high‑availability, distributed architectures, and advanced client features.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Why Redis Is More Than a Cache: From Basics to Clustering
Redis is an open‑source BSD‑licensed project that stores structured data in memory and can be used as a database, cache, and message broker. It supports strings, lists, hashes, sets, sorted sets, bitmaps, hyperloglogs, and geospatial indexes, and includes replication, Lua scripting, LRU eviction, transactions, Sentinel for high availability, and Cluster for automatic sharding.

1. Starting from Zero

The initial requirement was a simple API that returned a hot‑news list, but each request took about two seconds. The first quick fix was to add an HTTP Cache‑Control: max‑age=600 header so clients could cache the response for ten minutes, improving perceived performance for cached requests.

However, this approach has two drawbacks: cached data may become stale within the ten‑minute window, and clients that ignore the cache still incur the two‑second latency.

2. In‑Memory Cache on the Host

To eliminate the two‑second delay, the SQL query result was cached directly in the API server’s memory for one minute. Subsequent requests within that minute read the cache, reducing response time to near‑instant. With a traffic rate of 100 requests per second, only the first two seconds of traffic experience the delay.

This solution quickly exhausted the API server’s memory, prompting a move to a dedicated caching layer.

3. Server‑Side Redis

When the API servers ran out of memory, the cache was moved to a separate Redis server with a larger memory allocation.

3.1 Persistence

Redis can persist in‑memory data to disk, allowing recovery after a server crash and mitigating cache‑snow‑avalanche effects.

3.2 Sentinel and Replication

Sentinel monitors multiple Redis instances, provides alerts, and performs automatic failover. Replication creates one‑or‑more replica servers that keep a copy of the primary’s data, enabling high availability.

3.3 Cluster

Redis Cluster distributes data across multiple nodes using hash slots (16,384 slots). Each node owns a subset of slots; when nodes are added or removed, slots are migrated, and the cluster can redirect client requests to the appropriate node.

4. Client‑Side Redis

4.1 Data Types

String : binary‑safe string up to 512 MB.

List : ordered collection of strings.

Set : unordered collection of unique strings.

Sorted Set : ordered set of strings.

Hash : map of field‑value pairs.

Bitmap : bit‑level operations.

HyperLogLog : probabilistic data structure for cardinality estimation.

Each type is designed for specific use‑cases and has its own time‑complexity characteristics.

4.2 Transactions

Redis supports atomic execution of multiple commands as a transaction, ensuring that either all commands succeed or none are applied.

4.3 Lua Scripts

Lua scripts run atomically on the server, allowing complex logic (e.g., extending a key’s TTL while fetching it) to be executed in a single step, similar to remote evaluation.

4.4 Pipelining

Pipelining batches multiple commands over a single TCP connection, reducing round‑trip overhead, though it does not guarantee atomicity.

4.5 Distributed Locks

Redis recommends the Redlock algorithm for distributed locking, using a string key with a random value and Lua script to release the lock safely.

SET resource_name my_random_value NX PX 30000
if redis.call("get",KEYS[1]) == ARGV[1] then
    return redis.call("del",KEYS[1])
else
    return 0
end

Conclusion

This article abstracts Redis’s features and their purposes, helping readers choose appropriate solutions for specific scenarios without getting bogged down in low‑level details.

References

Redis documentation: https://github.com/antirez/redis-doc

Redis introduction: https://redis.io/topics/introduction

Persistence: https://redis.io/topics/persistence

Pub/Sub: https://redis.io/topics/pubsub

Sentinel: https://redis.io/topics/sentinel

Replication: https://redis.io/topics/replication

Cluster tutorial: https://redis.io/topics/cluster-tutorial

Transactions: https://redis.io/topics/transactions

Data types: https://redis.io/topics/data-types-intro

Distributed locks: https://redis.io/topics/distlock

Pipelining: https://redis.io/topics/pipelining

Lua scripting: https://redis.io/commands/eval

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

high availabilityredisClusterIn-Memory Database
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

0 followers
Reader feedback

How this landed with the community

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.