In a voting program, users can vote three times a day. In the background, I determined whether the number of records of the user on the current date exceeds three. Now, if I can get the voting link through the front-end page, and then send a 1000000 (large number of requests) request to the server in a short time... in a voting program, users can vote three times a day. In the background, I determined whether the number of records of the user on the current date exceeds three. Now, if I can get the voting link through the front-end page, and then send 1000000 (that is, a large number of requests) requests to the server in a short time, in this case, the number of records of a user on the current day may be more than three times. How can this be solved?
Reply content:
In a voting program, users can vote three times a day. In the background, I determined whether the number of records of the user on the current date exceeds three. Now, if I can get the voting link through the front-end page, and then send 1000000 (that is, a large number of requests) requests to the server in a short time, in this case, the number of records of a user on the current day may be more than three times. How can this be solved?
Redis is used to determine the user count on the day before each write. If the number is less than three, the data is written. Redis adopts single-process and single-thread mode, and operations are performed in serial mode to meet requirements.
Solution 1: use gap locks in transactions to prevent Phantom reads
Solution 2: the first three voting records for each user are read.
Locks the number of user records to be determined, used, and modified.
All the things mentioned above can solve this problem;
Let's talk about something else. I have never practiced it, and I don't know if I can do it. I just think about it,
1. check whether a time interval can be added to the vote. Record the timestamp of the last successful vote in one place, and then judge the time interval during the second vote.
2. Similar to the first idea, it is to record the number of votes for the day in one place.
Then I thought about it. There should still be the same problem. I suggest you use the redis mentioned above to handle it. It's easy to deal with it.