This article mainly explains the impact of replica set architecture on program development from the following aspects
The author divides the application development based on mongodb into two stages: software operation and maintenance and software development
This article will focus on the practical experience sharing in the later stage
- Understanding replica set and master-slave structure
- Follow database connection string
Basic concepts of replica set
When it comes to replica set, it is a set of replicas, one of the basic properties of distributed system.
Replica is one of the most common concepts of distributed system, which refers to a redundant way of data and services provided by distributed system. In the common distributed system, in order to provide highly available services, we often deal with data and services by replica. The concept of replica is related to the consistency of replica data.
In mongodb environment, it’s easy for us to find the basic meaning of the replica set from the public data
Mongodb replication (replica set) mongodb replication is the process of synchronizing data on multiple servers.
Replication provides redundant backup of data and stores data copies on multiple servers, which improves data availability and ensures data security.
Replication also allows you to recover data from hardware failures and service outages
Replica set roles
Mongodb replica set is a set of mongodb processes, which achieves high availability through a series of mechanisms.
mongod is the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations.
Replica set roles mainly include master node, slave node, and arbitration node. The master node is responsible for all writes.
The relationship between replica set and master-slave structure
Both belong to different mongodb database structures, and the master-slave structure has not been officially recommended
Deprecated since version 3.6: MongoDB 3.6 deprecates the use of
master-slave replication.Master-slave replication has been deprecated
for components of sharded clusters since MongoDB 3.2.
The main difference between the master-slave structure and the replica set is that the former has the concept of a real master server, while the replica set does not have the concept of a master server. The primary node is selected by election
Do not run an arbiter on systems that also host the primary or the secondary members of the replica set
The arbiter must be deployed on a separate server to avoid deployment with the primary and replication nodes
Traditional master-slave deployment
This is Mongo’s official more recommended deployment structure
Follow database connection string
Replica set is a deployment method of mongodb high availability architecture. It should be extremely transparent to the application using database. The internal master-slave instance switch should be senseless. That is to say, the application does not care about the specific database operation and which instance is to handle it.
We can specify how to connect the replica set in the program, and the correct connection mode can avoid the application program’s normal switch without awareness when the primary node goes down or fails, which is illustrated by a graph of the network
In fact, there is a problem in the left part of the figure. Connect an instance directly and indicate the primary. For the driver connecting DB database, it should not care which instance is the primary and which instance is the primary will change.
Imagine that O & M provides a set of configuration for the database connection string to be developed. How can we integrate it into the application?
For PHP applications, yii2 is used as the framework and yii2 mongodb is used as the driver
Either run php composer.phar require --prefer-dist yiisoft/yii2-mongodb or add "yiisoft/yii2-mongodb": "~2.1.0"
- Connection string
return [ 'class' => '\yii\mongodb\Connection', 'dsn' => 'mongodb://username:[email protected]:10000/databasename', 'options'=>[ 'socketTimeoutMS' => 1000 ] ];
Copy set connection
return [ 'class' => '\yii\mongodb\Connection', 'dsn' => 'mongodb://user:[email protected]_test.mongodb.domain.cn:30000,s2.mongodb.domain.cn:30000,s3._test.mongodb.domain.cn:30000/databasename', ];
The problem is coming.
1 should you connect all or only one database instance?
In the production environment, there are three replication collection instances, all of which are allocated to the connection string. I don’t have a good explanation for whether we should only connect to one. In theory, only one entry is needed for application, and later all will be handed over to the database for processing.
2 should connection timeout settings be displayed?
Https://docs.mongodb.com/manu… Official reference for connection string.
When it comes to timeout, let’s look at the database connection cycle
A complete request consists of three stages: 1. Establishing connection 2. Data transmission 3. Disconnecting.
Generally speaking, we talk about two types of timeout: connect timeout and socket timeout. The former is network connection timeout, and the latter is execution timeout.
If the connection time requested by the database server exceeds connectiontimeout, connectiontimeoutexception will be thrown, that is, the server connection timeout, and the connection is not established within the specified time. If the server takes too long to process data and exceeds socketimeout, it will throw socketimeoutexceptin, that is, the server response times out and the server does not return data to the client within the specified time. (the exception here is the exception returned by the Java driver)
These two parameters are officially given as connecttimeoutms and socketimeoutms
On the options parameter in the production environment
In the production environment, the parameters replicaset, connecttimeoutms and socketimeoutms are not configured.
The opinions in the article are not rigorous. Welcome to comment and communicate. The author is also exploring about the replica collection.
With uncertain thinking, learn the basic attitude of mongodb database: learn, practice, reference, improve and grow in problems.
Welcome to the public address “Tu Nan technology.”