How to configure automatic failover routing on Vercel
Configure automatic failover routing on Vercel by setting up multiple deployment targets and using Vercel's Edge Config to define routing rules with health checks. This ensures traffic automatically redirects to backup services when your primary deployment fails.
Prerequisites
- Active Vercel account with Pro plan or higher
- Multiple deployment environments or external services
- Basic understanding of DNS and routing concepts
- Access to Vercel dashboard and CLI
Step-by-Step Instructions
Create Multiple Deployment Targets
Set Up Edge Config Store
Configure Health Check Endpoints
/api/health.js:export default function handler(req, res) {
res.status(200).json({ status: 'healthy', timestamp: Date.now() });
}Deploy this to all your target environments to enable automated health monitoring.
Create Failover Middleware
middleware.js file in your project root:import { get } from '@vercel/edge-config';
import { NextResponse } from 'next/server';
export async function middleware(request) {
const config = await get('failover-config');
if (config?.enableFailover) {
// Check primary service health
const healthCheck = await checkHealth(config.primaryUrl);
if (!healthCheck.healthy) {
return NextResponse.redirect(config.backupUrl);
}
}
return NextResponse.next();
}Configure Edge Config Values
{
"enableFailover": true,
"primaryUrl": "https://your-primary-app.vercel.app",
"backupUrl": "https://your-backup-app.vercel.app",
"healthCheckInterval": 30000,
"timeout": 5000
}Click Save to apply the configuration.
Connect Edge Config to Your Project
EDGE_CONFIG as the key and paste the connection string from your Edge Config store. Apply this to all environments (Production, Preview, Development).Set Up Custom Domain Routing
Test and Monitor Failover
Common Issues & Troubleshooting
Middleware not executing failover logic
Ensure your middleware.js file is in the project root and includes the correct export const config matcher. Verify Edge Config connection string is properly set in environment variables.
Health checks timing out
Reduce the timeout value in your Edge Config and ensure your health check endpoints respond quickly. Add error handling to prevent middleware from blocking all requests during network issues.
Failover not working with custom domains
Verify your DNS settings point to Vercel's nameservers and check that both primary and backup deployments are accessible via HTTPS. Ensure SSL certificates are properly configured for all domains.
Edge Config updates not reflecting immediately
Edge Config changes can take up to 30 seconds to propagate globally. Use the Vercel CLI command vercel edge-config ls to verify your configuration is updated and wait for propagation before testing.