• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing Chat connections

 
Ranch Hand
Posts: 66
MyEclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 1357
39
IBM DB2 Netbeans IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 66
MyEclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1357
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 66
MyEclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 1357
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic