When does ybc evict?
sajal opened this issue · 1 comments
from the readme..
YBC may delete any item at any time due to various reasons, which depend on implementation details. So always expect that the added item may disappear at any time during subsequent requests.
Can I know which reasons apart from cache size getting large would lead to evictions? Specifically pertaining to the Go binding.
In my usecase, I would be using ybc as a hybrid between a kv store and a cache. i.e.
- I do want to expire items based on ttl.
- I do not mind LRU type evictions to free space. The total amount of data to be cached is relatively deterministic, so I can plan cache size ahead of time.
- I might need persistence for certain use cases.
Currently I use redis, and am looking to use something in process.
Sorry, this is not an issue but a question. I dont know which is the right mailing list to ask.
Currently there are two reasons, which may lead to cached item deletion:
- Storage space overflow. Newly added items may overwrite old items.
- Hash table bucket overflow. Items are located via index hashtable, which is split into fixed-length buckets. Each bucket may contain up to 16 items (see C_MAP_BUCKET_SIZE constant at https://github.com/valyala/ybc/blob/master/config.h ). Items are mapped into buckets by hashing their keys. If a single bucket already contains 16 items and newly added item traps into this bucket, then the newly added item overwrites existing item with smallest ttl value in this bucket. According to statistics, such a situation is quite rare (0.1% probability for fully populated hashtable).
So, if you set YBC storage size and max cached items count to values exceeding the real size and the number of stored items, then the probability of automatic items' deletion will be quite low (0.001% and lower), but will never reach zero. So, I wouldn't recommend using YBC as a kv storage (like redis), since it doesn't give 100% guarantee for item persistence until its' expiration.