Video in progress

Strong Middle ⚡ Redis and caching

The Redis log shows "scheduled to be closed ASAP for overcoming of output buffer limits". What is this, and how do you fix it?

Redis accumulates replies for each client in an output buffer. If a client reads slowly or a reply is huge, the buffer goes past the client-output-buffer-limit and Redis drops the connection. The culprit is usually a slow Pub/Sub subscriber or a replica.

📖 Answer

When Redis can't write a reply to the socket right away, it holds it in the client's output buffer. That buffer takes RAM and is limited depending on the client class:

# class   hard   soft  soft-seconds
client-output-buffer-limit normal  0     0     0
client-output-buffer-limit replica 256mb 64mb  60
client-output-buffer-limit pubsub  32mb  8mb   60

If it hits the hard limit, the connection is dropped immediately; if it stays above the soft limit for 60 seconds, it is dropped. These are the default values.

Three cases I've seen in prod:

  1. A slow Pub/Sub subscriber: the listener hits the DB on every message while the publisher sends thousands of messages per second. The buffer reaches 32 MB, the connection is dropped, and the messages in between are lost. The fix is to not do heavy work in the listener (drop the message into an internal queue and return right away), or to move to Streams.
  2. A replica during a full sync: while the master is sending the RDB, it accumulates new writes in the replica's buffer. With heavy writes, 256 MB isn't enough, the replica gets dropped and starts the full sync from scratch, in an endless loop. You need to raise the replica limit and repl-backlog-size.
  3. A normal client with a huge reply: there's no limit (0) for normal. If someone does a 2 GB HGETALL or KEYS *, Redis memory grows on that client's account. In Redis 7, the maxmemory-clients setting caps the memory of all clients.

Checking:

redis-cli CLIENT LIST | grep -E 'omem=[1-9]'   # omem is the output buffer size
redis-cli INFO clients                         # client_recent_max_output_buffer

Just raising the limit is a last resort. First find out why the client is reading slowly.