- Tutorials >
- Server Selection and Monitoring
Server Selection and Monitoring
On this page
Server Selection and Monitoring
Before any operation can be executed, the MongoDB PHP Library must first select a
server from the topology (e.g. replica set, sharded cluster). Selecting a server
requires an accurate view of the topology, so the driver (i.e. mongodb
extension) regularly monitors the servers to which it is connected.
In most other drivers, server discovery and monitoring is handled by a background thread; however, the PHP driver is single-threaded and must therefore perform monitoring between operations initiated by the application.
Consider the following example application:
Although the application consists of only a few lines of PHP, there is actually quite a lot going on behind the scenes! Interested readers can find this process discussed in greater detail in the following documents:
- Single-threaded Mode in the libmongoc documentation
- Server Discovery and Monitoring specification
- Server Selection specification
Connection String Options
There are several connection string options relevant to server selection and monitoring.
connectTimeoutMS
connectTimeoutMS
specifies the limit for both establishing a connection to
a server and the socket timeout for server monitoring (hello
commands).
This defaults to 10 seconds for single-threaded drivers such as PHP.
When a server times out during monitoring, it will not be re-checked until at
least five seconds
(cooldownMS)
have elapsed. This timeout is intended to avoid having single-threaded drivers
block for connectTimeoutMS
on each subsequent scan after an error.
Applications can consider setting this option to slightly more than the greatest
latency among servers in the cluster. For example, if the greatest ping
time
between the PHP application server and a database server is 200ms, it may be
reasonable to specify a timeout of one second. This would allow ample time for
establishing a connection and monitoring an accessible server, while also
significantly reducing the time to detect an inaccessible server.
heartbeatFrequencyMS
heartbeatFrequencyMS
determines how often monitoring should occur. This
defaults to 60 seconds for single-threaded drivers and can be set as low as
500ms.
serverSelectionTimeoutMS
serverSelectionTimeoutMS
determines the maximum amount of time to spend in
the server selection loop. This defaults to 30 seconds, but applications will
typically fail sooner if serverSelectionTryOnce
is true
and a smaller
connectTimeoutMS
value is in effect.
The original default was established at a time when replica set elections took
much longer to complete. Applications can consider setting this option to
slightly more than the expected completion time for an election. For example,
Replica Set Elections states that
elections will not typically exceed 12 seconds, so a 15-second timeout may be
reasonable. Applications connecting to a sharded cluster may consider a smaller
value still, since mongos
insulates the driver from elections.
That said, serverSelectionTimeoutMS
should generally not be set to a value
smaller than connectTimeoutMS
.
serverSelectionTryOnce
serverSelectionTryOnce
determines whether the driver should give up after
the first failed server selection attempt or continue waiting until
serverSelectionTimeoutMS
is reached. PHP defaults to true
, which allows
the driver to “fail fast” when a server cannot be selected (e.g. no primary
during a failover).
The default behavior is generally desirable for a high-traffic web applications, as it means the worker process will not be blocked in a server selection loop and can instead return an error response and immediately go on to serve another request. Additionally, other driver features such as retryable reads and writes can still enable applications to avoid transient errors such as a failover.
That said, applications that prioritize resiliency over response time (and
worker pool utilization) may want to specify false
for
serverSelectionTryOnce
.
socketCheckIntervalMS
socketCheckIntervalMS
determines how often a socket should be checked (using
a ping
command) if it has not been used recently. This defaults to 5 seconds
and is intentionally lower than heartbeatFrequencyMS
to better allow
single-threaded drivers to recover dropped connections.
socketTimeoutMS
socketTimeoutMS
determines the maximum amount of time to spend reading or
writing to a socket. Since server monitoring uses connectTimeoutMS
for its
socket timeouts, socketTimeoutMS
only applies to operations executed by the
application.
socketTimeoutMS
defaults to 5 minutes; however, it’s likely that a PHP web
request would be terminated sooner due to
max_execution_time,
which defaults to 30 seconds for web SAPIs. In a CLI environment, where
max_execution_time
is unlimited by default, it is more likely that
socketTimeoutMS
could be reached.
Note
socketTimeoutMS
is not directly related to server selection and
monitoring; however, it is frequently associated with the other options and
therefore bears mentioning.