DWQA QuestionsCategory: DatabaseCache penetration problem
George's spring asked 1 month ago

If (whether there is key in redis){
Return redis data
} else {
Query the database and put it into redis. If not, write to redis. Key = “”
This is the way I solve the problem of not having a key access the database when it doesn’t exist and not having it die when the concurrency is high
But there’s also a problem that if someone is making a malicious request for a lot of keys that don’t exist then my memory is going to be generating a lot of key=’ key1=’ that’s going to take up space in my memory and what do I do with that

2 Answers
Best Answer
Old wang who writes PHP answered 1 month ago

According to your description, I guess you want to reduce the number of times you query the database for invalid data by writing the non-existent key to the cache. This can use certain rules to verify the key. For example, with a 32-bit string, you can add some validation mechanism, even a relatively simple validation can help you filter a large number of invalid key. Alternatively, you can place all invalid keys in a single redis hash. This hash sets a reasonable expiration time so that the size of the invalid key can be relatively controlled

Old wang who writes PHP replied 1 month ago

For example, to verify the length of the key and whether the key is a specified character (for example, the valid key only contains the number of children) is to determine whether the key is valid. It needs to be based on the key generation rules in your business logic

Galeries lafayette answered 1 month ago

Your design itself is fine, but for this problem, consider introducing a rules engine or a system like risk control.
When checking the database, you can add a lock to avoid concurrent access to the same record.