Course contentsShow
← Databases & SQL
Lesson 1547 of 1,95314. High Availability and DurabilityFree lesson

Virtual IP and Load Balancer Integration

Route client traffic seamlessly to new primary nodes after an automated failover event.

In this lesson, you will learn how to orchestrate seamless database failover by decoupling client connectivity from specific physical node addresses using Virtual IPs and Load Balancers.

The Problem: Persistent Client Connections

When an automated leader election occurs, the old primary node goes down, and a secondary is promoted. If your application code is hard-coded to point to the old primary's IP address, your system will experience downtime until someone manually updates the connection strings. To achieve true high availability, we need a way to redirect traffic to the new leader without modifying application configuration.

The Solution: Virtual IP (VIP)

A Virtual IP is an IP address that is not tied to a specific physical network interface. In a High Availability setup, a cluster manager (like Keepalived or Pacemaker) assigns the VIP to the current leader.

  1. Failure Occurs: The leader fails.
  2. Election: A new node is elected as the primary.
  3. Gratuitous ARP: The cluster manager moves the VIP to the new primary and broadcasts a "gratuitous ARP" packet. This notifies the network switch that the MAC address associated with the VIP has changed, causing all subsequent traffic sent to that IP to arrive at the new primary instantly.

Load Balancer (Proxy) Integration

Alternatively, you can place a smart proxy (like HAProxy or ProxySQL) between your application and the database. The proxy performs two key functions:

  • Health Checking: It continuously polls nodes using liveness probes to verify which node is the current write-capable leader.
  • Traffic Routing: The application connects to the proxy, which maintains a persistent pool of connections to the database layer. When the proxy detects that the old leader is offline, it instantly routes incoming write queries to the newly promoted leader.
-- Example Proxy Configuration (conceptual)
listen database-cluster
    bind *:5432
    mode tcp
    # Proxy identifies the new leader via a health check script
    option httpchk GET /is_leader
    server node1 192.168.1.10:5432 check
    server node2 192.168.1.11:5432 check

By abstracting the database identity away from the physical hardware, your application maintains a stable connection point, making the transition between primary nodes transparent to the end-user.

Key Takeaway: By using Virtual IPs or smart load balancers, you abstract the database's physical location, allowing the cluster to reroute traffic automatically during failover without requiring application-level connection changes.