Skip to content

Bug: LFU score not incremented on cache hits #8

@tanbro

Description

@tanbro

Severity

High

Location

File: src/redis_func_cache/lua/lfu_put.lua
Lines: 43-74

Description

When updating an existing entry (line 43), the LFU score is NOT incremented. The code only sets frequency to 1 on new entries (line 74):

if item_exists == 1 then
    -- Update existing entry but DON'T increment frequency
else
    redis.call('HSET', hmap_key, hash, value)
    redis.call('ZADD', zset_key, 1, hash)  -- Only set to 1, never increment
end

Unlike LRU policies which update access order on every hit, LFU only initializes frequency to 1 and never increases it.

Impact

LFU eviction doesn't properly track access frequency, reducing cache effectiveness:

  • Popular items get evicted at same rate as unpopular items
  • Cache hit ratio decreases over time
  • LFU policy behaves closer to Random Replacement

Expected Behavior

LFU should increment the access frequency score on each cache hit in the GET script, and use this frequency for eviction decisions.

Proposed Solution

In lfu_get.lua, add frequency increment on cache hit:

if cached_value then
    -- Increment access frequency
    local current_freq = redis.call('ZSCORE', zset_key, hash)
    redis.call('ZADD', zset_key, current_freq + 1, hash)
    return cached_value
end

Additional Context

  • Affects all LFU policy variants (LfuPolicy, LfuMultiplePolicy, etc.)
  • LRU policies work correctly (timestamp updates on each access)
  • This may be why LRU-T is recommended over LFU in documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions