“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

Speaking of clusters, many services are deployed in clusters in production environments, so what are the knowledge points you need to know about mongodb clusters

Mongodb clusters are divided into master-slave clusters and sharded clusters

Why do you need clusters?

To ensure high availability of services

We can know that if we deploy a single point of mongodb service, it is very prone to single point of failure. If the mongodb service is unavailable, won’t the whole business be over?

When we deploy multiple mongodb nodes, they are related to each other, which is called cluster. According to whether the cluster is stateful, it can be divided into:

  • Stateful cluster

All services in a cluster need to store data

In a stateless cluster, the service itself does not store data. When a client accesses any service in the cluster, the result is the same regardless of the service actually accessed

Because these services do not store data, the data is retrieved from a common component

  • Stateless cluster

Services in the cluster do not need to store data

A stateful cluster, that is, a cluster that stores data itself, will have data consistency problems

Let’s look at the CAP principle:

Refers to three features in a distributed system

  • C (Consistency)

Data consistency

  • A (the Availability)

availability

  • P (Partition tolerance)

Zonal tolerance

In the CAP principle, you can only implement two at most, not all at once, and all clusters need to consider the last three features

Master-slave cluster

A master/slave cluster is an instance of the same data set maintained by a set of Mongods, a replica set containing the following nodes:

  • Multiple data bearer nodes
  • A quorum node, which is optional

When the primary node is unavailable, the primary node is elected as the secondary node that meets the requirements. If the number of members in the replica set is even, a mediator is added to elect the primary node

The arbiter is a Mongod process that does not maintain the dataset but maintains the quorum mechanism in the cluster by responding to heartbeat and election requests from other replica set members

In the data hosting node, only one member is treated as the primary node, while the other nodes are treated as secondary nodes

Master/slave replication clusters provide redundancy and improve data availability by:

  • Replication provides a degree of fault tolerance with multiple copies of data on different database servers

    This is done to prevent the loss of a single database server

So the question is, how do master and slave synchronize data?

What is the synchronization mechanism for the master/slave cluster?

The synchronization mechanism in Mongo DB is as follows:

  • oplog
  • heartbeat
  • The election mechanism
  • A copy of the rollback

What is Oplog?

Oplog is actually a collection of mongodb local databases

Local.oplog. Rs. The default size of a fixed set is 5% of the hard disk, and the default maximum size is not more than 50 GB

Oplog stores all write operations in mongodb

How is oplog initialized

Oplog primary (mongodb 1) and secondary (mongodb 2, mongodb 3); oplog primary (mongodb 1); Add secondary (mongodb 4)

Mongodb 4 then reads copies of mongodb 3 and mongodb 2 to see which one is the latest and synchronizes the oplog data

If the default source node fails to synchronize data or for other reasons, the new secondary node will directly synchronize data to the primary node

Specifies the oplog initialization mode

Of course, in mongodb, we can directly set the latest secondary to find who to synchronize data with, through the following command

db.adminCommand(replSetSyncFrom:"ip:port")

heartbeat

Heartbeat mechanism, the underlying implementation is generally send TCP packets to check connectivity, of course, there are also their own application packages to check whether the business is normal

In mongodb, the heartbeat mechanism takes 2 seconds by default. If the heartbeat mechanism takes longer than 2 seconds, the service is considered unavailable

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~