Chat Application Architecture | AWS | API Gateway | WebSockets | Microservices | System Design

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

  1. 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.
  2. 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.
  3. 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.
  4. 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

  1. Client 1 and Client 2 connect to the WebSocket API via API Gateway.
  2. API Gateway triggers the Connect Lambda function, which stores the connection ID in DynamoDB.
  3. When a client sends a message, API Gateway invokes the onMessage Lambda function.
  4. The onMessage Lambda function retrieves connection IDs from DynamoDB and forwards messages to the appropriate recipients using API Gateway.
  5. 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

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
Don’t worry about what others are doing better than you. Concentrate on beating your own records every day.
不要担心别人会做得比你好。你只需要每天都做得比前一天好就可以了
评论 抢沙发

请登录后发表评论

    暂无评论内容