I'm pretty new to AWS and I'm trying to figure out whether I should use EC2 or Lambda for my app's API. How much load can an EC2 instance handle, like how many concurrent requests can it process? On the flip side, if I go with Lambda, I've separated my functions, but I need to query MongoDB within these functions. Do I have to establish a new database connection for each function execution? Also, could multiple users accessing the system at the same time lead to race conditions?
2 Answers
EC2 gives you full control as it's a server you manage, so its performance ultimately depends on how you configure it. Lambda, on the other hand, is super handy as it automatically scales for you. For your MongoDB connections, it's best to set up your db connection outside the main function to reuse it for subsequent requests. And don’t stress too much about race conditions; each Lambda function runs in an isolated environment, but if two users try to order the last item at the same time, there could be issues unless you handle that in your logic.
It really depends on the setup of your EC2 instance. For a simple CRUD application, even a smaller instance like a t4.small can manage tens of concurrent requests, maybe even hundreds. One thing to keep in mind with Lambda is that although it can scale up easily, each cold start can take a bit longer, usually around 100-300ms. Don’t forget, managing your own server on EC2 means you also handle maintenance and updates, which is quite different from the hands-off approach with Lambda.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically