I am having some problems with sockets. I’m using express-session to manage session and it is storing session on redis.
I want to use pool for connections of redis. Express session uses RedisStore and RedisStore needs RedisClient, I cannot give it pool instead of RedisClient,
My Code :
const Redis = require('ioredis')
const session = require('express-session')
const RedisStore = require('connect-redis').default
var redisClient = new Redis({ host: `redis`, port: 6379 })
redisClient.on('ready', () => { console.log(`Redis is ready @${redisClient.options.host}`)})
redisClient.on('error', (err) => { console.log('Redis error: ', err) })
const sessionAge = 24 * 60 * 1000
module.exports = session({
store: new RedisStore({ client: redisClient }),
key: 'sid',
secret: 'secret',
saveUninitialized: false,
resave: false,
rolling: false,
cookie: { secure: false, maxAge: sessionAge, sameSite: 'lax' },
expires: sessionAge
})
2
Answers
I've found a way to create a fake RedisStore that does Redis transactions using the methods I created.
You are correct: to implement connection pooling for Redis in your Express.js application using
express-session
andconnect-redis
, you can indeed use theioredis
package.ioredis
supports connection pooling, meaning it manages multiple connections under the hood.But you seem to be under the impression that you need to explicitly configure or manage connection pooling for Redis within their application.
However,
ioredis
automatically handles connection pooling. When you a new instance ofRedis
, as you did withvar redisClient = new Redis({ host: 'redis', port: 6379 })
,ioredis
is already managing the connections efficiently. Each command issued throughioredis
is queued and executed using an available connection in a pool managed internally byioredis
.There is no need to manually set up pooling mechanisms as
ioredis
optimizes connection usage and management out of the box.The only additional considerations would be around fine-tuning the Redis client configurations:
Note:
redis/ioredis
issue 470 usesnew redis.Cluster()
for connection pooling, but for a single Redis instance,ioredis
handles connection pooling internally.The
sessionAge
was corrected to use milliseconds equivalent to 24 hours asexpress-session
expects this value in milliseconds.The error handling and logging in place will provide visibility into the health of the Redis connection.
When using
ioredis
, a single connection is not created per request. Instead,ioredis
manages a pool of connections internally. That means when you create a new instance of theRedis
class, it handles multiple connections under the hood, reusing and managing these connections efficiently across multiple requests. So, for every request, your application does not create a new Redis connection; it uses the pool managed byioredis
.Your solution involves manually managing a connection pool using a third-party library,
ioredis-conn-pool
, which is not part of the standardioredis
package. It allows for explicit control over the number of connections (with a minimum and maximum threshold), which can be beneficial in scenarios where very granular control over connection management is needed. You modify the methods ofRedisStore
to manage fetching and releasing connections from the pool for each session operation (get
,set
,destroy
,touch
), which adds complexity but offers full control over how connections are utilized.That seems… a bit harder to maintain and debug. And it relies on an additional third-party library (
ioredis-conn-pool
), which might have its own maintenance and compatibility issues.