Master-slave database read-write method and device, equipment and storage medium

By receiving database access requests in the master-slave database system and determining the target node according to the level to perform read operations, the service unavailability problem caused by the synchronization delay of the master-slave database is solved, and high availability and system stability are achieved.

CN119597844APending Publication Date: 2025-03-11GREE ELECTRIC APPLIANCE INC OF ZHUHAI +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411665316.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-20
Publication Date
2025-03-11

AI Technical Summary

Technical Problem

There is a delay in data synchronization between master and slave databases, which leads to the inability to use the service normally in some application scenarios, affecting the user experience.

Method used

By receiving database access requests, the operation type and level are determined, and the target node is determined in the master database node and the slave database node according to the level, and read operations are performed. This method determines the level of database access request by matching the level determination rules in the preset rule set, and selects the target node according to the level.

Benefits of technology

It realizes that the read operations are reasonably allocated to the database node based on the processing priority of database access requests, ensure that the requests with high processing priority are allocated to the primary database node, ensure the latest and consistency of data, reduce the load of the primary database node, and provide the overall stability and performance of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597844A_ABST
    Figure CN119597844A_ABST
Patent Text Reader

Abstract

The invention relates to a master-slave database read-write method and device, equipment and a storage medium, and the method comprises the steps: receiving a database access request, and determining a database operation type indicated by the database access request; under the condition that the database operation type is read operation, determining the level to which the database access request belongs, and determining a target node in the master database node and the slave database node according to the level; and executing a read operation in the target node according to the database access request. Therefore, the influence caused by time delay in data synchronization between the master database and the slave database can be reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of distributed databases, and in particular to a method, device, equipment and storage medium for reading and writing a master-slave database. Background Art

[0002] In related technologies, in order to improve the availability and response speed of services, a regional deployment mechanism for services is provided. Under this mechanism, databases in different regions must also be deployed in different regions, and consistency must be maintained between databases in different regions. In this regard, a common database data synchronization solution is to build databases in different regions into a master-slave synchronization cluster. In this architecture, the master database node is responsible for data writing operations, and after successful writing, the data is synchronized to the slave database node to ensure data consistency between the master and slave databases, while the data reading operation is mainly the responsibility of the slave database node to share the reading pressure of the master database node.

[0003] However, synchronization between the master and slave databases takes a certain amount of time. Although database nodes across regions are connected by dedicated lines, when the regions are far apart, the time it takes to transmit data through the dedicated lines will increase. In addition, dedicated lines have bandwidth limitations. If too much data needs to be transmitted at the same time, it will cause dedicated line congestion, which will increase the time for master-slave synchronization.

[0004] Based on the above problems, in some application scenarios, especially in high-real-time business scenarios, problems may arise due to the inability to synchronize the master and slave databases in real time, which may affect business continuity and user experience at the least, or even cause business unavailability at the worst. Summary of the invention

[0005] The present application provides a master-slave database reading and writing method, device, equipment and storage medium to solve the technical problem in the related art that due to the delay in data synchronization between the master and slave databases, the service cannot be used normally in some application scenarios, affecting the user experience.

[0006] In a first aspect, the present application provides a method for reading and writing a master-slave database, the method comprising:

[0007] receiving a database access request, and determining a database operation type indicated by the database access request;

[0008] In the case where the database operation type is a read operation, the level to which the database access request belongs is determined, and a target node is determined from among the master database node and the slave database node according to the level; and a read operation is performed in the target node according to the database access request.

[0009] In a possible implementation manner, determining the level to which the database access request belongs includes:

[0010] Matching the database access request with a level determination rule in a preset rule set;

[0011] The level to which the database access request belongs is determined according to the matching result.

[0012] In a possible implementation manner, determining the level to which the database access request belongs according to the matching result includes:

[0013] When the matching result is that the database access request matches any of the level determination rules, determining the level indicated by the level determination rule that matches the database access request as the level to which the database access request belongs;

[0014] When the matching result is that the database access request does not match any of the level determination rules in the preset rule set, the designated level is determined as the level to which the database access request belongs.

[0015] In a possible implementation manner, determining the target node among the master database node and the slave database node according to the level includes:

[0016] When the level to which the database access request belongs is the first level, the primary database node is determined as the target node.

[0017] In a possible implementation manner, determining the target node among the master database node and the slave database node according to the level includes:

[0018] When the level to which the database access request belongs is the second level, the slave database node is determined as the target node.

[0019] In a possible implementation manner, determining a slave database node as a target node includes:

[0020] Taking the slave database node closest to the request end of the database access request as the target node;

[0021] Alternatively, select the slave database node with the lowest current load as the target node.

[0022] In a possible implementation manner, determining the target node among the master database node and the slave database node according to the level includes:

[0023] In a case where the level to which the database access request belongs is the third level, determining, from the master database node and the slave database node, a database node closest to the requesting end of the database access request as a target node;

[0024] Alternatively, when the level to which the database access request belongs is the third level, a slave database node with the lowest current load is determined from the master database node and the slave database nodes as the target node.

[0025] In a second aspect, the present application provides a master-slave database reading and writing device, the device comprising:

[0026] A first determining module, configured to receive a database access request and determine a database operation type indicated by the database access request;

[0027] A second determination module is used to determine the level to which the database access request belongs when the database operation type is a read operation, and determine a target node among the master database node and the slave database node according to the level;

[0028] A read module is used to perform a read operation in the target node according to the database access request.

[0029] In a possible implementation manner, the second determining module includes:

[0030] A matching unit, used for matching the database access request with a level determination rule in a preset rule set;

[0031] The first determining unit is used to determine the level to which the database access request belongs according to the matching result.

[0032] In a possible implementation manner, the determining unit is specifically configured to:

[0033] When the matching result is that the database access request matches any of the level determination rules, determining the level indicated by the level determination rule that matches the database access request as the level to which the database access request belongs;

[0034] When the matching result is that the database access request does not match any of the level determination rules in the preset rule set, the designated level is determined as the level to which the database access request belongs.

[0035] In a possible implementation manner, the second determining module includes:

[0036] The second determining unit is configured to determine the primary database node as a target node when the level to which the database access request belongs is the first level.

[0037] In a possible implementation manner, the second determining module includes:

[0038] The third determining unit is configured to determine a slave database node as a target node when the level to which the database access request belongs is the second level.

[0039] In a possible implementation manner, the third determining unit is specifically configured to:

[0040] Taking the slave database node closest to the request end of the database access request as the target node;

[0041] Alternatively, select the slave database node with the lowest current load as the target node.

[0042] In a possible implementation manner, the second determining module includes:

[0043] A fourth determining unit is configured to determine, when the level to which the database access request belongs is the third level, a database node closest to the requesting end of the database access request from among the master database node and the slave database node as a target node;

[0044] Alternatively, when the level to which the database access request belongs is the third level, a slave database node with the lowest current load is determined from the master database node and the slave database nodes as the target node.

[0045] In a third aspect, the present application provides a device, comprising: at least one communication interface; at least one bus connected to the at least one communication interface; at least one processor connected to the at least one bus; at least one memory connected to the at least one bus, wherein the processor is configured to:

[0046] receiving a database access request, and determining a database operation type indicated by the database access request;

[0047] In the case where the database operation type is a read operation, the level to which the database access request belongs is determined, and a target node is determined from among the master database node and the slave database node according to the level; and a read operation is performed in the target node according to the database access request.

[0048] In a fourth aspect, the present application further provides a computer storage medium storing computer executable instructions, wherein the computer executable instructions are used to execute the master-slave database reading and writing method described in any one of the above items of the present application.

[0049] The above technical solution provided by the embodiment of the present application has the following advantages compared with the prior art: the method provided by the embodiment of the present application, by determining the database operation type indicated by the database access request when receiving the database access request; when the database operation type is a read operation, determining the level to which the database access request belongs, and determining the target node in the master database node and the slave database node according to the level, and performing the read operation in the target node according to the database access request, which can reasonably distribute the read operation to the database node based on the processing priority of the database access request. Under this mechanism, it is possible to distribute database access requests with relatively high processing priority to the master database node, so that even if there is a data synchronization delay between the master and slave databases, the latest and consistency of the read data can be ensured, and the high availability of the business service can be achieved. At the same time, it is possible to distribute database access requests with relatively low processing priority to the slave database node, thereby reducing the load on the master database node and providing the overall stability and performance of the system. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0051] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0052] One or more embodiments are exemplarily described by pictures in the corresponding drawings, and these exemplified descriptions do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and unless otherwise stated, the figures in the drawings do not constitute proportional limitations.

[0053] Figure 1 A schematic diagram of an application scenario involved in the technical solution provided in the embodiment of the present application;

[0054] Figure 2 A flowchart of an embodiment of a master-slave database reading and writing method provided in an embodiment of the present application;

[0055] Figure 3 A schematic diagram of a system architecture involved in an embodiment of the present application;

[0056] Figure 4 A flowchart of another master-slave database reading and writing method provided in an embodiment of the present application;

[0057] Figure 5A block diagram of an embodiment of a master-slave database reading and writing device provided in an embodiment of the present application;

[0058] Figure 6 A schematic diagram of the structure of a device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0059] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0060] The disclosure below provides many different embodiments or examples to realize the different structures of the present application. In order to simplify the disclosure of the present application, the parts and settings of specific examples are described below. Of course, they are only examples, and the purpose is not to limit the present application. In addition, the present application can repeat reference numbers and / or letters in different examples. This repetition is for the purpose of simplification and clarity, and does not itself indicate the relationship between the various embodiments and / or settings discussed.

[0061] Figure 1 A schematic diagram of the application scenario involved in the technical solution provided in the embodiment of the present application. Figure 1In the application scenario shown, the business service is deployed to three areas, including area A, area B and area C. Accordingly, database nodes are built in the three areas respectively, wherein one area may include one or more database nodes, and the number of database nodes in the same area is not limited. The database nodes in these areas are built into a master-slave synchronization cluster, for example, area B includes a master database node and a slave database node, and areas A and C only include slave database nodes. In actual applications, according to business needs, there can be only one or more master database nodes. In the case of multiple master database nodes, these master database nodes will support data writing, and multiple master database nodes also need to synchronize data with each other, and the data consistency requirements between master database nodes are higher. For example, if the master database node synchronizes data with the slave database node, it is generally required that 50% or 2 / 3 of the slave database node data synchronization is completed, and it can be considered that the data consistency between the master and slave databases has met the requirements, the master database node can continue other tasks, and the remaining slave database nodes continue to synchronize. In the case of multiple master database nodes, when the data of a master database node changes, only after the data change is synchronized to all other master database nodes, the data synchronization between the master database nodes is considered completed, and other tasks can be performed later, such as data synchronization from the master database node to the slave database node.

[0062] exist Figure 1 In the application scenario shown, the master database node in region B is responsible for data writing operations (and can also be responsible for some reading operations), and after successful writing (in the case of multiple master database nodes, the completion of data synchronization between multiple master database nodes means that the writing is successful on the master database node), it is synchronized to the slave database nodes in regions A, B, and C. For data reading operations, they are executed on the slave database nodes in the region according to the principle of reading nearby. For example, if a user in region A requests to read data through a service in region A, the data reading operation is preferentially executed on the database node in region A.

[0063] Under this master-slave database reading separation mechanism, due to the delay in data synchronization between the master and slave databases, the service cannot be used normally in some application scenarios, affecting the user experience.

[0064] For example, in the scenario where a user in region A resets his password and logs in, resetting the password involves a database write operation, specifically writing the reset password to the master database node in region B. After the master database node is successfully written, the reset password is synchronized to the slave database node. Login involves a database read operation, specifically including the user sending his user name and password to the cloud. The cloud queries the user's password from the slave database node in region A according to the principle of nearest reading, and then compares whether the password uploaded by the user is the same as the password queried from the database. If they are the same, the login is successful, and if they are different, the login fails. In this scenario, assume that after the user resets the password, the password on the master database node is changed from "123456" to "456789", after which the master database node synchronizes data with the slave database node. During the synchronization of the master-slave database, that is, before the synchronization of the master-slave database is completed, the old password "123456" is still stored in the slave database node. At this time, if the user requests to log in, the login request carries the reset new password "456789", and the cloud queries the user's password from the slave database node in area A and obtains the old password "123456". The two are inconsistent and the login fails.

[0065] It can be seen that due to the delay in data synchronization between the master and slave databases, the service cannot be used normally in some application scenarios, affecting the user experience.

[0066] In this regard, the present application provides a master-slave database reading and writing method, device, equipment and storage medium to reduce the impact caused by the delay in data synchronization between the master and slave databases. The following is an explanation of the master-slave database reading and writing method provided by the present application through specific embodiments.

[0067] Figure 2 The flowchart of the embodiment of the master-slave database reading and writing method provided in the embodiment of the present application is shown in FIG. Figure 2 Before explaining the process shown, the system architecture involved in the embodiment of the present application is first explained.

