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:
- The library is mature and proven, implementing your own
select
,poll,
orepoll
reactor will most likely encounter bugs. - Using an
int
to represent all socket types lacks type safety,asio
uses types such asboost::asio::ip::tcp::socket
andboost::asio::ip::udp::socket
. - It scales to thousands of concurrent connections by promoting asynchronous design patterns instead of a thread per connection.
- Adding multithreading support for enhanced scalability is trivial.
精彩评论