16
votes

we have our old website built on LAMP stack with user authentication. Now we have built a new social platform for the same users with live chat features, video and discussion forums etc and we have used node.js for this purpose.

When a user is logged in his account in the old site - lets say he can click on a link

www.xyz.com/social

which will take him to this new node.js platform.

So I'm not sure on how to pass user details from apache to node.js - All I will need is user id and then in node.js - I can query the mysql table and load the user details.

Simple Solution

The simple solution is to hash the user id and email and store the hash as key in memcache server and pass user details as values

$hash = md5($user_id+$email);
$memcache = new Memcache;
$memcache->connect("localhost",11211) or die ("could not connect");
$data = json_encode(array("id"=>$user_id,"name"=>"aaa"));
$memcache->set("key-".$hash,$data);

And then in the link pass the hash value as parameter like

www.xyz.com/social/$$hash-value$$

and in node js - retrieve the user details from memcache based on the hash key.

1) Is this the right away to approach this.

2) Will memcache support storing data of so many users (around 500 users at a given time) moving within the website from the old site to the new node.js site.

Thanks in advance for your inputs.

3
if you are using cookies in php and you are in the same domain you also having them in the nodejs app. So then you can decode your own cookies from node or make an http call to php with the user's cookies and let php tell you who the user really is. - Alex Michailidis

3 Answers

7
votes

1) Is this the right away to approach this.

Yes, it is right to do like this. But there can be better approache to this issue:

  • as @alex-rokabilis suggested. i.e. "cookies", just share above memcache key using cookies.
  • store details you need to share across Apache and node.js in session storage.

Choose invalidation conditions properly.

2) Will memcache support storing data of so many users

Yes, it will.
But it largly depends on your memcache deployment, how much RAM are you providing to it ?
Calculate or get a rough esteemate that how much RAM is needed to store 500 user details. Once memcache went into swap, then serving these much requests would be pain for memcache.


But, as per me, better method for you would be sticking to MySQL only.

  • User authenticated with LAMP stack
  • Provide it cookie md5(user-id . timestamp. KEY), and save this entry in database.
  • Get cookie in node.js application and lookup mysql table
  • Retrieve data accordingly
  • Add caching layer on top of these lookup calls of MySQL. (so you can keep selective kep-pairs in memcache only i.e. of active users with small timeout ~10sec? )
  • When user logout, just remove entry from MySQL table, and also delete key from memcache.

Reasons

  • How can you determine timeouts, because a user can remain infinitely (or as per timeout) logged into LAMP. But, he will get logged out from node.js according to timeout.
  • persistence will always be a problem if you rely entirely on memcache. How you will rebuild key-pairs after every memcache restart, if you don't have copy somewhere (MySQL) ?
3
votes

Memcache is meant to be a data caching service, not a data transfer service. There's no guarantee that a value stored in the cache will be available to the other component.

What you are trying to do will mostly work, but will likely have weird issues when you start putting some load on the system. You probably want to use a message queuing system like RabbitMQ or Kafka, or back memcached with a database.

0
votes

As the previous answers suggest, it's a right way with a bottleneck (load or traffic up to RAM) which can be a real issue.

I would like to suggest a new way. you don't actually need to reproduce "PHP's user level session data". You can use it right away from nodejs. To achieve this you need to change the old lamp code's session handler. Store sessions in memcache with json format. then access it directly from nodejs. since you haven't reproduce any data; you will save a lot of ram and I/O operation (if you store sessions in files or db) also you will save computation power. all these savings will bring you performance.

here is an example code to store session data as json in memcache.

https://github.com/lboynton/memcached-json-session-save-handler/blob/master/library/Lboy/Session/SaveHandler/Memcached.php

i didn't try the code. but i think you can handle it easily.