High performance TCP server in C#

It must be async, there is no way around this. High performance and scalability don’t mix with one-thread-per-socket. You can have a look at what StackExchange themselves are doing, see async Redis await BookSleeve which leverages the CTP features from the next C# release (so is on the edge and subject to changes, but it is cool). For even more bleeding edge the solutions evolves around leveraging SocketAsyncEventArgs Class which takes things one step further by eliminating the frequent allocations of async handlers associated with ‘classic’ C# async processing:

The SocketAsyncEventArgs class is part
of a set of enhancements to the
System.Net.Sockets.Socket class that
provide an alternative asynchronous
pattern that can be used by
specialized high-performance socket
applications. This class was
specifically designed for network
server applications that require high
performance. An application can use
the enhanced asynchronous pattern
exclusively or only in targeted hot
areas (for example, when receiving
large amounts of data).

Long story short: learn async or die trying…

BTW, if you’re asking why async, then read the three articles linked from this post: High Performance Windows programs. The ultimate answer is: the underlying OS design requires it.

Leave a Comment