Random EC2 instance CPU Spike Help needed

,

Hello All

Hoping someone can help me with a strange issue i have been experiencing. I run a Linux EC2 instance for my web applications along with AWS RDS. Generally my cpu usage is low (less than 20%) average but i randomly get spikes where it reaches 80-100% cpu usage for about 2 minutes or so and an alert is triggered. These happen randomly and i cannot figure out why or what is causing this since there is no pattern of something running on the server at those times such as crons or other tasks. Does anyone know where i can look to narrow down the cause. The server is running apache, php8.2 applications.

Attached is a image of the spikes

If you are using linux, you can connect to ec2 and maybe create a log file, something like this. https://askubuntu.com/questions/22021/how-to-log-cpu-load

Are you running one of the instance types with a burstable vCPU credit balance?

No the instance is not burstable.

Thanks ill check this out. I do have a bunch of application logs for API requests that go out and are sent to my application however nothing seems out of the normal on those.

If you are using Linux, turn on sar and pay attention to iowait. What is the storage backend type that you are using? Using EBS, there are still several different volume type, ranging from SSD to several HDD based.

Your symptoms sound like iowait issues.

You may be able to save money by using a burstable instance since average is <20%.

You said your app stack is apache/php. So monitor memory usage. Or in lieu of that, add a swapfile and see if you still have CPU spikes. If they’re gone, that means your system was running out of memory and the CPU was desperately shuffling and rearranging memory to make more room. This usually locks up the system for a while though, not for a single 5min spike.

Also correlate against other metrics. Anything else spike at that time? Disk IO? Network In/Out?

My storage is EBS. Can you please tell me how to turn on SAR that you are referring to. Im not really familiar with it.

There are several types of volume backing EBS. Which one are you using? SSD or HDD? There are also different types for each one :-).

There is Throughput Optimized, Cold HDD, General Purpose and Provisioned IOPS.

As for installing SAR, it depends on the Linux distribution you are using. For Debian and Ubuntu flavor, usually “sudo apt install sysstat” should do it. Google online for how to install, setup and use SAR to monitor system performance.

Thanks will do. Im not sure if its ssh or HDD honestly when looking at the volume details im only seeing type as gp2

The aws ec2 monitoring and ebs monitoring should be enough to tell you if you need to go down this path. You should see spikes in i/o at the same time. If they are, investigate further. If not, it is something else.

Sometime basic monitoring is good enough. Note that free AWS monitoring is collected metrics at 5 minutes interval, which is long enough that you might miss valuable metrics.

Start with the free monitoring, if that’s not enough, then start installing SAR on your system to get better idea of what’s causing the issue.

GP2 is General Purpose SSD. How large is the volume? Smaller volume has less IOPS.

Another possible cause for your spikes is the noisy neighbor. In the cloud, your instance VM might be running on the same HW that another larger instance is running hard (very hard) and using up system resources.

Its 50GIB with 150 IOPS

Case in point just dealt with the same thing earlier this week where every spike that caused an issue was completely missed by metrics. So yes, good point.

Thay is incredibly slow.

What would you recommend?

It should be able to burst up to 3000 iops

I’d look to run top in batch mode, save the output every minute or so and let it run via cron. When you see a spike, check the output of that specific minute and see what was using the most cpu at that time.

This may end up being a standard Linux question rather than EC2. Most of the people are right - look through your ClousWatch metrics for EC2 and EBS volumes. Pay attention to CPU credits (if T-based instance) and EBS queue depth. Both should be stable with little movements.

What instance size is this? You may be out of memory or CPU on the moment it is needing the power. Is the instance happen to be a webserver? It may be an attack or crawl attempt

On the Linux end - was PHP and it’s webserver configured properly? If fpm - did you set up enough workers not to drown the machine (hint: most distro default to 50 max and that is way too high)? Is your php max memory size too high?

Your question unfortunately is too open ended and need thorough monitoring. Try some free monitoring software to capture your system and process stats. It may point to underallocation of AWS resources.