Scaling Express sessions with Redis
Running our Express application with the NODE_ENV
set to production
will output the following message:
NODE_ENV=production npm start Warning: connection.session() MemoryStore is not designed for a production environment, as it will leak memory, and obviously only work within a single process.
The default session store for Express is an in-memory store; tying sessions to a single process does not scale.
Also, if the server crashes then we lose those sessions. If we want to scale the Express application to more than one server, we will need a memory store that is decoupled from the Express application. Express has a couple of optional stores; here we will use Redis via connect-redis
. Let's configure the vision application to use Redis as a session store.
npm install connect-redis ––save
We will now make a couple of changes to the Express server ./lib/express/index.js
. We start by bringing in the Redis
module we previously created, that configures and connects...