Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

redis-py raises TimeoutError while it waits for a pipeline or a blocking pop

Your error

redis.exceptions.TimeoutError: Timeout reading from localhost:63791 more line it can show

Fix it fast

Most common cause, 1 of 3

A blocking pop waits longer than the 5-second socket timeout

Give the worker that blocks its own client without a read limit, or with one longer than the block time.

Fix
import redis

worker = redis.Redis(host="localhost", port=6379, socket_timeout=None)
item = worker.blpop(["jobs"], timeout=30)
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

redis.exceptions.TimeoutError: Timeout reading from localhost:6379 means redis-py waited longer than socket_timeout for a reply. Since redis-py 8.0 that limit defaults to 5 seconds, so a BLPOP that blocks longer, a very large pipeline or a slow command on the server trips it. Raise socket_timeout above the longest wait, or split the pipeline. Applies to redis-py 8.x.

  • Applies to redis-py 8.1.0 (socket_timeout and socket_connect_timeout default to 5 s since 8.0.0), Python 3.11 to 3.15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Check which redis-py you run and whether the server logged slow commands. Version 8 or newer with an empty slowlog points to the client-side limit; long entries in the slowlog point to the server.

Run this first
python -m pip show redis | grep -i '^version'
redis-cli SLOWLOG GET 5

Then find the line you see in this list and open its fix:

  1. 1A blocking pop waits longer than the 5-second socket timeoutThe error arrives about 5 seconds into a BLPOP or BRPOP whose own timeout is longer, or 0
  2. 2One pipeline carries more replies than arrive within socket_timeoutThe error is raised from pipe.execute() on a bulk job, and smaller batches of the same commands succeed
  3. 3The server itself is busy with a slow commandSLOWLOG GET shows entries whose third value is in the millions of microseconds, often KEYS or a large DEL

What does the error look like?

  • redis.exceptions.TimeoutError: Timeout reading from localhost:6379
  • redis.exceptions.TimeoutError: Timeout reading from socket

Cause 1 of 3

A blocking pop waits longer than the 5-second socket timeout

You seeThe error arrives about 5 seconds into a BLPOP or BRPOP whose own timeout is longer, or 0

Check

Timeout reading from on a queue worker usually means the client gave up before the blocking command did. redis-py 8.0.0 changed socket_timeout and socket_connect_timeout to 5 seconds, and its release notes warn that a command blocking longer than the socket timeout may raise TimeoutError first. A BLPOP with timeout 30, or 0 for no limit, can then be cut off at 5 s.

Check
import redis
r = redis.Redis()
print(redis.__version__)
r.blpop(["jobs"], timeout=30)   # can raise after ~5 s on 8.x defaults

Fix

Give the worker that blocks its own client without a read limit, or with one longer than the block time. Keep the 5-second default on clients that serve web requests.

Fix
import redis

worker = redis.Redis(host="localhost", port=6379, socket_timeout=None)
item = worker.blpop(["jobs"], timeout=30)

Verify

On an empty list the call now returns None after the full 30 seconds instead of raising.

Verify
print(worker.blpop(["jobs"], timeout=30))

Confirmed by github.com

Did this fix it?

Cause 2 of 3

One pipeline carries more replies than arrive within socket_timeout

You seeThe error is raised from pipe.execute() on a bulk job, and smaller batches of the same commands succeed

Check

A pipeline can trip the same timeout because execute() sends every buffered command and then reads all the replies, so one huge pipeline is one long wait. Redis also has to hold all those replies in memory until the client reads them; its pipelining guide suggests batches of a reasonable size, such as 10,000 commands, then reading the replies before sending more.

Check
pipe = r.pipeline(transaction=False)
for key, value in items:          # how many items is this?
    pipe.set(key, value)
print(len(items))
pipe.execute()

Fix

Send the work in chunks, and give the bulk client a socket_timeout that fits one chunk.

Fix
bulk = redis.Redis(socket_timeout=30)
size = 10_000
for start in range(0, len(items), size):
    pipe = bulk.pipeline(transaction=False)
    for key, value in items[start:start + size]:
        pipe.set(key, value)
    replies = pipe.execute()

Verify

Each execute() returns a list with one reply per command in that chunk, and the job finishes.

Verify
print(len(replies))   # one reply per command in the last chunk

Confirmed by redis.io

Did this fix it?

Cause 3 of 3

The server itself is busy with a slow command

You seeSLOWLOG GET shows entries whose third value is in the millions of microseconds, often KEYS or a large DEL

Check

When every client times out at once, the delay is on the server. Redis runs a command on one thread and serves nobody else while it executes, and the slow log records exactly that execution time. A KEYS pattern over a large keyspace is the classic case; the Redis docs warn against using KEYS in application code.

Check
redis-cli SLOWLOG GET 10

Fix

Replace KEYS with incremental SCAN iteration in the code that called it, then watch the slow log again.

Fix
for key in r.scan_iter(match="session:*", count=1000):
    handle(key)

Verify

After a SLOWLOG RESET, no new multi-second entries appear while the job runs.

Verify
redis-cli SLOWLOG RESET
redis-cli SLOWLOG GET 10

Confirmed by redis.io

Did this fix it?

How do I stop it coming back?

  • Set socket_timeout explicitly on every redis.Redis() you create, so an upgrade of redis-py cannot change it behind your back.
  • Keep blocking pops on a separate worker client, and make their block time shorter than that client's socket_timeout.
  • Cap pipeline size in bulk scripts with a constant (for example 10,000 commands) instead of piping a whole dataset at once.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, stegnor.com, Menulo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.