MongoDB's PHP driving methods and techniques

  • 2020-05-06 11:58:04
  • OfStack

index creation sometimes blocks new connections

When a connection is made to the mongodb replica set, the driver first attempts to connect and validate each non-hidden node in the cluster. If a node is in an "down" state, it is skipped. However, if a node is in an "UP" state, but holds a write lock, the validation will not work, so the driver will be suspended.

Common errors in indexing prior to mongodb 2.6. All indexes are created in the foreground or background, and will be created in the foreground on the secondary side.

Version 1.5.3 of the PHP driver will be improved to allow authentication to second nodes when creating indexes.

reduces is_master_interval by

For applications with high availability requirements, it is recommended to check the default driver runtime configuration Settings.

The mongo.is_master_interval option controls how quickly the driver recovers when the replica set is re-selected.

The is_master_interval option defaults to 15s and sets the time interval at which the driver sends an "isMaster" request per mongod instance. These requests help the driver determine the topology of the replica set, specifically, the request detects which node is primary and can receive writes.

It is recommended that this value be set to 1 or 2 seconds so that the driver can quickly locate the primary node during cluster election or failover. Of course, it also depends on how many clients there are and the frequency of ping.

Note that when an primary node changes, such as an election or failover, there are always a few seconds when the driver receives an "MongoConnectionException" message "No candidate servers found". These exceptions need to be handled in your code or the application will be terminated.

understands connection handling and configures connection TimeoutMS

The PHP driver does not use connection pooling. Therefore, it is recommended that you create one connection per PHP process. However, if the web application has many PHP worker processes, many new database connections will be created, and the PHP driver cannot share connections between processes. Therefore, when the network node is slow and the server is busy, the PHP application is particularly vulnerable when creating an initial database connection.

In this case, it is recommended that you customize the connectionTimeoutMS option and pay attention to the down.ping_interval option in php.ini.

connectionTimeoutMS

The PHP driver does not display the definition of a default connection timeout. Instead, the default value is determined by the default_socket_timeout option in the php.ini file, and the default is 60 seconds. The connection will wait 60 seconds to disconnect, which is a bit long and needs to be lowered.

It is strongly recommended to set the connectionTimeoutMS option by displaying it in the URI option of the connection string. Set it to a value between 5 and 30 seconds.

mongo.ping_interval

Es128en.ping_interval has a default value of 5 seconds. This option sets the time interval at which the driver sends an ping request to each mongod instance to find an "down" node, which is used to track the driver's server blacklist. Tell the driver which nodes to ignore.


Related articles: