Create Redis Key - Easy Way to Configure All Your Redis Keys In One Place

Create Redis Key - Easy Way to Configure All Your Redis Keys In One Place
Create Redis Key - An NPM package

I created an NPM package to easily edit all of my Redis key templates and manage the structure of how I store related keys using a nested config object.

Create Redis Key is an NPM package, a Redis key creation utility for NodeJS (most effective when used with Typescript).


You might be wondering "Why would I need a package to create a Redis key? Isn't it just a string?"

Well, it is.

But even if an individual key is a string, most of the time we use many of them. Cache this, cache that, Cache 'Em All!

Have you ever had a backend app which utilizes Redis as a primary database?

I have one. A microservice which has many resources stored in Redis. It stores data that change in an expected time span, varying from less than a second to hours.

There comes the critical part(s): whether it be caching or storing data, keys mostly share a template & what makes individual keys different is that these keys include resource identifiers as a part of them since they are trying to locate different resources. Also, if a resource belongs to another resource, it's key starts with the key of the resource it belongs to.

Let's take Instagram as an example.

Simply we can store a user with id 1 at the key users:1. Then it makes sense to store the user with id 2 at the key users:2 and it goes on.

As we can see they share the template users:%UserID% and the template includes a resource identifier.

There are different kind of resources which belongs to a user like follows, followers, posts etc. We can store them at keys such as:

  • users:%UserID%:follows
  • users:%UserID%:followers
  • users:%UserID%:posts

which start with the key of the resource they belong to (users:%UserID%).

So, wouldn't it be nice if we had a way to easily manage all our keys?

Introducing Create Redis Key

A Redis key creation utility.

Create Redis Key Templates, which include parameters, using a nested config object & use your Redis Key Template strings to create Redis Keys.


First of all, import needed functions as follows:

import {
} from 'create-redis-key';

Create a Redis Keys Config object.

You should write as const at the end of the object for Typescript types to properly work.
const redisKeysConfig = {

  // app-statuses
  appStatus: ["app-statuses"],

  users: {
    SCOPE_FIRST_PART: ["users", createRedisKeyParam("UserID")],

    feed: {
      SCOPE_FIRST_PART: ["feed"],

      // users:%UserID%:feed:following
      following: ["following"],
      // users:%UserID%:feed:favorites
      favorites: ["favorites"],

    // users:%UserID%:follows
    follows: ["follows"],

    // users:%UserID%:followers
    followers: ["followers"],

    posts: {
      SCOPE_FIRST_PART: ["posts", createRedisKeyParam("PostID")],

      // users:%UserID%:posts:%PostID%:comments
      comments: ["comments"],
      // users:%UserID%:posts:%PostID%:likes
      likes: ["likes"],
} as const;

Then create a Redis Keys Templates Map using the config:

If you give an invalid config, return type will be never.
const RedisKeysMap = createRedisKeysMap(redisKeysConfig);

The resulting object will be this, which is a Redis Keys Templates Map:

  appStatus: 'app-statuses',
  users: {
    feed: {
      following: 'users:%UserID%:feed:following',
      favorites: 'users:%UserID%:feed:favorites'
    follows: 'users:%UserID%:follows',
    followers: 'users:%UserID%:followers',
    posts: {
      comments: 'users:%UserID%:posts:%PostID%:comments',
      likes: 'users:%UserID%:posts:%PostID%:likes'

Now here comes the part where we really try to create a Redis key. You will see how good the Developer Experience this package provides.

As you start to type, the Redis Keys Templates Map  you created will suggest properties since its a plain Javascript object.

If you hover over the property, you can see the corresponding template it is going to be on runtime! Isn't it awesome? You can inspect the result without the need to run it!

Then you will get parameter suggestions on your IDE based on the Redis Key Template you provided to createRedisKey() function, immediately as you type a comma.

All params on a Redis Key Template are required. You will get type errors if you don't provide all of them. You can use CTRL + Space for VS Code to show you the missing parameters.

You will end up writing something like this:

const likesOfPostRK = createRedisKey(
    UserID: "1234",
    PostID: "9876",

The above code generates a string which equals to:


And that's it. You can now use the generated key to read the resource from Redis.

There is actually 3 ways you can use this library. If you liked the idea, you can check more options and documentation of create-redis-key on GitHub.

Happy coding!