0

I have two different types of server and clients working at the moment and i am trying to decide which one would be better for an MMO server or at least a small MMO like server with at least 100 players at a time.

my first server is using a thread per connection model and sending objects over the socket using ObjectOutputStream.

my second server is using java nio to use only one thread for all the connections and using select to loop through them. this server is also using ObjectOutputStream to send data

for my question, what would be a better approach to an MMO server and if the single thread model is better how would sending an object over the socket channel be affected, would it not be read all the way and not get the full object?

for each object being sent over it just contains for example an int and 2 floats for sending position and player id.

Arcxz
  • 65
  • 1
  • 11
  • @Vimal can you explain why this would be better, from what i have read thread per connection is not a good idea because lots of threads is a bad thing. is this no longer the case? – Arcxz Dec 29 '17 at 07:32
  • Why not setup a stress-test, and check the performance of each alternative? – Yoav Gur Dec 29 '17 at 07:34
  • 100 players at a time is nothing. Start with threads, and only switch to NIO if you find you have a problem , or if you experience enormous growth. NIO with selectors only comes into play with tens of thousand of connections, if ever, and you'll find the `java.net` version a lot simpler to written and maintain. – user207421 Dec 29 '17 at 07:39
  • @YoavGur from my testing they both seem to be around the same neither one seems to have an obvious benefit over the other – Arcxz Dec 29 '17 at 07:39
  • @Arcxz too many thread depends on peak [here's a discussion](https://stackoverflow.com/questions/481970/how-many-threads-is-too-many) if peak size is large you can use [thread pool](https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html), its awesome. – Polish Dec 29 '17 at 07:42

1 Answers1

2

I will relate this question to why MMO use UDP over TCP. The reason being that UDP promises fast delivery whereas TCP promises guaranteed delivery.

A similar analogy can be applied to a single-threaded vs a multi-threaded model.Regardless of which you choose, your overall CPU cycles remain the same i.e. the server can process only so much information per second. Lets see what happens in each of these scenarios

1.Single-Threaded Model : In this, your own implementation or the underlying library will end up creating a pipeline where the requests start queuing. If you are at min load, the queue will remain virtually empty and execution will be real-time, however a lot of CPU may be wasted. At max load, there will be a long queue-up and execution will have latency with increasing load, however delivery will be guaranteed and CPU utilization will be optimum. Typically a slow client will slow everybody else down.

  1. Multi-Threaded Model : In this, depending on how your own implementation or the underlying library implements mutli-threading, parallel execution of requests will start happening. The catch to MT is that it's easy to get fooled. For example, java.util.concurrent.ThreadPoolExecutor doesnt actually do any parallel processing unless you set the queue size to a low value. Once parallel processing starts happening, at min load, your execution will be superfast and CPU utilization will be optimum and game performance will be great. However, at max load your RAM usage will be high and CPU utilization will still be optimum. Typically you'll need to put thread interrupts to avoid a slow client hogging all the threads, which will mean glitchy performance for the slow client. Additionally as you start exhausting your thread pool and resources, threads will either get queued or just get dropped leading to glitchy performance.

In gaming, performance matters more over stability, hence there is no question that you should use MT wherever you can, however tuning your thread parameters to compliment your server resources will decide whether its a boon or a complete bane

Monish Sen
  • 1,773
  • 3
  • 20
  • 32
  • this sounds like the answer i was looking for and i was planning to add udp to the mix after i decied on tcp so that i only use tcp for important packets. – Arcxz Dec 29 '17 at 19:27