Day 4 of 5
⏱ ~60 minutes
Redis in 5 Days — Day 4

Pub/Sub and Streams

Publish messages to channels, subscribe and handle messages, and use Redis Streams for reliable messaging.

Pub/Sub

Pub/Sub is fire-and-forget messaging. Publishers send to channels. Subscribers receive messages on channels they're subscribed to. No message history — if no subscriber is listening when a message arrives, it's lost.

Pub/Sub in Node.js
import { createClient } from 'redis';

// Subscriber needs its own connection
const subscriber = createClient();
const publisher = createClient();
await subscriber.connect();
await publisher.connect();

// Subscribe to a channel
await subscriber.subscribe('notifications', (message) => {
  console.log('Received:', JSON.parse(message));
});

// Publish from elsewhere
await publisher.publish('notifications', JSON.stringify({
  type: 'user_signup',
  userId: 123,
  email: '[email protected]'
}));

// Pattern subscribe (wildcard)
await subscriber.pSubscribe('events:*', (message, channel) => {
  console.log(`Channel ${channel}:`, message);
});
Redis Streams (reliable messaging)
# Streams have a persistent log — messages are stored
# Consumers can read from a specific position
# Consumer groups enable parallel processing

# Add to stream
XADD events * type order_placed userId 123 amount 49.99

# Read from stream
XRANGE events - +               # all messages
XREAD COUNT 10 STREAMS events 0 # from beginning

# Consumer group (parallel workers)
XGROUP CREATE events processors $ MKSTREAM
XREADGROUP GROUP processors worker1 COUNT 1 STREAMS events >
XACK events processors   # acknowledge processed
📝 Day 4 Exercise
Build a Real-Time Notification System
  1. U
  2. s
  3. e
  4. R
  5. e
  6. d
  7. i
  8. s
  9. p
  10. u
  11. b
  12. /
  13. s
  14. u
  15. b
  16. t
  17. o
  18. b
  19. u
  20. i
  21. l
  22. d
  23. a
  24. n
  25. o
  26. t
  27. i
  28. f
  29. i
  30. c
  31. a
  32. t
  33. i
  34. o
  35. n
  36. s
  37. y
  38. s
  39. t
  40. e
  41. m
  42. .
  43. W
  44. h
  45. e
  46. n
  47. a
  48. '
  49. u
  50. s
  51. e
  52. r
  53. _
  54. c
  55. r
  56. e
  57. a
  58. t
  59. e
  60. d
  61. '
  62. e
  63. v
  64. e
  65. n
  66. t
  67. i
  68. s
  69. p
  70. u
  71. b
  72. l
  73. i
  74. s
  75. h
  76. e
  77. d
  78. ,
  79. a
  80. s
  81. u
  82. b
  83. s
  84. c
  85. r
  86. i
  87. b
  88. e
  89. r
  90. l
  91. o
  92. g
  93. s
  94. i
  95. t
  96. a
  97. n
  98. d
  99. s
  100. t
  101. o
  102. r
  103. e
  104. s
  105. a
  106. n
  107. o
  108. t
  109. i
  110. f
  111. i
  112. c
  113. a
  114. t
  115. i
  116. o
  117. n
  118. i
  119. n
  120. R
  121. e
  122. d
  123. i
  124. s
  125. a
  126. s
  127. a
  128. l
  129. i
  130. s
  131. t
  132. .
  133. D
  134. i
  135. s
  136. p
  137. l
  138. a
  139. y
  140. t
  141. h
  142. e
  143. l
  144. a
  145. s
  146. t
  147. 1
  148. 0
  149. n
  150. o
  151. t
  152. i
  153. f
  154. i
  155. c
  156. a
  157. t
  158. i
  159. o
  160. n
  161. s
  162. .

Day 4 Summary

  • Pub/Sub: fire-and-forget. Fast, real-time. Messages are lost if no subscriber is active.
  • Streams: persistent log. Messages survive disconnections. Use for reliable event processing.
  • Pub/Sub needs separate connections for publishing and subscribing.
  • Use Streams over Pub/Sub when message delivery must be guaranteed.
Finished this lesson?