3.1.1.9. Configuring Applications and Related Services to Support Multiple Availability Zones and Regions While Minimizing Downtime
3.1.1.9. Configuring Applications and Related Services to Support Multi-AZ
Deploying infrastructure across AZs is only half the work — your application code and dependent services must also handle multi-AZ operation correctly.
Application-level considerations:
- Session management: Don't store sessions in instance memory. Use ElastiCache (Valkey or Redis OSS), DynamoDB, or ALB sticky sessions (less preferred — creates AZ affinity). Externalized state adds a network round trip to every request: if latency rises after moving sessions to ElastiCache, check the network path first (e.g., application nodes calling a cache node in another AZ) — Redis operations themselves are typically sub-millisecond.
- DNS caching: Set application-level DNS TTL low (60s) so failovers take effect quickly. Java's default is to cache forever.
- Connection retry logic: Database and cache connections should retry with exponential backoff when a failover occurs.
- Idempotency: During failover, requests may be retried. Ensure write operations are idempotent (using unique request IDs) to prevent duplicates.
Service-level multi-AZ configuration:
- RDS: Enable Multi-AZ in DB instance settings. Application uses the same endpoint — DNS transparently routes to the active instance.
- ElastiCache (Valkey or Redis OSS): Enable Multi-AZ with auto-failover in the replication group. Application must handle brief connection drops during failover.
- EFS: Multi-AZ by default — mount targets in each AZ's subnet.
- SQS/SNS: Multi-AZ by default. No configuration needed.
# Connection retry pattern for RDS failover
import time
import psycopg2
def get_connection(max_retries=5):
for attempt in range(max_retries):
try:
return psycopg2.connect(host=RDS_ENDPOINT, dbname=DB_NAME)
except psycopg2.OperationalError:
wait = 2 ** attempt # Exponential backoff: 1, 2, 4, 8, 16s
time.sleep(wait)
raise Exception("Failed to connect after failover")
Exam Trap: EBS volumes are AZ-specific — they can't be shared across AZs. If an instance fails in AZ-A and is replaced in AZ-B, the new instance can't access the old EBS volume. For shared storage across AZs, use EFS (NFS) or move data to S3/DynamoDB.