kernel event to improve network performance on .Net

I have built a simple tcp/udp network server library. I used it on lots of little programs and on one pretty important vehicle tracking server. The first version was using one thread for each connected client. It was working but it consumed a lot of memory. When the server reached something like 3000 simultanneous connections, the kernel was killing it for consuming to much memory.

I built a second version which was using network events. This allows you to directly receive network information from the kernel. It uses IOCP on Windows, epoll on Linux and kqueue on FreeBSD. This means that it’s not the program which frequently ask the kernel if there’s new data, it’s the kernel which tells the program that some new data has arrived.

The code change was pretty minimal. It’s just a little bit weird because you have to pre-allocate a buffer to allow the kernel to store data on it. I personally set a 128 bytes buffer. You have to add some BeginReceive calls everywhere and add some callbacks method.

My goal was to be able to reach 10 000 connections. After some few tests, I could reach 60 000 simulteanneous connections. The hardest part is in fact to allow the kernel to receive that many connection and to setup clients to open them. Once connections are opened, CPU consumption is always at 0% and server answers instantly to any client’s request.

That is for me the proof that .Net programs on Mono are well suited for massive network connection servers.