syswork
Redis/

Pub/Sub

SimulatorHow it works

Redis · Pub/Sub · how it works

How Redis Pub/Sub actually works

Fire-and-forget fan-out with no storage. Every message is pushed into each subscriber's output buffer inside Redis, which is why one slow consumer can become the server's memory problem.

Delivery
at-most-once
Storage
none
Model
push
Safety valve
output buffer limit

Modelled on Redis Open Source 8.10 · sources

▶ Open the simulator8 scenarios ↓

On this page

  • The path of one message
  • At-most-once, no storage
  • Push delivery and the output buffer
  • Without the limit
  • Slow reader vs dead network
  • When to use something else
  • What this simulation simplifies
  • Every scenario, step by step
  • References

The path of one message#

A client runs SUBSCRIBE orders and its connection switches into a subscribed state.[1] Redis adds the connection to the subscriber list for orders (a dictionary from channel name to client list). PSUBSCRIBE pay* adds the connection to a separate list of glob patterns. That's all the state there is: subscriptions live on connections, in memory.[2]

publisherPUBLISHordersredis-server process memorypubsub_channelsorders → A, Bpubsub_patternspay* → Cpay* doesn't matchorders: C is skipped← :2receiversoutput buffer · Asocket buffersubscriber Areadingoutput buffer · Bsocket: fullsubscriber Bslow readeroutput buffer · Csocket buffersubscriber Cnot matchedgrows when the socket is fullTCP (Transmission Control Protocol): Reliable, ordered byte stream between two hosts. Flow control (the window) stops a sender when the receiver's buffers are full.kernel + TCP windowother hosts1234
  1. PUBLISH orders "order#42" arrives on the publisher's connection.
  2. Redis looks up the channel's subscriber list, then checks every pattern.[2]
  3. For each match it appends the message to that client's output buffer and replies to the publisher with the number of receivers.[2][3]
  4. Redis writes each buffer to its non-blocking socket as far as the kernel accepts. What doesn't fit stays in the output buffer, in Redis's memory.[3]

Each delivery is a RESP array: ["message", channel, payload], or ["pmessage", pattern, channel, payload] for pattern matches. A client subscribed to both a channel and a matching pattern receives the message twice.[1]

See it happen

  • 01Fan-out: the happy path→

At-most-once, no storage#

Pub/Sub has no relation to the key space, so messages never reach RDB snapshots, and PUBLISH is propagated to replicas but not written to the AOF.[1][2] Subscribers send no acknowledgements. If nobody is subscribed when PUBLISH runs, the message is discarded and the reply is 0. If a subscriber is restarting, it misses everything published in the meantime, and there's no offset to resume from. Each message is delivered at most once.[1]

The subscriptions aren't persisted either. When Redis restarts, every connection is gone along with its subscriptions. Until each client reconnects and runs SUBSCRIBE again, PUBLISH reaches nobody.[2]

See it happen

  • 02Publishing into the void→
  • 03A subscriber restarts and misses messages→
  • 08Redis restarts: subscriptions vanish→

Push delivery and the output buffer#

Pub/Sub is push-based: Redis writes to the subscriber whether or not it's ready.[4] Redis uses non-blocking sockets, so it never waits for a slow client. When the kernel's socket buffers are full (TCP flow control has closed the window), the remaining bytes wait in the client output buffer, in Redis's own memory.[3][5] A slow consumer becomes Redis's memory problem.

That's why Redis ships with a limit for Pub/Sub clients. The default:[3][6]

client-output-buffer-limitdirective
pubsubclient class
32mbhard limit
8mbsoft limit
60soft seconds
  • Hard limit: the moment the buffer exceeds 32mb, the client is closed.
  • Soft limit: if the buffer stays above 8mb continuously for 60 seconds, the client is closed. Dropping below resets the timer.[6]

When a client is closed, the messages still in its output buffer are lost.[1] Redis logs Client … scheduled to be closed ASAP for overcoming of output buffer limits.[7] A client that reconnects while still slow hits the limit again.

See it happen

  • 04Stuck consumer hits the hard limit→
  • 05Slow consumer and the soft limit→

Without the limit#

Setting the limit to 0 0 0 removes it.[6] A stuck subscriber's buffer then grows without bound.

One consumer, everyone's outage

Client buffers are Redis memory, and enough of them lead to data eviction or out-of-memory errors.[3] Without swap, the Linux OOM killer can kill the Redis process[8], logging Out of memory: Killed process …[9], and every client is disconnected, including the healthy ones.

See it happen

  • 07No buffer limits: one consumer takes everyone down→

A slow reader and a dead network look the same#

If the network to a subscriber breaks silently, no FIN or RST arrives. Redis only sees that the TCP window stopped opening, which is exactly what a slow reader looks like, so the buffer grows until the limit closes the client.[5]

The subscriber only reads and never sends, so it may not notice for a long time: the connection is half-open. TCP keepalive (Redis defaults to tcp-keepalive 300 on its side) or an application-level PING health check is how clients detect it.[5][3][1]

See it happen

  • 06Network partition: slow and gone look the same→

When to use something else#

Pub/Sub is the right tool when losing a message is acceptable; for stronger guarantees the Redis docs point to Streams.[1] The difference is whether the broker keeps the data and who sets the pace.

Redis Pub/SubRedis StreamsKafka
Stores messagesNoYes, as a key (persisted if RDB/AOF is on)Yes, a replicated log on disk
DeliveryPushPull: XREAD / XREADGROUP, can BLOCKPull: consumers fetch
GuaranteeAt-most-onceAt-least-once with consumer groups and XACKAt-least-once by default
Consumer offlineMisses everythingResumes from its group's last IDResumes from its committed offset
Slow consumerGrows its output buffer in Redis until closedEntries wait in the stream (until trimmed)Lag grows (until retention deletes data)

Sources: Pub/Sub[1], Streams[10], Kafka[11][12][13].

  • Need to survive consumer restarts, or replay history? Use Redis Streams: an append-only log with consumer groups, acknowledgements (XACK) and a pending entries list.[10]
  • Need durable, replayable messaging where each consumer sets its own pace? Use a pull-based log like Kafka: consumers fetch what they can handle and fall behind (lag) instead of filling a broker buffer.[11]
  • On Redis Cluster 7+, SSUBSCRIBE/SPUBLISH (sharded Pub/Sub) keep traffic on the shard that owns the channel's slot instead of broadcasting to every node.[1]

What this simulation simplifies#

Model vs reality

  • Time is slowed down about a thousand times, and buffer limits are scaled from MB to KB to match.
  • TCP is modelled as a fixed window of messages rather than bytes, segments and ACK clocking. Retransmission uses a fixed timeout.
  • Message sizes approximate the RESP encoding.
  • The model checks the soft limit whenever a client's buffer grows or drains.
  • The kernel's OOM log line is shortened: the real one continues with the process's memory figures.

Every scenario, step by step

What happens in each scenario and when. Times are simulated seconds, as on the simulator's timeline. Click a time to open the simulator paused at that moment, then take control.

01

Fan-out: the happy path

Two services publish, three consume. Each PUBLISH is copied into every matching subscriber's output buffer and pushed down its TCP connection.

▶ Run it

SetupDefaults. checkout-api publishes to orders about every 700 ms, billing-api to payments about every 1100 ms. email-worker runs SUBSCRIBE orders, analytics SUBSCRIBE orders payments, fraud-check PSUBSCRIBE pay*. Each subscriber handles a message in 250 ms.

  1. 0.0sAll three connections are in subscribed state. Redis now maps orders → email-worker, analytics and payments → analytics, and holds the pattern pay* → fraud-check.
  2. 0.8sThe first PUBLISH orders arrives. Redis appends it to email-worker's and analytics' output buffers and replies (integer) 2 to checkout-api.
  3. ongoingEach PUBLISH payments goes to analytics through the channel and to fraud-check through the pattern, as a pmessage that carries the pattern too.

Every match is a separate copy in a separate output buffer. Every subscriber reads faster than messages arrive, so the buffers drain straight away and the run stays free of warnings.

Try next

  • Select email-worker and switch on Stop reading the socket: its output buffer starts to grow.
  • Pull out the Pattern subscriber block: fraud-check is gone and PUBLISH payments returns (integer) 1.
02

Publishing into the void

Pub/Sub keeps nothing. A message published to a channel with no subscribers is discarded on the spot.

▶ Run it

SetupAuto-publishing is off. The publishers send three messages by hand.

  1. 1.1scheckout-api publishes to orders: two receivers, (integer) 2.
  2. 2.8sbilling-api publishes to refunds. Nobody subscribes to it, so Redis drops the message on the spot and replies (integer) 0.
  3. 4.7sbilling-api publishes to payments. analytics gets it through the channel and fraud-check through pay*: (integer) 2.

Nothing is stored, not even briefly. The 0 in the reply is the publisher's only clue that nobody heard it.

Try next

  • From billing-api's inspector, Publish by hand to refunds again, or to orders.
