Hacker News new | ask | show | jobs
by nickdandakis 2476 days ago
Key-value stores satisfy a set of use-cases, as do NoSQL, and SQL.

Key-value is great when you have a key-value pair that you'd like to cache. Specifically in web workloads, maybe you'd like to cache the API response (value) to a certain API query (key) such that its returned without processing power from your backend. Or maybe user permissions per action, where they don't really change as they're reliant on the user's role.

I'm not entirely sure what a valid use-case would be for an embedded device, but if there's any kind of SQL query you would make on an embedded device that returns a response that hardly changes, you might also spin up some kind of key-value store such that it's returned without (well, faster than) SQL query time.

It probably doesn't make sense to completely replace a SQL database if you're trying to represent relational data.

1 comments

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?

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.