|
| 1 | +.. _connection-pool-overview: |
| 2 | + |
| 3 | +======================== |
| 4 | +Connection Pool Overview |
| 5 | +======================== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +This document describes how to use a connection pool to manage |
| 16 | +connections between applications and MongoDB instances. |
| 17 | + |
| 18 | +What is a Connection Pool? |
| 19 | +-------------------------- |
| 20 | + |
| 21 | +Definition |
| 22 | +~~~~~~~~~~ |
| 23 | + |
| 24 | +A :ref:`connection pool <connection-pool-overview>` is a cache of open, |
| 25 | +ready-to-use database connections maintained by the :driver:`driver </>`. |
| 26 | +Your application can seamlessly get connections from the pool, perform |
| 27 | +operations, and return connections back to the pool. Connection pools |
| 28 | +are thread-safe. |
| 29 | + |
| 30 | +Benefits of a Connection Pool |
| 31 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 32 | + |
| 33 | +A connection pool helps reduce application latency and the number of |
| 34 | +times new connections are created. |
| 35 | + |
| 36 | +A connection pool creates connections at startup. Applications do not |
| 37 | +need to manually return connections to the pool. Instead, connections |
| 38 | +return to the pool automatically. |
| 39 | + |
| 40 | +Some connections are active and some are inactive but available. |
| 41 | +If your application requests a connection and there’s an available |
| 42 | +connection in the pool, a new connection does not need to be created. |
| 43 | + |
| 44 | +Create and Use a Connection Pool |
| 45 | +-------------------------------- |
| 46 | + |
| 47 | +Use an Instance of your Driver's ``MongoClient`` Object |
| 48 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 49 | + |
| 50 | +Most :driver:`drivers </>` provide an object of type ``MongoClient``. |
| 51 | + |
| 52 | +Use one ``MongoClient`` instance per application unless the |
| 53 | +application is connecting to many separate clusters. Each |
| 54 | +``MongoClient`` instance manages its own connection pool to the |
| 55 | +MongoDB cluster or node specified when the ``MongoClient`` is created. |
| 56 | +``MongoClient`` objects are thread-safe in most drivers. |
| 57 | + |
| 58 | +.. note:: |
| 59 | + |
| 60 | + Store your ``MongoClient`` instance in a place that is globally |
| 61 | + accessible by your application. |
| 62 | + |
| 63 | +Authentication |
| 64 | +~~~~~~~~~~~~~~ |
| 65 | + |
| 66 | +To use a connection pool with LDAP, see |
| 67 | +:ref:`LDAP Connection Pool Behavior <ldap-connection-pool-behavior>`. |
| 68 | + |
| 69 | +Sharded Cluster Connection Pooling |
| 70 | +---------------------------------- |
| 71 | + |
| 72 | +:binary:`~bin.mongos` routers have connection pools for each node in the |
| 73 | +cluster. The availability of connections to individual nodes within a |
| 74 | +sharded cluster affects latency. Operations must wait for a connection |
| 75 | +to be established. |
| 76 | + |
| 77 | +Connection Pool Configuration Settings |
| 78 | +-------------------------------------- |
| 79 | + |
| 80 | +To configure the connection pool, set the options: |
| 81 | + |
| 82 | +- through the :ref:`MongoDB URI <mongodb-uri>`, |
| 83 | + |
| 84 | +- programmatically when building the ``MongoClient`` instance, or |
| 85 | + |
| 86 | +- in your application framework's configuration files. |
| 87 | + |
| 88 | +Settings |
| 89 | +~~~~~~~~ |
| 90 | + |
| 91 | +.. list-table:: |
| 92 | + :widths: 25,75 |
| 93 | + :header-rows: 1 |
| 94 | + |
| 95 | + * - Setting |
| 96 | + - Description |
| 97 | + |
| 98 | + * - :urioption:`maxPoolSize` |
| 99 | + |
| 100 | + - .. _maxpoolsize-cp-setting: |
| 101 | + |
| 102 | + Maximum number of connections opened in the pool. When the |
| 103 | + connection pool reaches the maximum number of connections, new |
| 104 | + connections wait up until to the value of |
| 105 | + :urioption:`waitQueueTimeoutMS`. |
| 106 | + |
| 107 | + *Default:* ``100`` |
| 108 | + |
| 109 | + * - :urioption:`minPoolSize` |
| 110 | + |
| 111 | + - .. _minpoolsize-cp-setting: |
| 112 | + |
| 113 | + Minimum number of connections opened in the pool. |
| 114 | + The value of :urioption:`minPoolSize` must be less than |
| 115 | + the value of :urioption:`maxPoolSize`. |
| 116 | + |
| 117 | + *Default*: ``0`` |
| 118 | + |
| 119 | + * - :urioption:`connectTimeoutMS` |
| 120 | + |
| 121 | + - Most drivers default to never time out. Some versions of the |
| 122 | + Java drivers (for example, version 3.7) default to ``10``. |
| 123 | + |
| 124 | + *Default:* ``0`` for most drivers. See your :driver:`driver </>` |
| 125 | + documentation. |
| 126 | + |
| 127 | + * - :urioption:`socketTimeoutMS` |
| 128 | + |
| 129 | + - Number of milliseconds to wait before timeout on a TCP |
| 130 | + connection. |
| 131 | + |
| 132 | + Do *not* use :urioption:`socketTimeoutMS` as a mechanism for |
| 133 | + preventing long-running server operations. |
| 134 | + |
| 135 | + Setting low socket timeouts may result in operations that error |
| 136 | + before the server responds. |
| 137 | + |
| 138 | + *Default*: ``0``, which means no timeout. See your |
| 139 | + :driver:`driver </>` documentation. |
| 140 | + |
| 141 | + * - :urioption:`maxIdleTimeMS` |
| 142 | + |
| 143 | + - Amount of time that a connection can be idle in the pool before |
| 144 | + closing. Idle connections close until the number of |
| 145 | + open connections equals :urioption:`minPoolSize`. |
| 146 | + |
| 147 | + *Default:* See your :driver:`driver </>` documentation. |
| 148 | + |
| 149 | + * - :urioption:`waitQueueTimeoutMS` |
| 150 | + |
| 151 | + - Maximum wait time in milliseconds that a can thread wait for |
| 152 | + a connection to become available. A value of ``0`` means there |
| 153 | + is no limit. |
| 154 | + |
| 155 | + *Default*: ``0``. See your :driver:`driver </>` documentation. |
| 156 | + |
| 157 | + * - :parameter:`ShardingTaskExecutorPoolMinSize` |
| 158 | + |
| 159 | + - Minimum number of outbound connections each TaskExecutor |
| 160 | + connection pool can open to any given :binary:`~bin.mongod` |
| 161 | + instance. |
| 162 | + |
| 163 | + *Default*: ``1``. See |
| 164 | + :parameter:`ShardingTaskExecutorPoolMinSize`. |
| 165 | + |
| 166 | + This setting only applies to sharded deployments. |
| 167 | + |
| 168 | + * - :parameter:`ShardingTaskExecutorPoolMaxSize` |
| 169 | + |
| 170 | + - Maximum number of outbound connections each TaskExecutor |
| 171 | + connection pool can open to any given :binary:`~bin.mongod` |
| 172 | + instance. |
| 173 | + |
| 174 | + *Default*: 2\ :sup:`64` - 1. See |
| 175 | + :parameter:`ShardingTaskExecutorPoolMaxSize`. |
| 176 | + |
| 177 | + This setting only applies to sharded deployments. |
| 178 | + |
| 179 | +.. toctree:: |
| 180 | + :titlesonly: |
| 181 | + :hidden: |
| 182 | + |
| 183 | + /tutorial/connection-pool-performance-tuning |
0 commit comments