03

A subscriber restarts and misses messages

analytics is redeployed. While it's gone, Redis doesn't hold anything for it. When it comes back it only sees new messages.

▶ Run it

Setupanalytics goes offline at 3 s (think: a deploy) and comes back at 9 s.

  1. 3.0sanalytics' connection closes. Redis removes its subscriptions immediately: nothing is kept for it from this point on.
  2. ongoingorders now reaches only email-worker, and payments only fraud-check. analytics' missed counter climbs with every message it should have got.
  3. 9.5sanalytics reconnects and runs SUBSCRIBE again. It missed 15 messages and has no way to ask for them: there is no offset to resume from.

A Pub/Sub subscriber only receives what is published while it is connected and subscribed. A restart, however short, is a gap in its data.

Try next

  • Use Take offline on email-worker or fraud-check, and Bring online whenever you like.
04

Stuck consumer hits the hard limit

email-worker stops reading its socket. Its TCP window fills, then its output buffer grows inside Redis until it crosses the hard limit and Redis cuts it off.

▶ Run it

SetupLimits are scaled down: hard 12 KB, soft 6 KB for 30 s. checkout-api publishes every 550 ms. email-worker stops reading at 2 s and only reads again at 25 s.

  1. 2.0semail-worker's read loop is stuck (a GC pause, a deadlock, a slow handler). Redis can't see that. It only sees the TCP window stop opening.
  2. 4.4sThe socket buffers are full. New orders messages now wait in email-worker's output buffer, in Redis's memory.
  3. 7.0sThe buffer passes the soft limit. That limit needs 30 s above the line, so it won't be what closes this client.
  4. 10.5sThe buffer crosses the 12 KB hard limit. Redis closes the client at once. Everything still in the buffer is dropped and counted as lost.
  5. 12.6sIts client library reconnects and re-subscribes. It missed 4 messages while away, and it still isn't reading, so the buffer starts filling again.
  6. 21.9sCut off a second time. It reconnects at 24 s, and at 25 s it finally reads again and catches up.

The hard limit protects Redis's memory by throwing the slow client's data away. Reconnecting doesn't fix a stuck consumer: it just restarts the cycle of fill, cut off, lose the buffer.

Try next

  • Raise the Hard limit in Blocks & settings: the client holds on longer and Redis holds more memory for it.
  • Pull out Client auto-reconnect: after the first cut-off, email-worker stays gone.
05

Slow consumer and the soft limit

fraud-check is slower than the payment rate. It never crosses the hard limit, but it stays above the soft limit for too long.

▶ Run it

SetupLimits: soft 4 KB for 5 s, hard 64 KB. billing-api publishes every 380 ms, but fraud-check needs 950 ms per message.

  1. 2.7sfraud-check falls behind. Its socket fills and the backlog moves into its output buffer.
  2. 4.6sThe buffer passes 4 KB and the soft-limit timer starts. The buffer never gets anywhere near the 64 KB hard limit.
  3. 10.3sIt has stayed above the soft limit for the whole 5 s. Redis closes it, with the same log line as for the hard limit.
  4. 12.5sfraud-check reconnects, having missed 6 messages. It's no faster than before.
  5. 23.5sThe same again: over the soft limit at 17.8 s, closed at 23.5 s. It will keep cycling for as long as it is slower than the publisher.

The soft limit catches consumers that are steadily too slow, not just stuck. analytics receives the same payments but handles each in 250 ms, so it backs up briefly and then keeps pace.

Try next

  • Lower fraud-check's Time to handle one message below 380 ms: the buffer drains and the timer resets.
  • Raise Soft duration in Blocks & settings to tolerate a longer burst.
06

Network partition: slow and gone look the same

The link to analytics is cut. Redis can't tell a broken network from a slow reader. analytics can't tell it has been dropped.

▶ Run it

SetupThe network link to analytics is cut at 2.5 s and restored at 15 s.

  1. 2.5sThe link to analytics is cut. No FIN or RST is sent, so neither side knows.
  2. 5.7sTo Redis this is a slow reader: the TCP window stops opening and the output buffer grows.
  3. 9.1sRedis closes the client for exceeding its buffer limit. The FIN can't cross the broken link, so analytics still believes it is subscribed: the connection is half-open.
  4. 15.0sThe link comes back.
  5. 15.4sThe FIN finally arrives. Only now does analytics learn it was dropped, 6 seconds after the fact.
  6. 17.1sIt re-subscribes, having missed 18 messages published while it sat on a dead connection.

