Method and system for generating distributed incremental serial number
By combining the advantages of MySQL and Redis, the method of generating distributed incremental serial numbers solves the MySQL performance bottleneck and Redis data consistency problems in high concurrency, and realizes efficient and reliable serial number generation and management.
Patent Information
- Application Number
- CN202411784173.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-06
- Publication Date
- 2025-04-11
AI Technical Summary
In the case of high concurrency, performance bottlenecks may occur when using MySQL, while using Redis cannot guarantee data consistency and durability, making it difficult to meet the needs of serial number generation in distributed systems.
Combining the advantages of MySQL and Redis, a method of generating distributed incremental serial numbers is adopted. By receiving serial number requests, check whether there are available serial numbers in the MySQL database. If not, a new serial number will be regenerated and divided into two groups. The first group will be fed back to the requesting party, and the second group will be saved to the Redis database for subsequent use.
It realizes efficient and reliable distributed serial number generation and management, ensures the global uniqueness and incrementality of serial number, reduces the number of accesses to MySQL, and improves the overall stability and reliability of the system.
Smart Images

Figure CN119807264A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and system for generating distributed incremental sequence numbers. Background Art
[0002] In today's complex distributed systems, with the rapid development of the Internet and cloud computing, enterprises are facing an urgent need to uniquely identify large amounts of data. The distributed serial number generation scheme has become an important technical means to meet this demand due to its global uniqueness and optional global increment characteristics. In particular, serial numbers are widely used in e-commerce, financial payments, logistics tracking and other fields, which can effectively identify key data such as unique order numbers, transaction IDs and logistics numbers, ensuring the efficiency and accuracy of the system.
[0003] Traditional serial number generation solutions mainly rely on centralized databases, such as MySQL. However, as the business scale expands, the bottleneck of single-point databases becomes apparent, resulting in performance degradation during concurrent access. To solve this problem, in-memory databases such as Redis have been gradually introduced as serial number storage solutions in recent years. With its high concurrency and high performance, Redis can quickly process a large number of serial number generation requests, thereby improving the overall response speed and stability of the system.
[0004] However, different storage solutions also face their own challenges. When using MySQL, although data consistency can be ensured, performance bottlenecks may occur in high concurrency situations. Using Redis can provide higher performance, but how to ensure data consistency and persistence is still an urgent problem to be solved. Therefore, combining the advantages of these two solutions, a new distributed sequence number generation mechanism is proposed to meet the growing business needs and technical challenges. This mechanism will make full use of the persistence characteristics of MySQL and the efficient read and write capabilities of Redis to achieve a more flexible and efficient sequence number generation solution that adapts to the application scenarios of modern distributed systems. Summary of the invention
[0005] In order to solve the deficiencies of the prior art, the technical solution adopted by the present invention includes:
[0006] A method for generating a distributed incremental sequence number, comprising:
[0007] Receive a sequence number request;
[0008] Checking whether there is an available serial number in the first database;
[0009] If there is no available sequence number, a new sequence number is regenerated and divided into two groups. The first group of sequence numbers is fed back to the requesting party, and the second group of sequence numbers is saved in the first database for subsequent use in the sequence number request.
[0010] Exemplarily, the checking whether there is an available serial number in the first database includes:
[0011] Obtain the start sequence number and the end sequence number from the sequence numbers in the first database;
[0012] If the start sequence number is equal to the end sequence number, it is determined that there is no available sequence number;
[0013] If the start sequence number is smaller than the end sequence number, it is determined that there is an available sequence number.
[0014] Exemplarily, the method further includes: if there is an available sequence number in the first database, directly obtaining the sequence number from the first database and feeding it back to the requester;
[0015] The first database may be Redis, and the second database may be MySQL.
[0016] Exemplarily, the regeneration of the new serial number is performed incrementally based on the end serial number, thereby ensuring the uniqueness and incrementality of the serial number.
[0017] Exemplarily, the method further includes: when the first database loses a sequence number due to an abnormality, restoring the sequence number using the latest sequence number stored in the second database to ensure that the historically generated sequence number will not be allocated repeatedly.
[0018] Exemplarily, the new serial numbers are divided into two groups, the first group of serial numbers is fed back to the requesting party, and the second group of serial numbers is saved in the first database, and the number of serial numbers in the second group is much larger than the number of serial numbers in the first group, and the latest serial numbers in the second database are updated at the same time.
[0019] The method also provides a device for generating a distributed incremental sequence number, characterized by comprising:
[0020] An interface module, used for receiving a sequence number request;
[0021] A checking module, used for checking the status of the serial number in the first database;
[0022] The generation module is used to regenerate new sequence numbers for grouping when no sequence numbers are available.
[0023] Exemplarily, the logic layer module further includes:
[0024] A storage module, responsible for storing the second group of serial numbers in the first database and updating the latest serial numbers in the second database;
[0025] The feedback module is used to feed back the first group of sequence numbers to the requesting party.
[0026] Exemplarily, the inspection module further includes:
[0027] An acquisition submodule, used to extract a start sequence number and an end sequence number from the first database;
[0028] The judgment submodule is used to judge the availability of the start sequence number and the end sequence number.
[0029] Exemplarily, the generation module uses an incremental algorithm to generate a new serial number based on the end serial number to ensure uniqueness and incrementality.
[0030] Exemplarily, the storage module has an exception handling function, and automatically switches to the second database to recover the sequence number when an exception occurs in the first database.
[0031] The present invention also provides a computer-readable storage medium, characterized in that a computer program is stored on the storage medium, and when the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
[0032] The present invention also provides an electronic device, characterized in that it includes a processor and a memory;
[0033] The memory is used to store computer programs;
[0034] The processor is configured to execute the method according to any one of claims 1 to 6 by calling a stored computer program.
[0035] The present invention also relates to a computer program product, comprising a computer program and / or instructions, characterized in that the computer program and / or instructions implement the steps of the above method when executed by a processor.
[0036] The beneficial effects of the present invention are as follows: the present invention combines the advantages of MySQL and Redis to provide an efficient and reliable distributed sequence number generation and management method, which is specifically reflected in the following advantages:
[0037] Ensure the global uniqueness and incrementality of the serial number; in any case, whether it is a system restart or other abnormal situation, the continuity of the serial number can be restored through MySQL, thus avoiding duplication or loss of the serial number.
[0038] By using Redis's efficient caching mechanism and pre-fetching serial numbers in batches, the number of accesses to MySQL is significantly reduced. This design not only reduces the pressure on the database, but also speeds up the allocation of serial numbers to meet the needs of high concurrent requests.
[0039] When Redis fails or the server restarts, the system can quickly switch to MySQL to ensure the continued availability of serial numbers, avoid service interruptions due to temporary unavailability, and improve the overall stability and reliability of the system.
[0040] By caching frequent serial number requests in Redis, direct access to MySQL is reduced, database resource consumption and operating costs are reduced, and it can still run efficiently under limited resources.
[0041] The device or system architecture set up by this method allows flexible expansion according to business needs, and more Redis instances and MySQL nodes can be added according to actual conditions to adapt to the growing number of requests and ensure the scalability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 The present invention is a flow chart of a method for generating distributed incremental sequence numbers.
[0043] Figure 2 It is a schematic diagram of an embodiment of the present invention regarding generating distributed incremental sequence numbers. DETAILED DESCRIPTION
[0044] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. The components of the embodiments of the present invention generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the claimed invention, but merely represents selected embodiments of the present invention. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0045] It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once an item is defined in one drawing, it does not need to be further defined and explained in the subsequent drawings. At the same time, in the description of the present invention, the terms "first", "second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.
[0046] The first aspect of the present invention relates to a process of steps as follows Figure 1 A method for generating a distributed incremental sequence number as shown, comprising:
[0047] Receive a sequence number request;
[0048] Checking whether there is an available serial number in the first database;
[0049] If there is no available sequence number, a new sequence number is regenerated and divided into two groups. The first group of sequence numbers is fed back to the requesting party, and the second group of sequence numbers is saved in the first database for subsequent use in the sequence number request.
[0050] like Figure 2 The following is a schematic diagram of the specific process of this embodiment:
[0051] Step 1: The business system sends a request to the distributed serial number generator to obtain the serial number.
[0052] Step 2: After receiving the request, the logic layer of the distributed serial number generator first checks whether there is an allocated serial number in the Redis cache. If there is, it will directly obtain and return it to the business system without accessing Mysql, so as to improve the allocation efficiency of the serial number generator. The specific judgment basis is: the serial numbers allocated in batches from MySQL will be stored in redis. When judging, if the starting serial number is less than the ending serial number, it proves that the serial numbers in redis have not been exhausted and can continue to be used; when the starting serial number is equal to the ending serial number, it proves that the allocated serial numbers have been exhausted and no more are allocated, and it is necessary to go to MySQL to regenerate and allocate in batches.
[0053] Step 3: If the sequence numbers in the Redis cache have been allocated, the logic layer needs to generate them in batches from MySQL (the generated starting sequence number is the current sequence number in storage), and the number of batches generated should be much larger than the number of single requests from the business system to ensure that the cache is effectively used. At the same time, the sequence numbers stored in MySQL are updated to the latest sequence numbers. The second set of sequence numbers will continue to increase with the current first database sequence number;
[0054] Step 4: The logic layer divides the serial numbers generated by Mysql in batches into two groups. The first group of serial numbers is used for the return of the business system, and the second group of serial numbers is saved in the Redis cache for subsequent request allocation. The number of the second group of serial numbers should be much larger than the first group of serial numbers to ensure that subsequent requests can successfully obtain feedback serial numbers.
[0055] Step 5: The logic layer returns the serial number to the business system.
[0056] For example, when Redis has an exception and the serial number is lost, the latest serial number stored in MySQL will be used for recovery to ensure that the historically generated serial numbers will not be reallocated. Specifically, MySQL stores the upper limit of the allocated serial numbers. When recovering, it is the same as the process of batch allocation of serial numbers. A batch of serial numbers are retrieved from the upper limit and saved in Redis for use, instead of recovering the already allocated serial numbers. For example:
[0057] (1) The upper limit of the initial sequence number of MySQL is 1;
[0058] (2) The first batch allocation is returned to redis 1-100. At this time, the upper limit of the MySQL sequence number is 101;
[0059] (3) At this time, the business system obtains the serial number from Redis. Suppose that when obtaining 50, Redis has an exception;
[0060] (4) After Redis restarts and recovers, it will go to MySQL to obtain the data again, and the data obtained is 101-200. The upper limit of MySQL is updated to 201.
[0061] (5) The business system obtains the serial number again and assigns it starting from 101.
[0062] In the rare scenario of redis exception, the main thing is to ensure that the allocated serial numbers will not be reallocated. A small range of faults may appear in the middle (such as 51-100 cannot be used), so the continuity of the serial numbers can be guaranteed in most normal situations.
[0063] In general, this solution uses a combination of Mysql and Redis to jointly complete the generation of distributed incremental serial numbers, which not only ensures the global uniqueness and incrementality of the serial numbers, but also meets high performance and high reliability.
[0064] The method also provides a device for generating a distributed incremental sequence number, characterized by comprising:
[0065] An interface module, used for receiving a sequence number request;
[0066] A checking module, used for checking the status of the serial number in the first database;
[0067] The generation module is used to regenerate new sequence numbers for grouping when no sequence numbers are available.
[0068] Exemplarily, the logic layer module further includes:
[0069] A storage module, responsible for storing the second group of serial numbers in the first database and updating the latest serial numbers in the second database;
[0070] The feedback module is used to feed back the first group of sequence numbers to the requesting party.
[0071] Exemplarily, the inspection module further includes:
[0072] An acquisition submodule, used to extract a start sequence number and an end sequence number from the first database;
[0073] The judgment submodule is used to judge the availability of the start sequence number and the end sequence number.
[0074] Exemplarily, the generation module uses an incremental algorithm to generate a new serial number based on the end serial number to ensure uniqueness and incrementality.
[0075] Exemplarily, the storage module has an exception handling function, and automatically switches to the second database to recover the sequence number when an exception occurs in the first database.
[0076] By using this device, the above-mentioned processing method can be executed and the corresponding technical effects can be achieved.
[0077] An embodiment of the present invention also provides a computer-readable storage medium capable of implementing all the steps in the method in the above embodiment, wherein a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, all the steps in the method in the above embodiment are implemented.
[0078] An embodiment of the present invention also provides an electronic device for executing the above method. As an implementation device of the method, the electronic device has at least a processor and a memory, and in particular, the memory stores data and related computer programs required for executing the method, and the processor calls the data and programs in the memory to execute all steps of the implementation method and obtains the corresponding technical effect.
[0079] Preferably, the electronic device may include a bus architecture, the bus may include any number of interconnected buses and bridges, and the bus will include various circuits linked together by one or more processors and memories. The bus may also link together various other circuits such as peripherals, voltage regulators, and power management circuits, which are all well known in the art and are therefore not further described herein. The bus interface provides an interface between the bus and the receiver and transmitter. The receiver and transmitter may be the same component, i.e., a transceiver, providing a unit for communicating with various other systems over a transmission medium. The processor is responsible for managing the bus and general processing, while the memory may be used to store data used by the processor when performing operations.
[0080] In addition, the electronic device may further include components such as a communication module, an input unit, an audio processor, a display, and a power supply. The processor (or controller, operation control) used may include a microprocessor or other processor device and / or a logic device, which receives input and controls the operation of various components of the electronic device; the memory may be one or more of a cache, a flash memory, a hard drive, a removable medium, a volatile memory, a non-volatile memory, or other suitable devices, which may store the above-mentioned related data information, and may also store a program for executing related information, and the processor may execute the program stored in the memory to realize information storage or processing, etc.; the input unit is used to provide input to the processor, for example, it may be a key or a touch input device; the power supply is used to provide power to the electronic device; the display is used to display display objects such as images and text, for example, it may be an LCD display. The communication module is a transmitter / receiver that sends and receives signals via an antenna. The communication module (transmitter / receiver) is coupled to the processor to provide input signals and receive output signals, which may be the same as the case of a conventional mobile communication terminal. Based on different communication technologies, multiple communication modules may be provided in the same electronic device, such as a cellular network module, a Bluetooth module and / or a wireless local area network module, etc. The communication module (transmitter / receiver) is also coupled to a speaker and a microphone via an audio processor to provide an audio output via the speaker and receive an audio input from the microphone, thereby realizing a common telecommunication function. The audio processor may include any suitable buffer, decoder, amplifier, etc. In addition, the audio processor is also coupled to a central processor, so that recording can be performed on the machine through the microphone, and the sound stored on the machine can be played through the speaker.
[0081] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0082] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A system that specifies the functions of a box or multiple boxes.
[0083] These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory produce a product including an instruction system, which is implemented in the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0084] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps of the functions specified in a box or multiple boxes. Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they know the basic creative concept. Therefore, the attached claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.
[0085] The above is only a preferred specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A method for generating a distributed incremental sequence number, characterized in that: include: Receive a sequence number request; Checking whether there is an available serial number in the first database; If there is no available sequence number, the second database will regenerate a new sequence number and divide the new sequence number into two groups. The first group of sequence numbers is fed back to the requester, and the second group of sequence numbers is saved in the first database for subsequent sequence number requests.
2. The method according to claim 1, characterized in that The checking whether there is an available serial number in the first database comprises: Obtain the start sequence number and the end sequence number from the sequence numbers in the first database; If the starting sequence number is equal to the ending sequence number, it is determined that there is no available sequence number; If the start sequence number is smaller than the end sequence number, it is determined that there is an available sequence number.
3. The method according to claim 2, characterized in that The method further comprises: if there is an available sequence number in the first database, directly obtaining the sequence number from the first database to feed back the requester; The first database may be Redis, and the second database may be MySQL.
4. The method according to claim 3, characterized in that The second database will regenerate the new serial number incrementally based on the end serial number to ensure the uniqueness and incrementality of the serial number.
5. The method according to claim 1, characterized in that The method further includes: when the first database loses a sequence number due to an exception, restoring the sequence number using the latest sequence number stored in the second database to ensure that the historically generated sequence number will not be allocated repeatedly.
6. The method according to claim 1, characterized in that The new serial numbers are divided into two groups, the first group of serial numbers is fed back to the requesting party, and the second group of serial numbers is saved in the first database, and the number of the second group of serial numbers is much larger than the number of the first group of serial numbers, and the latest serial numbers in the second database are updated at the same time.
7. A device for generating distributed incremental sequence numbers, characterized in that: include: An interface module, used for receiving a sequence number request; A checking module, used for checking the status of the serial number in the first database; The generation module is used to regenerate new sequence numbers for grouping when no sequence numbers are available.
8. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
9. An electronic device, characterized in that: including a processor and a memory; The memory is used to store computer programs; The processor is configured to execute the method according to any one of claims 1 to 6 by calling a stored computer program.
10. A computer program product comprising a computer program and / or instructions, characterized in that: When the computer program and / or the instructions are executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.