Home Database Mysql Tutorial How to design the architecture of MySQL Binlog storage system

How to design the architecture of MySQL Binlog storage system

Jun 02, 2023 pm 10:10 PM
mysql binlog

1. Introduction to kingbus

1.1 What is kingbus?

Kingbus is a distributed MySQL binlog storage system based on the raft strong consistency protocol. It can act as a MySQL Slave to synchronize binlog from the real Master and store it in a distributed cluster. At the same time, it also acts as a MySQL Master to synchronize the binlog in the cluster to other slaves. kingbus has the following characteristics:

Compatible with the MySQL replication protocol, synchronizes the binlog on the Master through Gtid, and supports the slave to pull the binlog from kingbus through Gtid.

Cross-regional data replication, Kingbus supports cross-regional data replication through the raft protocol. The binlog data written to the cluster is guaranteed to be strongly consistent across multiple nodes, and the binlog order is completely consistent with that on the master.

High availability, because Kingbus is built on the Raft strong consensus protocol, it can achieve high availability of the entire binlog pull and push service when more than half of the nodes in the cluster survive.

1.2 What problems can kingbus solve?

Kingbus can reduce Master’s network transmission traffic. In a replication topology with one master and multiple slaves, the master needs to send binlog to each slave. If there are too many slaves, the network traffic is likely to reach the upper limit of the master's network card. For example, when the master performs operations such as deleting a large table or online DDL, a large number of binlog events may be generated instantly. If 10 slaves are connected to the master, the network card traffic on the master will be amplified 10 times. If the master uses a Gigabit network card, the network card may be full if more than 10MB/S of traffic is generated. By connecting to the master via kingbus, slaves can be distributed to multiple machines to balance the transmission traffic.

To simplify the Master Failover process, you only need to promote a slave connected to the kingbus to master, and redirect the kingbus to the new master. Other slaves are still connected to the kingbus, and the replication topology remains unchanged.

Save the space used by the Master to store binlog files. Generally, MySQL uses more expensive SSDs. If the binlog file takes up a lot of space, the data stored in MySQL will have to be reduced. You can reduce the number of binlog files stored on the Master by storing all binlogs in kingbus

Support heterogeneous replication. Connect to kingbus through Alibaba's open source canal. kingbus continuously pushes binlog to canal. canal receives the binlog and then pushes it to the kafka message queue, and finally stores it in HBase. The business department writes SQL directly through Hive to achieve real-time analysis of the business. .

2. Kingbus overall architecture

The overall structure of kingbus is shown in the figure below:

Storage is responsible for storing raft log entry and Metadata. In Kingbus, raft log and mysql binlog are integrated. They are distinguished by different header information. The data part of raft log is binlog event, so there is no need to store the two types of logs separately. , save storage space. Because kingbus needs to store some meta-information, such as raft node voting information and the specific content of some special binlog events (FORMAT_DESCRIPTION_EVENT).

Raft replicates the lead election, log replication and other functions of the kingbus cluster, using the etcd raft library.

Binlog syncer only runs on the Lead node of the Raft cluster. There is only one syncer in the entire cluster. The syncer pretends to be a slave and establishes a master-slave replication connection to the Master. The Master will filter the binlog events that the syncer has accepted based on the executed_gtid_set sent by the syncer, and only send binlog events that the syncer has not received. This replication protocol is fully compatible with the MySQL master-slave replication mechanism. . After syncer receives the binlog event, it will do some processing according to the binlog event type, and then encapsulate the binlog event into a message and submit it to the raft cluster. Through the raft algorithm, this binlog event can be stored on multiple nodes and achieve strong consistency.

Binlog server is a Master that implements the replication protocol. The real slave can connect to the port monitored by the binlog server. The binlog server will send binlog events to the slave. The entire process of sending binlog events is implemented with reference to the MySQL replication protocol. When no binlog event is sent to the slave, the binlog server will periodically send heartbeat events to the slave to keep the replication connection alive.

API server is responsible for the management of the entire kingbus cluster, including the following:

Raft cluster membership operation, view cluster status, add a node, remove a node, update node information, etc.

Binlog syncer related operations: start a binlog syncer, stop the binlog syncer, and check the binlog syncer status.

Binlog server related operations: start a binlog server, stop the binlog server, and check the binlog server status. Various abnormalities in the server layer will not affect the raft layer. The server can be understood as a plug-in, which can be started and stopped on demand. When extending KingBus in the future, you only need to implement the server with relevant logic. For example, if you implement a kafka protocol server, you can consume the messages in kingbus through kafka client.

3.kingbus core implementation

3.1 Core implementation of storage

There are two log forms in storage, one is the raft log (hereinafter referred to as the raft log), which is generated and used by the raft algorithm, and the other is the user-form Log (that is, mysql binlog event). In the design of Storage, two Log forms are combined into one Log Entry. It is only distinguished by different header information. Storage consists of data files and index files, as shown in the following figure:

The segment has a fixed size (1GB) and can only be written additionally. The name is first_raft_index-last_raft_index, which indicates the raft index range of the segment.

Only the last segment can be written, and its file name is first_raft_index-inprogress. Other segments are read-only.

Read-only segments and corresponding index files are written and read through mmap.

The index content of the last segment is stored in both disk and memory. Reading the index only requires reading from memory.

3.2 Use of etcd raft library

The Etcd raft library is single-threaded when processing applied logs, committed entries, etc. Please refer to the link for specific functions. The processing time of this function must be as short as possible. If the processing time exceeds the raft election time, it will cause the cluster to be re-elected. This point requires special attention.

3.3 The core implementation of binlog syncer

The main job of binlog syncer is:

Pull binlog event

Parse and process binlog event

Submit binlog events to the raft cluster. Obviously, the pipeline mechanism can be used to improve the processing speed of the entire process. Kingbus uses a separate goroutine to process each stage, and connects different stages through pipelines. Since binlog syncer receives binlog events one by one, syncer cannot guarantee transaction integrity. It is possible that after syncer hangs, the master needs to be reconnected. At this time, the last transaction may be incomplete. Binlog syncer needs to find that the transaction is complete. With unique capabilities, kingbus implements the function of transaction integrity analysis, which is fully implemented with reference to the MySQL source code.

3.4 Core implementation of binlog server

The binlog server implements the function of a master. When the slave establishes a replication connection with the binlog server, the slave will send relevant commands, and the binlog server needs to respond to these commands. Finally send binlog event to slave. For each slave, the binlog server will start a goroutine to continuously read the raft log, remove the relevant header information, and turn it into a binlog event, and then send it to the slave.

The above is the detailed content of How to design the architecture of MySQL Binlog storage system. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles