skip to Main Content

I have implemented storing the results of a selection from a database (list) in Redis to speed up data loading on the site. When a cms user performs any operations (creates, deletes or edits article), the keys in redis are dropped and load fresh data from the database.

But sometimes it happens that one or two users do not drop their keys after performing operations with articles and old data remains in redis. The Internet was available, nothing was turned off. Why does this happen – are there any typical reasons that I need to know?

Do I need to block the database so that there are no multiple connections? But redis seems to be single-threaded. What am I doing wrong? The function for key drop is very simple:

function articlesRedisDrop($prefix)
{
    $keys = $redis->keys($prefix."*");
    
    if(count($keys) > 0)
    {
        foreach($keys as $key)
        {
            $redis->del($key);
        }
    }
}

2

Answers


  1. guess that an atomic question. After $redis->keys($prefix."*"), before $redis->del($key), another connection added refresh data to redis.

    You can try to combine get and del operations to single lua script.

    local keys = redis.call("keys",string.format("%s.*",KEYS[1]))
    for _, key in pairs(keys) do
      redis.call("del", key)
    end
    

    then run the script with eval command and prefix param. If you meet performance problem with keys command, you can try scan or store all prefix keys to a set then get and delete all.

    Login or Signup to reply.
  2. I was running into the same issue where I’d occasionally have keys that weren’t being deleted. Our Redis implementation was not in Cluster-mode but had a single read/write Primary and two read-only Replicas. If you don’t tell Redis to use the Primary for all "write" operations (Set, Remove, Delete, etc.) it may occasionally send some write requests to the Replica(s). Your implementation is not .NET (like mine) but we use a CommandFlags.DemandMaster param passed to the "write" operation calls. Maybe there is something similar in your framework…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search