Skip to content

Allow capping the redis connection pool - #7307

Merged
masenf merged 5 commits into
reflex-dev:mainfrom
flcrom:redis-pool-cap
Sep 30, 2026
Merged

masenf merged 5 commits into
reflex-dev:mainfrom
flcrom:redis-pool-cap

Conversation

@flcrom

@flcrom flcrom commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Refs #7179

get_redis() uses a bare Redis.from_url, so the pool can grow to whatever peak concurrency a worker ever sees and then keeps those sockets open. Setting max_connections in the URL does cap it, but from_url builds a plain ConnectionPool, which raises MaxConnectionsError at the cap instead of waiting.

This adds an opt-in cap. When REFLEX_REDIS_MAX_CONNECTIONS is set, get_redis() builds a BlockingConnectionPool of that size, so callers wait for a free connection (up to REFLEX_REDIS_POOL_TIMEOUT, default 2s) instead of failing. Nothing changes unless you set it.

A few notes:

  • The default stays unbounded, and the state manager and token manager still get separate pools. I left a default bound and a shared pool for maintainers to decide. Their close() paths currently disagree on who owns the pool.
  • Each pub/sub listener holds a connection for good (2 on the token manager, 1 for the state manager's lock subscriber), so the cap needs some headroom.
  • The cap applies to each client: the state manager, the token manager and the health check each have their own pool.
  • A cap below 3 raises EnvironmentVarValueError at startup, so the token manager keeps one connection free for commands besides its two listeners.
  • REFLEX_REDIS_POOL_TIMEOUT must be above 0 and below redis_lock_expiration (default 10s), so a state update cannot wait on the pool past its lock expiry. A zero wait would fail at the cap, like the plain pool.
  • get_redis_sync() stays uncapped. It's only used for a one-off ping.

Tested with the repro from the issue against fakeredis with some added latency: unset grows to 101 connections, ?max_connections=10 drops 91 of 100 ops, REFLEX_REDIS_MAX_CONNECTIONS=10 holds at 10 with no failures. Unit tests, ruff and a scoped pyright run pass locally. Full pyright ran out of memory on my machine, and I didn't run the redis-backed tests against a real server.

Review in cubic

AI help: used for parts of this change.

@flcrom
flcrom requested a review from a team as a code owner September 24, 2026 10:16
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Adds Redis connection pool configuration to runtime.

The PR appears safe to merge based on the reviewed changes and resolved prior findings.

Summary

Adds an opt-in blocking connection pool for asynchronous Redis clients, with a configurable connection cap and wait timeout. The uncapped behavior remains the default, and tests cover pool selection, validation, and waiting for a released connection.

Reviews (5) · Last reviewed commit: "Merge branch 'main' into redis-pool-cap"

Comment thread reflex/utils/prerequisites.py Resolved
Comment thread reflex/utils/prerequisites.py Resolved
Comment thread reflex/utils/prerequisites.py Resolved
@codspeed

codspeed Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 147 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing flcrom:redis-pool-cap (782bdb2) with main (47187ef)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 5 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread reflex/utils/prerequisites.py Resolved
Comment thread reflex/utils/prerequisites.py Resolved
Comment thread reflex/utils/prerequisites.py Resolved
flcrom and others added 2 commits September 25, 2026 14:47
Move the pool cap checks into _redis_pool_limits so get_redis stays flat.
A zero pool timeout made a saturated pool fail at once, which is the
behavior the blocking pool replaces. Clarify in the news fragments that
the cap applies to each Redis client.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files (changes from recent commits).

You’re at about 91% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact [email protected].

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

@masenf masenf added the on deck PRs lined up to review / merge next label Sep 30, 2026
@masenf
masenf merged commit d8b3cf1 into reflex-dev:main Sep 30, 2026
146 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants