Skip to content

Considerations

Thorben Kuck edited this page Sep 19, 2018 · 4 revisions

What should you consider

These Connections might sound great! Maybe you want to have seperate Connections to reduce the load of certain Connections by introducing multiple Connections for different main Topics! Maybe you want to create a Chat-Connection to decouple the heavy load chat from your main-programm, or create a Game-Connection for your Multiplayer-Game, which transfers the ingame commands.

Like everything that sounds awsome, there are some catches to it

Connections are expensive!

To create a new Connection means to create a new Socket. This procedure is expensive, once the connection is established. This consists of a 5-way custom handshake on top of a 3-way TCP handshake, which is network intensive.

Furhter: Any Blocking Connection (TCP) needs (by default) 1 Threads: A Thread, which listens for Objects received over the Network. Then there are (depending on the Connection) maybe one or more threads, that take Object that are beeing send or received and extract those into theyre own Threads. So in total you might end up with even more threads.

Those 2 factors (creating a socket and creating multiple Threads) are the main reasons, why Connections are so expensive. They take time and resources to complete! In most Server-Environments, this is not exceptable.

Multiple Connections do not reduce the overall load!

Whilst it might sound like a reduce in load, if you extract your Chat and In-Game-Commands into 2 seperate Connections, you might be interrested to know, that it does only reduce the Network-Load a little in a treadof for more running Threads.

Those Threads need to exists to make an Connection (which is asynchronous) even possible! So you shift the load from the Network to the CPU, which might be a bad Idea!

Further, in an worst case, you have more overall load using multiple Connections. If you receive 2 Objects from the same ClientStart over different Connections, the Network-load stays the same, whilst the CPU load is increased relativ to only one Connection.

[OUTDATED] Do not Create a Connection inside of an ClientConnectedHandler!

I have repeatedly state this, but it is realy important, that you absolutly do NOT call Client.createNewConnection(ConnectionKeyClass) inside of an ClientConnectedHandler! This WILL lead to an infinite recursion!

A Client is an representation for an physical Computer, connected using Sockets. The procedure of creating new Connections, first creates a new Socket-Connection and then tells the Server, that this Socket-Connection is connected to another, already existing Client! Therefor, before anything else, a new Client will be created for the new Socket-Connection and the ClientConnectedHandler will be asked to handle the new Client! This means, if you create a new Connection inside of an ClientConnectedHandler, you will create a new Connection, every time a new Connection is established!

This behaviour might be changed in the near future, but at the moment, this problem exists!

As of Version 2.0, this behavior has changed. You may now freely create Connection whenever you want.

Best Practices

  • Use as few Connections as possible
    Since the process of creating an new Connection and maintaining a Connection is workload intensive, you should only use a Connection if relay needed. Maybe your Programm does not need 6 Connections for 6 Tasks, but only 2! Analyzing the traffic over those Connection might help you kick out a Connection or two.
  • Initialize Connections at an appropriate time
    We can't say, do it as soon or late as possible. Maybe the CPU is not as powerful to maintain a handful Connections for every Client and maybe those Connections are only used at several points in time. Try to optimize the time, any Connection has by either creating an Connection as soon, or as late as possible!
  • Let the server initialize the Connection
    This is just a Recommendation. Normally the Server should keep the Logic to everything. If you create the Connection at the Server-Side, you have the power to change this behavior for every client by only changing the current Server.
  • Consider closing Connections
    If you do not need Connections any longer, close it! Do not close it, if you need it at another point in time, but if you are finished over an connection, just call Connection.close(); to free up CPU and RAM resources.
  • If you can, do not use multiple Connections
    This is an addition to the first BP. Think about your Programm, does it need Connections at all? Is maybe the Default Connection enough to saturate every requirement? In most cases is the least work load archived when using only 1 Connection.
Clone this wiki locally