Technology Sharing | How Etcd Realizes Distributed Load Balancing and Distributed Notification and Coordination

Etcd is a highly consistent distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines. Etcd's more application scenarios are used for service registration and discovery (introduced in the previous article). In addition, it can also be used for key-value pair storage. Applications can read and write data in Etcd (similar to memory Database redis) can also be used for message publishing and subscription of distributed systems, distributed notification and coordination, and load balancing in distributed systems, etc.

Message publishing and subscription

In a distributed system, the most applicable communication method between components is message publishing and subscription. That is to build a configuration sharing center, where data providers publish messages, and message consumers subscribe to the topics they care about. Once a topic has a message to publish, the subscribers will be notified in real time. In this way, centralized management and dynamic update of distributed system configuration can be achieved.

insert image description here

Some configuration information used in the application is placed on Etcd for centralized management. The usage of this kind of scenario is usually like this: when the application is started, it actively obtains the configuration information from Etcd once, and at the same time, registers a Watcher on the Etcd node and waits, and Etcd will notify the subscribers in real time every time the configuration is updated in the future , so as to achieve the purpose of obtaining the latest configuration information.

In the distributed search service, the meta information of the index and the node status of the server cluster machines are stored in Etcd for each client to subscribe to. Using Etcd's key TTL function can ensure that the machine status is updated in real time.

Distributed notification and coordination

The distributed notification and coordination mentioned here are somewhat similar to message publishing and subscription. In a distributed system, the most applicable communication method between components is message publishing and subscription. That is to build a configuration sharing center, where data providers publish messages, and message consumers subscribe to the topics they care about. Once a topic has a message to publish, the subscribers will be notified in real time. In this way, centralized management and dynamic update of distributed system configuration can be achieved.

The Watcher mechanism in Etcd is used here. Through the registration and asynchronous notification mechanism, notification and coordination between different systems in a distributed environment are realized, so that data changes can be processed in real time. The implementation method is usually like this: different systems register the same directory on Etcd, and set Watcher to observe the changes of the directory (if there is a need for changes in subdirectories, you can set the recursive mode), when a system updates Etcd directory, the system with Watcher set will receive the notification and deal with it accordingly.

Principles and methods of distributed notification and coordination:

  • Low-coupling heartbeat detection via Etcd. The detection system and the detected system are associated through a directory on Etcd rather than directly, which can greatly reduce the coupling of the system.
  • System scheduling is done through Etcd. A certain system consists of two parts, the console and the push system. The responsibility of the console is to control the push system to perform corresponding push work. Some operations performed by managers on the console actually only need to modify the status of certain directory nodes on Etcd, and Etcd will automatically notify the push system client registered with Watcher of these changes, and the push system will make corresponding changes. Push tasks.
  • Complete work reports through Etcd. In most similar task distribution systems, after subtasks are started, they register a temporary working directory with Etcd, and regularly report their progress (write the progress to this temporary directory), so that the task manager can know the task in real time schedule.

insert image description here

load balancing

In a distributed system, in order to ensure high availability of services and data consistency, multiple copies of data and services are usually deployed to achieve peer-to-peer services. Even if one of the services fails, it will not affect the use. Although such an implementation will lead to a decline in data writing performance to a certain extent, it can achieve load balancing during data access. Because each peer service node has complete data, the user's access traffic can be distributed to different machines.

insert image description here

Etcd's own load balancing:

The information access stored in Etcd's own distributed architecture supports load balancing. After Etcd is clustered, each core node of Etcd can handle user requests. Therefore, it is also a good choice to directly store message data with a small amount of data but frequently accessed in Etcd, such as the secondary code table commonly used in business systems. The working process of the secondary code table is generally like this. The code is stored in the table, and the specific meaning represented by the code is stored in Etcd. When the business system calls the process of looking up the table, it needs to look up the meaning of the code in the table. Therefore, if a small amount of data in the secondary code table is stored in Etcd, it is not only convenient to modify, but also easy to access in large quantities.

Etcd provides load balancing functions for other applications:

Use Etcd to maintain a load balancing node table. Etcd can monitor the status of multiple nodes in a cluster. When a request is sent, it can forward the request to multiple surviving nodes in a polling manner. Similar to KafkaMQ, Zookeeper is used to maintain load balance between producers and consumers. Etcd can also be used to do the work of Zookeeper.

reference documents

Etcd official website
Etcd
Etcd load balancing golang plugin

insert image description here

Supongo que te gusta

Origin blog.csdn.net/anyRTC/article/details/127860517
Recomendado
Clasificación