node.jsgoogle-app-engineexpress-sessiongoogle-appengine-node

Stabilizing express session on AppEngine Node.js Standard environment


I have implemented session mechanism in my app on GAE node.js standard environment using express-session without store value. Though it works without problems on localhost, session seems to be terminated regardless of how cookie.maxAge is configured on GAE node.js standard environment. This is what I have:

app.use(session({
  cookie: {
    maxAge: 31536000000, // 1 year
    secure: true
  },
  secret: SECRET,
  resave: true,
  saveUninitialized: true
}));

I assume this is caused by GAE not persisting memory and if so, I should add store option. I looked for an easiest option and found memcache, but it's not yet available on GAE node.js standard.

  1. Is this a problem of the app not having store option?
  2. What is the easiest way to have a store on GAE node.js standard?
  3. How can I use memcache on this environment?

Solution

  • You are correct that memory is not persistent in the App Engine standard environment: requests arriving to the same instance will be able to re-use memory, however, your app might run on many instances and these are anyway ephemeral.

    GCP does not yet offer a memcache solution from Node.js on App Engine. The current recommendation is to use a third party solution like Redis Labs Memcache (see tutorial) Once you have configured a memcache instance, you should be able to use a MemecachedStore for your express session. See this sample

    Another alternative could be to use Cloud Datastore to store sessions. See the nodejs-datastore-session node module. I have not tried it myself.