{"id":"57046e591828e1bb","repo":"redis/redis-py","slug":"too-many-connections-57046e","errorCode":null,"errorMessage":"Too many connections","messagePattern":"Too many connections","errorType":"exception","errorClass":"MaxConnectionsError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":3321,"sourceCode":"                connection_pool=self,\n                duration_seconds=time.monotonic() - start_time_created,\n            )\n\n        return connection\n\n    def get_encoder(self) -> Encoder:\n        \"Return an encoder based on encoding settings\"\n        kwargs = self.connection_kwargs\n        return Encoder(\n            encoding=kwargs.get(\"encoding\", \"utf-8\"),\n            encoding_errors=kwargs.get(\"encoding_errors\", \"strict\"),\n            decode_responses=kwargs.get(\"decode_responses\", False),\n        )\n\n    def make_connection(self) -> \"ConnectionInterface\":\n        \"Create a new connection\"\n        if self._created_connections >= self.max_connections:\n            raise MaxConnectionsError(\"Too many connections\")\n        self._created_connections += 1\n\n        kwargs = dict(self.connection_kwargs)\n\n        # Create the connection first, then record metrics only on success\n        if self.cache is not None:\n            connection = CacheProxyConnection(\n                self.connection_class(**kwargs), self.cache, self._lock\n            )\n        else:\n            connection = self.connection_class(**kwargs)\n\n        # Record new connection created (starts as IDLE) - only after successful construction\n        record_connection_count(\n            pool_name=get_pool_name(self),\n            connection_state=ConnectionState.IDLE,\n            counter=1,\n        )","sourceCodeStart":3303,"sourceCodeEnd":3339,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/connection.py#L3303-L3339","documentation":"Raised as MaxConnectionsError (a ConnectionError subclass, exceptions.py:282-288) by ConnectionPool.make_connection (connection.py:3318-3321) when the number of connections already created (_created_connections) has reached max_connections and a new one is requested. This is client-side pool exhaustion — the cap was hit, not a server refusal.","triggerScenarios":"Issuing more concurrent operations than max_connections allows with the default (non-blocking) ConnectionPool, or leaking connections (failing to release them) until the cap is reached. Each get_connection that finds no free connection calls make_connection, which raises when the cap is exceeded.","commonSituations":"High-concurrency workloads with max_connections set too low; connection leaks from unhandled exceptions in pipeline/transaction code; long-running blocking commands (BLPOP, MONITOR) tying up connections; forking without _checkpid reset; threads each opening many connections.","solutions":["Raise max_connections to match your real concurrency: redis.ConnectionPool(max_connections=200).","Use BlockingConnectionPool if you want callers to wait for a free connection instead of failing immediately.","Audit for connection leaks — ensure every borrowed connection is released; prefer the high-level client/pipeline API which manages release automatically.","Avoid holding connections across long blocking commands; use dedicated connections for MONITOR/blocking ops."],"exampleFix":"# before\npool = redis.ConnectionPool(max_connections=10)\n# 11th concurrent op raises MaxConnectionsError: Too many connections\n\n# after\npool = redis.BlockingConnectionPool(max_connections=50, timeout=5)\n# callers wait up to 5s for a free connection instead of failing","handlingStrategy":"retry","validationCode":"def pool_with_headroom(estimated_concurrency: int) -> redis.ConnectionPool:\n    cap = max(estimated_concurrency * 2, 50)\n    return redis.ConnectionPool(max_connections=cap)\n\nclient = redis.Redis(connection_pool=pool_with_headroom(my_thread_count))","typeGuard":null,"tryCatchPattern":"from redis.exceptions import MaxConnectionsError\nfor _ in range(3):\n    try:\n        return client.get(\"k\")\n    except MaxConnectionsError:\n        time.sleep(0.2)\nraise MaxConnectionsError(\"pool exhausted after retries\")","preventionTips":["Size max_connections to at least 1.5-2x peak concurrency.","Use BlockingConnectionPool to make callers wait instead of failing on exhaustion.","Audit for connection leaks; prefer the high-level client API which releases automatically.","Use dedicated connections for long-running blocking commands."],"tags":["connection-pool","resource-limits","configuration","connection"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}