From: Logan Shaw on
一首诗 wrote:
> Hi all!
>
> I am preparing to write a server application (you may think of it as
> an online game server), but I not sure how to do it correctly. For
> example——
>
> 1. UDP or TCP,which one could gain more performance?

My view is that TCP does a good job of solving a bunch of difficult issues
for you. It does a good job of dealing with packet loss, changes in
latency, out of order arrival, starting and stopping connections, and so on.

It is possible you will need an even better solution than what TCP provides
in some area, and if so, UDP can give you the flexibility to implement that.
But if you start with UDP, it will take a lot of hard work and a lot of
experience to make your solution as good as TCP, so making it better than
TCP will take even more work. For that reason, I would only attempt to use
UDP if I had a really good, demonstrated reason to prefer it over TCP.

Having said that, TCP is not suitable for everything. With some realtime
applications, you would prefer not to retransmit data. TCP does not give
you an option to give up on transmitting data when the water has already
flowed under the bridge (that means when it is too late). But even then,
choosing UDP is not necessarily the best overall solution because the gain
from not retransmitting data is small, and you are still giving up all the
other benefits of TCP.

> 2. Should I catch data in memory instead of write it to db
> immediately?

That depends on how fast your database is, how critical performance is,
and of course it also depends on how much memory you have. It also
depends on how critical the data is and what the consequences are if
the data is lost. And keep in mind that the database itself may do
this sort of optimization and cache some data in memory even after it
handed over to the database.

> 3. How to make it run on cluster?

There are a wide range of solutions to this.

In some cases you can divide the work up into some logical pieces that
don't need to talk to each other; for example, maybe each user has a
session, and a user's session does not need to interact with another
user's session, so you can then assign every session to one machine in
the cluster and let it store all the data.

In other cases, it may be easy to make the code stateless, so that any
request can be processed by any server. Or maybe there is just a small
amount of state and it's easy to retrieve that from some central storage,
process the request, and the store the state back in the central storage.

There are probably other solutions to this. You could allow machines
in the cluster to each do part of the work and allow them to talk to
each other, possibly using a central directory (or even network broadcast)
so that every machine can know which other machine to call for whatever
operations it needs.

> 4. How to make hot backup?

This depends on so many things it's impossible to give a general answer.
The best guidance I can offer is that first you must incorporate this into
the design from the beginning because you will not have good success if
you wait to the end to try to add it. And second, you should design the
system so that it is possible for the backup component to see the data
in some sort of consistent state, so that the backup is useful. This
could be achieved a number of ways, such as with locking or with
transactions (so that you make records of the operations which are
in progress) or with application-specific logic where you can simply
allow certain inconsistencies to exist if they are inconsequential.

> 5. Java or C/C++ or something else?

I think you should base this decision on the general merits of the
language. There isn't much about game server applications that makes
one of these languages especially suited for it more than the other
languages. Java has some good error-handling features in the form
of exceptions and array bounds checking and so on (and error-handling
is critically important on servers) but C/C++ and other languages have
benefits too, of course.

> 6. What is happening when an IP package was received?

I don't know how to interpret this question. If you are wondering
about network protocols in general, there are some good books and some
good documentation on that subject. The short answer to what literally
happens when an IP packet is received is that, usually, the kernel
feeds it into an in-kernel implementation of the network protocol,
and the application sees this behavior through system calls that hide
the details of how all this works.

> 7. Could I get some inspiration from how people write web server?

Yes, but only to the extent that your problem is similar to serving
web pages. Web browsers tend to connect, download some stuff, and
then disconnect after a finite (and predictable) period of time. They
may reconnect later, but they usually do not stay connected
indefinitely. However, web servers have driven a lot of good
research into scalability of network applications, and they have
also driven some development of ways to track sessions in a cluster
and so on.

- Logan