DNS-aware Persistent Connections

What exactly is the problem statement?

Is that even a valid problem?

  1. golang net/http: There is a similar issue raised on golang’s net/http package. https://github.com/golang/go/issues/23427
  2. square/okhttp: Again similar issue with some discussions. https://github.com/square/okhttp/issues/3374
  3. akka/akka-http: https://github.com/akka/akka-http/issues/1226
  4. pgbouncer/pgbouncer:A PostgreSQL connection pooler has similar issue reported. https://github.com/pgbouncer/pgbouncer/issues/383
  5. cURL: A good discussion in cURL’s mailing list. https://curl.haxx.se/mail/lib-2017-06/0021.html
  1. This is indeed an issue or a question which is faced by many.
  2. There is no clear solution that is being suggested or implemented anywhere.

Let’s demo the issue

package mainimport (
func main() {
tr := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
TLSHandshakeTimeout: 5 * time.Second,
IdleConnTimeout: 100 * time.Second,
go func(rt http.RoundTripper) {
for {
time.Sleep(1 * time.Second)
c := http.Client{Transport:tr}
resp, err := c.Get("http://api.razorpay.com")
if err != nil {
log.Printf("Failed to do request: %v", err)

io.Copy(ioutil.Discard, resp.Body)
log.Printf("resp status: %v", resp.Status)
ch := make(chan struct{})
2020/08/02 01:17:45 resp status: 200 OK
2020/08/02 01:17:46 resp status: 200 OK
2020/08/02 01:17:48 resp status: 200 OK
2020/08/02 01:17:50 resp status: 200 OK

The solution

Why is this a responsibilty of the server to terminate connections?

  1. DNS resolved IPs corresponding to a domain or a sub domain can change due to variety of reasons, like, load balancer rotating IPs, weighted DNS entries etc. and not every time the older IPs may stop working.
  2. The way http connection works is by resolving the DNS IPs before establishing connections to those IPs. DNS never comes into picture once a connection has been established and is kept alive.

Where do load balancers fit into the picture here?

What about http2?

Strict DNS based service discovery in envoy

The 421 (Misdirected Request) status code in http2

But I would still want to handle this at my client.


  1. DNS aware persistent connections is a common topic raised at multiple places.
  2. There is no standard client-side implementation to handle this.
  3. Envoy’s strict DNS pattern falls closer to a DNS-aware client for persistent connections, but it’s not advisable to be used.
  4. The only available client-side solutions are to use time bound or number of requests bound persistent connections as available in some languages or frameworks.




A curious engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why FMCG Leaders Bet Big on eCommerce?

4 Use Cases for the Terraform CDK

What is Serverless?

Powerful admin dashboard with rails activeadmin.

The Crodo Ambassador Program

Open Source Code In Product Development: Best Practices And Risk Mitigation

Launch AWS ec2 instance using Ansible

Building AVA

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Varshney

Abhishek Varshney

A curious engineer

More from Medium

Distributed System Consensus

WebSocket Security

First steps in Monitoring Micronaut apps with Prometheus and Grafana

Manage Kubernetes Admission Webhook's certificates with cert-manager CA Injector and Vault PKI📝…