Keepalive in varnish

Varnish

Default in varnish 5.2.1 keepalive is enabled, and set with a timeout of timeout_idle=5 seconds. which means that if no traffic goes through the tcp pipe in 5 seconds the TCP connection is closed.

If you are running HLS with a chunksize of 8 seconds you want to change timeout_idle to 8 seconds. you can change timeout_idle in a default installation by adding the following to /etc/varnish/varnish.params.

DAEMON_OPTS="-p timeout_idle=8"

Verify (windows)

To verify that keepalive is working, check your netstat command in windows. You should have one TCP connection to your varnish. if the sourceport keeps changing your keepalive does not work and your computer is opening new http connections to your varnish for each request. if the sourceport is constant youre browser is using the same TCP connection for all its requests.

This command checks your netstat each 8th second and look for connections to your server at port 80.

while (1) {netstat -no | findstr your-server-ip:80; sleep 8}

Flags for netstat

-n dont do dns lookup (faster)
-o show process id for conenction (in this case firefox PID)

Benefits

Below is a test against one of the varnish server with keepalive configured at 8 seconds same as the .ts chunk files. and with keepalive unconfigured, using its default 5 seconds keepalive. The Round trip time to the server using ping was 25ms. A correct configured keepalive returns the result at average 23ms faster than an unconfigured keepalive.

Keepalive Configured Keepalive Configured Keepalive Configured Keepalive Configured Keepalive Unconfigured Keepalive Unconfigured Keepalive Unconfigured Keepalive Unconfigured
379 372 382 373 413 371 371 372
376 373 370 378 421 371 376 371
374 373 371 397 392 369 373 373
414 397 400 422 440 428 444 427
402 421 399 423 442 422 427 428
423 422 434 421 467 450 449 447
436 425 425 424 469 452 455 451
426 433 422 423 457 456 451 451
397 394 399 397 440 428 424 425
426 428 429 425 446 475 474 476
425 426 421 426 469 453 454 451
407,09 405,82 404,73 409,91 441,45 425 427,09 424,73

Links

SvennD: Light reading on how Keepalive Works