MongoDB's PHP Drive method and Techniques _mongodb

Source: Internet
Author: User
Tags configuration settings connection pooling failover mongodb time interval

Creating an index sometimes blocks new connections

When a connection is established with the MongoDB replica set, the driver first attempts to connect and validate each of the nodes that are not hidden in the cluster. If a node is a "down" state, it is skipped. However, if a node is a "up" state but holds a write lock, the validation will not proceed, so the driver will be suspended.

A common problem with indexing is before the MongoDB 2.6 version. All the indexes are created in the foreground or backstage and will be created in the foreground at the secondary end.

The 1.5.3 version of the PHP driver is improved, allowing validation to second node validation when creating an index.

Reduce Is_master_interval value

For applications with high availability requirements, it is recommended that you check the configuration settings for the default driver runtime.

The Mongo.is_master_interval option controls how fast recovery can be driven when the replication set is re-election.

The Is_master_interval option defaults to 15s, setting the time interval for the driver to send a "ismaster" request for each Mongod instance. These requests help the driver determine the topology of the replica set, specifically the request to detect which node is primary and can receive write operations.

It is recommended that this value be set to 1 or 2 seconds to enable the driver to quickly navigate to the primary node in the cluster election or failover. Of course, it also depends on how many clients and how often the ping is.

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

Understand connection handling and configure connection Timeoutms

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

In this case, it is recommended that you customize the CONNECTIONTIMEOUTMS option and note the Mongo.ping_interval option in php.ini.

Connectiontimeoutms

The PHP driver does not show the definition of a default connection timeout. Instead, the default value is determined by the Default_socket_timeout option in the php.ini file, which defaults to 60 seconds. The connection will wait 60 seconds to disconnect, a little longer and need to be lowered.

It is strongly recommended that you display the Set CONNECTIONTIMEOUTMS option in the URI option of the connection string. Set it to a value between 5-30 seconds.

Mongo.ping_interval

The default value for Mongo.ping_interval is 5 seconds. This option sets the time interval for the driver to send ping requests to each Mongod instance to discover the "down" node, which is used to track the server blacklist of the driver. Tell the driver which nodes are ignored.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.