Red packet distribution method, device and equipment and storage medium
By obtaining the red envelope quota pre-deduction request, verifying the legality of the remaining quota, and asynchronously executing the red envelope distribution transaction, and combining Redis and MQ queues to ensure transaction consistency, the problem of red envelope over-issuance in high-concurrency scenarios is solved, and the accuracy of red envelope distribution and system performance are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- CHINA ASSET MANAGEMENT CO LTD
- Filing Date
- 2026-01-28
- Publication Date
- 2026-06-05
AI Technical Summary
In high-concurrency scenarios, existing technologies cannot effectively prevent the over-issuance of red envelopes during the distribution process, leading to financial losses.
By obtaining the red envelope quota pre-deduction request, executing the pre-deduction transaction, verifying the legality of the remaining quota, updating the current quota in response to legality, and asynchronously calling the preset application to execute the red envelope distribution transaction; in case of illegality, the current quota remains unchanged, using Lua scripts in Redis for atomic operation control, and combining MQ message queues and dead-letter queues to ensure transaction consistency.
This improved the accuracy of red envelope distribution, prevented over-issuance, and enhanced system performance and transaction processing reliability.
Smart Images

Figure CN122155782A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of e-commerce technology, and in particular to a method, apparatus, device, and storage medium for distributing red envelopes. Background Technology
[0002] With the rapid development of fintech, the scale of online payment, social red envelopes, marketing subsidies and other business scenarios has exploded. Especially during key periods such as holidays and e-commerce promotions, red envelope distribution business often faces millions or even tens of millions of high-concurrency requests.
[0003] In high-concurrency deduction scenarios, multi-threaded concurrent deduction can lead to over-issuance, meaning the actual amount issued exceeds the preset total quota, directly causing financial losses. Summary of the Invention
[0004] This application provides a method, apparatus, device, and storage medium for distributing red envelopes to improve the accuracy of red envelope distribution.
[0005] Firstly, a method for distributing red envelopes is provided, which includes: Obtain the red packet quota pre-deduction request and the current quota, wherein the red packet quota pre-deduction request carries the pre-deduction amount; Perform a pre-deduction transaction on the current quota based on the pre-deduction amount to obtain the remaining quota; The legality of the pre-deduction transaction is verified based on the remaining quota. In response to determining that the pre-deduction transaction is valid, the value of the current quota is updated to the value of the remaining quota, and a preset application is invoked to execute the red packet distribution transaction; In response to determining that the pre-deduction transaction is invalid, the current quota value remains unchanged, and the red packet distribution transaction is stopped.
[0006] Secondly, a red envelope distributing device is provided, the device comprising: The acquisition module is used to acquire the red packet quota pre-deduction request and the current quota, wherein the red packet quota pre-deduction request carries the pre-deduction amount; The pre-deduction module is used to perform a pre-deduction transaction on the current quota according to the pre-deduction amount to obtain the remaining quota; The verification module is used to verify the legality of the pre-deduction transaction based on the remaining quota; The execution module is used to update the value of the current quota to the value of the remaining quota in response to determining that the pre-deduction transaction is legal, and to call a preset application to execute the red packet distribution transaction; The stop execution module is used to, in response to determining that the pre-deduction transaction is invalid, keep the current quota value unchanged and stop executing the red packet distribution transaction.
[0007] In some embodiments, the red envelope distributing device further includes: The judgment module is used to determine whether the preset application has successfully executed the red envelope distribution transaction; The re-invocation module is used to, in response to determining that the preset application has failed to execute the red envelope distribution transaction, place the red envelope distribution transaction message into a preset queue, so as to re-invoke the preset application to execute the red envelope distribution transaction.
[0008] In some embodiments, the red envelope distributing device further includes: The execution result acquisition module is used to acquire the execution result of re-calling the preset application to execute the red envelope distribution transaction; The rollback module is used to roll back the value of the current quota based on the value of the pre-deducted quota in response to determining that the execution result is a failure, so that the value of the current quota is rolled back to the value before the execution of the pre-deducted transaction.
[0009] In some embodiments, the red envelope distributing device further includes: The data acquisition module is used to acquire the metadata of the rollback and the identification information of the pre-deducted transaction; The storage module is used to store the metadata of the rollback and the identification information of the pre-deducted transaction.
[0010] In some embodiments, the red envelope distribution device is executed via a pre-defined Lua script in Redis.
[0011] In some embodiments, the verification module includes: The determination submodule is used to determine whether the remaining quota is greater than or equal to 0; The first determining submodule is used to determine the legality of the pre-deduction transaction in response to determining that the remaining quota is greater than or equal to 0. The second determination submodule is used to determine that the pre-deduction transaction is invalid in response to determining that the remaining quota is less than 0.
[0012] In some embodiments, the execution module includes: The data acquisition submodule is used to acquire the identification information of the user who initiated the request for pre-deduction of the red envelope quota, as well as the amount of the red envelope; The execution submodule is used to transmit the user's identification information and the amount of the red envelope to the preset application, so that the preset application can execute the red envelope distribution transaction according to the user's identification information and the amount of the red envelope.
[0013] Thirdly, a computer device is provided, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the above method steps.
[0014] Fourthly, a computer-readable storage medium is provided, on which a computer program is stored, which, when executed by a processor, implements the above method steps.
[0015] The aforementioned method, apparatus, computer equipment, and storage medium for distributing red envelopes first obtain a red envelope quota pre-deduction request and the current quota, wherein the red envelope quota pre-deduction request carries a pre-deduction amount. Then, a pre-deduction transaction is executed on the current quota according to the pre-deduction amount to obtain the remaining quota. Next, the legality of the pre-deduction transaction is verified based on the remaining quota. In response to determining that the pre-deduction transaction is legal, the value of the current quota is updated to the value of the remaining quota, and a preset application is invoked to execute the red envelope distribution transaction. In response to determining that the pre-deduction transaction is invalid, the value of the current quota remains unchanged, and the execution of the red envelope distribution transaction is stopped. This scheme can accurately determine the value of the current quota used for red envelope distribution, improving the accuracy of red envelope distribution and avoiding over-distribution. Attached Figure Description
[0016] Figure 1 This is an application environment diagram of the red envelope distribution method provided in the embodiments of the present invention; Figure 2 This is a flowchart illustrating the method for distributing red envelopes provided in an embodiment of the present invention; Figure 3 This is another flowchart illustrating the red envelope distribution method provided in an embodiment of the present invention; Figure 4 This is a structural block diagram of the red envelope distributing device provided in an embodiment of the present invention; Figure 5 An internal structural diagram of a computer device provided in an embodiment of the present invention. Detailed Implementation
[0017] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0018] The method for distributing red envelopes provided in this application can be applied to, for example... Figure 1In the application environment shown, terminal 102 communicates with server 104 via a network. A user generates a pre-deduction request for a red envelope quota through terminal 102, the request carrying a pre-deduction amount. Server 104 obtains the pre-deduction request, parses it to obtain the pre-deduction amount, and further obtains the current quota. Then, server 104 executes a pre-deduction transaction on the current quota based on the pre-deduction amount to obtain the remaining quota. The legality of the pre-deduction transaction is then verified based on the remaining quota. In response to determining that the pre-deduction transaction is legal, the value of the current quota is updated to the value of the remaining quota, and a preset application is invoked to execute the red envelope distribution transaction. In response to determining that the pre-deduction transaction is invalid, the value of the current quota remains unchanged, and the execution of the red envelope distribution transaction is stopped. Terminal 102 can be, but is not limited to, various personal computers, laptops, smartphones, tablets, and portable wearable devices, and server 104 can be implemented using a standalone server or a server cluster consisting of multiple servers.
[0019] In one embodiment, such as Figure 2 As shown, a method for distributing red envelopes is provided. This method is applied to... Figure 1 Taking the server in the example, the following steps are included: Step S101: Obtain a red packet quota pre-deduction request, wherein the red packet quota pre-deduction request carries a pre-deduction amount; In some embodiments, the pre-deduction limit can be determined based on the amount of the red envelope to be distributed. The pre-deduction limit is the amount of the red envelope distributed in a single transaction during the red envelope campaign. For example, when the amount of the red envelope to be distributed is 5 yuan, the amount of the red envelope to be distributed is set as the pre-deduction limit, i.e., the pre-deduction limit is set to 5 yuan. When a red envelope quota pre-deduction request sent by the terminal is detected, the pre-deduction limit is parsed from the red envelope quota pre-deduction request.
[0020] Step S102: Perform a pre-deduction transaction on the current quota according to the pre-deduction amount to obtain the remaining quota; In some embodiments, the current quota is the total amount of red envelopes available for distribution during a specific red envelope campaign at a designated time point. The remaining quota is the amount of red envelopes remaining after a single distribution during the red envelope campaign, i.e., remaining quota = current quota - pre-deducted amount.
[0021] Step S103: Verify the legality of the pre-deduction transaction based on the remaining quota; By verifying the legality of the pre-deduction transaction, it is determined whether to proceed with the subsequent distribution of red envelopes. In some embodiments, the step of verifying the legality of the pre-deduction transaction based on the remaining quota includes: (A1) Determine whether the remaining quota is greater than or equal to 0; (A2) In response to determining that the remaining quota is greater than or equal to 0, the pre-deduction transaction is determined to be valid; (A3) In response to determining that the remaining quota is less than 0, the pre-deduction transaction is determined to be invalid.
[0022] When the remaining quota is greater than or equal to 0, it means that the current quota of the red envelope is sufficient, and the conditions for successfully distributing the current red envelope are met. When the remaining quota is less than 0, it means that the current quota of the red envelope is insufficient, and the conditions for successfully distributing the current red envelope are not met.
[0023] Step S104: In response to determining that the pre-deduction transaction is legal, update the current quota value to the value of the remaining quota, and call the preset application to execute the red packet distribution transaction.
[0024] Upon confirming the validity of the pre-deduction transaction, the pre-deduction is considered successful, and the current quota value is updated to the remaining quota value. Furthermore, the execution of the red envelope distribution transaction can be accelerated. In this embodiment, the pre-deduction transaction and the red envelope distribution transaction are decoupled, improving system performance through asynchronous red envelope distribution.
[0025] In some embodiments, invoking a preset application to perform a red envelope distribution transaction includes: (B1) Obtain the identification information of the user who initiated the request to pre-deduct the red envelope quota, and the amount of the red envelope; (B2) The user's identification information and the amount of the red envelope are transmitted to the preset application so that the preset application can perform the red envelope distribution transaction according to the user's identification information and the amount of the red envelope.
[0026] The user's identification information and the amount of the red envelope can be encapsulated in a message. The message is then sent to a pre-defined application, which parses the message to extract the user's identification information and the amount of the red envelope, and then sends the corresponding amount of red envelope to the user.
[0027] In some embodiments, after invoking a preset application to perform the red envelope distribution transaction step, the method further includes: Step S106: Determine whether the preset application has successfully executed the red envelope distribution transaction; Step S107: In response to determining that the preset application has failed to execute the red envelope distribution transaction, the message of the red envelope distribution transaction is placed in a preset queue to re-invoke the preset application to execute the red envelope distribution transaction.
[0028] In some embodiments, the preset queue is a dead-letter queue. Further, upon determining that the preset application has successfully executed the red envelope distribution transaction, the execution result of the red envelope distribution transaction can be updated to success. The above method of calling the preset application to execute the red envelope distribution transaction and re-calling the preset application to execute the red envelope distribution transaction can be implemented using the following code.
[0029] / / MQ producer public void sendRedPacket(String txId) { rabbitTemplate.convertAndSend("redpacket-exchange", "redpacket.routing", new RedPacketMessage(txId, userId, amount), message ->{ / / Dead letter queue configuration message.getMessageProperties() .setExpiration("10000") / / 10-second timeout .setHeader("x-dead-letter-exchange", "dlx-exchange"); return message; }); } / / MQ consumer @RabbitListener(queues = "redpacket-queue") public void handleRedPacket(RedPacketMessage message) { boolean success = AppService.sendRedPacket( message.getUserId(), message.getAmount() ); if (!success) { throw new AppException("Distribution failed"); / / Trigger dead letter } txService.updateStatus(message.getTxId(), "completed"); } As can be seen from the code above, this embodiment of the application implements asynchronous red envelope distribution through an MQ message queue. After the pre-deduction transaction is successful, the producer only needs to send one MQ message to return the pre-deduction success information to the upper layer, without waiting for the red envelope distribution result from the preset application. Calling the preset application to distribute red envelopes requires waiting for the preset application's response; this time-consuming operation is executed asynchronously by an independent consumer thread, thus better adapting to high-concurrency scenarios for red envelope distribution.
[0030] Furthermore, in this embodiment, a dual dead-letter guarantee is configured for the MQ message to address the risk of red envelope distribution failure. 1. Red envelope distribution timeout guarantee: A message validity period is set. If a consumer fails to complete consumption within the message validity period due to network lag, service downtime, or other reasons, the message enters the dead-letter queue. 2. Red envelope distribution failure guarantee: In response to the failure of the preset application to execute the red envelope distribution transaction, the message enters the dead-letter queue. Messages in the dead-letter queue can be configured with a delayed retry time. In response to the reaching of the delayed retry time, the preset application is re-invoked to execute the red envelope distribution transaction.
[0031] Step S105: In response to determining that the pre-deduction transaction is invalid, the current quota value is kept unchanged, and the red packet distribution transaction is stopped.
[0032] In response to the determination that the pre-deduction transaction is invalid, the pre-deduction is considered unsuccessful. At this time, the deducted quota is rolled back, that is, the current quota value is kept unchanged.
[0033] Suppose a certain red envelope campaign has a total quota of 100 yuan, and each user's pre-deduction limit is 50 yuan. Three users simultaneously initiate red envelope distribution requests, corresponding to threads A, B, and C. Since the red envelope distribution method in this application is executed through a pre-defined Lua script in Redis, atomic operation control is possible. The specific process is as follows: As shown in Table 1 below, thread A first obtains the current quota of 100, then performs a pre-deduction transaction on the current quota according to the pre-deduction amount, resulting in the remaining quota value of 100 - 50 = 50 yuan. Since the remaining quota of 50 is greater than 0, the pre-deduction transaction is valid. Therefore, the current quota value is updated to the remaining quota value of 50 yuan, and the preset application is called to execute the transaction of distributing a 50 yuan red envelope.
[0034] Next, thread B obtains the current quota of 50, performs a pre-deduction transaction on the current quota according to the pre-deduction amount, and obtains the remaining quota value = 50 - 50 = 0 yuan. Since the remaining quota of 0 equals 0, the pre-deduction transaction is valid. Therefore, the current quota value is updated to the remaining quota value of 0 yuan, and the preset application is called to execute the transaction of distributing a 50 yuan red envelope.
[0035] Finally, thread C obtains the current quota of 0, performs a pre-deduction transaction on the current quota according to the pre-deduction amount, and obtains the remaining quota value = 0 - 50 = -50 yuan. Since the remaining quota -50 is less than 0, the pre-deduction transaction is invalid. Therefore, the current quota value is kept at 0 yuan, and the red envelope distribution transaction is stopped. In summary, only threads A and B successfully requested red envelope distribution, and there was no over-distribution.
[0036] Table 1
[0037] If not executed by the default Lua script in Redis, i.e., without atomic operation control, the possible flow of threads A, B, and C requesting red envelope distribution is shown in the table below. Due to the lack of atomic operations, the red envelope distribution method in this application is broken down into multiple independent steps. Operations between multiple threads interrupt and overwrite each other, causing the update of the current quota to lose accuracy, i.e., causing data inconsistency. As shown in Table 2 below, the total quota is 100, while the actual red envelope distribution amount is 50*3=150, thus causing over-distribution, with an over-distribution amount of 150-100=50.
[0038] Table 2
[0039] The above-mentioned "execute a pre-deduction transaction on the current quota according to the pre-deduction amount to obtain the remaining quota; verify the legality of the pre-deduction transaction based on the remaining quota; in response to determining that the pre-deduction transaction is legal, update the value of the current quota to the value of the remaining quota; in response to determining that the pre-deduction transaction is illegal, keep the value of the current quota unchanged" can be implemented using the following code: -- KEYS[1]: Red Packet Quota Key -- ARGV[1]: Deduction Quantity local new_quota = redis.call('HINCRBY', KEYS[1], 'quota', -ARGV[1]) if new_quota>= 0 then -- Generate transaction ID local tx_id = redis.call('INCR', 'global:tx_id') -- Write pre-deduction record redis.call('HSET', 'tx:'..tx_id, 'user', ARGV[2], 'amount', ARGV[1],'status', 'reserved') return tx_id -- Returns the transaction ID else redis.call('HINCRBY', KEYS[1], 'quota', ARGV[1]) -- Rollback return -1 end In some embodiments, after the step of placing the message of the red envelope distribution transaction into a preset queue in response to determining that the preset application has failed to execute the red envelope distribution transaction, so as to re-invoke the preset application to execute the red envelope distribution transaction, the method further includes: Step S108: Obtain the execution result of re-calling the preset application to execute the red envelope distribution transaction; Step S109: In response to determining that the execution result is a failure, a rollback operation is performed on the current quota value based on the pre-deducted quota value, so that the current quota value is rolled back to the value before the pre-deducted transaction was executed.
[0040] Suppose a certain red envelope campaign has a total quota of 100 yuan, and the pre-deduction amount for a single transaction is 30 yuan. After executing the pre-deduction transaction, the remaining quota is 100 - 30 = 70 yuan. Since the remaining quota is greater than 0, the pre-deduction transaction is valid, and the current quota is updated to 70. If the red envelope distribution transaction fails due to reasons such as third-party interface connection timeout or system crash when calling the preset application, then the current quota value should be rolled back to the value before the pre-deduction transaction. That is, the remaining quota is added to the current quota, resulting in a rolled-back current quota value of 100.
[0041] In some embodiments, after the step of rolling back the value of the current quota based on the value of the pre-deducted quota in response to determining that the execution result is a failure, so that the value of the current quota is rolled back to the value before the execution of the pre-deducted transaction, the method further includes: Step S110: Obtain the metadata of the rollback and the identification information of the pre-deducted transaction; Step S111: Store the metadata of the rollback and the identification information of the pre-deducted transaction.
[0042] The rollback metadata includes rollback time, rollback reason, and rollback amount. The pre-deduction transaction identifier serves as a unique identifier for the pre-deduction transaction. The rollback metadata and the pre-deduction transaction identifier can be persistently stored, generating immutable rollback audit records. These audit records can be used to trace and verify the reasons for failed red envelope distributions and which users were affected.
[0043] The above rollback steps can be implemented using the following code: @RabbitListener(queues = "dlx-queue") public void handleDeadLetter(RedPacketMessage message) { / / First step: Quota rollback redisTemplate.execute(ROLLBACK_SCRIPT, Collections.singletonList("quota:"+message.getActivityId()), String.valueOf(message.getAmount())); / / Second layer: Audit records auditService.logRollback( message.getTxId(), "WeChat delivery failed" new Date() ); / / Update transaction status txService.updateStatus(message.getTxId(), "rolled_back"); } The method is executed via a pre-defined Lua script in Redis. When executing the Lua script, Redis treats it as a single unit, executing it in a single thread, without processing commands from other clients until the script completes or times out.
[0044] In the above method for distributing red envelopes, a red envelope quota pre-deduction request and the current quota are first obtained, with the pre-deduction request carrying the pre-deduction amount. Then, a pre-deduction transaction is executed on the current quota according to the pre-deduction amount to obtain the remaining quota. Next, the legality of the pre-deduction transaction is verified based on the remaining quota. In response to determining that the pre-deduction transaction is legal, the value of the current quota is updated to the value of the remaining quota, and a preset application is invoked to execute the red envelope distribution transaction. In response to determining that the pre-deduction transaction is invalid, the value of the current quota remains unchanged, and the execution of the red envelope distribution transaction is stopped. This scheme can accurately determine the value of the current quota used for red envelope distribution, improving the accuracy of red envelope distribution.
[0045] In one embodiment, such as Figure 4 As shown, a red envelope disbursement device 20 is provided, including: an acquisition module 201, a pre-deduction module 202, a verification module 203, an execution module 204, and a stop execution module 205. Wherein: The acquisition module 201 is used to acquire the red envelope quota pre-deduction request and the current quota, wherein the red envelope quota pre-deduction request carries the pre-deduction amount; the pre-deduction module 202 is used to execute the pre-deduction transaction on the current quota according to the pre-deduction amount to obtain the remaining quota; the verification module 203 is used to verify the legality of the pre-deduction transaction according to the remaining quota; the verification module 204 is used to update the value of the current quota to the value of the remaining quota in response to determining that the pre-deduction transaction is legal, and call a preset application to execute the red envelope distribution transaction; the stop execution module 205 is used to keep the value of the current quota unchanged in response to determining that the pre-deduction transaction is illegal, and stop executing the red envelope distribution transaction.
[0046] In some embodiments, the red envelope distribution device 20 further includes a judgment module and a re-invocation module. The judgment module is used to determine whether the preset application has successfully executed the red envelope distribution transaction; the re-invocation module is used to, in response to determining that the preset application has not successfully executed the red envelope distribution transaction, place the red envelope distribution transaction message into a preset queue to re-invoke the preset application to execute the red envelope distribution transaction.
[0047] In some embodiments, the red envelope distribution device 20 further includes an execution result acquisition module and a rollback module. The execution result acquisition module is used to acquire the execution result of re-invoking the preset application to execute the red envelope distribution transaction; the rollback module is used to, in response to determining that the execution result is a failure, perform a rollback operation on the current quota value based on the pre-deducted quota value, so that the current quota value is rolled back to the value before the pre-deducted transaction was executed.
[0048] In some embodiments, the red envelope disbursement device 20 further includes a data acquisition module and a storage module. The data acquisition module is used to acquire the rolled-back metadata and the identification information of the pre-deduction transaction; the storage module is used to store the rolled-back metadata and the identification information of the pre-deduction transaction.
[0049] In some embodiments, the red envelope distribution device is executed via a pre-defined Lua script in Redis.
[0050] In some embodiments, the verification module 203 includes: a determination submodule, a first determination submodule, and a second determination submodule. The determination submodule is used to determine whether the remaining quota is greater than or equal to 0; the first determination submodule is used to determine that the pre-deduction transaction is valid in response to determining that the remaining quota is greater than or equal to 0; and the second determination submodule is used to determine that the pre-deduction transaction is invalid in response to determining that the remaining quota is less than 0.
[0051] In some embodiments, the execution module 204 includes a data acquisition submodule and an execution submodule. The data acquisition submodule is used to acquire the identification information of the user initiating the red packet quota pre-deduction request, and the amount of the red packet; the execution submodule is used to transmit the user's identification information and the amount of the red packet to the preset application, so that the preset application executes the red packet distribution transaction based on the user's identification information and the amount of the red packet.
[0052] Specific limitations regarding the red envelope distribution device can be found in the limitations on the red envelope distribution method above, and will not be repeated here. Each module in the aforementioned red envelope distribution device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in the processor of a computer device in hardware form or independently of it, or stored in the memory of a computer device in software form, so that the processor can call and execute the corresponding operations of each module.
[0053] In one embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 5 As shown, the computer device includes a processor, memory, network interface, and database connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and database. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores data. The network interface communicates with external terminals via a network connection. When executed by the processor, the computer program implements a method for distributing red envelopes (digital cash gifts).
[0054] Those skilled in the art will understand that Figure 5 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0055] In one embodiment, a computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the above method steps.
[0056] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed by a processor, implements the above method steps.
[0057] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0058] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0059] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.
Claims
1. A method for distributing red envelopes, characterized in that, The method includes: Obtain the red packet quota pre-deduction request and the current quota, wherein the red packet quota pre-deduction request carries the pre-deduction amount; Perform a pre-deduction transaction on the current quota based on the pre-deduction amount to obtain the remaining quota; The legality of the pre-deduction transaction is verified based on the remaining quota. In response to determining that the pre-deduction transaction is valid, the value of the current quota is updated to the value of the remaining quota, and a preset application is invoked to execute the red packet distribution transaction; In response to determining that the pre-deduction transaction is invalid, the current quota value remains unchanged, and the red packet distribution transaction is stopped.
2. The method for distributing red envelopes according to claim 1, characterized in that, After the step of calling the preset application to execute the red envelope distribution transaction, it also includes: Determine whether the preset application has successfully executed the red envelope distribution transaction; In response to determining that the preset application has failed to execute the red envelope distribution transaction, the message of the red envelope distribution transaction is placed in a preset queue so that the preset application can be called again to execute the red envelope distribution transaction.
3. The method for distributing red envelopes according to claim 2, characterized in that, After determining that the preset application has failed to successfully execute the red envelope distribution transaction, and placing the red envelope distribution transaction message into a preset queue to re-invoke the preset application to execute the red envelope distribution transaction, the method further includes: Obtain the execution result of re-invoking the preset application to execute the red envelope distribution transaction; In response to determining that the execution result is a failure, a rollback operation is performed on the current quota value based on the pre-deducted quota value, so that the current quota value is rolled back to the value before the pre-deducted transaction was executed.
4. The method for distributing red envelopes according to claim 3, characterized in that, After the step of responding to determining that the execution result is a failure and performing a rollback operation on the current quota value based on the pre-deducted quota value, so that the current quota value is rolled back to the value before the execution of the pre-deducted transaction, the method further includes: Obtain the metadata of the rollback and the identification information of the pre-deducted transaction; Store the metadata of the rollback and the identification information of the pre-deducted transaction.
5. The method for distributing red envelopes according to claim 1, characterized in that, The method for distributing red envelopes is executed through a pre-defined Lua script in Redis.
6. The method for distributing red envelopes according to claim 1, characterized in that, The step of verifying the legality of the pre-deduction transaction based on the remaining quota includes: Determine whether the remaining quota is greater than or equal to 0; In response to determining that the remaining quota is greater than or equal to 0, the pre-deduction transaction is determined to be valid; In response to determining that the remaining quota is less than 0, the pre-deduction transaction is determined to be invalid.
7. The method for distributing red envelopes according to claim 1, characterized in that, The step of invoking a preset application to execute the red envelope distribution transaction includes: Obtain the identifier information of the user who initiated the request to pre-deduct the red envelope quota, as well as the amount of the red envelope; The user's identification information and the amount of the red envelope are transmitted to the preset application, so that the preset application can perform the red envelope distribution transaction based on the user's identification information and the amount of the red envelope.
8. A red envelope dispensing device, characterized in that, The device includes: The acquisition module is used to acquire the red packet quota pre-deduction request and the current quota, wherein the red packet quota pre-deduction request carries the pre-deduction amount; The pre-deduction module is used to perform a pre-deduction transaction on the current quota according to the pre-deduction amount to obtain the remaining quota; The verification module is used to verify the legality of the pre-deduction transaction based on the remaining quota; The execution module is used to update the value of the current quota to the value of the remaining quota in response to determining that the pre-deduction transaction is legal, and to call a preset application to execute the red packet distribution transaction; The stop execution module is used to, in response to determining that the pre-deduction transaction is invalid, keep the current quota value unchanged and stop executing the red packet distribution transaction.
9. A computer device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that the processor, when executing the computer program, implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.