skip to Main Content

Is it possible to create a script for Redis that flush its memory when it is above a certain value?
In my specific case, I want a flush when the memory is above 90%.
What is the best way, via bash script or Lua script?

2

Answers


  1. Chosen as BEST ANSWER

    That's the way to obtain the allocated memory redis-cli -h 1.2.3.4 -p 6379 memory stats | sed -n 4p. Now it's easy to create a bash script


  2. I would use a Lua script as it will perform faster, atomically, and it would be easy to use both from redis-cli and any application code.

    Here a Lua script to get memory used and maxmemory, the percent, and an action placeholder. It uses both MEMORY STATS and INFO memory to illustrate.

    MEMORY STATS brings structured information, but doesn’t include maxmemory or total_system_memory, as INFO memory does. CONFIG GET is not allowed from Lua scripts.

    local stats = redis.call('MEMORY', 'STATS')
    local memused = 0
    for i = 1,table.getn(stats),2 do
        if stats[i] == 'total.allocated' then
            memused = stats[i+1]
            break
        end
    end
    local meminfo = redis.call('INFO', 'memory')
    local maxmemory = 0
    for s in meminfo:gmatch('[^\r\n]+') do
        if string.sub(s,1,10) == 'maxmemory:' then
            maxmemory = tonumber(string.sub(s,11))
        end
    end
    local mempercent = memused/maxmemory
    local action = 'No action'
    if mempercent > tonumber(ARGV[1]) then
        action = 'Flush here'
    end
    return {memused, maxmemory, tostring(mempercent), action}
    

    Use as:

    > EVAL "local stats = redis.call('MEMORY', 'STATS') n local memused = 0 n for i = 1,table.getn(stats),2 do n     if stats[i] == 'total.allocated' then n     memused = stats[i+1] n break n end n end n local meminfo = redis.call('INFO', 'memory') n local maxmemory = 0 n for s in meminfo:gmatch('[^\r\n]+') do n     if string.sub(s,1,10) == 'maxmemory:' then n     maxmemory = tonumber(string.sub(s,11)) n end n end n local mempercent = memused/maxmemory n local action = 'No action' n if mempercent > tonumber(ARGV[1]) then n     action = 'Flush here' n end n return {memused, maxmemory, tostring(mempercent), action}" 0 0.9
    1) (integer) 860264
    2) (integer) 100000000
    3) "0.00860264"
    4) "No action"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search