[0068] See also Figure 3 , is a schematic diagram of a system architecture involved in an embodiment of the present application. By comparison Figure 3 and Figure 1It can be found that in the technical solution provided in the embodiment of the present application, a database proxy service is added, and the service nodes in each region are connected to the database proxy service, and the database proxy service is responsible for connecting to the database cluster. Under this architecture, the database operations of the service nodes in each region will be forwarded by the database proxy service to the database node for execution, and the execution results will be returned to the service node. During the whole process, each service node is unaware of the business and does not need to modify the business code, so this architecture has good stability and scalability.

[0069] In one embodiment, Figure 2 The execution subject of the process shown is the database proxy service.

[0070] like Figure 2 As shown, the process includes the following steps:

[0071] Step 201: Receive a database access request and determine the database operation type indicated by the database access request.

[0072] Step 202: When the database operation type is a read operation, determine the level to which the database access request belongs, and determine a target node in the master database node and the slave database node according to the level.

[0073] Step 203: Execute a read operation in the target node according to the database access request.

[0074] For ease of understanding, steps 201 to 203 are explained uniformly below:

[0075] pass Figure 3 From the description of the system architecture shown, it can be seen that the service node in the region sends a database access request to the database proxy service, and then the database proxy service will receive the database access request. Here, after receiving the database access request, the database proxy service does not blindly forward it to the master database node or the slave database node, but first parses the database access request to determine the database operation type indicated by the database access request. Usually, two types of database operations are involved, namely write operations (such as insert operations, modify operations, and delete operations) and read operations.

[0076] When the database operation type indicated by the database access request is a write operation, the database proxy service will perform the corresponding write operation in the master database node to write the data to the master database node, and synchronize the data from the master database node to the slave database node. This ensures the consistency and integrity of the data.

[0077] In the case where the database operation type indicated by the database access request is a read operation, the database proxy service will determine the level to which the database access request belongs, and determine the target node from the master database node and the slave database node according to the level. Here, the level to which the database access request belongs can reflect the processing priority of the database access request, which means that the database proxy service will determine a target node from the master database node and the slave database node to perform the read operation based on the processing priority of the database access request.

[0078] For example, if the processing priority of the database access request is relatively high, the primary database node may be selected to perform the read operation to ensure the latest and consistency of the data. For example, in the scenario of resetting the password and logging in, the user usually logs in immediately after resetting the password. When the user logs in, if the password reading operation is performed on the primary database node, the reset new password "456789" will be accurately read, and the user's login request also carries the reset new password "456789". The two are consistent, and the login is successful.

[0079] For another example, if the processing priority of a database access request is relatively low, a slave database node may be selected to perform the read operation to reduce the load pressure on the master database node.

[0080] Optionally, the level of the database access request is determined based on multiple factors such as the urgency of the database access request, user permissions, system load, etc.

[0081] As for how to determine the level to which the database access request belongs, and determine the target node in the master database node and the slave database node according to the level, detailed explanations are given in other embodiments below and are not described in detail here.

[0082] The technical solution provided by the embodiment of the present application determines the type of database operation indicated by the database access request when receiving the database access request; when the database operation type is a read operation, determines the level to which the database access request belongs, and determines the target node in the master database node and the slave database node according to the level, and performs the read operation in the target node according to the database access request, which can reasonably distribute the read operation to the database node based on the processing priority of the database access request. Under this mechanism, it is possible to distribute database access requests with relatively high processing priority to the master database node, so that even if there is a data synchronization delay between the master and slave databases, the latest and consistency of the read data can be ensured, and the high availability of the business service can be achieved. At the same time, it is possible to distribute database access requests with relatively low processing priority to the slave database node, thereby reducing the load on the master database node and providing the overall stability and performance of the system.

[0083] Above through Figure 2 The process shown explains the technical solution provided by the embodiment of the present application as a whole. The following describes how to determine the level to which the database access request belongs.

[0084] In one embodiment, the specific implementation of determining the level to which a database access request belongs includes matching the database access request with a level determination rule in a preset rule set; and determining the level to which the database access request belongs according to the matching result. Further, the specific implementation of determining the level to which a database access request belongs according to the matching result includes: when the matching result is that the database access request matches any level determination rule, determining the level indicated by the level determination rule matching the database access request as the level to which the database access request belongs; when the matching result is that the database access request does not match any level determination rule in the preset rule set, determining the specified level as the level to which the database access request belongs.

