Module 1 - Data Storage

Redis Deep Dive

The Swiss Army knife of in-memory data stores. Far more than just a cache.

1What is Redis?

Redis (Remote Dictionary Server): An in-memory data structure store used as a database, cache, message broker, and queue. Single-threaded, incredibly fast (100K+ ops/sec).

< 1ms
Latency
100K+ ops/s
Throughput
Up to RAM
Data Size

2Data Structures

Strings

Basic key-value. Store text, numbers, JSON, binary.

SET user:1 'john'

Lists

Ordered collection. Use for queues, timelines.

LPUSH queue:emails 'msg1'

Sets

Unique unordered items. Tags, unique visitors.

SADD tags:post:1 'redis' 'cache'

Sorted Sets

Unique items with scores. Leaderboards, rankings.

ZADD leaderboard 100 'alice'

Hashes

Field-value pairs. Store objects.

HSET user:1 name 'john' age 30

Streams

Append-only log. Event sourcing, messaging.

XADD events * type 'click'

3Common Use Cases

Caching

Cache database queries, API responses, sessions

Session Store

Store user sessions with automatic expiry

Rate Limiting

Track request counts per user/IP with TTL

Leaderboards

Sorted sets for real-time rankings

Pub/Sub

Real-time messaging between services

Distributed Locks

SETNX for mutual exclusion

Queues

Lists as simple job queues

Real-time Analytics

HyperLogLog for unique counts

4Persistence Options

RDB (Snapshotting)

Point-in-time snapshots at intervals. Fast restart, but may lose recent data.

+ Fast, compact- Data loss risk

AOF (Append-Only File)

Log every write operation. Better durability, slower startup.

+ Durable- Larger files, slower

RDB + AOF

Combine both for durability and fast restarts.

+ Best of both- More disk usage

5Clustering & HA

Redis Sentinel

Monitoring, failover, and notification. For high availability.

Redis Cluster

Automatic sharding across nodes. Horizontal scaling.

Replication

Master-replica for read scaling and failover.

Managed Options

AWS ElastiCache, Redis Cloud, Azure Cache

6Key Takeaways

1Redis is an in-memory data structure store-not just a cache
2Supports strings, lists, sets, sorted sets, hashes, streams
3Use for caching, sessions, rate limiting, leaderboards, pub/sub
4RDB + AOF provides both durability and fast restarts
5Single-threaded but incredibly fast due to in-memory ops

?Quiz

1. Best Redis data structure for a real-time leaderboard?

2. Redis is single-threaded. How is it still so fast?