# Redis as a Message Broker — Redis Fundamentals: Caching, Data Structures and Persistence

Source: https://www.skillbyai.com/en/redis/redis-message-broker

> Compare Redis Pub/Sub with Streams and know when to choose a dedicated broker such as Kafka or RabbitMQ.

## Pub/Sub is fire-and-forget

Plain Pub/Sub delivers messages only to subscribers connected **right now** — nothing is stored, so an offline consumer misses it forever. **Redis Streams** (`XADD`/`XREAD`) add a durable, replayable log for when messages must not be lost.

## Not a Kafka replacement

For lightweight, low-latency messaging Redis is excellent; for massive, guaranteed-durable, multi-day event streams, a dedicated broker like Kafka or RabbitMQ is usually a better fit.

**Quiz:** Does plain Redis Pub/Sub store messages for subscribers who are offline?

- [ ] Yes, forever
- [ ] Yes, but only for 24 hours
- [x] No — only currently connected subscribers get it
- [ ] Only if AOF is disabled

*Answer:* No — only currently connected subscribers get it. Pub/Sub has no storage — use Redis Streams when messages need to survive for offline consumers.

## Final quiz 1 of 8

Final quiz

**Quiz:** Which Redis data type is best for a live leaderboard?

- [ ] List
- [ ] String
- [x] Sorted set
- [ ] Pub/Sub

*Answer:* Sorted set. Sorted sets keep members ranked by score automatically.

## Final quiz 2 of 8

Final quiz

**Quiz:** Which command safely iterates keys on a production server?

- [ ] KEYS *
- [ ] FLUSHALL
- [ ] DEBUG
- [x] SCAN

*Answer:* SCAN. SCAN walks the keyspace in small non-blocking batches.

## Final quiz 3 of 8

Final quiz

**Quiz:** What does SET key value EX 60 do?

- [x] Stores the value and expires it after 60 seconds
- [ ] Stores it for 60 days
- [ ] Appends 60 to the value
- [ ] Locks the key

*Answer:* Stores the value and expires it after 60 seconds. EX sets the TTL in seconds together with the value.

## Final quiz 4 of 8

Final quiz

**Quiz:** In cache-aside, what happens on a cache miss?

- [ ] Redis queries the database itself
- [x] The app reads the database and then fills the cache
- [ ] The request fails
- [ ] The cache is flushed

*Answer:* The app reads the database and then fills the cache. The application owns loading data into the cache.

## Final quiz 5 of 8

Final quiz

**Quiz:** Which persistence option logs every write command?

- [ ] RDB
- [ ] Pub/Sub
- [x] AOF
- [ ] Streams only

*Answer:* AOF. AOF replays commands to rebuild the dataset with minimal loss.

## Final quiz 6 of 8

Final quiz

**Quiz:** Which maxmemory-policy suits a pure cache?

- [ ] noeviction
- [ ] volatile-ttl only
- [ ] none
- [x] allkeys-lru

*Answer:* allkeys-lru. Evicting least recently used keys keeps hot data in memory.

## Final quiz 7 of 8

Final quiz

**Quiz:** Why avoid exposing Redis directly to the internet?

- [x] It was designed for trusted networks and can be hijacked
- [ ] It becomes slower
- [ ] It stops persisting
- [ ] It disables TTLs

*Answer:* It was designed for trusted networks and can be hijacked. Bind to private interfaces, require auth and use a firewall.

## Final quiz 8 of 8

Final quiz

**Quiz:** Which Redis feature keeps a durable, replayable log of messages?

- [ ] Plain Pub/Sub
- [x] Streams
- [ ] SCAN
- [ ] EXPIRE

*Answer:* Streams. Streams (XADD/XREAD) retain messages for consumers that connect later.
