How to Implement DNS Load Balancing with Multiple A Records for Improved API Performance


2 views

When your API starts experiencing performance bottlenecks on shared hosting, the immediate solution often involves upgrading to a VPS. However, a more elegant approach exists: distributing traffic between both servers using multiple A records. This DNS-based load balancing technique can significantly improve response times while maintaining redundancy.

Multiple A records for the same domain create a simple load balancing mechanism called DNS Round Robin. When you configure:

example.com.    300    IN    A    192.0.2.1
example.com.    300    IN    A    203.0.113.2

DNS servers will alternate the order of these records in responses, effectively distributing requests between both IP addresses.

Here's how to configure this on popular DNS providers:

# Cloudflare API example (using curl)
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records" \
-H "Authorization: Bearer API_TOKEN" \
-H "Content-Type: application/json" \
--data '[
  {
    "type": "A",
    "name": "api.example.com",
    "content": "192.0.2.1",
    "ttl": 300
  },
  {
    "type": "A",
    "name": "api.example.com",
    "content": "203.0.113.2",
    "ttl": 300
  }
]'

Browsers typically use the first A record returned by DNS, but this behavior isn't guaranteed. Modern browsers may implement their own DNS caching and selection algorithms. For API clients, consider these factors:

  • DNS TTL values (shorter TTLs allow faster failover but increase DNS queries)
  • Client-side DNS caching behavior
  • Health checks (basic DNS lacks built-in health monitoring)

For more sophisticated traffic management, consider these enhancements:

# Weighted DNS example (not universally supported)
api.example.com.    300    IN    A    192.0.2.1
api.example.com.    300    IN    A    203.0.113.2
; Weight annotation (implementation-specific)
api.example.com.    IN    TXT    "weight=192.0.2.1:70;203.0.113.2:30"

Implement a monitoring script to ensure both servers are responsive:

#!/bin/bash
# Basic health check script
servers=("192.0.2.1" "203.0.113.2")

for server in "${servers[@]}"; do
  if ! curl -s --connect-timeout 2 "http://$server/health" | grep -q "healthy"; then
    # Trigger DNS update or alert
    echo "Server $server failed health check" | mail -s "API Server Down" admin@example.com
  fi
done

When your API starts gaining traction, shared hosting often becomes the first performance bottleneck. The 503 errors you're seeing typically indicate resource exhaustion - your host is hitting its CPU/memory limits when processing concurrent requests.

Yes, you can absolutely set up multiple A records for the same domain. This technique is called DNS round robin, where DNS servers rotate through the list of IP addresses when resolving your domain.

example.com.    IN  A   192.0.2.1
example.com.    IN  A   203.0.113.2

When a client queries your DNS:

  1. The DNS server returns both IP addresses
  2. Most clients will use the first IP in the list
  3. Subsequent queries get the records rotated

DNS round robin has some limitations you should know:

// Example of checking DNS records programmatically (Node.js)
const dns = require('dns');

dns.resolve4('yourdomain.com', (err, addresses) => {
  console.log(Available IPs: ${addresses});
});

For more sophisticated traffic distribution:

  • Use a cloud load balancer (AWS ALB, NGINX, HAProxy)
  • Implement geographic DNS (Route53, Cloudflare)
  • Consider Anycast routing for global APIs

After implementation, monitor both servers:

# Sample monitoring script (Bash)
while true; do
  curl -s -o /dev/null -w "%{http_code}" api.yourdomain.com
  sleep 60
done