Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .ai/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,10 @@ multi-stream `XREAD`; a new subscription interrupts it through a private wake
stream. Never park a blocking read in the executor per subscription: at a dozen
open downlinks that starved every other store call in the worker (25-50% failed
streams at 25 browsers on uvicorn `--workers 4`). Diskcache's async
subscriptions likewise poll without blocking and sleep on the loop.
subscriptions likewise poll without blocking and sleep on the loop. Each loop's
client uses a `BlockingConnectionPool` of 100 that waits up to 20s for a free
connection: redis-py 8 caps a plain pool at 100 and raises past it, which
failed 7% of streams at 2000 browsers on 4 uvicorn workers.

### Deployment Topology and Shared Storage

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to `dash` will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [UNRELEASED]

### Fixed
- [#4185](https://github.com/plotly/dash/pull/4185) Fix `RedisSharedStorage` streaming callbacks failing under load with redis-py 8 (`MaxConnectionsError: Too many connections`): async calls now wait for a free connection.

## [4.5.1] - 2026-11-19

### Removed
Expand Down
35 changes: 25 additions & 10 deletions dash/_shared_storage/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
_READER_BLOCK_MS = 5000
_WAKE_TTL_MS = 60_000
_DEFAULT_URL = "redis://localhost:6379"
# Connections per event loop's async client, and how long a call waits for a
# free one before it fails.
_ASYNC_POOL_SIZE = 100
_ASYNC_POOL_WAIT = 20.0

# A topic's keys live this many ttls past its last publish or poll. A poll on a
# topic with a ttl blocks for at most a third of that lifetime, so a waiting
Expand Down Expand Up @@ -431,18 +435,29 @@ def _new_async_client(self) -> Any:
_require_redis()
from redis import asyncio as aredis # type: ignore[import-not-found,import-untyped] # pylint: disable=import-outside-toplevel,import-error

# redis-py 8 caps a pool at 100 connections and raises past that, so a
# burst of pumps publishing at once failed. A blocking pool waits for a
# free connection instead, and keeps the count Redis sees bounded.
limits = {
"max_connections": _ASYNC_POOL_SIZE,
"timeout": _ASYNC_POOL_WAIT,
}
if self._url is not None:
return aredis.Redis.from_url(self._url)
# Same server and options as the client we were handed.
pool = self._redis.connection_pool
connection_class = getattr(
aredis.connection, pool.connection_class.__name__, aredis.Connection
)
return aredis.Redis(
connection_pool=aredis.ConnectionPool(
connection_class=connection_class, **pool.connection_kwargs
pool = aredis.BlockingConnectionPool.from_url(self._url, **limits)
else:
# Same server and options as the client we were handed.
sync_pool = self._redis.connection_pool
connection_class = getattr(
aredis.connection,
sync_pool.connection_class.__name__,
aredis.Connection,
)
)
pool = aredis.BlockingConnectionPool(
connection_class=connection_class,
**limits,
**sync_pool.connection_kwargs,
)
return aredis.Redis(connection_pool=pool)

def _loop_state(self) -> "_LoopState":
loop = asyncio.get_running_loop()
Expand Down
23 changes: 23 additions & 0 deletions tests/shared_storage/test_redis_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,29 @@ async def scenario():
client.close()


@pytest.mark.parametrize("passed_client", [False, True])
def test_async_burst_larger_than_the_pool_waits_for_connections(passed_client):
"""redis-py 8 caps a pool at 100 connections and raises past it. A burst of
pumps publishing at once must wait for a connection, not fail."""
client = redis.Redis.from_url(REDIS_URL) if passed_client else None
store = RedisSharedStorage(
url=None if passed_client else REDIS_URL,
client=client,
key_prefix=f"dash:sstest:{uuid.uuid4().hex[:12]}",
)
store.start()

async def scenario():
await asyncio.gather(*(store.apublish("burst", i) for i in range(500)))
await asyncio.gather(*(store.aset(f"k{i}", i) for i in range(500)))

asyncio.run(scenario())
assert store.get("k499") == 499
store.close()
if client is not None:
client.close()


@pytest.mark.parametrize("flavor", ["sync", "async"])
def test_quiet_topic_outlasts_the_socket_timeout(flavor):
"""redis-py 8 times sockets out after 5 s, as long as an XREAD block: a
Expand Down
Loading