9

What are pros and cons for implementing own heartbeat and setting`keepalive for a socket?

I read somewhere, that keepalive sometimes can fail and connection will be closed anyway (depends on network structure). Another thing is that own heartbeat can detect if application is responsive (not only the socket).

My main goal is to ensure all of these: keep connection alive, even though no data is send (beside possible heartbeat), fast connection loss detection on both sides, application responsiveness detection.

I have already implemented a simple heartbeat on two ends and it works great, however I wonder if maybe I could replace it with out of the box keepalive feature.

user207421
  • 305,947
  • 44
  • 307
  • 483
Wojciech Kulik
  • 7,823
  • 6
  • 41
  • 67

1 Answers1

5

One problem with TCP's built in keepalive feature is that it's not always easily configurable. For example, on Linux there are various options to setsockopt() (e.g. TCP_KEEPIDLE, TCP_KEEPCNT, and TCP_KEEPINTVL) that you can use to set the keepalive's behavior to what you want, but in other OS's those behaviors are not easily adjusted, at least not programmatically. So if you want your program's keepalive behavior to be portable to various OS's and behave consistently on all of them, rolling your own heartbeat is generally the way to go.

On the other hand, there may be some programs or network protocols out there that don't easily support the concept of a heartbeat/no-op message (or you might want your program to be able to use many protocols, without having to come up with separate keepalive logic for every supported protocol), and in that case you might want to use the built-in keepalive because it has the ability to send and receive "transparent" keepalive packets that do not affect the contents of the TCP data stream. In that case, the built-in keepalive can be useful (especially if you only really need the keepalive code to work under Linux).

Community
  • 1
  • 1
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Are you aware of any situation when keep alive won't work? Besides configuration things. – Wojciech Kulik Mar 04 '16 at 23:16
  • Well, the control software I write at my job is one example: It needs to run on Mac, Linux, and Windows, and it needs to be able to notify the operator within a second or two if connectivity to the server is lost (since it is controlling the sound system at a live show and if the operator can't control the sound anymore he needs to know that right away so he can take action). For this use case, keepalive isn't sufficient because under Windows and Mac it could take up to several hours for the client computer to figure out that the connectivity has been lost. – Jeremy Friesner Mar 05 '16 at 03:54
  • Does keepalive packets sends transparent keepalive packet only after tcp_keepalive_time ( default value 7200 in Linux ) has expired ? – Invictus May 10 '18 at 09:22
  • Also should a tcp probe be sent before ideal timeout on firewall to keep connection alive in case data flow is not continuous to prevent firewall from ageing out the connection ? – Invictus May 10 '18 at 09:32