How I Reduced the Cost of Running a 24/7 EC2 Web Server

I run a 24/7 instance for a twitch TTS generator. The server used to get overloaded at peak traffic and expend CPU credits and RAM, causing me to have to reboot. Obviously, this is not ideal. The temporary solution was to increase the performance power of the instance. This comes at a cost, and for a while I was willing to pay it. Eventually I fixed the root problem; performance needed to be measured and optimized.

Gathering Information

Taking note of the stack, there were a few things I could think of causing issues.

  • MySQL was being used for data that could be in .JSON format as it did not change often
  • Apache was using a lot of memory at high traffic due to requests to that database

Apache assigns an individual apache process for each active connection. This causes memory to spike as multiple apache processes are spawned. Nginx has a handful of static worker processes, causing the memory footprint to have a far more stable baseline. In some test scenarios, Nginx used a fraction of the memory apache used. This was the first change.

Additonally, MySQL was taking up RAM that it didn't need to. The reason I was using MySQL was for dynamic entry of FAQ's and the privacy policy. These weren't changing enough to justify use of a relational database. I switched those to a JSON file and uninstalled MySQL. The dynamic alerts are now managed through DynamoDB.

The Results

I swapped out apache for NGINX and uninstalled MySQL. There was a significant drop in memory usage. I was able to switch from a t4g.medium to a t4g.small. I could probably drop it even lower if I wanted to. In the past week, it hasn't jumped over 20% usage.