{"record":{"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":3336,"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":3318,"sourceCodeEnd":3354,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/connection.py#L3318-L3354","documentation":"Raised as MaxConnectionsError('Too many connections') by ConnectionPool.make_connection when the number of connections already created (_created_connections) has reached max_connections. MaxConnectionsError subclasses ConnectionError. This is the bounded (default) ConnectionPool refusing to open a new socket beyond its cap — it does not block; the caller sees the exception immediately.","triggerScenarios":"Borrowing more than max_connections connections concurrently from a single ConnectionPool without releasing them. Long-held connections (e.g. pubsub, blocking BLPOP, MONITOR) accumulating against a small pool. Connection leak from not releasing.","commonSituations":"Default max_connections=100 exceeded under heavy concurrency or due to leaked connections. Blocking commands (BLPOP, WAIT) pinning connections. Pubsub/monitor listeners holding many. Forked workers each inheriting a full pool.","solutions":["Raise max_connections to match your expected concurrency.","Ensure connections are always released — use context managers or let the client manage the pool (Redis.execute_command returns the connection automatically).","Reduce the number of concurrently held connections (fewer pubsub listeners, shorter blocking commands, bounded thread/async pools).","If you want blocking behaviour instead of an immediate error, use BlockingConnectionPool."],"exampleFix":"# before\npool = ConnectionPool(max_connections=10)  # raises under load\n# after\npool = ConnectionPool(max_connections=50)\n# or block-wait instead of erroring\nfrom redis.connection import BlockingConnectionPool\npool = BlockingConnectionPool(max_connections=50, timeout=10)","handlingStrategy":"retry","validationCode":"from redis.connection import BlockingConnectionPool\n\ndef build_pool(max_connections, blocking=True, timeout=10):\n    # Prefer a blocking pool if you want to wait rather than error at the cap.\n    cls = BlockingConnectionPool if blocking else ConnectionPool\n    return cls(max_connections=max_connections, timeout=timeout) if blocking else cls(max_connections=max_connections)","typeGuard":null,"tryCatchPattern":"import time\nfrom redis.exceptions import MaxConnectionsError\n\nfor attempt in range(4):\n    try:\n        return r.get('key')\n    except MaxConnectionsError:\n        if attempt < 3:\n            time.sleep(0.1 * (2 ** attempt))\n            continue\n        raise  # pool is genuinely saturated; raise so callers can shed load","preventionTips":["Size max_connections to your real concurrency.","Always release connections (let the Redis client manage the pool).","Keep blocking commands/pubsub on a separate, adequately sized pool.","Use BlockingConnectionPool if you prefer waiting over immediate failure."],"tags":["connection-pool","resource-limits","configuration"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}