1 Answer
- Newest
- Most votes
- Most comments
0
To achieve low latency, scalability, and cost efficiency for your use case, you can consider the following
- Use Amazon API Gateway with WebSocket support to handle the WebSocket connections. API Gateway will manage the connections and route messages to the appropriate backend services.
- For processing personalized JSON files and executing Typescript/JavaScript/Python code, use AWS Lambda. Lambda allows you to run functions in response to events, in this case, messages coming from API Gateway. This will provide fast spin-up, auto-scaling, and cost efficiency.
- To reduce the latency of reading and writing JSON files, use Amazon ElastiCache (either Redis or Memcached) as an in-memory data store. When a user connects, load the JSON file from S3 into ElastiCache. Perform updates in ElastiCache and persist the data back to S3 when the user disconnects. This approach will significantly reduce latency compared to loading the JSON directly from S3.
- Store the personalized JSON files in an Amazon S3 bucket.
- To reduce latency globally, use Amazon CloudFront with Lambda@Edge. With Lambda@Edge, you can run Lambda functions closer to the users, reducing latency.
In summary, the architecture would look like this:
Users connect to API Gateway (WebSocket) which triggers Lambda functions. Lambda functions read/write data from/to ElastiCache (in-memory storage). ElastiCache loads data from S3 when the user connects and stores data back to S3 when the user disconnects. Use CloudFront and Lambda@Edge to reduce global latency.
Relevant content
- Accepted Answerasked a year ago
- asked 3 years ago
- asked a month ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago
Many thanks for getting back to me. Is it really possible to essentially have one lambda instance per user? Also couldn't we just store the JSON in the lambda and persist it back to S3 upon disconnect? Is there some CloudFront/Terraform for setting it up?
Lambda functions are stateless and there is no way to assign a specific user to a specific instance. For that reason, as @sdtslmn mentioned, you should save the interim objects outside of the function memory.
I would also look at using DynamoDB as the interim store instead of Elasticache, to have a fully serverless solution.