Node.js session variables not working (get lost) on heroku

Ahhh… so this issue took me a while to figure out and caused some frustration: I use session variables in my node.js app, and locally everything working perfectly fine. However, on heroku, some mysterious behavior was taking place: sometimes session vars were preserved and working OK, but sometimes they would just get blank/undefined, and there was no pattern to this madness!

I took a few steps to troubleshoot this:
1) Moved from memorystore to Mongo DB session store, as recommended for production environment
2) Opened up Charles (network monitoring tool) to make sure all requests were processed, since I’m switching a parameter in my app and updating my session variable via AJAX call
3) Took a hard, long look at my heroku logs and found that I had TWO web processes running! So if I set a variable with one process, then the page reload is done by another web process, of course all session info gets lots (however, I’m still thinking since it’s Mongo storage it should have preserved the session vars).

Here’s a snapshot of the log where you can see the root of the issue:

2012-07-12T21:00:12+00:00 app[web.2]: new demo set: teens
2012-07-12T21:00:12+00:00 app[web.2]: demo: teens 
//session var preserved on same web.2 process 

2012-07-12T21:00:17+00:00 app[web.2]: new demo set: moms
2012-07-12T21:00:17+00:00 app[web.1]: demo: undefined 
//aha! web.1 has no idea what it is

For now, the app is working as expected, phew. However, I’m going to look more into why the values are not being read from Mongo and how this can still work when multiple processes are running (for the scaled up version of the app)

2 thoughts on “Node.js session variables not working (get lost) on heroku

  1. Pingback: Tanya Nam » Blog Archive » Manage or code: pick your own adventure

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s