Php redis does mysql caching, how does asynchronous redis synchronize to mysql database?

  ios, question

When the company conducts lucky draw or red envelope activities, there are always malicious requests for large number of visits. Querying mysql for repeated verification cannot limit the large number of concurrent transactions. There will always be repeated insertions, which will result in multiple prizes.
I want to use redis as mysql’s cache, but the problem now is how to write redis data back to mysql. It is impossible to write back mysql every time I check it. In that case, the problem is not solved at all.
The idea now is whether php, or some other technology, can be used to regularly write the data in redis back to mysql. The program only interacts with redis.
Hope to be able to give specific logic or solutions, online answers are too general to solve the problem.

Thank you

On January 16, add:
My question is not clearly described.
Let me elaborate on the problems encountered. It is not the question of Read out of invalid data when the prize was awarded, but whether this person has a chance to win a lottery.
There is a lottery table. If this person has won the lottery, insert a record in the table.
The current implementation is:

1. Read the lucky draw form to judge whether this person has won the prize.
 2. If there is a record in the table, it is smoked, and you can tell directly that you have no chance.
 3. If there is no record in the table, follow the lottery logic and insert the lottery table.

Under normal circumstances, there is no problem.
However, some people use malicious scripts to award prizes, that is, the same person makes a large number of requests, one second may have 120 requests or more, and more than one person will award prizes.
The problem lies in step 1.
For example, suppose each person can only draw a prize once, because the request is too fast, the same person’s A and B requests come almost at the same time, A finishes the lottery logic, and during the process of inserting records in the lottery table, because of mysql’s performance, B can’t read the record in the table when going to step 1, because the insertion of A is not completed at all. So request b will go through the lottery logic again. Cause the same person to draw twice, and then insert the drawing table.
What I care about is whether B can judge that there is data in the lottery table when A is inserted into the lottery table.
So I think the problem is that mysql is not writing fast enough and reading fast enough, so I will use redis as a layer of fast cache.
The lucky draw we do is to win 100% of a single prize and only limit the number of prizes, so we must ensure that everyone can only draw once and try our best to solve it at the procedural level.

Added on January 20
It is very inspiring to see everyone’s answers. Thank you very much.
As for the issue that concurrency is not as big as imagined, here’s the thing. Our server is a single ECS in Alibaba Cloud, equipped with 4 stone 8G and exclusive 50m broadband, centos, which only does external activities.
This is the number of views for a lottery that started on December 24 and lasted less than 4 hours.

This is the monitoring of TCP connections at that time.

Lottery is different from website access. Most of these participants visit the server at the same time. Personally, I think that without upgrading the server configuration, software level optimization can fully support these accesses. At present, I think the bottleneck is mysql.

The reason why you have so many problems is that you have confused your thinking, have not understood what these problems are, and have drugged yourself after getting sick.

1. [There are always malicious requests for large visits], this is a system security problem, what you need to do is to prevent attacks.

2. [Querying mysql for repeated verification cannot limit large concurrency]: this is a lack of function. Since there are deficiencies, we will immediately develop new functions.

3. [There will always be repeated insertions, which will result in multiple prizes]: this is a Bug of the existing system, and the problem needs to be corrected through debugging.

To sum up, what is the relationship between these problems and redis?