{"record":{"id":"600f05615dacc0ac","repo":"redis/redis-py","slug":"too-many-connections","errorCode":null,"errorMessage":"Too many connections","messagePattern":"Too many connections","errorType":"exception","errorClass":"MaxConnectionsError","httpStatus":null,"severity":"error","filePath":"redis/asyncio/connection.py","lineNumber":2847,"sourceCode":"\n            if is_created:\n                await record_connection_create_time(\n                    connection_pool=self,\n                    duration_seconds=time.monotonic() - start_time_created,\n                )\n\n            return connection\n        except BaseException:\n            await self.release(connection)\n            raise\n\n    def get_available_connection(self):\n        \"\"\"Get a connection from the pool, without making sure it is connected\"\"\"\n        try:\n            connection = self._available_connections.pop()\n        except IndexError:\n            if len(self._in_use_connections) >= self.max_connections:\n                raise MaxConnectionsError(\"Too many connections\") from None\n            connection = self.make_connection()\n        self._in_use_connections.add(connection)\n        return connection\n\n    def get_encoder(self):\n        \"\"\"Return an encoder based on encoding settings\"\"\"\n        kwargs = self.connection_kwargs\n        return self.encoder_class(\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):\n        \"\"\"Create a new connection.  Can be overridden by child classes.\"\"\"\n        # Note: We don't record IDLE here because async uses a sync make_connection\n        # but async record_connection_count. The recording is handled in get_connection.\n        return self.connection_class(**self.connection_kwargs)","sourceCodeStart":2829,"sourceCodeEnd":2865,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/asyncio/connection.py#L2829-L2865","documentation":"The non-blocking ConnectionPool raises MaxConnectionsError (a ConnectionError subclass) when get_available_connection finds the free list empty and the in-use count has reached max_connections. Unlike BlockingConnectionPool it does not wait. The most common cause is connections not being returned, but it also fires under genuine load spikes.","triggerScenarios":"Every connection is in use (len(_in_use_connections) >= max_connections) and none are idle when the pool is asked for another connection; e.g. concurrency exceeds the cap, a command holds a connection for a long time, or a leak never releases.","commonSituations":"Long-running commands blocking connections; missing await client.aclose()/release; pubsub or monitor connections counted against the pool; load spikes with a too-small pool.","solutions":["Increase max_connections to match your real concurrency.","Ensure every acquired connection is released (use 'async with redis:' or try/finally), and look for leaks via pool metrics.","Switch to BlockingConnectionPool so callers wait instead of erroring.","Shorten long-running commands or move pubsub to a dedicated client/pool."],"exampleFix":"# before\npool = ConnectionPool(host='redis.local', max_connections=10)\n# after\npool = BlockingConnectionPool(host='redis.local', max_connections=50, timeout=10)","handlingStrategy":"retry","validationCode":"if pool.get_connection_count() and all_in_use:\n    # raise capacity or shed load before calling\n    ...","typeGuard":null,"tryCatchPattern":"from redis.exceptions import MaxConnectionsError\nfor attempt in range(retries):\n    try:\n        return await client.get(key)\n    except MaxConnectionsError:\n        await asyncio.sleep(backoff(attempt))\nraise","preventionTips":["Size max_connections to peak concurrency and release connections promptly.","Consider BlockingConnectionPool when bursts are expected.","Monitor in-use vs idle connection counts."],"tags":["connection-pool","capacity","runtime","async"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}