Thursday, June 28, 2018

Downside of many caches in spring

Leave a Comment

Due to the limitation of not being able to evict entries based on a partial key, I am thinking of a workaround using the cache name as my partial key and evicting all (there would only be one) entries in the cache. For example, let's say there are 2 key-value pairs like so:

"123@name1" -> value1, "124@name2" -> value2

Ideally, at the time of eviction, I would like to remove all keys that contain the string "123". However, as this is not supported, the workaround I'm thinking of is to have the following:

"123" cache: "name1" -> value1

"124" cache: "name2" -> value2

Then at eviction, I would simply specify to remove all keys in "123" cache

The downside of this of course is that there would be a lot of different caches. Is there any performance penalty to this?

From reading this, it seems Redis at least only uses the cache name as a prefix. So it is not creating multiple separate caches underneath it. But I would like to verify my understanding.

I am also looking to use Redis as my underlying cache provider if that helps.

1 Answers

Answers 1

You can use few approaches to overcome this :

  1. Use grouped data structures like sets, sorted sets and hashes : Each one of them supports really high number of member elements. So you can use them to store your cache items,and do the relevant lookups. However, do go through the performance difference ( would be very small ) on this kind of lookup compared to a direct key-value lookup. Once you want to evict a group of cache keys which are of similar type, you just remove that data structure key from redis.

  2. Use redis database numbers : You would need to edit redis.conf to increase maximum number of redis database numbers possible. Redis databases are just numbers which provide namespacing in which your key-values can lie. To group similar items, you would put them in the same database number, and empty the database with a single command whenever you want to flush that group of keys. The caveat here is that, though you would be able to use same redis connection, you would have to switch databases through redis SELECT command

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment