This article presents a robust active-passive multi-Region disaster recovery pattern for Amazon API Gateway WebSocket APIs. It addresses the unique challenge of persistent WebSocket connections being bound to a specific region by detailing a design that automatically reconnects clients and restores application state during a regional outage. The solution leverages Route 53 for DNS-based failover, DynamoDB Global Tables for cross-Region state replication, and client-side reconnection logic.
Read original on AWS Architecture BlogWebSocket applications require persistent, bidirectional communication and face significant challenges during regional outages as connections are severed and shared state can be lost. This design pattern focuses on building an active-passive multi-Region architecture to ensure high availability and disaster recovery for such applications, particularly those built on Amazon API Gateway WebSocket APIs.
The fundamental idea behind this solution is the separation of application state from the WebSocket connection itself. This allows clients to reconnect to an alternative region and restore their session context without interruption. Key components include:
Under normal operation, clients connect to the primary region via Route 53. API Gateway invokes AWS Lambda functions for connection events and messages, which interact with DynamoDB for application state. DynamoDB Global Tables replicate this state to the secondary region. A canary Lambda function continuously probes the primary API Gateway, publishing metrics to CloudWatch. If the canary detects failures, a CloudWatch alarm triggers, marking the Route 53 health check as unhealthy. Route 53 then updates DNS to point clients to the secondary region, where they reconnect and restore their state from the local DynamoDB replica.
Managing State and Authentication
When clients reconnect after failover, they must re-authenticate using a Lambda authorizer deployed in both regions. Crucially, application state should be keyed by a persistent user identifier (e.g., `sub` claim from an authenticated token) rather than the ephemeral WebSocket connection ID. For relational databases, Aurora Global Database offers similar cross-region replication, though failover times might be slightly longer due to replica promotion.
The system relies on an application-level heartbeat for both clients and the canary. The canary, a Lambda function, opens a WebSocket connection, sends a heartbeat, and expects a `pong` response. This is crucial for accurately detecting a regional outage. A CloudWatch alarm monitors the canary's health metric, triggering Route 53 failover upon consecutive failures or missing data. This proactive monitoring ensures rapid detection and redirection of traffic, minimizing downtime for real-time applications.