High Availability
Ensure your database stays online with automatic failover and standby replicas.
Overview
High Availability (HA) in SailOps PostgreSQL provides:
- Automatic failover - Less than 30 seconds
- Standby replicas - In different availability zones
- Zero data loss - Synchronous replication
- Transparent to applications - Same connection string
Architecture
Enabling High Availability
During Database Creation
- Go to Services > Create > PostgreSQL
- Toggle High Availability to ON
- Complete the configuration
- Click Create
On Existing Database
- Go to your database
- Click Edit
- Toggle High Availability to ON
- Click Save
Enabling HA on an existing database may cause a brief restart. Plan for a maintenance window.
How Failover Works
Automatic Detection
SailOps continuously monitors:
- Database process health
- Network connectivity
- Disk I/O
- Replication lag
Failover Process
When a primary fails:
- Detection - Issue detected (< 10 seconds)
- Validation - Confirms primary is truly down
- Promotion - Standby promoted to primary (< 20 seconds)
- DNS Update - Connection endpoint updated
- Application reconnects - Automatic with proper error handling
Total failover time: < 30 seconds
Application Behavior
During failover:
- Connections to old primary are dropped
- New connections route to new primary
- Applications should retry failed queries
Connection Handling
Same Connection String
Your connection string stays the same (format: {service-name}.{namespace}):
postgresql://postgres:[email protected]:5432/postgres
The internal DNS automatically resolves to the current primary. The namespace is assigned to your project.
Application Requirements
For seamless failover, applications should:
- Handle connection errors gracefully
- Implement retry logic
- Use connection pooling
Example: Node.js with Retry
const { Pool } = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
connectionTimeoutMillis: 5000,
});
async function queryWithRetry(sql, params, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await pool.query(sql, params);
} catch (error) {
if (attempt === maxRetries) throw error;
// Wait before retry
await new Promise(r => setTimeout(r, 1000 * attempt));
}
}
}
Replication Lag
Synchronous Replication
By default, HA uses synchronous replication:
- Zero data loss - All writes confirmed on standby
- Slightly higher latency - Waits for standby confirmation
Monitoring Lag
View replication status:
SELECT
pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) as lag_bytes
FROM pg_stat_replication;
In the UI:
- Go to your database
- Click Metrics tab
- View Replication Lag chart
Lag Alerts
Set up alerts for replication lag:
- Go to Monitoring > Alerts
- Create alert: Replication Lag > 1 MB
- Configure notifications
Testing Failover
Simulate Failover
Contact support to perform a planned failover test:
- Schedule maintenance window
- Support initiates failover
- Verify application behavior
- Confirm data integrity
What to Test
- Application reconnects successfully
- No data is lost
- Queries resume within acceptable time
- Alerts fire correctly
Read Replicas
Purpose
Read replicas handle read-only queries:
- Offload reporting queries
- Reduce primary load
- Geographic distribution (Enterprise)
Connection
Read replicas have separate connection strings:
# Primary (read/write)
postgresql://user:password@{db-name}-rw.{project-name}:5432/postgres
# Replica (read-only)
postgresql://user:password@{db-name}-ro.{project-name}:5432/postgres
Example: A database named mydb in project production would have:
- Primary:
mydb-rw.production - Replica:
mydb-ro.production
Application Pattern
const primaryPool = new Pool({ connectionString: PRIMARY_URL });
const replicaPool = new Pool({ connectionString: REPLICA_URL });
// Use replica for reads
async function getUser(id) {
return replicaPool.query('SELECT * FROM users WHERE id = $1', [id]);
}
// Use primary for writes
async function createUser(data) {
return primaryPool.query('INSERT INTO users ...');
}
Maintenance Windows
Planned Maintenance
SailOps performs maintenance during:
- Time: Off-peak hours (configurable)
- Notice: 24 hours advance notice
- Duration: Usually < 5 minutes
Maintenance Types
| Type | Impact | Frequency |
|---|---|---|
| Minor updates | Brief failover | Weekly |
| Major updates | Planned downtime | Quarterly |
| Security patches | Brief failover | As needed |
Configure Maintenance Window
- Go to database settings
- Set preferred maintenance window
- Receive advance notifications
Monitoring HA
Health Indicators
| Indicator | Healthy | Warning |
|---|---|---|
| Primary status | Ready | N/A |
| Standby status | Ready | Down |
| Replication lag | < 1 MB | > 10 MB |
| Failover history | None | Recent |
HA Events
View HA events:
- Go to database Events tab
- Filter by "Replication" or "Failover"
- Review history
Pricing
High Availability doubles compute costs (standby replica running synchronously):
- Primary instance: Full compute cost for selected class and resources
- Standby replica: Additional compute cost (same as primary)
The exact cost depends on your selected compute class (Eco, Standard, Production) and flavor (Small, Medium, Large, XLarge). View current pricing in the dashboard when enabling HA.
For production workloads, the cost of HA is typically much less than the cost of downtime.
Troubleshooting
Standby Not Syncing
- Check replication lag metrics
- Review database logs
- Contact support if persists
Failover Taking Too Long
- Check application retry logic
- Verify DNS resolution
- Review network configuration
Application Not Reconnecting
- Implement proper error handling
- Use connection pooling
- Set appropriate timeouts