CLO-216: balancerd: enforce a max connection limit - #38436
Draft
jubrad wants to merge 1 commit into
Draft
Conversation
balancerd had no connection ceiling, so it accepted connections until the container hit its memory limit and was OOMKilled, dropping every established session rather than shedding only the load it could not serve. Add a `balancerd_max_connections` dyncfg (default 5000, 0 disables) enforced across the pgwire and HTTPS listeners by a shared limiter. Connections beyond the limit are refused with a fatal pgwire error (SQLSTATE 53300) or an HTTP 503, and the condition is visible through the new `mz_balancer_connection_rejected_total` and `mz_balancer_connection_limit` metrics plus a log line on entering and leaving the limited state. Closes: CLO-216 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
jubrad
force-pushed
the
justin/clo-216-balancerd-should-enforce-a-max-connection-limit-instead-of
branch
from
August 24, 2026 19:48
c66dc06 to
ddabe04
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue
balancerd has no connection ceiling. It accepts connections until the container hits its memory limit and is OOMKilled, which drops every established session instead of shedding only the load it cannot serve. A self-managed deployment at a 256Mi limit lost both replicas at ~400-500 concurrent connections; clients saw
the connection is lostandSSL error: unexpected eof, nothing pointing at the proxy.Linear: https://linear.app/materializeinc/issue/CLO-216/balancerd-should-enforce-a-max-connection-limit-instead-of-being
Solution
New
balancerd_max_connectionsdyncfg (default 5000,0disables), so it is tunable through LaunchDarkly like the otherbalancerd_*flags and settable at startup with--default-config=balancerd_max_connections=N. A singleConnectionLimiteris shared by the pgwire and HTTPS listeners, since memory is shared; the internal HTTP port stays unlimited so health and metrics keep working while the limit is in effect.Over the limit, pgwire gets a fatal
ErrorResponsewith SQLSTATE 53300 and HTTPS gets a raw 503. Two new metrics,mz_balancer_connection_rejected_totalandmz_balancer_connection_limit, pair with the existingmz_balancer_connection_activeto make the state diagnosable and give HPA a utilization signal (DEP-235). The log line fires on entering and leaving the limited state rather than per connection.The default of 5000 sits above cloud's HPA target of 2600 connections per pod, so cloud behavior is unchanged until the flag is set.
Testing
ConnectionLimiterunit test covers acquire/release, refusal at the limit, lowering the limit below the active count, and0. A newmax_connectionsmzcompose workflow intest/balancerd/mzcompose.pyruns balancerd with a limit of 1 and asserts the second connect fails with the server's message, that the established connection keeps working, and that both metrics are exported.Gotchas for the reviewer
max_connections.doc/user/data/metrics.ymlis regenerated bybin/lint, not hand-edited.🤖 Generated with Claude Code