This article introduces two solutions for synchronizing MySQL and Redis cache
1. Solution 1 (UDF)Scenario analysis: When we perform data operations on the MySQL database, the corresponding data is synchronized to Redis at the same time. After synchronization to Redis, the query operation is searched from Redis The process is roughly as follows: Set a trigger for the data to be operated in MySQL and monitor the operation When the client (NodeServer) writes data to MySQL, the trigger is triggered, and the MySQL UDF function is called after the trigger UDF function can write data into Redis to achieve synchronization effect Solution Analysis:
Demo CaseBelow is the MySQL table Below is the parsing code of UDF Define the corresponding trigger 2. Solution 2 (parsing binlog)Before introducing Solution 2, let's first introduce the principle of MySQL replication, as shown in the following figure:
Solution 2 is:
For example, the following is an analysis of a cloud database instance: The cloud database and the local database are in a master-slave relationship. The cloud database is the primary database that mainly provides write services, and the local database is the secondary database that reads data from the primary database. After the local database reads the data, it parses the Bin log and then writes the data to Redis for synchronization. The client then reads the data from Redis. The difficulty of this technical solution lies in: how to parse the MySQL Bin Log. However, this requires a very deep understanding of binlog files and MySQL. At the same time, since binlog exists in multiple forms such as Statement/Row/Mixed level, the workload of analyzing binlog to achieve synchronization is very large. Canal Open Source TechnologyCanal is an open source project under Alibaba, developed in pure Java. Based on database incremental log analysis, it provides incremental data subscription and consumption. Currently, it mainly supports MySQL (also supports mariaDB) The open source reference address is: https://github.com/liukelin/canal_mysql_nosql_sync How it works (mimicking MySQL replication):
Architecture: Server represents a canal running instance, corresponding to a jvm An instance corresponds to a data queue (one server corresponds to 1..n instances) instance module:
The general parsing process is as follows:
For more information about Cancl, please search on Baidu The following is the operation topology diagram The synchronization of MySQL tables adopts the chain of responsibility model, and each table corresponds to a Filter . For example, the class design used in zvsync is as follows: The following are the classes used in the concrete zvsync . Whenever a table is added or deleted, it can be added or deleted directly. 3. AdditionalThe above introduction in this article is about synchronization from MySQL to cache. However, in actual development, some people may use the following solution:
This concludes this article on the synchronization solution for MySQL and redis cache. For more information about MySQL and redis cache synchronization, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Vue implements two routing permission control methods
>>: Use CSS to achieve circular wave effect
Preface When we write code, we occasionally encou...
If you need to use an image to implement the use ...
This article shares the specific code of jQuery t...
This article shares the specific code of node+exp...
HTML comments, we often need to make some HTML co...
The order in which objects call methods: If the m...
Copy code The code is as follows: <span style=...
1. How to represent the current time in MySQL? In...
Table of contents Preface 1. Get the current time...
This article describes how to compile and install...
ElementUI implements the table list paging effect...
This article is the second article about objects ...
Before using idea to write JSP files, you need to...
1. Use CSS Copy code The code is as follows: style...
1. Problem symptoms Version: MySQL 5.6, using the...