Skip to content

In a serverless event driven architecture, I wish to add a Chat service. Is it better to use and EC2 or Lambda?

0

I’m currently running a serverless, REST-based, event-driven architecture with Lambda, API Gateway, and RDS, and now I need to add a real-time chat service. One option is to use API Gateway WebSockets backed by Lambda and DynamoDB—but I’m reluctant to handle long-lived WebSocket connections with Lambda, and I’d prefer not to introduce DynamoDB just for connection state.

I simply need to maintain persistent connections. Which architecture would you recommend for this use case, and why?

2 Answers
0

If you use API Gateway WebSockets, you do not need to maintain the connection in the Lambda function. The connection is handled by API Gateway. You do need to manager the connections in a DynamoDB table to know where to send messages to.

Saying that, I would recommend using AppSync Events instead. It is a serverless pub/sub system, that uses websockets. The clients subscribe on channels and they can publish to channels. A channel can be a group chat, so all participants will receive the messages, or it can be a specific user id, and only that user, that subscribed on that channel, will receive the messages.

AWS
EXPERT
answered a year ago
AWS
EXPERT
reviewed a year ago
0

For adding a real-time chat service to your existing serverless architecture, AWS Lambda with API Gateway WebSockets is actually well-suited for your use case, despite your concerns.

API Gateway's WebSocket APIs are designed to handle persistent connections while Lambda functions handle the business logic when messages are sent. This approach aligns well with your existing serverless architecture and maintains the benefits of serverless computing: reduced infrastructure management, automatic scaling, and pay-per-use pricing.

AWS provides a specific pattern for creating WebSocket chat applications using API Gateway, Lambda, and DynamoDB. DynamoDB in this context is primarily used to store connection IDs and user information, which is essential for managing WebSocket connections and routing messages to the right recipients.

While you mentioned reluctance to introduce DynamoDB, it's particularly well-suited for this purpose as it provides the low-latency, high-throughput storage needed for connection management. The serverless approach would reduce your overall maintenance workload compared to managing EC2 instances, as AWS handles the underlying infrastructure.

If your concern is about long-lived connections with Lambda, it's important to note that Lambda is only invoked when messages are sent or when connections are established/disconnected - not continuously while connections are idle. API Gateway manages the persistent WebSocket connections, while Lambda functions are only triggered for specific events.

This serverless approach would be more cost-effective for variable workloads and would integrate seamlessly with your existing serverless components, maintaining the benefits of reduced backend maintenance that you're already experiencing.
Sources
Seeking Advice on Serverless Architecture for an AI Chat App with Flutter Frontend | AWS re:Post
Create a websocket chat application with API Gateway - AWS Lambda
Serverless logic tier - AWS Serverless Multi-Tier Architectures with Amazon API Gateway and AWS Lambda

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.