Open godruoyi opened 4 days ago
Agree, this a common issue of sharded LRU, that why this LRU hit rate is slightly lower than regular LRU.
Would you happen to have any better suggestions? Or could we change the hash function, such as using a consistent hashing algorithm?
Description
When we specify two or more shards, our current algorithm leads to uneven key distribution, causing some data to be overwritten and resulting in incorrect cache lengths. Please refer to #17, For example:
When initializing the above code, we set each shard's list length to 128:
This means each shard can store a maximum of 128 elements, And we use the following code to calculating which shard should store when set a key-value pair:
Since the hash algorithm cannot guarantee even distribution, many keys might be assigned to the same shard. When the number of elements in a shard exceeds 128, previous data gets deleted. This leads to incorrect cache lengths as mentioned in #17.
Suggestion
Currently, there isn't a better solution to avoid this issue. Perhaps we could consider increasing the shardsize length, for example, setting it to size * 0.8.
However, this will still waste a lot of space either way. Please let me know your thoughts.