3

I am writing a server daemon (in C) to run under Linux and I need to make a choice for the algorithm to use to deliver notification messages to my users. I have 2 choices:

  1. Push. Establish a connection for all registered users and keep it alive. When message arrives, push it to the client through the established TCP connection.
  2. Poll. Make a connect() every 60 seconds from the client side, check if there any message and disconnect. The disadvantage is that messages will not arrive instantly to the client.

To decide which method to use I need to know how much memory does an established connection take , on the kernel side. I can calculate how much memory do I need in the userspace myself, but I don't know how the networking stuff works in Linux kernel. So, I have 2 questions: which method would you recommend me to implement and how much resources does an established TCP connection (which is not transmitting data at the moment) take? The daemon will be serving data to thousands of users, some of them frequently using the service, some of them not.

Nulik
  • 6,748
  • 10
  • 60
  • 129
  • it's unix. everything's a file. so you have the same "file" overhead as anything else, plus whatever specifics are stored along with the "file" for the network-related stuff. but unless you're planning on serving millions of simultaneous connections with a single machine, or are highly memory constrained, the file-/socket-handle overhead is probably going to be minor. – Marc B Sep 09 '16 at 16:05
  • @MarcB , over time it could be millions. So I have to estimate into the future, because it is possible that many of those registered users will be just sitting there, without activity, consuming a connection. – Nulik Sep 09 '16 at 16:25
  • if you have millions of concurrent users using your one single server, you'll run the cpu into the ground, regardless of how much memory you have or how small you keep the per-user overhead. – Marc B Sep 09 '16 at 16:27
  • @MarcB Well, this daemon I am developing, is going to be used only for notification purpose, the application daemons are running on another hardware. – Nulik Sep 09 '16 at 16:29
  • 2
    Possible duplicate of [How much memory is consumed by the Linux kernel per TCP/IP network connection?](http://stackoverflow.com/questions/8646190/how-much-memory-is-consumed-by-the-linux-kernel-per-tcp-ip-network-connection) – Malt Sep 10 '16 at 17:14
  • I would suggest using "push". Your suggested "poll" implementation would need handling 1e6/60 incoming connections every second and I would *guess* that will be harder than keeping up 1e6 idle connections. See also: https://mrotaru.wordpress.com/2013/06/20/12-million-concurrent-connections-with-migratorydata-websocket-server/ and https://mrotaru.wordpress.com/2013/10/10/scaling-to-12-million-concurrent-connections-how-migratorydata-did-it/ – Mikko Rantalainen May 07 '18 at 07:01

0 Answers0