I'm currently hosting a Flask API as a socket server on EC2 and I'm looking for some advice on the best AWS services to use for performance and cost efficiency. I've heard options like Lambda and ECS Fargate, but I'm not sure about the pros and cons of each. What would you recommend?
3 Answers
If you're less familiar with socket APIs, try using the WebSocket API Gateway. It could simplify things for you!
Using EC2 or ECS is pretty much the same, but Fargate tends to be a bit pricier. To save some cash, consider reserved instances or a savings plan. If you're open to it, Lambda could be a solution, but it's not a walk in the park. You'd need a setup for database and session management, since you can't save anything persistently in Lambda. DynamoDB is a great option for session management, but large databases can be a pain since you can't just toss a MySQL database in Lambda and call it good.
Lambda for persistent web sockets? That sounds sketchy.
I have a separate MySQL database for another module that handles chat. Could I use that for the Flask API?
I’ve been running an Electron app connected via SocketIO for two years. Personally, I think EC2 or Lightsail work best for this. Just keep an eye on data size and egress costs, since exceeding certain limits can rack up expenses, but that threshold is usually pretty high.
Socket API means WebSocket, primarily for chat functionality.