In this blog, we’ll dive into one of the ways to build a scalable chat application using AWS Cloud. No fluff—let’s get straight to it!
Functional Requirement
User should be able to chat using we or mobile app.
️Tech Stack
Front End
Website: HTML5 / JavaScript (SPA) / Nextjs
Mobile App: Appropriate Tools
🤝 Interactions
All Client to Server interactions over WebSockets
Request/response payload: JSON
️ Backend
Backend will be Serverless as far as possible
Deployed on ️ Cloud
Scale on demand:
Logical Architecture
AWS Architecture
🧩 Components and Their Roles
-
Clients (Client 1, Client 2, … n Clients)️
- These are WebSocket clients that connect to the API Gateway WebSocket API.
- They send and receive messages through WebSockets.
-
API Gateway (WebSocket API)
- API Gateway manages WebSocket connections.
- It provides endpoints for WebSocket connection, disconnection, and message handling.
- It routes events to the appropriate AWS Lambda functions.
-
Lambda Functions
- Connect Lambda Function
- Executes when a client establishes a WebSocket connection.
- Stores the client’s connection ID in DynamoDB for message routing.
- Disconnect Lambda Function
- Triggers when a client disconnects.
- Removes the connection ID from DynamoDB to prevent unnecessary message delivery.
- onMessage Lambda Function
- Handles incoming messages from WebSocket clients.
- Retrieves connection IDs from DynamoDB to forward messages to the correct recipient.
- Connect Lambda Function
-
DynamoDB
- Stores active WebSocket connections (connection IDs).
- Used for tracking which clients are online and enables message delivery to the correct recipients.
How This ️ Architecture Works
- Client 1 and Client 2 connect to the WebSocket API via API Gateway.
- API Gateway triggers the Connect Lambda function, which stores the connection ID in DynamoDB.
- When a client sends a message, API Gateway invokes the onMessage Lambda function.
- The onMessage Lambda function retrieves connection IDs from DynamoDB and forwards messages to the appropriate recipients using API Gateway.
- When a client disconnects, API Gateway calls the Disconnect Lambda function, which removes the connection ID from DynamoDB.
How Far Can It Scale ?
- API Gateway supports up to millions of concurrent WebSocket connections.
- Lambda can handle thousands of requests per second (subject to AWS account limits).
- DynamoDB scales on-demand and can handle millions of transactions per second with proper indexing and partitioning.
Conclusion
This architecture is fully serverless, scalable, and cost-efficient for handling real-time WebSocket connections. It works well for medium to large-scale applications and can handle millions of users with minimal infrastructure management.
原文链接:Chat Application Architecture | AWS | API Gateway | WebSockets | Microservices | System Design
暂无评论内容