[0085] Among them, the preset rule set includes multiple level determination rules, and each level determination rule is usually composed of a condition part and a level indication part. Among them, the condition part defines the conditions for triggering the rule, and these conditions can be set based on various attributes of the database access request, such as the request source (such as IP address, user account, user role, etc.), request time, and one or more of the attributes of the requested object. Accordingly, as an optional implementation method, the database access request matches the level determination rule, including one or more of the following combinations: the operation object indicated by the database access request is a specified object; the request end of the database access request is a specified user; the request end of the database access request has a specified permission level.

[0086] The level indication part defines the level indicated by the rule when the condition is met. Here, the level is predefined and can be a score or classification based on some quantitative standard. Exemplarily, the level includes the first level and the second level, or includes the first level and the third level, or includes the first level, the second level and the third level. Among them, the processing priority represented by the first level is the highest, for example, the processing priority of the database access request of the high real-time business is the highest, and the processing priorities represented by the second level and the third level are both lower than the first level. Optionally, in the case where the level includes the first level, the second level and the third level, the processing priority represented by the second level is lower than the third level.

[0087] In the technical solution provided in the embodiments of the present application, different level determination rules can be defined based on different combinations of conditions to adapt to different business needs and business logic, and the level determination rules are configurable. For example, users (such as operation and maintenance personnel) can configure and manage the level determination rules through configuration files or management interfaces to adapt to changes in the database environment or new business logic.

[0088] Based on the above description, in this embodiment, the execution subject of the embodiment of the present application traverses the level determination rules in the rule set, and each time it traverses to a level determination rule, it extracts relevant attributes from the database access request, such as the request source, the operation object, etc., and then determines whether the extracted attributes meet the condition part of the level determination rule. If it meets, it means that the database access request matches the level determination rule, so that the level indicated by the level determination rule is determined as the level to which the database access request belongs. If the level to which the database access request belongs cannot be determined after traversing all the level determination rules in the rule set, the specified level (for example, the second level or the third level) is determined as the level to which the database access request belongs. In addition, if the database access request matches multiple level determination rules at the same time, this conflict can be resolved by defining the rule priority and giving priority to the level indicated by the level determination rule with a high priority.

[0089] The following are examples of level determination rules:

[0090] Example 1: *FROM'user_info'.*, 0

[0091] Among them, 0 represents the master database node (and 1 represents the slave database node), and user_info is the name of the data table. This level determination rule indicates that all read operations for the database table user_info belong to the first level and are executed on the master database node.

[0092] Example 2: *FROM'*'.*WHERE user_id in(1001,1002),0

[0093] Among them, 0 represents the master database node (and 1 represents the slave database node), 1001 and 1002 are user IDs. This level determination rule indicates that all read operations initiated by user 1001 and user 1002 belong to the first level and are executed on the master database node.

[0094] Example 3, *FROM'user_info'.*WHERE user_id in (1001, 1002), 0

[0095] Among them, 0 represents the master database node (in addition, 1 represents the slave database node), 1001 and 1002 are user IDs, and user_info is the name of the data table. This level determination rule means that all read operations of users 1001 and 1002 on the database table user_info belong to the first level and are executed on the master database node.

[0096] The above are only examples of level determination rules. In practical applications, those skilled in the art can flexibly configure the level determination rules according to business needs. For example, for a product query scenario, the query operation of popular products can be executed on the main database node. For another example, for a certain time period (such as a time period with a large number of concurrent write operations), the read operation of the division within the time period can be set to be executed on the slave database node. The embodiment of the present application does not limit the level determination rules.

[0097] The above embodiment, by matching the database access request with the level determination rules in the preset rule set, determines the level to which the database access request belongs according to the matching results, thereby being able to simply and efficiently determine the level to which the database access request belongs. At the same time, since the level determination rules are highly configurable and flexible, this approach makes the service system as a whole highly scalable and maintainable.

[0098] Figure 4 A flowchart of another method for reading and writing a master-slave database provided in an embodiment of the present application. Figure 4 The process shown is based on the above embodiment, and focuses on how to determine the target node in the master database node and the slave database node according to the level to which the database access request belongs. Figure 4 As shown, the following steps are included:

[0099] Step 401: receiving a database access request;

[0100] Step 402, determine the database operation type indicated by the database access request; if the database operation type is a write operation, execute step 403; if the database operation type is a read operation, execute step 404.

[0101] Step 403: Execute a write operation in the primary database node according to the database access request;

[0102] Step 404, determine the level to which the database access request belongs; if the level to which the database access request belongs is the first level, execute step 405; if the level to which the database access request belongs is the second level, execute step 406; if the level to which the database access request belongs is the third level, execute step 407.

[0103] Step 405 , determine the primary database node as the target node; and execute step 408 .

[0104] Step 406 , determine the slave database node as the target node; and execute step 408 .

[0105] Step 407: Determine the database node closest to the request end of the database access request from the master database node and the slave database node as the target node, or determine the database node with the lowest current load from the master database node and the slave database node as the target node.

[0106] Step 408: Execute a read operation in the target node according to the database access request.

[0107] Exemplarily, the first level represents the highest processing priority, for example, the processing priority of a database access request of a high real-time business is the highest, and the processing priorities represented by the second and third levels are both lower than the first level. Optionally, in the case where the levels include the first level, the second level, and the third level, the processing priority represented by the second level is lower than the third level, that is, the processing priority represented by the third level is between the processing priorities represented by the first level and the second level respectively.

[0108] When it is determined that the level to which the database access request belongs is the first level, the master database node is determined as the target node, so that the read operation is performed in the master database node according to the database access request. This mechanism can allocate database access requests with a relatively high processing priority to the master database node, thereby ensuring the latest and consistency of the read data even if there is a data synchronization delay between the master and slave databases.

[0109] When it is determined that the level to which the database access request belongs is the second level, the slave database node is determined as the target node, so that the read operation is performed in the master database node according to the database access request. This mechanism can reduce the load of the master database node and provide the overall stability and performance of the system. Exemplarily, when the level to which the database access request belongs is the second level, the database access request can be reasonably allocated to one of the slave database nodes for execution in combination with other allocation strategies. For example, in combination with the nearest allocation strategy, the slave database node closest to the request end of the database access request in the slave database node is used as the target node, which is usually the slave database node in the area where the request end is located, which can achieve the shortest network time consumption and the fastest response speed in the case of master-slave read-write separation. For another example, in combination with the load optimal allocation strategy, the slave database node with the lowest current load in the slave database node is used as the target node, which can effectively avoid some slave database nodes from becoming hot spots and causing performance bottlenecks in the case of master-slave read-write separation.

[0110] When it is determined that the level to which the database access request belongs is the third level, the database node closest to the request end of the database access request is determined from the master database node and the slave database node as the target node, or the database node with the lowest current load is determined from the master database node and the slave database node as the target node, and then the read operation is performed on the target node according to the database access request. It should be noted here that the target node can be the master database node or the slave database node, and when it is determined that the level to which the database access request belongs is the second level, the target node determined in combination with the nearest allocation strategy or the load optimal allocation strategy is the slave database node.

[0111] By determining the database node closest to the request end of the database access request from the master database node and the slave database node as the target node, the database access request can be processed as efficiently as possible even if the processing priority of the database access request is not high. By determining the database node with the lowest current load from the master database node and the slave database node as the target node, it is possible to effectively avoid some database nodes becoming hot spots and causing performance bottlenecks.

[0112] Figure 4 The process shown can reasonably distribute read operations to database nodes based on a flexible level management mechanism. Under this mechanism, database access requests with higher processing priorities can be distributed to the master database node, so that even if there is a data synchronization delay between the master and slave databases, the latest and consistency of the read data can be ensured, thereby achieving high availability of business services. At the same time, database access requests with lower processing priorities can be distributed to the slave database nodes, thereby reducing the load on the master database node and providing overall stability and performance of the system.

[0113] In order to better understand the technical solution provided by the embodiments of the present application, the following specific examples are given:

[0114] Business services and databases are deployed to cloud servers in different regions to provide services to users, and database proxy software ProxySQL is added. Services in different regions connect to ProxySQL just like connecting to ordinary databases, making it business-insensitive and without modifying business codes. ProxySQL is responsible for connecting to the real database cluster, forwarding database operations to the real database nodes for execution, and returning the execution results to the service. No service needs to be modified during the whole process.

[0115] Taking the user operation of "reset password and log in" as an example, resetting the password involves a database write operation, which is executed on the master database node. Login involves a database read operation, which can be executed on the master database node or the slave database node. Specifically, if it is determined that the level of the database access request for login is the first level, the processing priority is relatively high, and high real-time processing is required, then the database access request can be executed on the master database node. In this way, both the "reset password" and "login" database operations are sent to the master database node, so that both database operations are executed on the master database node. Therefore, the synchronization delay between the master and slave databases will not have any impact on these two operations, the business will be completed normally, and the user will successfully log in after resetting the password.

