Cloud & Virtualization
Failover Explained: Keeping Your Service Online When a Server Fails
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيEvery server eventually has a bad day: a kernel panic, a full disk, a failed update, a network problem or a mistake during maintenance. Failover moves traffic from the failed component to a healthy one so your users notice little or nothing. It is not a single product but a combination of redundancy, health checks and a switching method. This article explains the main patterns, their complexity, and how to build a realistic setup on virtual servers.
The three ingredients of failover
Any failover design needs:
- Redundancy: a second copy of the component that can take over, whether that is a web server, a database or a whole site.
- Detection: health checks that decide when the primary is truly down, not just slow for a second.
- Switching: a way to redirect traffic, such as updating DNS, moving an IP address or changing a load balancer pool.
Without all three, you just have a spare server someone must switch manually at 3 a.m.
Active-passive vs active-active
| Model | How it works | Pros | Cons |
|---|---|---|---|
| Active-passive | One node serves traffic, a standby waits and takes over on failure | Simpler, easy to reason about data consistency | Standby capacity sits idle, switch takes some seconds or minutes |
| Active-active | Several nodes serve traffic at the same time behind a load balancer | No idle capacity, losing one node only reduces capacity | Application must be stateless or share state carefully |
Stateless web servers are easy to run active-active. Databases are usually run active-passive (a primary with one or more replicas) because writing to two primaries at once creates conflicts.
Common switching methods
DNS failover
A monitoring service checks your primary server and, if it fails, points the DNS record at the secondary. It works across regions. The downside is caching: resolvers and browsers may keep the old address until the record's TTL expires, and some ignore low TTLs. Use a short TTL (for example 60 to 300 seconds) on records you plan to fail over.
Floating or virtual IP
Two servers in the same location share an IP address that can be moved from one to the other. On Linux, keepalived implements this with VRRP when the network allows it; on cloud platforms, a floating IP is usually reassigned through the provider's API. Switching is fast because clients keep using the same address.
A minimal keepalived configuration on the primary:
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
virtual_ipaddress {
192.0.2.10/32
}
}The backup uses state BACKUP and a lower priority. Check your provider's documentation first, since many virtual networks do not allow multicast VRRP and require API-based IP reassignment instead.
Load balancer health checks
A load balancer such as Nginx, HAProxy or a managed service sends traffic only to backends that pass health checks. When a backend fails, it is removed from the pool automatically. The load balancer itself then becomes a component that needs redundancy.
Database failover
Databases are the hardest part, because data must not be lost or duplicated.
- Replication first: PostgreSQL streaming replication or MySQL/MariaDB replication keeps a standby up to date.
- Promotion: when the primary fails, a replica is promoted to primary. Tools such as Patroni (PostgreSQL) or Orchestrator (MySQL) can automate this.
- Fencing: make sure the old primary cannot come back and accept writes, otherwise you risk two primaries and diverging data, known as split-brain.
- Replication lag: with asynchronous replication, the newest transactions may not have reached the replica yet. Decide how much data loss is acceptable (your RPO) before choosing synchronous or asynchronous mode.
Failover is not backup
Replication copies mistakes instantly. If someone runs DROP TABLE on the primary, the replica drops it too. Failover protects against hardware and service failure; backups and snapshots protect against deletion, corruption and ransomware. You need both. Read our guide on server scaling for traffic spikes for the related topic of capacity planning.
A realistic setup on virtual servers
For a small but serious project:
- Two web VPS instances in the same location, running the same code, with sessions in Redis or the database.
- A load balancer or floating IP in front of them.
- A primary database VPS with a streaming replica, ideally on a separate host.
- Regular off-server backups and snapshots before risky changes.
- External uptime monitoring that alerts you when anything fails over.
Servers in different locations survive a full data center problem but add latency between components, so many teams keep the live pair together and use a second region for backups and disaster recovery. You can deploy servers in several locations from the deploy page.
Test it, or it does not exist
Schedule failover drills on a staging environment, then carefully in production during a quiet hour:
- Stop the web service on one node and confirm traffic continues.
- Shut down the primary database and time how long promotion takes.
- Confirm the alerts actually reached you.
- Write down every manual step you needed and automate or document it.
FAQ
What is the difference between failover and high availability?
High availability is the goal: keeping a service usable with minimal downtime. Failover is one of the mechanisms that achieves it, by switching to a standby component when the primary fails. Redundancy, monitoring and good deployment practices are other parts of the same goal.
How fast is DNS failover?
It depends on the TTL and on how strictly resolvers respect it. With a short TTL, most clients switch within a few minutes, but some cached answers can linger longer. If you need faster switching, a floating IP or load balancer in the same location is usually quicker.
Do I need failover for a small website?
Not always. For a personal site or small business page, good backups, snapshots and uptime monitoring may be enough, since a restore takes little time. Failover becomes worth its cost when downtime directly loses money or customers, such as an online store or a paid API.