2.1.1.10. Real-Time APIs and Edge Compute (API Gateway WebSocket APIs, CloudFront Functions, Lambda at Edge)
2.1.1.10. Real-Time APIs and Edge Compute (API Gateway WebSocket APIs, CloudFront Functions, Lambda at Edge)
💡 First Principle: Real-time features need the server to push data the moment it changes, which requires a persistent connection rather than client polling; edge features should run in the cheapest runtime that can do the job, as close to the viewer as possible.
WebSocket APIs in "Amazon API Gateway":
- A client opens one persistent, two-way connection. Routes decide which integration receives each message:
$connect(authorize here with a Lambda authorizer or IAM, and record the connection),$disconnect,$default, and custom routes picked by a route selection expression such as$request.body.action. - Every connection has a connection ID. Backends push to a client by calling the
@connectionsAPI (POSTtohttps://{api-id}.execute-api.{region}.amazonaws.com/{stage}/@connections/{connection_id}), and can also read connection details or force a disconnect withDELETE. - Publish-subscribe fan-out: store connection IDs with their topics in
"DynamoDB"on$connect; when an event arrives (from EventBridge, SNS or a DynamoDB stream), a Lambda function looks up the subscribers and posts to each one. AGoneException(HTTP 410) means the client is gone, so delete that ID.$disconnectis best-effort (the connection is already closed), so do not rely on it alone for cleanup. - Quotas that shape the design: a connection is closed after 10 minutes idle or 2 hours in total, and neither is adjustable, so clients send heartbeats and reconnect. Messages are up to 128 KB (sent in frames of up to 32 KB). New connections default to 500 per second per account and Region.
- Alternatives:
"AWS AppSync"GraphQL subscriptions and AppSync Events provide managed pub/sub over WebSockets without tracking connection IDs yourself;"AWS IoT Core"serves MQTT for device fleets. REST polling is the anti-pattern these replace.
CloudFront Functions vs. Lambda@Edge:
"CloudFront Functions" | "Lambda@Edge" | |
|---|---|---|
| Runtime | JavaScript | Node.js and Python |
| Events | Viewer request, viewer response | Viewer request/response and origin request/response |
| Scale and duration | Millions of requests per second, sub-millisecond | Up to 10,000 requests per second per Region, seconds |
| Network, file system, request body | No | Yes |
| Data store | CloudFront KeyValueStore | Any service it can call |
- CloudFront Functions fit lightweight work on every request: cache-key normalization, header changes (security headers,
True-Client-IP), URL redirects and rewrites, and validating a signed token such as a JWT. KeyValueStore holds lookup data such as redirect maps. - Lambda@Edge fits work that needs libraries, more memory, a network call to another service, the request body, or the origin events. An origin-request function runs only on cache misses, so expensive lookups there are paid far less often than on viewer events.
- One distribution can use both on different event types, for example a CloudFront Function on viewer request for redirects and a Lambda@Edge function on origin request to call a personalization service.
⚠️ Exam Trap: Picking Lambda@Edge for simple header or URL manipulation at very high request rates (more latency and cost for no gain), or picking CloudFront Functions for logic that must call an external API or read the request body, which it cannot do.