Redis

Fast, in-memory data structure store for databases, caches, and message brokers

What is Redis?

Redis (Remote Dictionary Server) is a fast, open-source, in-memory data structure store that can be used as a database, cache, and message broker. It supports various data structures like strings, hashes, lists, sets, sorted sets, and more, all stored in memory for extremely fast access.

Built for performance, Redis can handle millions of operations per second with sub-millisecond latency. Its versatility makes it popular for caching, session storage, real-time analytics, and messaging via its built-in Pub/Sub capabilities.

Redis operates primarily as an in-memory database but provides persistence options to disk, combining the speed of memory-based operations with the durability of traditional databases when needed.

🚀 Core Features

In-Memory Performance

All data stored in RAM for microsecond-level access times and millions of operations per second.

Rich Data Types

Native support for strings, lists, sets, hashes, sorted sets, bitmaps, and more.

Persistence Options

RDB snapshots and AOF logging for data durability and recovery.

Pub/Sub Messaging

Built-in publish-subscribe capabilities for real-time messaging and event distribution.

Atomic Operations

All operations are atomic, ensuring data consistency in concurrent environments.

Clustering & HA

Built-in clustering, replication, and high availability features for production deployments.

🗃️ Data Structures

Strings

Binary-safe strings up to 512MB

SET key "value"
GET key
INCR counter

Lists

Ordered collections of strings

LPUSH list "item"
RPOP list
LRANGE list 0 -1

Sets

Unordered collections of unique strings

SADD set "member"
SISMEMBER set "member"
SUNION set1 set2

Hashes

Field-value pairs (like objects)

HSET hash field "value"
HGET hash field
HGETALL hash

Sorted Sets

Sets ordered by score values

ZADD zset 1 "first"
ZRANGE zset 0 -1
ZRANK zset "first"

Streams

Append-only log data structure

XADD stream * field value
XREAD STREAMS stream 0
XGROUP CREATE stream group

🔌 RESP (REdis Serialization Protocol)

RESP is the simple, text-based protocol used for communication with a Redis server. It's designed to be human-readable while remaining efficient for high-performance applications. RESP supports different data types and provides a standardized way for clients to interact with Redis.

The protocol is stateless and connection-oriented, using TCP connections between clients and the Redis server. Each command and response follows a specific format that makes it easy to implement Redis clients in any programming language.

Protocol Features

  • Simple: Easy to implement and debug
  • Fast: Minimal parsing overhead
  • Human-readable: Text-based format
  • Binary-safe: Supports binary data

Data Type Prefixes

+ Simple Strings
- Errors
: Integers
$ Bulk Strings
* Arrays

Example RESP Communication

Command: SET key value
Client sends (RESP format):
*3 # Array with 3 elements
$3 # Bulk string of length 3
SET # The command
$3 # Bulk string of length 3
key # First argument
$5 # Bulk string of length 5
value # Second argument
Server responds (RESP format):
+OK # Simple string response
Meaning: Command executed successfully
More RESP Examples:
Integer response:
:42 → Returns integer 42
Error response:
-ERR → Error message

📡 Messaging Capabilities

Pub/Sub Pattern

Redis provides built-in publish-subscribe messaging for real-time communication between applications.

# Publisher
PUBLISH channel "message"
# Subscriber
SUBSCRIBE channel
PSUBSCRIBE pattern*

List-based Queues

Use Redis lists as simple message queues with blocking operations for efficient message processing.

# Producer
LPUSH queue "message"
# Consumer
BRPOP queue 0

Redis Streams (Advanced Messaging)

Redis Streams provide a more sophisticated messaging solution with features like consumer groups, message acknowledgments, and message history.

Key Features:

  • • Append-only log structure
  • • Consumer groups for load balancing
  • • Message acknowledgments
  • • Message history and replay
  • • Range queries by time or ID

Example Commands:

XADD stream * sensor temperature 25.5
XGROUP CREATE stream processors 0
XREADGROUP GROUP processors consumer1
XACK stream processors message-id

🎯 Common Use Cases

Caching & Performance

  • Application Cache: Store frequently accessed data in memory for fast retrieval
  • Session Storage: Web application session data with automatic expiration
  • Page Caching: Store rendered HTML pages for faster web responses
  • API Response Cache: Cache expensive API calls and database queries

Real-time Applications

  • Chat Applications: Real-time messaging with pub/sub patterns
  • Live Analytics: Real-time metrics and dashboard updates
  • Gaming Leaderboards: Sorted sets for rankings and scores
  • Event Streaming: Process and route events in real-time

Performance & Deployment

Performance Metrics

Throughput: 1M+ operations/second (single instance)
Latency: Sub-millisecond for in-memory operations
Memory: Extremely memory-efficient data structures
Network: Pipelined commands for reduced round trips

Deployment Options

Single Instance: Simple deployment for development/small apps
Master-Replica: Read scaling with automatic failover
Redis Cluster: Horizontal scaling across multiple nodes
Redis Sentinel: High availability and monitoring