Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memcache suitable for passing user details from Apache to Node.js

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.

like image 699
Gublooo Avatar asked Oct 19 '15 12:10

Gublooo


People also ask

Does Nodejs replace Apache?

If you're prepared to re-write your PHP in JavaScript, then yes, Node. js can replace your Apache. If you place an Apache or NGINX instance running in reverse-proxy mode between your servers and your clients, you could handle some requests in JavaScript on Node.


2 Answers

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) ?
like image 179
skbly7 Avatar answered Oct 11 '22 14:10

skbly7


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.

like image 36
Jesse Weigert Avatar answered Oct 11 '22 15:10

Jesse Weigert