How to deal with the scenario of sending red packets with high concurrency?

problem description

because the company has the demand for answering questions and handing out red envelopes, the activities are equivalent to giving red envelopes, and there is no concept of grabbing red envelopes. That is, there is a daily limit of, say, 10,000. For the business of sending red packets, it is more appropriate to use transactions plus locks, but looking at the online solutions, most of them recommend optimistic locks, but optimistic locks are rejected. Since our demand is to give out red packets as long as they are answered correctly, and the topic is basically to send sub-questions, it leads to a large amount of red packets

the environmental background of the problems and what methods you have tried

if you do not use the lock mechanism at the earliest, you will find that you will often overissue a few yuan, followed by an exclusive lock, and you will find that the execution of the database will be long, and a large amount of concurrent delivery will lead to blocking.

Jan.25,2022

consider writing the code of 10, 000 red packets to redis. After the answer is correct, the data is fetched from the redis, and the concurrent operation is guaranteed by redis


optimistic lock will be rejected. You can consider using retry to solve the problem.


according to the requirements and scenarios you describe, you only need to solve the blocking problem when the concurrency is large

if you allow red packets to be delayed for a while, you can consider adding a queue to send red packets asynchronously


add a countdown to open the red packet and try


redis atomic operation.


I also recommend using redis to judge the delivery of red packets, which can well deal with high concurrency scenarios

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf43f4-3123d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf43f4-3123d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?