Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing Chat connections

 
Sreyan Chakravarty
Ranch Hand
Posts: 65
Firefox Browser MyEclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I have seen a lot of sample applications on the web that show how to build a simple Chat Server with multiple Chat Clients. This fairly simple procedure involves having the server running and the client connect to it, the Chat Server now stores the open connection of the client using a Collection like Vector in most cases and ArrayList.

This is the part which bothers me.

Is this really a scalable solution ? In the real world millions of people are online and chatting, so do real servers store the open connections in a collections like structure ?

Or is there a better methodology for doing that ? If so please share them. How is it really done ?
 
Claude Moore
Ranch Hand
Posts: 832
7
IBM DB2 Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreyan Chakravarty wrote:
Is this really a scalable solution ? In the real world millions of people are online and chatting, so do real servers store the open connections in a collections like structure ?

Well, first I would say that in real world chat applications would not rely on a single server instance ! As far as I know, how many sockets you can have opened at the same time on a given machine is not a language matter, is more related to underlying SO capabilities. If I don't remember wrong, on Linux for example sockets and file descriptors are treated in a very similar way, and using ulimit settings is quite easy to raise up maximum number of allowed opened sockets to a fair high number. Of course, this number is not infinite. If you need to track them programmatically, you need of course to use some data structure, and in this case language adopted does matter. Suppose that you have a so large number of connections you cannot keep them all in main memory:you should try to swap them on disk. In java you cannot swap sockets object to disk - sockets are not serializable. In C socket function opens a socket and returns you a socket ID which is a integer, so you could easily serialize a collection of ints. But the real question, IMO, in this case should be: when a similar scenario is realistic ? I guess that with a couple of GB of RAM, a server would be able to keep in memory a number of Socket object by far greater than number of socket that the SO can actually manage without crashing or loosing performance.
 
Sreyan Chakravarty
Ranch Hand
Posts: 65
Firefox Browser MyEclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude Moore wrote:
Sreyan Chakravarty wrote:
Is this really a scalable solution ? In the real world millions of people are online and chatting, so do real servers store the open connections in a collections like structure ?

Well, first I would say that in real world chat applications would not rely on a single server instance ! As far as I know, how many sockets you can have opened at the same time on a given machine is not a language matter, is more related to underlying SO capabilities. If I don't remember wrong, on Linux for example sockets and file descriptors are treated in a very similar way, and using ulimit settings is quite easy to raise up maximum number of allowed opened sockets to a fair high number. Of course, this number is not infinite. If you need to track them programmatically, you need of course to use some data structure, and in this case language adopted does matter. Suppose that you have a so large number of connections you cannot keep them all in main memory:you should try to swap them on disk. In java you cannot swap sockets object to disk - sockets are not serializable. In C socket function opens a socket and returns you a socket ID which is a integer, so you could easily serialize a collection of ints. But the real question, IMO, in this case should be: when a similar scenario is realistic ? I guess that with a couple of GB of RAM, a server would be able to keep in memory a number of Socket object by far greater than number of socket that the SO can actually manage without crashing or loosing performance.


Just out of curiosity tell me how multiple server instances would help us ? So each server instance would store a certain number of opened connections ? Is that how it works ? And thanks for that excellent explanation that why sockets cannot be swapped to disk.
 
Claude Moore
Ranch Hand
Posts: 832
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreyan Chakravarty wrote:
Just out of curiosity tell me how multiple server instances would help us ? So each server instance would store a certain number of opened connections ? Is that how it works ? And thanks for that excellent explanation that why sockets cannot be swapped to disk.

More or less, yes, you may distribute requests load among a given number of services instances - how to achieve this depends mainly upon adopted technology, but basically there is some piece of software which acts as load balancer.
 
Sreyan Chakravarty
Ranch Hand
Posts: 65
Firefox Browser MyEclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude Moore wrote:
Sreyan Chakravarty wrote:
Just out of curiosity tell me how multiple server instances would help us ? So each server instance would store a certain number of opened connections ? Is that how it works ? And thanks for that excellent explanation that why sockets cannot be swapped to disk.

More or less, yes, you may distribute requests load among a given number of services instances - how to achieve this depends mainly upon adopted technology, but basically there is some piece of software which acts as load balancer.


Could you name a load balancer that I can use in my case ? ;-) I am looking to make this project as realistic as possible.
 
Claude Moore
Ranch Hand
Posts: 832
7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As previously stated, which load balancer to adopt depends upon choosen technology for a given project. For example, most - not to say 'all' - Java application servers provide a clustering mechanism that allows you to distribute your load among different appserver instances. Container based solution (as docker) should allow you to run multiple server instances from a given image, and usually as far as I know (sorry no personal experience with such server side tech) there are software products allowing you to handle details like the number of running containers as well as to addressing requests to a specific image.
Because we are talking about chat application, I would suggest you to have a look at MOM software like Apache Mq. It is open and free, you can quite easily run multiple brokers in cluster, it supports failover and, of course, it is focused on handling messages among different peers. You can focus only to define a proper message format to build a near production ready chat program.
Hope this helps.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!