5
votes

Is there a valid reason to not use TcpListener for implementing a high performance/high throughput TCP server instead of SocketAsyncEventArgs?

I've already implemented this high performance/high throughput TCP server using SocketAsyncEventArgs went through all sort of headaches to handling those pinned buffers using a big pre-allocated byte array and pools of SocketAsyncEventArgs for accepting and receiving, putting together using some low level stuff and shiny smart code with some TPL Data Flow and some Rx and it works perfectly; almost text book in this endeavor - actually I've learnt more than 80% of these stuff from other-one's code.

However there are some problems and concerns:

  1. Complexity: I can not delegate any sort of modifications to this server to another member of the team. That bounds me to this sort of tasks and I can not pay enough attention to other parts of other projects.
  2. Memory Usage (pinned byte arrays): Using SocketAsyncEventArgs the pools are needed to be pre-allocated. So for handling 100000 concurrent connections (worse condition, even on different ports) a big pile of RAM is uselessly hovers there; pre-allocated (even if these conditions are met just at some times, server should be able to handle 1 or 2 such peaks everyday).
  3. TcpListener actually works good: I actually had put TcpListener into test (with some tricks like using AcceptTcpClient on a dedicated thread, and not the async version and then sending the accepted connections to a ConcurrentQueue and not creating Tasks in-place and the like) and with latest version of .NET, it worked very well, almost as good as SocketAsyncEventArgs, no data-loss and a low memory foot-print which helps with not wasting too much RAM on server and no pre-allocation is needed.

So why I do not see TcpListener being used anywhere and everybody (including myself) is using SocketAsyncEventArgs? Am I missing something?

1
@usr Thanks, you are right and as I've mentioned at point 3. I'm doing exactly as what you say! Accepting is happening in a simple loop, on a dedicated Thread (Task created with TaskCreationOptions.LongRunning option). - Kaveh Shahbazian
Then I don't understand why you are suggesting a choice between SocketAsyncEventArgs and TcpListener. Why not use both? The listener is out of play as soon as a connection has been accepted. In my mind it has nothing to do with processing the connection. - usr
SocketAsyncEventArgs causes a buffer of byte array get pinned, so can not be garbage collected effectively and causes memory fragmentation which leads to higher CPU and RAM usage; plus all extra hurdles one should go through to prepare and manage pools of SocketAsyncEventArgs objects; all in all it need much more extra work and maintenance and more importantly this knowledge/experience is not easily/safely/reliably transferable to another developer. - Kaveh Shahbazian
I need clarification on this: Is this question about TcpListener at all? In what way? It seems you are asking: "Can I simply replace SocketAsyncEventArgs with the usual APM/TAP async IO?" This would not have to do anything with TcpListener at all. - usr
As I've described TcpListener provides a much simpler programming model plus the same performance charactristics of SocketAsyncEventArgs. But in all projects I've studied (like fracture (F#), SocketAwaitable, SuperSocket and many other samples and blog posts here & there) I can not find anybody using TcpListener. Now I ask, Why? - Kaveh Shahbazian

1 Answers

3
votes

I see no evidence that this question is about TcpListener at all. It seems you are only concerned with the code that deals with a connection that already has been accepted. Such a connection is independent of the listener.

SocketAsyncEventArgs is a CPU-load optimization. I'm convinced you can achieve a higher rate of operations per second with it. How significant is the difference to normal APM/TAP async IO? Certainly less than an order of magnitude. Probably between 1.2x and 3x. Last time I benchmarked loopback TCP transaction rate I found that the kernel took about half of the CPU usage. That means your app can get at most 2x faster by being infinitely optimized.

Remember that SocketAsyncEventArgs was added to the BCL in the year 2000 or so when CPUs were far less capable.

Use SocketAsyncEventArgs only when you have evidence that you need it. It causes you to be far less productive. More potential for bugs.

Here's the template that your socket processing loop should look like:

while (ConnectionEstablished()) {
 var someData = await ReadFromSocketAsync(socket);
 await ProcessDataAsync(someData);
}

Very simple code. No callbacks thanks to await.


In case you are concerned about managed heap fragmentation: Allocate a new byte[1024 * 1024] on startup. When you want to read from a socket read a single byte into some free portion of this buffer. When that single-byte read completes you ask how many bytes are actually there (Socket.Available) and synchronously pull the rest. That way you only pin a single rather small buffer and still can use async IO to wait for data to arrive.

This technique does not require polling. Since Socket.Available can only increase without reading from the socket we do not risk to perform a read that is too small accidentally.

Alternatively, you can combat managed heap fragmentation by allocating few very big buffers and handing out chunks.

Or, if you don't find this to be a problem you don't need to do anything.