sharing session information among multiple instance of netty server (http, websocket)

544 views
Skip to first unread message

john mayer

unread,
Nov 8, 2014, 4:14:29 AM11/8/14
to ne...@googlegroups.com
Hi,
  I am developing an web service based on netty framework. To make it scalable, i am trying to run multiple instance of the application across multiple server.Using load balancer i am forwarding the request to different server. Lets say , one user connected to server1 .
     Another user connected to server2. how may i share session information among server that user1 have to know user2 is connected now. i could not find any way. if you have suggestion regarding the issue please suggest.

 

Ngoc

unread,
Nov 10, 2014, 9:33:28 PM11/10/14
to ne...@googlegroups.com
There are many ways:

1. Config load balancer

You can config your load balancer so that it directs the same client to the same server. The load balancer may do that based on the client IP, or it injects cookie to responses, to mark the server ID, so that when subsequent requests from the same client come in, it knows which server to redirect the requests to. There are 2 kinds of load balancer: level 4 and level 7. Level 7 can understand HTTP so that it can inject cookie to responses and take the cookie out from requests. Level 4 doesn't care about the (HTTP) protocol above the TCP connection (it only cares about sending bytes between clients and servers, it doesn't look into the bytes to understand their meaning). Level 7 is more featureful but cost more resources than level 4.

Note about HTTPS:
If you want to use HTTPS but the load balancer doesn't handle HTTPS, you have to use level 4 (because the purpose of HTTPS is to prevent man-in-the-middle like load balancers to understand the bytes).

If you use this solution, investigate about popular load balancers/reverse proxies like HAProxy and Nginx. Example:
https://serversforhackers.com/editions/2014/07/29/haproxy-ssl-termation-pass-through/

2. Save session at client side

You can save session data to cookie at your application level (write logic at the servers to save session data to cookie).

Notes:
* If you use WebSocket, the server can send multiple WebSocket frames, but you can only set the response cookie only once before upgrading the connection from HTTP to WebSocket.
* Cookie has size limit of 4KB, you can't save too much data in it.
* You may need to encrypt data in cookie, because it's stored at the client side, so it may be inspected and altered.

3. Save session data at server side

Save it in DB or something more realtime like Hazelcast:
http://hazelcast.org/

To avoid all the headache, you can just use a framework that has built-in support for the problem, like Xitrum (disclaimer: I'm its creator :D):
http://xitrum-framework.github.io/

john mayer

unread,
Nov 11, 2014, 2:59:43 AM11/11/14
to ne...@googlegroups.com

Thanks Ngoc. I am using Haproxy with sticky session approach for load balancing. It is working fine.  My problem is with sharing information across all server. Yes, you have mentioned about hazelcast. i am also trying to use it and testing it. Again about xtrium,  is it possible to use with netty framework as my development task almost finished.

thanks 

Ngoc

unread,
Nov 18, 2014, 2:39:17 AM11/18/14
to ne...@googlegroups.com
If your development task is almost finished, and you only need to share information across all servers, you can just add Hazelcast to finish.

Xitrum is based on Netty. If you want other features in Xitrum (see the brief feature list at Xitrum home page http://xitrum-framework.github.io/ and at full feature list at Xitrum Guide http://xitrum-framework.github.io/guide.html), there are many ways to add Xitrum to your projects:
* Run it at another port.
* Run it at the same port by combining your handlers with Xitrum handlers: http://xitrum-framework.github.io/guide/3.19/en/handler.html#custom-handlers
Reply all
Reply to author
Forward
0 new messages