Hacker News new | ask | show | jobs
by onaraft 2476 days ago
I've been looking at Redis recently as a potential solution to some caching I might want to do, and one hurdle I encountered almost immediately is:

Suppose I have some function, get_members_of_group(dt, groupname). It returns a set of strings. There are arbitrarily many groups, so I can't just cache all of them in one key. Someone might make a new group at any time, and groups are never deleted or changed, they just age out.

So far, this seems great. I make get_members_of_group_from_redis() first check redis for a key group_members_{dt}_{name}. If it's there, return it. If it's not, get it, cache it, return it.

But groups can also be empty.

Redis doesn't let me store an empty set. If a group comes back empty, the fact that it's empty can't be cached because you can't tell the difference between "not cached yet" and "cached but empty".

I've googled it a bit and all of the workarounds (sentinel key/value pair, storing "empty" at that key so the SMEMBERS fails with an error, etc) are hacky and make me not want to use Redis.

Is this a me problem and everyone is OK with slamming their db with hits if a cached function returns empty, or is everyone just ignoring all non-str datatypes and storing everything as JSON? Or is it just that weird to have a function that might return an empty set that nobody else has this problem?

4 comments

Yes, Redis requires more manual tracking of such things. What you have to understand is that it is incredibly fast: You can easily do hundred of thousands of sismember lookups within the time it takes to serve a usual request.

> or is everyone just ignoring all non-str datatypes and storing everything as JSON

I doubt it. I've been surprised by how many of the datatypes I have found uses for.

I'd guess that most people don't see a sentinel value to distinguish between empty and unknown as a serious problem.
Supposedly you can use a Lua script - although that’s a somewhat big hammer for such a simple problem...
Isolate the hack in a function with a snarky comment.

Sometimes speed is worth it.