Database switching method and device
By detecting and synchronizing hot spot data records in low-speed databases to high-speed databases, and presetting temporary tables in low-speed databases for updating, the business stagnation caused by hot spot data synchronization in the existing technology is solved, and a rapid and non-stop database switching is achieved.
Patent Information
- Application Number
- CN202210706139.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-21
- Publication Date
- 2025-08-12
- Estimated Expiration
- 2042-06-21
AI Technical Summary
In the prior art, when synchronizing hotspot data records, access to upper-level applications is required to ensure consistency of data records, resulting in business stagnation or errors.
The preset hot spot data records in the low-speed database are detected through the preset hot and cold distinction mechanism and synchronized to the high-speed database. At the same time, temporary tables are preset in the low-speed database for update operations to ensure that access is not suspended during the synchronization process.
It realizes fast synchronization of hot spot data records, improves access speed, and does not need to pause access to hot spot data records during synchronization, and realizes continuous service database switching.
Smart Images

Figure CN115080662B_ABST
Abstract
Description
Technical Field
[0001] This specification relates to the field of data processing technology, and in particular to a database switching method and device. Background Art
[0002] A database is a warehouse that organizes, stores, and manages data according to its structure. It provides data storage and access services for upper-layer applications. The database's response speed and stability are extremely important to upper-layer applications. Summary of the Invention
[0003] In view of this, this specification provides a database switching method and device.
[0004] Specifically, this specification is implemented through the following technical solutions:
[0005] A database switching method, comprising:
[0006] Detect hot data records in the low-speed database based on the preset hot and cold distinction mechanism;
[0007] After detecting the hotspot data record, synchronizing the hotspot data record from the low-speed database to the high-speed database, and updating the database status of the hotspot data record from not switched to being switched;
[0008] In response to an update command sent by an upper-layer application, querying a database status of a target data record pointed to by the update command;
[0009] When the database status of the target data record is switching, the update command is sent to the high-speed database, and a temporary data record corresponding to the update command is written into a temporary table preset in the low-speed database;
[0010] After the temporary data record is written successfully, an update success is returned to the upper layer application.
[0011] Optionally, also include:
[0012] In response to a read command directed to the target data record sent by an upper layer application, querying a database status of the target data record;
[0013] When the database status of the target data record is switching, the read command is sent to the low-speed database.
[0014] Optionally, detecting hotspot data records in the low-speed database according to a preset hot and cold distinction mechanism includes:
[0015] Monitor the lock failure events reported by the low-speed database for the target data record;
[0016] When the number of lock failure events that occur in the target data record reaches a preset number of failures, the target data record is determined as a hotspot data record.
[0017] Optionally, also include:
[0018] After synchronizing the hotspot data records from the low-speed database to the high-speed database, checking whether the hotspot data records in the low-speed database and the high-speed database are consistent based on a preset checking rule;
[0019] When the hotspot data records in the low-speed database and the high-speed database are consistent, the database status of the hotspot data records is updated from a switching-in-progress state to a switching-completed state.
[0020] Optionally, also include:
[0021] When the database status of the target data record is in the switching completion state, the update command is sent to the high-speed database, and a temporary data record corresponding to the update command is written into a temporary table preset in the low-speed database;
[0022] After the high-speed database is updated successfully and the temporary data record is written successfully, a return of update success is sent to the upper layer application;
[0023] In response to a read command directed to the target data record sent by an upper layer application, when the database status of the target data record is a switching completion status, the read command is sent to the high-speed database.
[0024] Optionally, also include:
[0025] In the case that the hotspot data records in the low-speed database and the high-speed database are inconsistent, the hotspot data record in the high-speed database is deleted, and the database status of the hotspot data record is updated back to unswitched.
[0026] Optionally, also include:
[0027] When the database status of the target data record is not switched, the update command and the read command from the upper layer application are both sent to the low-speed database.
[0028] Optionally, before synchronizing the hotspot data record from the low-speed database to the high-speed database, the method further includes:
[0029] The identifier of the hotspot data record is synchronized to the high-speed database, and the initial field value is written into the target field that needs to be synchronized.
[0030] Optionally, synchronizing the hotspot data record from the low-speed database to the high-speed database includes:
[0031] The initial field value in the high-speed database is updated according to the low-speed field value under the target field of the hotspot data record in the low-speed database to achieve synchronization of the hotspot data record.
[0032] Optionally, also include:
[0033] Synchronizing the temporary data records in the temporary table to corresponding data records in the low-speed database based on a preset period;
[0034] For each period, counting the number of temporary data records corresponding to the hot data records in the period;
[0035] When the number of the temporary data records is less than the hotspot threshold, the database status of the hotspot data records is updated back to unswitched.
[0036] Optionally, also include:
[0037] After the database status of the hotspot data record is updated back to unswitched, the hotspot data record stored in the high-speed database is deleted.
[0038] Optionally, the capacity of the high-speed database is smaller than the capacity of the low-speed database.
[0039] Optionally, the data record includes the remaining amount of resources of the account.
[0040] A database switching device, comprising:
[0041] A hotspot detection unit detects hotspot data records in the low-speed database based on a preset hot and cold distinction mechanism;
[0042] a hotspot synchronization unit, which, after detecting the hotspot data record, synchronizes the hotspot data record from the low-speed database to the high-speed database, and updates the database status of the hotspot data record from not switched to being switched;
[0043] a status query unit, responding to an update command sent by an upper layer application, querying a database status of a target data record pointed to by the update command;
[0044] an update execution unit, which sends the update command to the high-speed database and writes a temporary data record corresponding to the update command into a temporary table preset in the low-speed database when the database status of the target data record is switching;
[0045] The result returning unit returns an update success to the upper layer application after the temporary data record is written successfully.
[0046] An electronic device, comprising:
[0047] processor;
[0048] a memory for storing processor-executable instructions;
[0049] The processor implements the aforementioned method by running the executable instructions.
[0050] A computer-readable storage medium stores computer instructions, which implement the steps of the above method when executed by a processor.
[0051] Using the above-mentioned implementation mode, in response to the update command sent by the upper-layer application, the database status of the target data record pointed to by the update command can be queried. When the database status is switching, it can be indicated that the target data record is a hot data record and is being synchronized from the low-speed database to the high-speed database. Then, on the one hand, the update command can be sent to the high-speed database, and on the other hand, the temporary data record corresponding to the update command can be written into the temporary table preset in the low-speed database. After the temporary data record is written successfully, the update success is returned to the upper-layer application.
[0052] The above technical solution allows, on the one hand, for hotspot data records to be synchronized to the high-speed database in a timely manner when a hotspot access occurs, enabling database switching for the hotspot data records and improving access speed for the hotspot data records. Furthermore, during the hotspot data record synchronization process, corresponding update operations can be performed simultaneously in both the high-speed and low-speed databases, without suspending data access to the hotspot data records, thus achieving database switching without service interruption. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] Figure 1 This is a schematic diagram of the architecture of a database switching system shown in an exemplary embodiment of this specification.
[0054] Figure 2 This is a flowchart of a database switching method shown in an exemplary embodiment of this specification.
[0055] Figure 3 This is a hardware structure diagram of an electronic device where a database switching device is located, shown as an exemplary embodiment of this specification.
[0056] Figure 4 This is a block diagram of a database switching device shown in an exemplary embodiment of this specification. DETAILED DESCRIPTION
[0057] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all embodiments consistent with this specification. Rather, they are merely examples of apparatus and methods consistent with certain aspects of this specification, as detailed in the appended claims.
[0058] The terms used in this specification are for the purpose of describing specific embodiments only and are not intended to limit this specification. As used in this specification and the appended claims, the singular forms "a," "an," "the," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It should also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
[0059] It should be understood that although the terms first, second, third, etc. may be used in this specification to describe various information, such information should not be limited to these terms. These terms are merely used to distinguish information of the same type from one another. For example, first information may also be referred to as second information, and similarly, second information may also be referred to as first information without departing from the scope of this specification. Depending on the context, the term "if" as used herein may be interpreted as "when," "when," or "in response to determining."
[0060] A database is a warehouse that organizes, stores, and manages data according to its structure. It provides data storage and access services for upper-layer applications. The database's response speed and stability are extremely important to upper-layer applications.
[0061] Most upper-layer applications will experience hotspot access, that is, certain data records in the database are frequently accessed by upper-layer applications within a certain period of time. These data records that are frequently accessed in a short period of time can be called hot data records, such as popular products and popular accounts.
[0062] For example, a financial account may experience frequent outgoing transactions within a short period of time. The corresponding financial application will frequently access the account to perform corresponding accounting operations. The data record containing the balance of the financial account is a hot data record. Alternatively, a popular product may experience a surge in demand within a short period of time. The corresponding sales application will frequently update the inventory of the popular product within a short period of time. The data record containing the inventory of the popular product is also a hot data record.
[0063] These hot data records can be synchronized from their original database to a faster-responding database to meet the responsiveness requirements of upper-layer applications when hot data access occurs. However, when synchronizing hot data, access to upper-layer applications often needs to be suspended to ensure data record consistency, which can lead to business stagnation or even errors.
[0064] This specification provides a database switching solution that can synchronize hotspot data records without suspending access to upper-layer applications.
[0065] Figure 1 This is a schematic diagram of the architecture of a database switching system shown in an exemplary embodiment of this specification.
[0066] Please refer to Figure 1 , the database switching system may include a low-speed database, a high-speed database, an upper-layer application and a middle layer.
[0067] A high-speed database is typically a database with a relatively fast response speed but a relatively small capacity, while a low-speed database is typically a database with a relatively slow response speed but a relatively large capacity. The response speed refers to the speed at which the database executes commands from upper-layer applications, which may include update commands, read commands, etc.
[0068] A large number of data records of upper-layer applications are usually stored in a low-speed database with larger capacity. When hotspot access occurs, the relevant hotspot data records can be synchronized from the low-speed database to the high-speed database, thereby improving the access speed of the hotspot data records.
[0069] The middle layer sits between the upper-layer application and the database during database access. It sends commands from the upper-layer application to the database and returns command execution results from the database to the upper-layer application. The middle layer's processing logic can be integrated into the physical device where the upper-layer application resides, or it can be located in a separate physical device, such as a server or server cluster. This specification does not impose any specific restrictions on this.
[0070] Figure 2 This is a flowchart of a database switching method shown in an exemplary embodiment of this specification.
[0071] Please refer to Figure 2 The database switching method can be applied to the aforementioned Figure 1 The middle layer shown includes the following steps:
[0072] Step 202: Detect hotspot data records in the low-speed database according to a preset hot and cold distinction mechanism.
[0073] In this specification, a hot / cold distinction mechanism for detecting hotspot access may be pre-set, and hotspot data records in a low-speed database may be detected according to the hot / cold distinction mechanism.
[0074] In one example, hotspot data records can be detected by monitoring lock failure events reported by a low-speed database, that is, the hot and cold distinction mechanism can be that the number of lock failure events occurring within a predetermined time period reaches a preset number of failures.
[0075] The database lock mechanism is used to ensure data consistency within the database. When accessing data records in the database, it is often necessary to first obtain a lock on the corresponding data record. Only after obtaining the lock can operations such as writing or updating be performed. If the lock is acquired by another process, a wait is required. If the wait timeout occurs, a lock acquisition failure event is reported.
[0076] In this example, the lock failure event reported by the low-speed database can be monitored. The lock failure event corresponds to a data record in the low-speed database. For this data record, the number of lock failure events that occur in the data record within a preset time period can be counted, and then it can be determined whether the number of lock failure events reaches the preset failure number. If the preset failure number is reached, it can be determined that the data record has a hotspot access, and the data record can be determined as a hotspot data record.
[0077] In another example, data records of possible hotspot accesses and the time when the hotspot accesses occur may be pre-set according to the service characteristics of the upper-layer application.
[0078] For example, a corporate account makes large-scale payments at midnight on the 1st of each month. Then midnight on the 1st of each month can be determined as the time when the hotspot access occurs, and the data record of the corporate account's fund balance at the time of occurrence is the hotspot data record.
[0079] Of course, in other examples, other methods may be used to detect hotspot data records in the low-speed database, and this specification does not impose any special restrictions on this.
[0080] Step 204: After detecting the hotspot data record, synchronize the hotspot data record from the low-speed database to the high-speed database, and update the database status of the hotspot data record from not switched to being switched.
[0081] In this specification, the database status of each data record can be maintained. The initial status of the database status is not switched, which means that the corresponding data record is still provided with read, write, update and other access services by the low-speed database and has not yet been switched to the high-speed database.
[0082] Based on the aforementioned step 202, after detecting the hot data record in the low-speed database, in order to improve the access speed of the hot data record, the hot data record can be synchronized from the low-speed database to the high-speed database, and the database status of the hot data record can be updated from not switched to switching. The switching state can indicate that the corresponding data record is being synchronized from the low-speed database to the high-speed database.
[0083] In this specification, before synchronizing hotspot data records, the hotspot data records to be synchronized can be initialized in the high-speed database so that in the subsequent hotspot data record synchronization process, the update commands newly initiated by the upper-layer application can be executed in the high-speed database.
[0084] The initialization process may include: first writing the identifier of the hotspot data record (such as account identifier, product identifier, etc.) in the high-speed database, that is, creating a new data record in the high-speed database, the identifier of the new data record is the identifier of the hotspot data record, and then the preset initial field value can be written into the target field that needs to be synchronized corresponding to the hotspot data record identifier in the high-speed database.
[0085] Among them, the target field is usually a field that has hot access, such as the product inventory in the aforementioned popular product example, the account balance in the aforementioned popular account example, etc. The field value of the target field needs to be synchronized to the high-speed database.
[0086] For example, assuming that the hot data record detected in the above steps is the data record where the funds balance of Company A's account is located, the target field is the funds balance. Before synchronizing the hot data record, a data record corresponding to Company A's account can be created in the high-speed database. For example, a record belonging to Company A's account is first created in the high-speed database, and then the funds balance field of Company A's account in the high-speed database is set to a preset initial field value, such as 0 yuan, that is, 0 is written in the funds balance field of Company A's account in the high-speed database.
[0087] Step 206: In response to the update command sent by the upper layer application, query the database status of the target data record pointed to by the update command.
[0088] In this specification, upper-layer applications' access to data records in a database generally includes update access and read access. For update access, the upper-layer application can send an update command, while for read access, the upper-layer application can send a read command. Both update and read commands can specify the data record identifier of the data record to be accessed, such as an account ID or product ID.
[0089] In this step, after receiving the update command sent by the upper layer application, the database status of the data record specified by the update command (hereinafter referred to as the target data record for easy distinction) may be queried first.
[0090] Step 208 : When the database status of the target data record is switching, the update command is sent to the high-speed database, and a temporary data record corresponding to the update command is written into a temporary table preset in the low-speed database.
[0091] Based on the query result of step 206, if the database status of the target data record is "switching", it indicates that the target data record is a hot data record and its frequently accessed target field is being synchronized from the low-speed database to the high-speed database. To improve the subsequent access speed of the target data record, the update command can be sent to the high-speed database, and the high-speed database will execute the update operation of the target data record. At this time, regardless of whether the target field value of the target data record in the low-speed database has been synchronized to the high-speed database, the update operation can be executed in the high-speed database.
[0092] Specifically, after the target field value of the target data record in the low-speed database has been synchronized to the high-speed database, the update command of the upper-layer application can be directly executed in the high-speed database, for example, to update the fund balance or inventory. If the target field value of the target data record in the low-speed database has not yet been synchronized to the high-speed database, since the target field value of the target data record in the high-speed database is the initial value in the aforementioned initialization process, the update command will also be executed based on the initial value. Subsequent synchronization processes can then continue to perform updates based on the low-speed field value in the low-speed database.
[0093] On the other hand, since the target data record is undergoing a database switching operation, to ensure data reliability, a temporary data record corresponding to the update command can also be written into a temporary table preset in the low-speed database. The temporary data record will subsequently be periodically and asynchronously updated to the corresponding data record in the low-speed database.
[0094] Step 210: After the temporary data record is written successfully, a message indicating that the update is successful is returned to the upper layer application.
[0095] In this specification, since the database status of the target data record is switching, that is, the database switching of the target data record has not been completed, the execution result of the update command by the low-speed database can be returned to the upper-level application at this time, and after the temporary data record is successfully written, a write success message can be returned to the upper-level application.
[0096] In this specification, the speed of writing the temporary data record in the temporary table is much faster than directly executing the corresponding update command in the data record. After the temporary data record is successfully written to the temporary table, a write success is returned to the upper-level application. For the upper-level application, the execution speed of the update command is also improved.
[0097] It can be seen from the above description that this manual can query the database status of the target data record pointed to by the update command in response to the update command sent by the upper-level application. When the database status is switching, it can be explained that the target data record is a hot data record and is being synchronized from the low-speed database to the high-speed database. On the one hand, the update command can be sent to the high-speed database, and on the other hand, the temporary data record corresponding to the update command can be written into the temporary table preset in the low-speed database, and after the temporary data record is written successfully, the update success is returned to the upper-level application.
[0098] The above technical solution allows, on the one hand, for hotspot data records to be synchronized to the high-speed database in a timely manner when a hotspot access occurs, enabling database switching for the hotspot data records and improving access speed for the hotspot data records. Furthermore, during the hotspot data record synchronization process, corresponding update operations can be performed simultaneously in both the high-speed and low-speed databases, without suspending data access to the hotspot data records, thus achieving database switching without service interruption.
[0099] In this specification, when the database status of the target data record is not switched, it can be said that the target data record is not a hot data record and there is no need to switch the database. The target data record is still provided with access services by the low-speed database, and the update command can be sent to the low-speed database. After the low-speed database successfully updates the target data record, an update success message can be returned to the upper-level application.
[0100] In this specification, the upper-layer application may also send a read command to query the target field value of the target data record. For this read command, the database status of the target data record may also be queried first. If the database status of the target data record is not switched, the read command may be sent to the low-speed database. If the database status of the target data record is switching, the read command may also be sent to the low-speed database.
[0101] It is worth noting that when the database status of the target data record is switching, for a low-speed database, the read command queries the target field value in the corresponding data record, and does not include the temporary data record in the temporary table. For the target data record, if its temporary data record in the temporary table has not yet been updated to the corresponding target data record, the target field value in the target data record read at this time may be inaccurate. Compared with the slow access speed of hot data records in the database, this short-term data inaccuracy is acceptable. After the temporary data record in the temporary table is updated to the target data record, the target field value read is the accurate value.
[0102] Of course, in other examples, when the database status of the target data record is switching, the read command can also be sent to the high-speed database. Since the data has not been verified, the target field value queried in the high-speed database may also be inaccurate. This manual does not impose any special restrictions on this.
[0103] In this manual, to ensure data consistency, after the hotspot data records are synchronized from the low-speed database to the high-speed data, the data consistency can also be checked. After the verification is correct, the database status of the hotspot data records can be updated from switching to switching completed.
[0104] In this specification, whether the hotspot data records in the low-speed database and the high-speed database are consistent can be checked based on preset checking rules.
[0105] The verification rules may include verification times, verification cycles, etc.
[0106] For example, the verification rule can be to perform 5 checks with a verification cycle of 1 day. That is, after synchronizing the hotspot data records from the low-speed database to the high-speed database, check whether the hotspot data records in the low-speed database and the high-speed database are consistent every day for 5 consecutive days. During these 5 days, the hotspot data records will still be updated and accessed. If the hotspot data records are consistent for 5 consecutive days, it can be considered that the high-speed database is running stably, and the database status of the hotspot data records is updated from switching to switching completed. If the hotspot data records are inconsistent at a certain time, a rollback operation of the database switch can be performed, and the low-speed database is still used to provide access services for the hotspot data records.
[0107] In this specification, for update commands for target data records in the switching-completed state, the update commands can be sent to the high-speed database, which then provides high-speed database access services to upper-layer applications. Furthermore, similar to the database state being switched, temporary data records corresponding to the update commands can also be written to a temporary table preset in the low-speed database, thereby maintaining the full set of data records in the low-speed database.
[0108] For the update command of the target data record in the switching completion state, after the high-speed database is successfully updated and the temporary data record is successfully written, an update success message can be returned to the upper-layer application.
[0109] It is worth noting that if the temporary data record fails to be written, the low-speed database will return a write failure. After the low-speed database write fails, the upper-layer application can wait for a preset period of time and then re-initiate the update command. At this time, the temporary data record will be re-written to the temporary table of the low-speed database, and the update command will also be sent to the high-speed database. For the high-speed database, since the update command has already been executed, it will perform idempotent processing and will not repeat the update.
[0110] In this specification, for a read command of a target data record in a switching completion state, the read command can be sent to a high-speed database, and then accurate data can be read from the high-speed database and returned to an upper-layer application.
[0111] In this specification, when the hotspot data records in the low-speed database and the high-speed database are inconsistent, a database switch rollback operation is performed. On the one hand, the database status of the hotspot data record can be updated from being switched back to not switched, so that subsequent access commands for the hotspot data record are sent to the low-speed database. On the other hand, because the low-speed database stores the full set of data records, the hotspot data record in the high-speed database can be directly deleted during the database switch rollback.
[0112] In this specification, when a hot data record is no longer hot-accessed, the database status of the hot data record can be updated back to unswitched, and the hot data record in the high-speed database can be deleted, thereby releasing storage resources of the high-speed database.
[0113] In one example, for each hot data record, the number of temporary data records corresponding to the hot data record in the temporary table within a temporary data record update cycle can be counted. If the number of temporary data records is less than a preset hotspot threshold, it can indicate that the access frequency of this hot data record has decreased, and it can be determined that it is no longer a hotspot access.
[0114] Of course, in other examples, other methods can also be used to detect data records that no longer have hotspot accesses. For example, the middle layer can count the number of accesses to the hotspot data records within a certain period of time, etc. This manual does not impose any special restrictions on this.
[0115] The database switching method provided in this specification is described in detail below by taking the remaining amount of resources of an account, such as the fund balance of an account, as an example.
[0116] The low-speed database stores the balances of all accounts. In some business scenarios, some accounts may experience concentrated transactions, such as concentrated loan disbursements from lending accounts. Due to the relatively slow response speed of the low-speed database, some transactions may fail to be recorded, meaning that the balances corresponding to some transactions in the relevant accounts in the low-speed database may fail to update.
[0117] By using the database switching solution provided in this specification, a high-speed database with fast accounting speed can be set up on top of a low-speed database. Since high-speed databases often have limited capacity and cannot accommodate the full range of account currency balance records, it is possible to detect accounts with hotspot access, that is, accounts with concentrated account transactions, and then synchronize the fund balances of these accounts (hereinafter referred to as account balances) to the high-speed database, which provides high-speed accounting services for these accounts. The accounting can refer to the update of the balances of the corresponding accounts in the database.
[0118] In this example, three database states can be set: unswitched, switching, and completed. The unswitched state indicates that the account has not been accessed through a hotspot, and services such as bookkeeping and balance inquiries are provided by the low-speed database. The initial database state for each account is unswitched. Switching indicates that the corresponding account has been accessed through a hotspot, and services such as bookkeeping and balance inquiries are being switched from the low-speed database to the high-speed database. For example, the corresponding account balance is being synchronized from the low-speed database to the high-speed database, but the account balance verification has not yet been completed. Completed indicates that the account balances in the low-speed and high-speed databases have been verified, and services such as bookkeeping and balance inquiries for the account that has been accessed through a hotspot have been switched to the high-speed database, which now provides high-speed bookkeeping services to upper-layer applications.
[0119] account Database status Company A Not switched Company B Switching C user Switching completed
[0120] Table 1
[0121] In this example, the database status of each account's fund balance can be maintained. Refer to Table 1 for an example. The database status for Enterprise A's account is Not Switched, the database status for Enterprise B's account is Switching, and the database status for User C is Switching Completed.
[0122] In this example, the lock failure events for account balance update operations reported by the low-speed database can be monitored, and then the number of lock failure events for the same account within a preset time period can be counted, and it can be determined whether the number reaches the preset failure number.
[0123] For example, it can be determined whether the number of lock failure events for the same account reaches 10 times within 1 minute. If it reaches 10 times, it can be considered that the account has been accessed by a hotspot, and the record containing the account balance can be determined as a hotspot data record.
[0124] In this example, after detecting an account that has been accessed through a hotspot, the database status of the account can be updated from not switched to switching, and the account balance can be synchronized from the low-speed database to the high-speed database.
[0125] account Database status Company A Switching Company B Switching C user Switching completed
[0126] Table 2
[0127] For example, if a hotspot access is detected for Enterprise A's account, the database status of Enterprise A's account can be updated from "Not Switched" to "Switching", resulting in the database status of the various account balances shown in Table 2. The balance of Enterprise A's account can then be synchronized from the low-speed database to the high-speed database. Before synchronizing the balance of Enterprise A's account, a data record corresponding to Enterprise A's account can be created in the high-speed database, and the value of the balance field (i.e., the target field) can be set to 0.
[0128] After synchronizing the account balance to the high-speed database, the high-speed and low-speed databases can be checked for consistency based on a pre-set reconciliation cycle. If they are consistent, the high-speed database can provide accounting services for the account to upper-layer applications. If they are inconsistent, a rollback operation can be performed.
[0129] Using Company A's account as an example, after synchronizing the balance of Company A's account to the high-speed database, the high-speed and low-speed databases can be checked for consistency for seven consecutive days. For example, the high-speed and low-speed databases can be checked for consistency at midnight for seven consecutive days. Of course, in addition to checking the account balance, other checks can be performed to ensure consistency in the number of entries made to Company A's account in each verification cycle between the high-speed and low-speed databases. This manual does not impose any specific restrictions on this.
[0130] account Database status Company A Switching completed Company B Switching C user Switching completed
[0131] Table 3
[0132] If the balances of the A company account in the high-speed database and the low-speed database are consistent, the database status of the A company account can be updated from the switching in progress state to the switching completed state, and the database status of the fund balances of each account shown in Table 3 is obtained.
[0133] If the balances of Company A's account in the high-speed database and the low-speed database are inconsistent, the database status of Company A's account can be updated from switching back to not switching, and the data records of Company A's account in the high-speed database can be deleted.
[0134] In this example, under different database states, different databases may provide accounting and balance inquiry services.
[0135] Taking the account of Company A as an example, after receiving a balance access command for the account, the database status of Company A can be obtained. The balance access command can include a balance update command involving inflow or outflow, or a balance query command.
[0136] When the database status of A's enterprise account is not switched, both the balance update command and the balance query command can be sent to the low-speed database, which provides the corresponding service.
[0137] When the database status of the A enterprise account is switching, the balance update command can be sent to the high-speed database, and the high-speed database executes the balance update.
[0138] It is worth noting that when the high-speed database executes the balance update command, the fund balance of the A enterprise account that needs to be synchronized from the low-speed database to the high-speed database may or may not have been synchronized.
[0139] If the synchronization is complete, the high-speed database can directly perform the balance update operation based on the synchronized balance of the A company account.
[0140] If synchronization has not yet been completed, the high-speed database can first update the balance of Company A's account according to the update amount involved in the balance update command, and then perform another balance update operation when its balance is synchronized from the low-speed database. For example, if the current balance update operation is to record 100 yuan, and the balance of Company A's account in the high-speed database is the initial value of 0 before synchronization is completed, the accounting operation can be directly executed to update the balance to 100 yuan. If the balance of Company A's account in the low-speed database is 1000 yuan, then when the balance of 1000 yuan is synchronized, the accounting operation can also be directly executed to update the balance of Company A's account from 100 yuan to 1100 yuan.
[0141] On the other hand, the temporary data record corresponding to the balance update command can also be written to a temporary table in the low-speed database. The temporary data record in the temporary table of the low-speed database can be periodically recorded to the corresponding account balance, for example, every 10 seconds or 20 seconds. In this example, after the temporary data record is successfully written to the temporary table of the low-speed database, a successful update is returned to the upper-level application. In other words, even if the balance update in the high-speed database fails, a successful message can be returned to the upper-level application. Because the temporary data record in the low-speed database has been successfully written, the data in the low-speed database is accurate.
[0142] After synchronizing the account balance in the low-speed database to the high-speed database, this manual records the account's active transactions in a temporary table in the low-speed database. This ensures that the low-speed database records the full set of active transactions, allowing for a timely switch back to the low-speed database in the event of high-speed database instability. Furthermore, compared to bookkeeping operations, writing to temporary tables is often much faster. This manual returns a successful bookkeeping (i.e., update success) message to the upper-level application after the temporary data record is successfully written. This can also, to a certain extent, improve the bookkeeping speed of hotspot access accounts, thereby enhancing the service experience.
[0143] If the database status of Company A's account is switching, balance query commands can be sent to either the slow or fast database. Balance queries in this state may experience temporary balance inaccuracies, but this is acceptable compared to the increased accounting speed.
[0144] When the database status of A's company account is switched, the balance update command can still be sent to the high-speed database on the one hand, and the high-speed database can execute the balance update. On the other hand, the temporary data record corresponding to the balance update command can be written into the temporary table of the low-speed database. After the high-speed database is successfully updated and the temporary data record is successfully written, the update success can be returned to the upper-level application. If the high-speed database update fails, a failure message can be returned to the upper-level application. For example, if the account is debited and the amount debited is greater than the account balance, the high-speed database will return an update failure message, and then the upper-level application can return an update failure message, that is, the high-speed database can intercept the overdraft of funds.
[0145] When the database status of the A enterprise account is switched, the balance query command can be sent to the high-speed database, and the high-speed database provides the corresponding balance query service.
[0146] It can be seen from the above description that by adopting the technical solution provided in this specification, for the hotspot account that is switching status, on the one hand, the balance update command can be sent to the medium-speed database, and the high-speed database will execute the balance update. On the other hand, the temporary data record corresponding to the balance update command is written into the temporary table of the low-speed database. If the temporary data record is written successfully, the update success message can be returned to the upper-level application, thereby eliminating the need to suspend the accounting operation of the hotspot account and realizing database switching without service interruption.
[0147] Of course, in other examples of this specification, the switching state can be divided into more detailed categories, for example, the switching state can be further divided into a synchronization state and a verification state. The synchronization state represents the process of synchronizing the hotspot data records in the low-speed database to the high-speed database; the verification state represents the process of verifying whether the hotspot data records in the low-speed database and the high-speed database are consistent after the synchronization state is completed.
[0148] Similarly, the switching completion state can also be divided into more detailed categories. For example, the switching completion state can be further divided into the update switching state and the customer switching state. Among them, the update switching state means that after verifying that the hotspot data in the two databases are consistent, in response to the update command of the upper-layer application, the high-speed database successfully records the account and the temporary data record in the low-speed data is successfully written. The update success is returned, but the query service of the hotspot data record is still provided by the low-speed database; the customer switching state is a state after the update switching state, which means that the query service of the hotspot data record is also switched to the high-speed database.
[0149] The specific division of database status can be set by developers according to business needs, and this manual does not impose any special restrictions on this.
[0150] Corresponding to the aforementioned embodiment of the database switching method, this specification also provides an embodiment of a database switching device.
[0151] The embodiments of the database switching device of this specification can be applied in electronic devices. The device embodiments can be implemented by software, hardware, or a combination of software and hardware. Taking software implementation as an example, as a device in a logical sense, it is formed by the processor of the electronic device in which it is located reading the corresponding computer program instructions in the non-volatile memory into the memory and running them. From the hardware level, if Figure 3 The following is a hardware structure diagram of the electronic device where the database switching device of this manual is located, except Figure 3 In addition to the processor, memory, network interface, and non-volatile memory shown, the electronic device in which the device is located in the embodiment may also include other hardware according to the actual function of the electronic device, which will not be described in detail.
[0152] Figure 4 This is a block diagram of a database switching device shown in an exemplary embodiment of this specification.
[0153] Please refer to Figure 4 The database switching device can be applied to the aforementioned Figure 3 On the electronic device shown, such as a server, the device includes: a hotspot detection unit, a hotspot synchronization unit, a status query unit, an update execution unit and a result return unit.
[0154] Among them, the hotspot detection unit detects hotspot data records in the low-speed database according to a preset hot and cold distinction mechanism;
[0155] a hotspot synchronization unit, which, after detecting the hotspot data record, synchronizes the hotspot data record from the low-speed database to the high-speed database, and updates the database status of the hotspot data record from not switched to being switched;
[0156] a status query unit, responding to an update command sent by an upper layer application, querying a database status of a target data record pointed to by the update command;
[0157] an update execution unit, which sends the update command to the high-speed database and writes a temporary data record corresponding to the update command into a temporary table preset in the low-speed database when the database status of the target data record is switching;
[0158] The result returning unit returns an update success to the upper layer application after the temporary data record is written successfully.
[0159] Optionally, the status query unit queries the database status of the target data record in response to a read command sent by an upper-layer application and directed to the target data record, and sends the read command to the low-speed database when the database status of the target data record is switching.
[0160] Optionally, the hotspot detection unit:
[0161] Monitor the lock failure events reported by the low-speed database for the target data record;
[0162] When the number of lock failure events that occur in the target data record reaches a preset number of failures, the target data record is determined as a hotspot data record.
[0163] Optionally, the step of executing database switching also includes: after synchronizing the hotspot data records from the low-speed database to the high-speed database, checking whether the hotspot data records in the low-speed database and the high-speed database are consistent based on preset verification rules; when the hotspot data records in the low-speed database and the high-speed database are consistent, updating the database status of the hotspot data records from the switching status to the switching completed status.
[0164] Optionally, when the database status of the target data record is in the switching completion state, the update execution unit sends the update command to the high-speed database and writes a temporary data record corresponding to the update command into a temporary table preset in the low-speed database;
[0165] The result returning unit returns an update success to the upper layer application after the high-speed database is updated successfully and the temporary data record is written successfully;
[0166] In response to a read command directed to the target data record sent by an upper layer application, when the database status of the target data record is a switching completion status, the read command is sent to the high-speed database.
[0167] Optionally, the step of executing database switching further includes: when the hotspot data records in the low-speed database and the high-speed database are inconsistent, deleting the hotspot data record in the high-speed database and updating the database status of the hotspot data record back to unswitched.
[0168] Optionally, the step of executing database switching further includes: when the database status of the target data record is not switched, sending both the update command and the read command from the upper layer application to the low-speed database.
[0169] Optionally, before synchronizing the hotspot data record from the low-speed database to the high-speed database, the hotspot synchronization unit further includes: synchronizing the identifier of the hotspot data record to the high-speed database, and writing an initial field value in the target field to be synchronized.
[0170] Optionally, the hotspot synchronization unit updates the initial field value in the high-speed database according to the low-speed field value under the target field of the hotspot data record in the low-speed database to achieve synchronization of the hotspot data record.
[0171] Optionally, the step of executing database switching also includes: synchronizing the temporary data records in the temporary table to the corresponding data records in the low-speed database based on a preset period; for each period, counting the number of temporary data records corresponding to the hotspot data records in the period; when the number of temporary data records is less than the hotspot threshold, updating the database status of the hotspot data record back to unswitched.
[0172] Optionally, the step of executing database switching further includes: after updating the database status of the hotspot data record back to unswitched, deleting the hotspot data record stored in the high-speed database.
[0173] Optionally, the capacity of the high-speed database is smaller than the capacity of the low-speed database.
[0174] Optionally, the data record includes the remaining amount of resources of the account.
[0175] The implementation process of the functions and effects of each unit in the above-mentioned device is specifically described in the implementation process of the corresponding steps in the above-mentioned method, and will not be repeated here.
[0176] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to the partial description of the method embodiments. 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 displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this specification. A person of ordinary skill in the art can understand and implement it without paying any creative work.
[0177] The systems, devices, modules, or units described in the above embodiments may be implemented by computer chips or entities, or by products having certain functions. A typical implementation device is a computer, which may be in the form of a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email transceiver, game console, tablet computer, wearable device, or any combination of these devices.
[0178] In a typical configuration, a computer includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0179] Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.
[0180] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be used to store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices, or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.
[0181] Corresponding to the embodiment of the aforementioned database switching method, this specification further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the program is executed by a processor, the following steps are implemented:
[0182] Detect hot data records in the low-speed database based on the preset hot and cold distinction mechanism;
[0183] After detecting the hotspot data record, synchronizing the hotspot data record from the low-speed database to the high-speed database, and updating the database status of the hotspot data record from not switched to being switched;
[0184] In response to an update command sent by an upper-layer application, querying a database status of a target data record pointed to by the update command;
[0185] When the database status of the target data record is switching, the update command is sent to the high-speed database, and a temporary data record corresponding to the update command is written into a temporary table preset in the low-speed database;
[0186] After the temporary data record is written successfully, an update success is returned to the upper layer application.
[0187] Optionally, also include:
[0188] In response to a read command directed to the target data record sent by an upper layer application, querying a database status of the target data record;
[0189] When the database status of the target data record is switching, the read command is sent to the low-speed database.
[0190] Optionally, detecting hotspot data records in the low-speed database according to a preset hot and cold distinction mechanism includes:
[0191] Monitor the lock failure events reported by the low-speed database for the target data record;
[0192] When the number of lock failure events that occur in the target data record reaches a preset number of failures, the target data record is determined as a hotspot data record.
[0193] Optionally, also include:
[0194] After synchronizing the hotspot data records from the low-speed database to the high-speed database, checking whether the hotspot data records in the low-speed database and the high-speed database are consistent based on a preset checking rule;
[0195] When the hotspot data records in the low-speed database and the high-speed database are consistent, the database status of the hotspot data records is updated from a switching-in-progress state to a switching-completed state.
[0196] Optionally, also include:
[0197] When the database status of the target data record is in the switching completion state, the update command is sent to the high-speed database, and a temporary data record corresponding to the update command is written into a temporary table preset in the low-speed database;
[0198] After the high-speed database is updated successfully and the temporary data record is written successfully, a return of update success is sent to the upper layer application;
[0199] In response to a read command directed to the target data record sent by an upper layer application, when the database status of the target data record is a switching completion status, the read command is sent to the high-speed database.
[0200] Optionally, also include:
[0201] In the case that the hotspot data records in the low-speed database and the high-speed database are inconsistent, the hotspot data record in the high-speed database is deleted, and the database status of the hotspot data record is updated back to unswitched.
[0202] Optionally, also include:
[0203] When the database status of the target data record is not switched, the update command and the read command from the upper layer application are both sent to the low-speed database.
[0204] Optionally, before synchronizing the hotspot data record from the low-speed database to the high-speed database, the method further includes:
[0205] The identifier of the hotspot data record is synchronized to the high-speed database, and the initial field value is written into the target field that needs to be synchronized.
[0206] Optionally, synchronizing the hotspot data record from the low-speed database to the high-speed database includes:
[0207] The initial field value in the high-speed database is updated according to the low-speed field value under the target field of the hotspot data record in the low-speed database to achieve synchronization of the hotspot data record.
[0208] Optionally, also include:
[0209] Synchronizing the temporary data records in the temporary table to corresponding data records in the low-speed database based on a preset period;
[0210] For each period, counting the number of temporary data records corresponding to the hot data records in the period;
[0211] When the number of the temporary data records is less than the hotspot threshold, the database status of the hotspot data records is updated back to unswitched.
[0212] Optionally, also include:
[0213] After the database status of the hotspot data record is updated back to unswitched, the hotspot data record stored in the high-speed database is deleted.
[0214] Optionally, the capacity of the high-speed database is smaller than the capacity of the low-speed database.
[0215] Optionally, the data record includes the remaining amount of resources of the account.
[0216] The foregoing description of this specification describes specific embodiments. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0217] The above description is only a preferred embodiment of this specification and is not intended to limit this specification. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of this specification should be included in the scope of protection of this specification.
Claims
1. A database switching method, comprising: Detect hot data records in the low-speed database based on the preset hot and cold distinction mechanism; After detecting the hotspot data record, synchronizing the hotspot data record from the low-speed database to the high-speed database, and updating the database status of the hotspot data record from not switched to being switched, so as to indicate that the hotspot data record is being synchronized from the low-speed database to the high-speed database; After synchronizing the hotspot data records from the low-speed database to the high-speed database, checking whether the hotspot data records in the low-speed database and the high-speed database are consistent based on a preset checking rule; When the hotspot data records in the low-speed database and the high-speed database are consistent, updating the database status of the hotspot data record from the switching state to the switching completed state; In response to an update command sent by an upper-layer application, querying a database status of a target data record pointed to by the update command; When the database status of the target data record is switching, the update command is sent to the high-speed database, and a temporary data record corresponding to the update command is written into a temporary table preset in the low-speed database; After the temporary data record is written successfully, an update success is returned to the upper layer application.
2. The method according to claim 1, further comprising: In response to a read command directed to the target data record sent by an upper layer application, querying a database status of the target data record; When the database status of the target data record is switching, the read command is sent to the low-speed database.
3. The method according to claim 1, wherein detecting hot data records in a low-speed database according to a preset hot / cold distinction mechanism comprises: Monitor the lock failure events reported by the low-speed database for the target data record; When the number of lock failure events that occur in the target data record reaches a preset number of failures, the target data record is determined as a hotspot data record.
4. The method according to claim 1, further comprising: When the database status of the target data record is in the switching completion state, the update command is sent to the high-speed database, and a temporary data record corresponding to the update command is written into a temporary table preset in the low-speed database; After the high-speed database is updated successfully and the temporary data record is written successfully, a return of update success is sent to the upper layer application; In response to a read command directed to the target data record sent by an upper layer application, when the database status of the target data record is a switching completion status, the read command is sent to the high-speed database.
5. The method according to claim 1, further comprising: In the case that the hotspot data records in the low-speed database and the high-speed database are inconsistent, the hotspot data record in the high-speed database is deleted, and the database status of the hotspot data record is updated back to unswitched.
6. The method according to claim 1, further comprising: When the database status of the target data record is not switched, the update command and the read command from the upper layer application are both sent to the low-speed database.
7. The method according to claim 1, before synchronizing the hotspot data record from the low-speed database to the high-speed database, further comprising: The identifier of the hotspot data record is synchronized to the high-speed database, and the initial field value is written into the target field that needs to be synchronized.
8. The method according to claim 7, wherein synchronizing the hotspot data records from the low-speed database to the high-speed database comprises: The initial field value in the high-speed database is updated according to the low-speed field value under the target field of the hotspot data record in the low-speed database to achieve synchronization of the hotspot data record.
9. The method according to claim 1, further comprising: Synchronizing the temporary data records in the temporary table to corresponding data records in the low-speed database based on a preset period; For each period, counting the number of temporary data records corresponding to the hot data records in the period; When the number of the temporary data records is less than the hotspot threshold, the database status of the hotspot data records is updated back to unswitched.
10. The method according to claim 9, further comprising: After the database status of the hotspot data record is updated back to unswitched, the hotspot data record stored in the high-speed database is deleted.
11. The method according to claim 1, wherein the capacity of the high-speed database is smaller than that of the low-speed database.
12. The method according to claim 1, wherein the hotspot data record includes the remaining amount of resources of the account.
13. A database switching device, comprising: A hotspot detection unit detects hotspot data records in the low-speed database based on a preset hot and cold distinction mechanism; a hotspot synchronization unit, which, after detecting the hotspot data record, synchronizes the hotspot data record from the low-speed database to the high-speed database, and updates the database state of the hotspot data record from "not switched" to "switching", so as to indicate that the hotspot data record is being synchronized from the low-speed database to the high-speed database; The hotspot synchronization unit is further configured to: after synchronizing the hotspot data record from the low-speed database to the high-speed database, check whether the hotspot data records in the low-speed database and the high-speed database are consistent based on a preset verification rule; and, if the hotspot data records in the low-speed database and the high-speed database are consistent, update the database status of the hotspot data record from a switching-in-progress state to a switching-completed state; a status query unit, responding to an update command sent by an upper layer application, querying a database status of a target data record pointed to by the update command; an update execution unit, which sends the update command to the high-speed database and writes a temporary data record corresponding to the update command into a temporary table preset in the low-speed database when the database status of the target data record is switching; The result returning unit returns an update success to the upper layer application after the temporary data record is written successfully.
14. An electronic device comprising: processor; a memory for storing processor-executable instructions; The processor implements the method according to any one of claims 1 to 12 by running the executable instructions.
15. A computer-readable storage medium having computer instructions stored thereon, wherein when the instructions are executed by a processor, the steps of the method according to any one of claims 1 to 12 are implemented.
Citation Information
Patent Citations
Database processing method and system and data access method and system
CN104598459A
Accounting method and device for hotspot account
CN111061748A