2

I am writing code in C++ for a data server in Linux. I have a socket list in the form of file descriptors for all the subscriber connections. My server need to continuously send data to the subscriber. I have about 100 to 200 subscriber at max. What is the fastest way to push data to all the subscribers simultaneously? I need the data to reach the subscriber as soon as possible. I am thinking of some asynchronous methods but not sure how should I implement that. Should I just call the system asynchronous method? Or should I create multiple threads to handle each of the subscriber? Or should I implement some sort of thread pool?

Thanks.

4 Answers 4

3

It probably depends a lot on your system (Single CPU?) and the work load (is it IO bound, memory bound or CPU bound?). Your best bet therefore is to implement something and benchmark it. If you want to exploit multiple processors and you're mostly CPU bound then having a small thread pool (but not one per connection) is a good idea. If you're memory bound and keeping the memory footprint small is important then probably using a single thread is a good idea. If you're IO bound do whatever is easiest for you to implement since it won't make any difference in the grand scheme of things, but your time is still valuable.

For the record boost::asio is a nice, portable way of doing asynchronous comms in C++.

Also it's not entirely clear from the question, but multicast might be appropriate here, which would shift almost all of the network overhead off the server. Provided it's the same data you're sending to all subscribers this would be well worth looking at.

Sign up to request clarification or add additional context in comments.

7 Comments

ASIO is a good option. And Boost is great, but you don't need it for ASIO. Mordor also looks promising, but I haven't had a chance to use it yet.
That does look interesting, I'll put that on my "libraries to test out" list, thanks!
Thanks for the reply. Actually I have tried testing Boost::Asio. I think I measured that each call to asynchronous write is about 10 micro seconds. However, the system calls to Linux ::send is only about 7 micro seconds. I am wondering am I doing something wrong over here as it doesn't seem to make sense because asynchronous write is slower than the synchronous write in Linux. Have somebody experience with this?
@Steveng - you should be comparing peak and sustainable I/O throughput, not time spent calling the APIs.
Multicast will not help much if the data needs to be sent reliably.
|
2

I don't know Linux well but do not use one thread per connection. Use whatever async method allows you to handle read/write results in an event-driven (callback) way, if such exists.

2 Comments

Thanks Steve. The latency of my system is more important in this case. I need the data to reach the subscriber as soon as possible. So what variables should I measure when I am choosing which method to adopt?
Set up an 'echo' test where a server bounces back received packets. Measure average roundtrip packet latency under differing loads and packet sizes (to suit your requirements obviously).
0

The "Advanced Programming in the Unix Environment" by W. Richard Stevens (classic book) describes ways to do advanced IO with multiplexing among multiple file descriptors (section 12.5.1 on pg. 396). Yes, I have the book in front of me. :)

I think there are 2 ways of doing this:

  1. allocating a thread pool and delegate n file descriptors to be processed by each thread.
  2. do all this in a single process using asynchronous IO using select and poll syscalls.

Now, this depends on how much data you want to push out to the clients. I'd suggest going with option 2 first and then explore 1 if 2 doesn't satisfy.

Comments

0

check

epoll
for multiplexing . This is much more advanced than select and lots of drawbacks of select have been covered in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.