How does mysql ensure the order of messages?
Let me give you an example. We have built a mysql binlog
synchronization system before. The pressure is still very high. The daily synchronization data has to reach hundreds of millions, which means that the data is intact from a mysql database. Synchronize to another mysql library (mysql -> mysql). A common point is that, for example, a big data team needs to synchronize a mysql library to perform various complex operations on the data of the company's business system.
When you add, delete or modify a piece of data in mysql, 3 logs of the addition, deletion or modification will appear binlog
, and then these three binlog
will be sent to MQ, and then consumed in sequence When executing, you must at least ensure that people come in order, right? Otherwise, it was originally: add, modify, delete; but you just changed the order and executed it to delete, modify, and add. Isn't that completely wrong?
Originally, when this data was synchronized, the data should have been deleted at the end; but as a result, you got the order wrong, and the data was retained in the end, and the data synchronization went wrong.
Let’s take a look at the two scenarios where the order will be out of order:
RabbitMQ: one queue, multiple consumers. For example, the producer sends three pieces of data to RabbitMQ, in the order of data1/data2/data3, and what is pushed into a memory queue of RabbitMQ. There are three consumers who consume one of the three pieces of data from MQ. As a result, consumer 2 completes the operation first and saves data2 to the database, followed by data1/data3. This isn't obviously messed up.
Kafka: For example, we created a topic with three partitions. When the producer writes, he can actually specify a key. For example, if we specify an order id as the key, then the data related to this order will definitely be distributed to the same partition, and the data in this partition must be There is an order.
When consumers take out data from the partition, they must also be in order. At this point, the order is still ok and there is no confusion. Next, we may create multiple threads in the consumer to process messages concurrently. Because if the consumer consumes and processes in a single thread, and the processing is time-consuming, for example, it takes tens of ms to process a message, then only dozens of messages can be processed in 1 second, which is too low throughput. If multiple threads run concurrently, the order may be messed up.
solution
RabbitMQ
Split multiple queues, each queue has one consumer, which is just more queues, which is indeed a troublesome point; or there is only one queue but corresponding to one consumer, and then this consumer uses a memory queue internally for queuing, and then distributes Give the bottom layer different workers to handle.
Kafka
One topic, one partition, one consumer, internal single-thread consumption, single-thread throughput is too low, generally this will not be used.
Write N memory queues, and data with the same key will go to the same memory queue; then for N threads, each thread consumes one memory queue respectively, so that Ensure orderliness.
The above is the detailed content of How does mysql ensure the order of messages?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).
