开发者

Scalable UDP server

I need t开发者_Go百科o make a Server that will listen and answer to UDP packets, It will listen 10 ports.

The packets are very small, no more than 20 bytes. Each packet will modify or search into a huge hash-table.

But it have to process 15k packets per second.

I can develop in c, c++ or qt.

There is any special guidelines that need to meet this requirements ? What is the basic design to use ? Is threading a need ?


For this sort of performance, I would consider a select()-based "commutator loop":

  • Open the listening UDP sockets
  • Call select() to determine sockets with readable data
  • For each readable socket, read bytes (non-blocking read)
  • When you get a complete packet, process (and write, as needed)

You can build a simple table that dispatches on select() results.

This approach will give you the best possible performance, because it's as close as you can get to the operating system calls, with minimal overhead. It runs as a single process, minimizing context switching and getting the best cache locality.

Next, if you find things are CPU bound, consider ways to use multiple CPU cores with multiple threads or processes. For example, can you have each processor handling N of your 10 sockets, with the hash table in shared memory.

Finally, I'd be careful about using a lot of threads (e.g. a large worker pool of threads, etc.). At extreme performance levels, thread overhead can get significant.


Boost.Asio is the perfect choice for a scalable UDP server. Using it has several advantages over programming to the socket APIs directly:

  1. The library is mature and proven, implementing your own select, poll, or epoll reactor will most likely encounter bugs.
  2. Using an int to represent all socket types lacks type safety, asio uses types such as boost::asio::ip::tcp::socket and boost::asio::ip::udp::socket.
  3. It scales to thousands of concurrent connections by promoting asynchronous design patterns instead of a thread per connection.
  4. Adding multithreading support for enhanced scalability is trivial.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