facebook / CacheLib

Pluggable in-process caching engine to build and scale high performance services
https://www.cachelib.org
Apache License 2.0
1.18k stars 254 forks source link

Some questions in resizing the cachelib pool size #287

Closed KIDSSCC closed 7 months ago

KIDSSCC commented 7 months ago

Hello,I apologize for the disturbance.In our current work scenario,we want to create cache for multiple threads while each thread performs independent cache storage.Refering to the following section in the Cachelib User Guide,

If you're using multiple cache instances, please consider if you can simplify to using a single instance via multiple cache pools. CacheLib is highly optimized for concurrency; using multiple cachelib instances is typically an anti-pattern unless you have a good reason.

We are now using multiple cache pools while each pool corresponds to one thread and use key prefix to avoid the same key. At runtime,we hope to resize pool size dynamically.However,we now find that when resizeing the pool(especially when shrink pool size) sometimes the memory has not been properly released。 For example,I add 2 pools each with a size of 1GB named A and B in the cachelib instance with a size of 2GB and in A and B, each stored 1GB of data respectively(poolStat.freeMemoryBytes() now equal to 0).I call the resizePools method to move 512MB from A to B.After the function call,the poolStat.freeMemoryBytes() of A is 0 and that of B is 512MB.But I can't store more items in the pool B.Is that the correct features or I make some mistakes in pool using? Or, alternatively, may I use the multiple cachelib instances instead of multiple pools? If so, How should I resize a cache after it has been created? Our core requirement is that when shrink the size of a pool or a cache.a portion of the stored items should be evicted from the cache according to the corresponding eviction policy if necessary(no enough free space in the pool ).I would greatly appreciate

jaesoo-fb commented 7 months ago

Hi @KIDSSCC,

Just in case, have you enabled the pool resizing? As you know, the pool resize is performed asynchronously.

Or, alternatively, may I use the multiple cachelib instances instead of multiple pools?

The size of Cache is static once instantiated, so the resizing of the cache instance is not supported.

KIDSSCC commented 7 months ago

Hi, @jaesoo-fb .Thanks very much,I have solved my problem with your repyl.