Client Buffers

Difficulty: Advanced

Question

What happens when a client's output buffer fills up in Redis, which clients are most affected, and how do you tune and monitor it?

Answer

Redis keeps an output buffer per client for replies that have been generated but not yet sent over the socket. If that buffer exceeds the limit set by client-output-buffer-limit, Redis closes the client's connection to protect server memory. This most often hits Pub/Sub subscribers that cannot keep up and replicas that fall behind during a full sync, and in the replica case it can cause an endless resync loop.

How the limits work

The setting has the form client-output-buffer-limit <class> <hard> <soft> <soft-seconds>:

  • Hard limit: if the buffer reaches it, the client is disconnected immediately.
  • Soft limit: if the buffer stays above it continuously for soft-seconds, the client is disconnected.
  • 0 disables a limit.

The classes and their defaults:

ClassDefaultWhy
normal0 0 0 (no limit)Normal clients request replies and read them, so buffers stay bounded
replica256mb 64mb 60Replicas receive the write stream during and after sync
pubsub32mb 8mb 60Publishers can push faster than a subscriber reads

When a client is dropped, the server log shows a line saying it was scheduled to be closed for overcoming output buffer limits.

Who gets hit and why

  1. Slow Pub/Sub subscribers: Pub/Sub is fire-and-forget. If a publisher sends order events faster than a subscriber processes them, messages pile up in that subscriber's buffer. Once past 32 MB, or above 8 MB for 60 seconds, the subscriber is disconnected and misses everything in between.
  2. Replicas during full sync: while the primary streams the RDB, new writes are buffered for the replica. On a write-heavy primary with a large dataset, the buffer can exceed 256 MB before the replica finishes loading. The primary drops the replica, the replica reconnects, asks for another full sync, and the cycle repeats.
  3. Normal clients with huge replies: KEYS *, HGETALL on a giant hash, or LRANGE 0 -1 on a long list can create very large buffers. With the default normal limit of 0, nothing stops this, so it shows up as memory spikes instead.

The opposite direction exists too: the query buffer holds incoming, not-yet-parsed commands. Its limit is client-query-buffer-limit (default 1 GB).

Monitoring

  • CLIENT LIST shows per-client omem (output buffer memory), qbuf (query buffer), tot-mem and flags (P for pubsub, S for replica).
  • INFO clients shows client_recent_max_output_buffer and client_recent_max_input_buffer.
  • INFO replication and the logs reveal repeated full syncs.

Fixes and tuning

  • For replicas, raise the replica limit, for example 512mb 256mb 120, sized to cover your write rate times the time a full sync takes. Also raise repl-backlog-size so brief disconnects resume with a partial sync.
  • For Pub/Sub, make subscribers faster, add more subscribers per channel pattern, or switch to Redis Streams, where messages are stored and a slow consumer catches up instead of being dropped.
  • Avoid huge replies; use SCAN, HSCAN and paged LRANGE instead.
  • Since Redis 7.0, maxmemory-clients can cap the total memory used by all client connections and evict the biggest clients first.
  • Client buffers count toward Redis memory usage, so an unchecked buffer can push the server into eviction or OOM.
Replica full-sync buffer loop. If the replica buffer limit is too small, full syncs can repeat forever.
  1. Replica requests full sync: Primary starts streaming the RDB.
  2. Writes keep arriving: They are buffered for the replica.
  3. Buffer passes 256 MB: Hard limit for the replica class.
  4. Primary drops the replica: Sync is abandoned.
  5. Replica reconnects: Requests another full sync, loop repeats.
  6. Fix: Raise replica limit and backlog size.

Code examples

Inspecting and tuning buffer limits

# Current limits for all classes
redis-cli CONFIG GET client-output-buffer-limit

# Give replicas more room during full sync
redis-cli CONFIG SET client-output-buffer-limit 'replica 512mb 256mb 120'

# Pub/Sub subscribers: allow larger bursts
redis-cli CONFIG SET client-output-buffer-limit 'pubsub 64mb 16mb 60'

# Find clients with large output buffers (omem in bytes)
redis-cli CLIENT LIST | awk '{for(i=1;i<=NF;i++) if($i ~ /^(id|addr|flags|omem)=/) printf "%s ", $i; print ""}'

# Recent peaks
redis-cli INFO clients | grep recent_max

CONFIG GET reports the replica class as slave and values in bytes. Here a pubsub client (flags=P) has 7 MB buffered, close to the 8 MB soft limit.

Key points

  • Output buffers hold replies not yet sent; exceeding the limit disconnects the client.
  • Hard limit disconnects immediately; soft limit disconnects after staying above it for N seconds.
  • Defaults: normal 0 0 0, replica 256mb 64mb 60, pubsub 32mb 8mb 60.
  • Slow Pub/Sub subscribers lose messages when disconnected; Streams avoid this.
  • Too-small replica limits can cause endless full-sync loops.
  • Monitor omem in CLIENT LIST and client_recent_max_output_buffer in INFO clients.

Common follow-up questions

  • Why does the normal client class have no limit by default?
  • How would you size the replica output buffer for a 30 GB dataset?
  • Why do Redis Streams handle slow consumers better than Pub/Sub?
  • What does maxmemory-clients do and when would you set it?

Interview tips

  • Explain hard vs soft limits precisely; it shows you have read the config, not just the headline.
  • The replica resync loop is a great real-world example that impresses interviewers.
  • Connect the Pub/Sub case to its fire-and-forget design and suggest Streams as the fix.

Concepts covered

client-output-buffer-limit, Pub/Sub Slow Consumers, Replica Output Buffer, Query Buffer, CLIENT LIST

More Redis & Caching questions