Redis

This video, "Redis Deep Dive w/ a Ex-Meta Senior Manager" by Hello Interview, provides a comprehensive technical breakdown of how Redis works and how to leverage it during system design interviews at top tech companies.


🚀 Overview: Why Redis?

Redis (REmote DIctionary Server) is an open-source, in-memory key-value data store.

  • Performance: Sub-millisecond latency because it operates entirely in RAM.

  • Simplicity: Single-threaded event loop prevents race conditions on single keys.

  • Versatility: It is far more than a simple cache; it provides complex data structures that solve high-scale problems with minimal code.


🛠️ Core Data Structures & Usage

The video emphasizes choosing the right tool for the job. Below is a summary of the data structures discussed:

Data Structure

Best Use Case

Key Commands

Strings

Simple caching, session tokens, basic counters.

SET, GET, INCR

Hashes

Storing objects (e.g., User Profile: Name, Age, Email).

HSET, HGET

Lists

Basic queues or "Recent Items" feeds (FIFO/LIFO).

LPUSH, RPOP

Sets

Unordered unique collections (e.g., Unique visitors).

SADD, SISMEMBER

Sorted Sets

Leaderboards, sliding window rate limiters.

ZADD, ZRANGE

Streams

Message bus, async job queues (durable log).

XADD, XREADGROUP


🏘️ Redis Cluster: Scalability & High Availability

To handle more data than fits on one machine, Redis uses a Cluster architecture.

  • Sharding: Data is split across 16,384 "hash slots." Each node is responsible for a subset of these slots.

  • Redirection (MOVED error): If a client sends a request to the wrong node, the node responds with a MOVED error and the correct IP. The client then updates its internal "map" and retries.

  • Replication: Each primary node has a replica. If a primary fails, the cluster promotes a replica to primary to ensure uptime.


🔑 Key System Design Patterns

1. The Cache-Aside Pattern

This is the standard way to use Redis as a cache:

  1. Check Cache: If data exists (Hit), return it.

  2. Read DB: If it doesn't exist (Miss), read from the database.

  3. Update Cache: Write that data back to Redis for future requests.

  • Note: Use TTL (Time To Live) to ensure data eventually expires and doesn't become "stale."

2. Rate Limiting

  • Fixed Window: Use INCR on a key like user:123:min:59. If the value > limit, block the request.

  • Sliding Window: Use Sorted Sets. Add a member with the current timestamp as the score. Remove old timestamps using ZREMRANGEBYSCORE. Count the remaining members to see if they exceed the limit.

3. Leaderboards (Sorted Sets)

Redis handles leaderboards efficiently using Skip Lists.

  • Time Complexity: for adds and updates.

  • Efficiency: You can retrieve the "Top 10" or a specific user's rank instantly even with millions of users.

4. Async Job Queues (Streams)

Unlike simple Pub/Sub, Redis Streams are durable.

  • Consumer Groups: Multiple workers can listen to one stream. Redis ensures each message is delivered to only one worker in the group.

  • Failure Recovery: If a worker crashes, other workers can "claim" the pending message using XCLAIM to ensure no job is lost.


⚠️ Important Trade-offs

  • Persistence: While Redis can save to disk (RDB/AOF), it is primarily in-memory. It is not a replacement for a source-of-truth database for mission-critical, durable data.

  • Memory Cost: RAM is significantly more expensive than Disk (SSD/HDD). Don't store massive, rarely accessed blobs in Redis.


Here is a "cheat sheet" comparison between Redis Streams and Apache Kafka. While they share similar concepts like "Consumer Groups," they are built for very different scales and reliability requirements.


📊 Comparison Table: Redis Streams vs. Apache Kafka

Feature

Redis Streams

Apache Kafka

Storage Medium

In-Memory (RAM)

Disk-based (Sequential Log)

Latency

Sub-millisecond (Ultra-low)

Low (typically 5–50ms)

Throughput

High (tens of thousands/sec)

Extreme (millions/sec)

Durability

Best-effort (via RDB/AOF)

Very High (Replicated logs)

Retention

Limited by RAM (usually hours/days)

Massive (can store years of data)

Complexity

Simple (Single binary/Cluster)

High (Brokers, KRaft/Zookeeper)

Max Message Size

Small (avoid > 1MB)

Large (can handle several MBs)

Ecosystem

Redis CLI, standard clients

Kafka Connect, KSQL, Schema Registry


🔍 Key Structural Differences

1. Data Persistence & Replayability

  • Kafka is a "source of truth." It stores every message on disk across multiple servers. You can "rewind" time and replay messages from a year ago if you have the disk space.

  • Redis Streams is primarily for "in-flight" data. While it can save to disk, its primary home is RAM. If your stream grows too large, it will consume all server memory. It is typically used with a MAXLEN threshold to automatically prune old messages.

2. The Consumer Model

Both use Consumer Groups, but they manage them differently:

  • Kafka: Uses Partitions. A partition is the unit of parallelism. If you have 10 partitions, you can have a maximum of 10 active consumers in a group.

  • Redis Streams: Does not use partitions. Multiple consumers in a group can compete for any message in the stream. It uses a PEL (Pending Entries List) to track which consumer has which message and whether they have acknowledged (XACK) it.

3. Operational Complexity

  • Redis is famous for its "it just works" nature. If you already have a Redis instance for caching, enabling Streams is zero extra effort.

  • Kafka is a distributed system "beast." It requires careful tuning of JVM heaps, disk I/O, and network settings. It usually requires a dedicated infrastructure person or a managed service like Confluent.


⚖️ Decision Matrix: Which one to pick?

Choose Redis Streams if...

  • You already use Redis and want to avoid adding new infrastructure.

  • You need the absolute lowest latency (e.g., high-frequency trading or real-time gaming).

  • Your data volume is moderate (e.g., less than 1TB per day).

  • You only need to store message history for a short window (e.g., the last 10,000 events).

Choose Apache Kafka if...

  • You are building a mission-critical data pipeline where data loss is unacceptable.

  • You need to replay/audit historical data frequently.

  • You have massive scale requirements (ingesting logs from thousands of microservices).

  • You need to integrate with Big Data tools like Hadoop, Spark, or Snowflake (Kafka has better "Sink/Source" connectors).


Would you like me to walk through a specific system design scenario (like a "Live Bus Tracking System") and show how you would implement it using one of these?

Last updated