# DNS Resolution in Go and Cgo

[Grab](https://yomu.fyi/company/grab) · Ryan Law · May 24, 2017

**Type:** Problem & solution

## Summary

Go applications experiencing load balancing issues across AWS Elastic Load Balancer (ELB) nodes trace uneven traffic distribution to IP address sorting defined in RFC 6724. Comparing Go's native DNS resolver with Cgo and glibc's getaddrinfo shows that both initially sort destination addresses using Rule 9 longest matching prefix rules. Disabling IPv6 on the network interface causes C and Cgo resolvers to return IP addresses in randomized order, while the native Go resolver continues deterministic sorting. Examination of net/addrselect.go reveals that Go's native resolver implements only a subset of the RFC rules and omits dynamic source address selection. Achieving permanent parity requires modifying the Go source code directly.

## Context

A load balancing issue occurred on AWS Elastic Load Balancer (ELB) nodes due to deterministic IP address sorting under RFC 6724.

## Approach / What changed

Investigated DNS resolution across native Go, Cgo, and C by comparing getaddrinfo behavior with Go's net package, disabling interface-level IPv6, and reviewing net/addrselect.go.

## Takeaways

- Go's native DNS resolver implements only a subset of RFC 6724 rules and lacks dynamic source address selection.
- Disabling IPv6 on the network interface randomizes address order for Cgo and C resolvers using getaddrinfo, but does not affect Go's native resolver.
- Cgo executes C code on an operating system thread rather than on a lightweight goroutine.

**Tags:** [AWS](https://yomu.fyi/topic/aws), [Go](https://yomu.fyi/topic/go)

[Read original post](https://engineering.grab.com/dns-resolution-in-go-and-cgo)
