Making dns_erlang Fast: Binary Tricks for the BEAM
Summary
DNSimple's dns_erlang post details BEAM-based binary optimization techniques applied to a high-throughput DNS server. The team rewrote hot paths in dns_erlang to reduce CPU per query and increase queries per second without using NIFs or C, instead exploiting how the BEAM handles binaries, match contexts, and immutable data. Key patterns include a single accumulator that carries the message through all sections, staying in the match context for decoding, matching large chunks instead of byte-by-byte using 56-bit SWAR-friendly chunks, and reorganizing name suffix caching to use cheap binary comparisons; together these changes yielded measurable gains on production-like workloads and reduced allocations. The article also discusses what did not work, the broader implications for the BEAM and OTP, and how the improvements translated to customer performance and resilience, along with upstream adoption in OTP and Elixir.