Difference between keepAliveTimeout and timeout?

Viewed 16166

Reading Node's doc about server.keepAliveTimeout:

If the server receives new data before the keep-alive timeout has fired, it will reset the regular inactivity timeout, i.e., server.timeout.

How is that different from server.timeout?

It sounds like they are both about socket timeout which only start to tick on inactivity.

7 Answers

Keep-Alive is a header part of the Http Protocol. The Keep-Alive header will allow multiple Http Requests to be send over a single connection instead of using multiple.

So setting the keepAliveTimeout will decide how long these Keep-Alive connections are allowed to stay open. The timeout decides the maximum amount of time the server will wait for a client's response.

more info about Keep-Alive can be found here: https://blog.stackpath.com/glossary-keep-alive/

server.timeout is time (in ms) of inactivity allowed once request received ( need not set always , default is 2 mins )

server.keepAliveTimeout is time (in ms) server will wait and keep the connection open after last response. ( need not set always , default is 5sec )

The keepAliveTimeout is the time before destroying the socket, while the timeout is the time needed to presume a socket has timed out. keepAliveTimeout destroys the socket, timeout only 'decides' that it has disconnected. And it's mentioned that "If the server receives new data before the keep-alive timeout has fired, it will reset the regular inactivity timeout", this is obvious as if it receives new data from this socket, it means it's still alive and the timeout should reset

From the nodejs perspective

server.timeout

Timeout in milliseconds. Default: 120000 (2 minutes). The number of milliseconds of inactivity before a socket is presumed to have timed out.

A value of 0 will disable the timeout behavior on incoming connections.

The socket timeout logic is set up on connection, so changing this value only affects new connections to the server, not any existing connections.

To change the default timeout use the --http-server-default-timeout flag.

server.keepAliveTimeout

Timeout in milliseconds. Default: 5000 (5 seconds). The number of milliseconds of inactivity a server needs to wait for additional incoming data, after it has finished writing the last response, before a socket will be destroyed. If the server receives new data before the keep-alive timeout has fired, it will reset the regular inactivity timeout, i.e., server.timeout.

A value of 0 will disable the keep-alive timeout behavior on incoming connections. A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.

The socket timeout logic is set up on connection, so changing this value only affects new connections to the server, not any existing connections.

In other words:

server.timeout can inform the client how long the server is willing to keep the connection server.keepAliveTimeout resets the server.timeout when it receive data before the keep-alive timeout has fired

I realized later @Software Person also told the same thing in another way, right!

Each connection terminates after the request is completed or the server does not sends a response i.e. remains idle/inactive.

(e.g. for an incoming request the server didn't send back the response, in that case the connection will remain open till the specified timeout).

Since each connection gets terminated after the request is served so creating multiple connections may reduce the loading time.This is done by enabling the Keep-Alive, which avoids the need to repeatedly open and close a new connection.

Now the keep-alive timeout lets the connection remain open only for the time specified between two consecutive request.

(e.g. if the keepalive-timeout is 10 ms and the two consecutive requests take more than 10ms then the connection will get close.Also, if each request has a timeout of 15ms so it will wait for the first request to be completed till 15 seconds if it takes more than that then also the timeout will occur.)

I started the bounty, but here's my take on it. Someone please let me know if this is correct.

server.keepAliveTimeout

Doc

The number of milliseconds of inactivity a server needs to wait for additional incoming data, after it has finished writing the last response, before a socket will be destroyed.

Explanation

Since a sockets are reused, they are kept alive for another request. It will start the timer from the time it has finished writing the last bit of the last request's response and if no other request comes in for the timeout's duration, the socket will be destroyed.

server.timeout

Doc

The number of milliseconds of inactivity before a socket is presumed to have timed out. By default, the Server's timeout value is 2 minutes, and sockets are destroyed automatically if they time out. However, if a callback is assigned to the Server's 'timeout' event, timeouts must be handled explicitly.

Explanation

If a socket does not send or receive info for this duration, socket will be destroyed. The difference with server.keepAliveTimeout is that this timeout does not wait for the previous request to be done to start the timeout. It can timeout during a request if nothing is sent or received.

What I don't understand

What is the meaning of "inactivity" here? If the socket receives data, is it inactive? Or is the socket active only when writing data?

server.timeout

The number of milliseconds of inactivity before a socket is presumed to have timed out.

server.keepAliveTimeout

The number of milliseconds of inactivity a server needs to wait for additional incoming data, after it has finished writing the last response, before a socket will be destroyed. If the server receives new data before the keep-alive timeout has fired, it will reset the regular inactivity timeout, i.e., server.timeout.

I think, the main difference between those definitions is the "after it has finished writing the last response" sentence. In other words:

  • server.timeout: includes the last activity (request or response).
  • server.keepAliveTimeout: only includes the last response.
Related