Keep Connections Alive : Browser - Load Balancer - Web Server - mod jk - Tomcat : Part 4

This series is about how connections are kept alive between different components along the route.
 Browser - Load Balancer - Web Server - mod jk - Tomcat 

Part 1 : Browser - Load Balancer.
Part 2 : Load Balancer - Apache HTTP Server
Part 3 : Apache HTTP Server - Mod jk
Part 4 : Mod jk - Tomcat

Mod jk - Tomcat Servers

What might break the connection between mod_jk and tomcat servers during the processing of a request?

When mod_jk receives a request from the Apache web server, it will re-use or make a new connection to the Tomcat Servers. Many configuration parameters control how mod_jk manage and maintain the connection pool, or how a connection is acquired or probed before sending requests through it, but majorly, once a request is accpeted and being processed by the Tomcat servers, two parameters control the behaviour of the connection.
  • socket_timeout
Socket timeout in seconds used for the communication channel between JK and remote host. If the remote host does not respond inside the timeout specified, JK will generate an error, and retry again. If set to zero (default) JK will wait for an infinite amount of time on all socket operations.

The source code for mod_jk, can be downloaded here, shows that it uses this value to set receive timeout and send timeout. (jk_connect.c)

struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO,(const void *) &tv, sizeof(tv));
setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO,(const void *) &tv, sizeof(tv));

  • socket_keepalive
Mod_jk makes the following call if keepalive is set to true.

if (keepalive) {  set = 1;
  if (setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, (SET_TYPE)&set,
sizeof(set))) {

However, as discussed in part 2, Linux's keepalive routines by default will not kick in until after two hours (7200 secs), controlled by the tcp_keepalive_time parameter.

The Answer

Remember, there is ususally not a load balancer, who likes to drop connections with its idle timeout setting, between mod_jk and tomcat servers. If there is no firewall in between, with socket_timeout defaulted to 0, the connection between mod_jk and tomcat will never get broken. If it takes a hour for a servlet to return, the connection will be open for a hour, even if the client times out a long time before. 

  • How about setting socket_timeout to non-zero?
If socket_timeout is set to a non-zero value, mod_jk will timeout on the connection. However, the tomcat server will still have a thread processing this request for however long it takes. In addition, if the mod_jk retires parameter is defaulted to 2, mod_jk will resend the request to Tomcat. If a request is known to take a long time, due to slow database query, or cpu processing, it really doesn't make sense for the mod_jk to timeout and retry. Moreover, once mod_jk retries and times out again, it will mark the tomcat server as down, and if using a lb worker, mod_jk will retry on other tomcat servers in the lb, until every one fails.  So my opinion is, don't specify the socket_timeout unless u know retry helps.  

No comments:

Post a Comment