[0116] It can be seen that by placing both database writes and reads on the master database node, the problem of data synchronization delay can be solved. However, it is inappropriate to place all writes and reads on the master database node, because one of the purposes of the database master-slave synchronization cluster is to separate database write and read operations and perform them on different types of databases, so as to relieve the pressure on the master database node while ensuring data accuracy and consistency.

[0117] In this regard, an embodiment of the present application proposes: treating database operations differently according to the business. For example, read operations with high real-time performance and high time sensitivity are sent to the master database node. General read operations are not highly time-sensitive, and executing them on the slave database node is sufficient to meet business needs. Therefore, general read operations are sent to the slave database node.

[0118] The above ProxySQL can complete this task. ProxySQL can dynamically configure the level determination rules, and then ProxySQL will determine the level of the current database access request according to this rule, and then determine which node of the database cluster the database access request will be sent to for execution. The specific implementation is as follows:

[0119] ①Configure the IP and port (or domain name) of the master and slave database nodes and other information for connection, and mark the master and slave, for example:

[0120] INSERT INTO mysql_servers(hostgroup_id,hostname,port)VALUES(0,'East China Master Node',3306);

[0121] INSERT INTO mysql_servers(hostgroup_id,hostname,port)VALUES(1,'North China slave node',3306);

[0122] INSERT INTO mysql_servers(hostgroup_id,hostname,port)VALUES(1,'South China slave node',3306);

[0123] In the above example, there are three database nodes, where "hostgroup=0" represents the master database node and "hostgroup=1" represents the slave database node.

[0124] ②Configure the level determination rules. For example:

[0125] INSERT INTO mysql_query_rules(rule_id,match_pattern,destination_hostgroup,apply)VALUES(1,*FROM'user_info'.*,0)

[0126] This level determination rule indicates that the read operation of querying user information (user_info) accesses the primary database node.

[0127] Under the above configuration rules, assuming that user information (account, password) is stored in the database table user_info, when a user logs in, the user information needs to be verified, the user_info table will be queried, and ProxySQL will send the database query operation to the primary database node for execution.

[0128] For other unconfigured non-"high real-time" query requests, they will not be forwarded to the primary database node, thereby reducing the load pressure on the primary database node.

[0129] Figure 5 This is a block diagram of an embodiment of a master-slave database reading and writing device provided in an embodiment of the present application. Figure 5 As shown, the device comprises:

[0130] A first determining module 51 is used to receive a database access request and determine a database operation type indicated by the database access request;

[0131] A second determination module 52 is used to determine the level to which the database access request belongs when the database operation type is a read operation, and determine a target node in the master database node and the slave database node according to the level;

[0132] The reading module 53 is used to perform a read operation in the target node according to the database access request.

[0133] In a possible implementation manner, the second determining module 52 includes:

[0134] A matching unit, used for matching the database access request with a level determination rule in a preset rule set;

[0135] The first determining unit is used to determine the level to which the database access request belongs according to the matching result.

[0136] In a possible implementation manner, the determining unit is specifically configured to:

[0137] When the matching result is that the database access request matches any of the level determination rules, determining the level indicated by the level determination rule that matches the database access request as the level to which the database access request belongs;

[0138] When the matching result is that the database access request does not match any of the level determination rules in the preset rule set, the designated level is determined as the level to which the database access request belongs.

[0139] In a possible implementation manner, the second determining module 52 includes:

[0140] The second determining unit is configured to determine the primary database node as a target node when the level to which the database access request belongs is the first level.

[0141] In a possible implementation manner, the second determining module 52 includes:

[0142] The third determining unit is configured to determine a slave database node as a target node when the level to which the database access request belongs is the second level.

[0143] In a possible implementation manner, the third determining unit is specifically configured to:

[0144] Taking the slave database node closest to the request end of the database access request as the target node;

[0145] Alternatively, select the slave database node with the lowest current load as the target node.

[0146] In a possible implementation manner, the second determining module 52 includes:

[0147] A fourth determining unit is configured to determine, when the level to which the database access request belongs is the third level, a database node closest to the requesting end of the database access request from among the master database node and the slave database node as a target node;

[0148] Alternatively, when the level to which the database access request belongs is the third level, a slave database node with the lowest current load is determined from the master database node and the slave database nodes as the target node.

