Redis HMSETEX

Redis script for an HMSETEX command

We have added an implementation of HMSETEX to our redis-scripts repo. The repo is a collection of scripts packaged up as a module for Node.js to make them easy to use in Node apps. Hope it helps!

Backstory

Redis’ existence-aware commands are a useful way to atomically modify keys based on their state. For example, SET[EX|NX] is a fast and concurrency-safe way of setting keys only if they exist (XX) or don’t exist (NX).

Not all keys support this option, namely HMSET. HMSET will always create the key to set its fields if it doesn’t exist.

In cases where we only want to modify hash keys that exist (say, our app relies on ttl to clear out keys), it would be convenient to have some kind of HMSETEX so we can perform the check/set logic atomically in a single command. This is much safer and simpler than having to check/set across round trips between your app and Redis, and less prone to concurrency bugs too.

Never miss a post