How to Separate Session Data from Other Data in Redis

• ~400 words • 2 minute read

This is pretty straightforward. Recently I worked an an Express app that used Redis to store both general session data as well as basic data pertinent to the app. I wanted to store the session information in one database — in case I needed to change something or wanted to purge all of the active sessions — and the app data in another.

The way Redis works you don't specify names for your databases like you might in a SQL database. You simply refer to them by their index number, with 0 being the default if you don't specify anything.

In working through this on my app I kept running into a snag where, no matter what I tried, the session data and general data kept getting stored in the same index. It didn't impact the functionality of things, but it really frustrated me! The pertinent lines in my app were these:

// This creates the RedisClient, defaulting to db index 0
var redisClient = redis.createClient(process.env.REDIS_URL);

// Tell Express to store session data in db index 1
app.use(session({
    store: new RedisStore({
        client: redisClient,
        db: 1
    }),
    secret: 'shhhh',
    name: 'session-name',
    resave: false,
    saveUninitialized: false
}));

Frustratingly this resulted all of the data be stored in index 1!

I tried calling redisClient.select(0) after initially creating the client thinking that specifying db:1 in the creation of the session middleware would achieve what I wanted. But it doesn't work that way. I've have to manually select index 0 every time I wanted to write general app data to the database because the session middleware was automatically reselecting index 1.

That's... just gross.

The problem as it turned out was that I was using the same redisClient for both my general app data and the session data. What I needed to do was create a new Redis client and pass that along for the session like so:

app.use(session({
    store: new RedisStore({
        client: redis.createClient(process.env.REDIS_URL),
        db: 1
    }),
    secret: 'shhhh',
    name: 'session-name',
    resave: false,
    saveUninitialized: false
}));

This ended up giving me my desired result: all of the session data was stored in index 1 and the general application data was in index 0.

For someone that dabbles in Redis + Express more often the solution might be obvious, but hopefully this saves someone the headache I had trying to solve this!