Lesson 2 / 25
Running Redis and Using redis-cli
Start Redis locally and experiment with messaging commands.
A playground in one command
The quickest way to experiment is the official container image: docker run -p 6379:6379 redis:8 (or valkey/valkey for Valkey, the Linux Foundation fork created in 2024, which supports the same Pub/Sub and Streams commands). Connect with redis-cli; open two terminals to watch a publisher and a subscriber interact. In production you would use a managed service (Amazon ElastiCache or MemoryDB, Azure Managed Redis, Google Memorystore, Redis Cloud) or a self-managed deployment with replication. Clients talk the RESP protocol; RESP3 (available since Redis 6) lets a single connection receive Pub/Sub pushes while still running other commands, whereas with RESP2 a connection in subscribed mode can only issue subscription commands, PING, RESET and QUIT. Mind the licensing landscape: Redis changed licences in 2024 and added an AGPLv3 option with Redis 8, while Valkey remains BSD-licensed; both are widely used.
Two terminals, one channel
Run the subscriber first, then publish from the second terminal.
docker run -d --name redis -p 6379:6379 redis:8
# terminal 1
redis-cli SUBSCRIBE orders.updates
# terminal 2
redis-cli PUBLISH orders.updates '{"orderId":"o-1001","status":"shipped"}'
# PUBLISH replies with how many subscribers received the message
# inspect active channels and subscriber counts
redis-cli PUBSUB CHANNELS 'orders.*'
redis-cli PUBSUB NUMSUB orders.updatesA walkie-talkie channel
Pub/Sub is a walkie-talkie channel: everyone tuned in hears the message at that moment. If your set was switched off, nobody repeats it for you.
Quick check: With the RESP2 protocol, what can a connection do after it issues SUBSCRIBE?
- Run any Redis command as usual
- Only PUBLISH
- Nothing until it disconnects
- Only subscription-related commands plus PING, RESET and QUIT
Answer
Only subscription-related commands plus PING, RESET and QUIT — In RESP2 a subscribed connection is limited to subscription commands; RESP3 lifts this restriction.