[0149] like Figure 6 As shown, an embodiment of the present application provides a device, including a processor 111, a communication interface 112, a memory 113 and a communication bus 114, wherein the processor 111, the communication interface 112, and the memory 113 communicate with each other through the communication bus 114.

[0150] Memory 113, used for storing computer programs;

[0151] In one embodiment of the present application, the processor 111 is used to execute the program stored in the memory 113 to implement the master-slave database reading and writing method provided by any one of the aforementioned method embodiments, including:

[0152] receiving a database access request, and determining a database operation type indicated by the database access request;

[0153] In the case where the database operation type is a read operation, the level to which the database access request belongs is determined, and a target node is determined in a master database node and a slave database node according to the level; and a read operation is performed in the target node according to the database access request.

[0154] An embodiment of the present application further provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the master-slave database reading and writing method provided in any of the aforementioned method embodiments are implemented.

[0155] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0156] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0157] It should be understood that the terms used herein are only for the purpose of describing specific example embodiments and are not intended to be limiting. Unless the context clearly indicates otherwise, the singular forms "one", "an" and "said" as used herein may also be meant to include plural forms. The terms "include", "comprise", "contain", and "have" are inclusive, and therefore specify the existence of stated features, steps, operations, elements and / or parts, but do not exclude the existence or addition of one or more other features, steps, operations, elements, parts, and / or combinations thereof. The method steps, processes, and operations described herein are not interpreted as necessarily requiring them to be performed in the specific order described or illustrated, unless the execution order is clearly indicated. It should also be understood that additional or alternative steps may be used.

[0158] The above description is only a specific implementation of the present application, so that those skilled in the art can understand or implement the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest range consistent with the principles and novel features applied for herein.

Claims

1. A master-slave database reading and writing method, characterized in that: The method comprises: receiving a database access request, and determining a database operation type indicated by the database access request; In the case where the database operation type is a read operation, determining the level to which the database access request belongs, and determining a target node in a master database node and a slave database node according to the level; A read operation is performed in the target node according to the database access request.

2. The method according to claim 1, characterized in that Determining the level to which the database access request belongs includes: Matching the database access request with a level determination rule in a preset rule set; The level to which the database access request belongs is determined according to the matching result.

3. The method according to claim 2, characterized in that Determining the level to which the database access request belongs according to the matching result includes: When the matching result is that the database access request matches any of the level determination rules, determining the level indicated by the level determination rule that matches the database access request as the level to which the database access request belongs; When the matching result is that the database access request does not match any of the level determination rules in the preset rule set, the designated level is determined as the level to which the database access request belongs.

4. The method according to any one of claims 1 to 3, characterized in that: Determining the target node in the master database node and the slave database node according to the level includes: When the level to which the database access request belongs is the first level, the primary database node is determined as the target node.

5. The method according to any one of claims 1 to 3, characterized in that: Determining the target node in the master database node and the slave database node according to the level includes: When the level to which the database access request belongs is the second level, the slave database node is determined as the target node.

6. The method according to claim 5, characterized in that The step of determining a slave database node as a target node includes: Taking the slave database node closest to the request end of the database access request as the target node; Alternatively, select the slave database node with the lowest current load as the target node.

7. The method according to any one of claims 1 to 3, characterized in that: Determining the target node in the master database node and the slave database node according to the level includes: In a case where the level to which the database access request belongs is the third level, determining, from the master database node and the slave database node, a database node closest to the requesting end of the database access request as a target node; Alternatively, when the level to which the database access request belongs is the third level, a database node with the lowest current load is determined from the master database node and the slave database node as the target node.

8. A master-slave database reading and writing device, characterized in that: The device comprises: A first determining module, configured to receive a database access request and determine a database operation type indicated by the database access request; A second determination module is used to determine the level to which the database access request belongs when the database operation type is a read operation, and determine a target node in the master database node and the slave database node according to the level; A read module is used to perform a read operation in the target node according to the database access request.

9. A device, characterized in that: include: at least one communication interface; at least one bus connected to the at least one communication interface; at least one processor connected to the at least one bus; at least one memory connected to the at least one bus, wherein the processor is configured to: receiving a database access request, and determining a database operation type indicated by the database access request; In the case where the database operation type is a read operation, the level to which the database access request belongs is determined, and a target node is determined from among the master database node and the slave database node according to the level; and a read operation is performed in the target node according to the database access request.

10. A computer storage medium, characterized in that: Computer executable instructions are stored, and the computer executable instructions are used to execute the master-slave database reading and writing method according to any one of claims 1 to 7.