user4520
user4520

Reputation: 3457

Is it better to reuse connections, or renew them for sending data frequently?

I have a TCP server application which listens for connections from clients that send "heartbeat" packets frequently in rather small intervals, around 20 seconds. There are a few hundred such clients, 1000 at most.

It has previously been implemented (not by me) such that a new TCP connection is established for every heartbeat, then it is closed.

It seems to me that it would be better to keep the connection open and reuse it for the communication, but I'm not sure. Is re-establishing the connections a waste of time and bandwidth and keeping a few hundred open connections (in the server's case) not a problem, or, on the contrary, is it a big waste of resources not to close them?

Upvotes: 2

Views: 514

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123280

It depends on the amount of memory you have, of the latency of the connection and of the frequency of the heartbeat what the best options is:

  • Each TCP connection needs 1xRTT time to setup, so creating a new connection each time is costly in terms of time but not resources.
  • Each open TCP connection has a state which needs to be preserved on the system, which means it takes up memory resources.

Upvotes: 2

Related Questions