Skip to content

Session max age gets disregarded when in cluster mode #2656

Closed
@sbsg35

Description

@sbsg35

Description

Session maxAge gets disregarded when using cluster mode along with express-session and connect-redis.
The session TTL from the cookie.maxAge is not applied.

When I switch out for ioredis, it works as expected. It also works when I have a single node using the createClient constructor.

To recreate:

  1. Start up a cluster. I used the create cluster script to achieve this.
  2. Dependencies:
    "connect-redis": "^7.1.0",
    "express": "^4.18.2",
    "express-session": "^1.17.3",
    "redis": "^4.6.11",
  1. Run the following code and visit http://localhost:3000/
import RedisStore from "connect-redis";
import session from "express-session";
import { createCluster } from "redis";

import express from "express";
const app = express();

const redisClient = createCluster({
  rootNodes: [
    {
      socket: {
        host: "127.0.0.1",
        port: 7001,
      },
    },
  ],
  useReplicas: true,
});

redisClient.connect().catch(console.error);

let redisStore = new RedisStore({
  client: redisClient,
  prefix: "myapp:",
});

app.use(
  session({
    store: redisStore,
    resave: true,
    saveUninitialized: false, 
    secret: "mysecret",
    cookie: {
      secure: false,
      httpOnly: false,
      maxAge: 30 * 1000, // 30 seconds
    },
  })
);

app.get("/", (req, res) => {
  const sess = req.session;
  (sess as any).userId = "87098798";
  (sess as any).otherData = "abc";
  res.send("Hello World!");
});

app.listen(3000, () => {
  console.log(`Example app listening on port 3000`);
});
  1. Inspect your redis store and you can see that the TTL has no limit
image

Node.js Version

18.17.0

Redis Server Version

7.2.3

Node Redis Version

4.6.11

Platform

macOS

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions