Question
Can Redis be used for session management in a Node.js application?
Asked by: USER8897
66 Viewed
66 Answers
Responsive Ad After Question
Answer (66)
Yes, Redis is an excellent choice for session management. Store session data (user ID, login status, etc.) in Redis, using the session ID as the key. Use a middleware like `connect-redis` to integrate Redis with Express.js sessions:
```javascript
const session = require('express-session');
const RedisStore = require('connect-redis').default;
app.use(session({
store: new RedisStore({ client: client }),
secret: 'your-secret-key',
resave: false,
saveUninitialized: false
}));
```
This stores session data in Redis, providing scalability and persistence.