Last modified: March 23, 2026

This article is written in: 🇺🇸

Redis

Redis is an open-source, in-memory data store that serves as a high-performance cache, message broker, and general-purpose database. It is often called a "data structure server" because it natively supports rich types like strings, lists, sets, sorted sets, and hashes. Because every operation happens in memory, Redis achieves sub-millisecond latency for both reads and writes, making it one of the most widely adopted caching layers in modern backend architectures. Understanding how to leverage Redis effectively can dramatically improve the performance, scalability, and responsiveness of your applications.

+-----------+        +-----------+        +-----------+
  | Client A  |        | Client B  |        | Client C  |
  | (Web App) |        | (Worker)  |        |  (CLI)    |
  +-----+-----+        +-----+-----+        +-----+-----+
        |                     |                     |
        | SET/GET             | LPUSH/BRPOP         | PUBLISH
        |                     |                     |
        +----------+----------+----------+----------+
                   |                     |
                   v                     v
        +-------------------------------------------+
        |            Redis Server (Single)          |
        |-------------------------------------------|
        |  Engine: Single-threaded event loop        |
        |  Storage: In-memory key-value store        |
        |  Features:                                 |
        |   - Data Structures (String, List, Set...) |
        |   - Pub/Sub messaging                      |
        |   - Lua scripting                          |
        |   - Transactions (MULTI/EXEC)              |
        |   - TTL-based key expiration               |
        +---------------------+---------------------+
                              |
                   (Optional persistence)
                              |
              +---------------+---------------+
              |                               |
              v                               v
        +-----------+                   +-----------+
        | RDB Dump  |                   | AOF Log   |
        | (Snapshot) |                   | (Append)  |
        +-----------+                   +-----------+

Key Concepts

In-Memory Data Store

Key-Value Store

Data Expiration

Redis Data Structures

Redis supports several first-class data structures, each optimized for different access patterns.

|                    Redis Data Structures                      |
  +--------------------------------------------------------------+
  |                                                              |
  |  STRING        LIST             SET           SORTED SET     |
  |  +------+     +---+---+---+   +---+---+---+  +-----+-----+  |
  |  | "Hi" |     | A | B | C |   | X | Y | Z |  |a:1.0|b:2.5|  |
  |  +------+     +---+---+---+   +---+---+---+  +-----+-----+  |
  |                                                              |
  |  HASH                           STREAM                       |
  |  +--------+---------+          +-----+-----+-----+           |
  |  | field1 | value1  |          | ID1 | ID2 | ID3 |           |
  |  | field2 | value2  |          +-----+-----+-----+           |
  |  +--------+---------+                                        |
  +--------------------------------------------------------------+

Strings

Lists

Sets

Sorted Sets

Hashes

Redis Persistence

By default Redis is an in-memory store, but it offers two durability mechanisms that can be used independently or together.

Redis Server
                    +----------------+
                    |   In-Memory    |
                    |   Dataset      |
                    +-------+--------+
                            |
           +----------------+----------------+
           |                                 |
     (Point-in-time                   (Every write
       snapshot)                        appended)
           |                                 |
           v                                 v
   +---------------+                +----------------+
   |   RDB File    |                |   AOF File     |
   | (binary dump) |                | (command log)  |
   +---------------+                +----------------+
   | - Compact     |                | - Durable      |
   | - Fast load   |                | - Human-readable|
   | - Data loss   |                | - Larger size  |
   |   between     |                | - Rewritable   |
   |   snapshots   |                |   via BGREWRITE|
   +---------------+                +----------------+

RDB (Redis Database)

AOF (Append-Only File)

Combining RDB and AOF

Redis Cluster and Replication

Replication

|    Primary      |
                  | (Read + Write)  |
                  +--------+--------+
                           |
            +--------------+--------------+
            |  async replication          |  async replication
            v                             v
   +-----------------+           +-----------------+
   |   Replica 1     |           |   Replica 2     |
   |  (Read Only)    |           |  (Read Only)    |
   +-----------------+           +-----------------+

Redis Cluster

+-------------------+    +-------------------+    +-------------------+
  | Node A (Primary)  |    | Node B (Primary)  |    | Node C (Primary)  |
  | Slots 0-5460      |    | Slots 5461-10922  |    | Slots 10923-16383 |
  +---------+---------+    +---------+---------+    +---------+---------+
            |                        |                        |
            v                        v                        v
  +-------------------+    +-------------------+    +-------------------+
  | Node A' (Replica) |    | Node B' (Replica) |    | Node C' (Replica) |
  +-------------------+    +-------------------+    +-------------------+

Pub/Sub Capabilities

+-----------+           +------------------+          +--------------+
  | Publisher |  PUBLISH  |  Redis Channel   | message  | Subscriber A |
  | (Service) +---------->| "notifications"  +--------->| (Web Socket) |
  +-----------+           +--------+---------+          +--------------+
                                   |
                                   | message
                                   v
                          +--------------+
                          | Subscriber B |
                          | (Worker)     |
                          +--------------+

Using Redis as a Cache

Cache-Aside (Lazy Loading)

Write-Through

Cache Invalidation

Cache Key Design

Common Usage Patterns

Redis vs Memcached

Feature Redis Memcached
Data structures Strings, lists, sets, hashes, streams Strings only
Persistence RDB snapshots, AOF log None (pure cache)
Replication Built-in primary-replica Not built-in
Clustering Redis Cluster (hash slots) Client-side consistent hashing
Pub/Sub Native support Not supported
Scripting Lua scripting on the server Not supported
Threading model Single-threaded event loop Multi-threaded
Max value size 512 MB 1 MB (default)
Memory efficiency Moderate (structural overhead) High (slab allocator)
Use case Cache, message broker, data store Simple high-throughput caching

Redis Clients

To interact with Redis from your backend code, you need a Redis client library. Popular options include:

Choose a client library that best fits your programming language, concurrency model, and deployment requirements.

Monitoring and Maintenance

Security Considerations