Both sides are wrong for a while. Redis buffers for a peer that's gone, and the subscriber waits on a connection that's dead. TCP keepalive or an application-level PING shortens that window.

Try next

  • Do the same to email-worker with Stop reading the socket instead: from Redis's side it looks identical.
  • Lower the Hard limit and Redis gives up on the dead peer sooner.
07

No buffer limits: one consumer takes everyone down

The output buffer limit is removed (the lego block is pulled out). One stuck subscriber grows its buffer until the host runs out of memory.

▶ Run it

SetupThe Output buffer limits block is pulled out, and the host has 40 KB of headroom for client buffers. checkout-api publishes every 300 ms. email-worker stops reading at 1.5 s and never recovers.

  1. 1.5semail-worker gets stuck.
  2. 2.8sIts backlog moves into its output buffer. With no limit configured, nothing will ever close it.
  3. 13.0sThe buffer has used up the host's memory. The kernel's OOM killer stops Redis.
  4. 13.4sEvery subscriber loses its connection, including the two healthy ones. The publishers get connection errors, and those messages are gone too.
  5. 16.0sRedis restarts with zero subscriptions. Until the clients re-subscribe, PUBLISH reaches nobody and returns 0.
  6. 16.7sEveryone re-subscribes, including email-worker, which is still stuck.
  7. 29.1sIts buffer grows again, and Redis is killed a second time.

The output buffer limit exists to protect everyone else. Without it, one stuck consumer turns into a full outage, and it repeats as soon as that consumer reconnects.

Try next

  • Put the Output buffer limits block back: email-worker gets cut off instead, and nobody else notices.
  • Raise Host memory headroom in Blocks & settings: the crash comes later, but it still comes.
08

Redis restarts: subscriptions vanish

Subscriptions exist only as state on live connections. A Redis restart wipes them and everything in flight.

▶ Run it

SetupRedis crashes at 4 s. A supervisor restarts it 3 s later.

  1. 4.0sRedis crashes. Every connection, every subscription and every output buffer disappears with the process.
  2. 4.3sBoth publishers get connection errors. Nothing queues those messages anywhere.
  3. 4.5sThe subscribers see their connections close and start reconnecting.
  4. 7.0sRedis is back, with no subscriptions at all: they were never written to RDB or AOF.
  5. 7.7sThe first PUBLISH orders after the restart beats the subscribers back and reaches nobody.
  6. 7.7sThe subscribers run SUBSCRIBE again, and delivery resumes.

Subscriptions are connection state. Recovery depends entirely on each client reconnecting and re-running SUBSCRIBE.

Try next

  • Pull out Client auto-reconnect: after the restart nobody comes back, and every PUBLISH returns 0.
  • Select redis-server and press Crash redis-server, then Restart now, at any moment you like.

References

Modelled on Redis Open Source 8.10. Pub/Sub, output buffer limits and their defaults are documented for this release and have been stable for many releases; sharded Pub/Sub (Redis 7.0+) is marked where mentioned. Source-code references point to the Redis and Linux repositories as of the date checked. Sources last checked 2026-11-16. Every claim on this page and in the simulator comes from the sources below; anything the simulation simplifies is listed under “What this simulation simplifies”.

  1. [1]Redis Pub/Sub. Redis documentation. Accessed 2026-11-16.
  2. [2]src/pubsub.c (channel and pattern tables, PUBLISH). Redis source repository. Accessed 2026-11-16.
  3. [3]Redis client handling. Redis documentation. Accessed 2026-11-16.
  4. [4]Redis serialization protocol specification. Redis documentation. Accessed 2026-11-16.
  5. [5]RFC 9293: Transmission Control Protocol (TCP). IETF. Accessed 2026-11-16.
  6. [6]redis.conf (self-documented default configuration). Redis source repository. Accessed 2026-11-16.
  7. [7]src/networking.c (output buffer limit log line). Redis source repository. Accessed 2026-11-16.
  8. [8]Redis administration. Redis documentation. Accessed 2026-11-16.
  9. [9]mm/oom_kill.c (OOM killer log line). Linux kernel source. Accessed 2026-11-16.
  10. [10]Redis Streams. Redis documentation. Accessed 2026-11-16.
  11. [11]Apache Kafka design (4.3). Apache Kafka documentation. Accessed 2026-11-16.
  12. [12]KafkaConsumer (Kafka 4.3 API). Apache Kafka documentation. Accessed 2026-11-16.
  13. [13]Introduction (Kafka 4.3). Apache Kafka documentation. Accessed 2026-11-16.