CPU Spikes Caused by Periodic Scheduled Jobs

In this article I’ll show you how to eliminate periodic spikes in the CPU that are caused by the jobs that execute periodically. Figure 1 shows such a situation. You can see the spikes occurring every minute.

Figure 1. CPU spikes caused by periodic-scheduled jobs

There is a good reason to take some steps toward reducing the spikes. When the CPU usage increases, it will eventually hit the ceiling for this hardware and make the instance not responsive at times.

At first I didn’t know where the spikes were coming from. After doing some tuning, I found they are caused by a few jobs. Then I used this query to find the jobs' scheduling details as well as some other info about them.

The script gives you a very good breakdown for the jobs. I found out that I have one group of periodic jobs scheduled every minute (Figure 2), at 00 seconds past the minute. There was also another group of jobs running at a five minute interval.

Figure 2. Periodic jobs scheduled every one minute

These jobs are making periodic updates in the databases of the instance. Sometimes the updates affect many rows so that the spikes are higher and wider. Sometimes the spikes aggregate the impact of the five minute scheduled jobs as they, too, were scheduled to run every five minutes starting at 00 seconds past the minute. All the jobs were starting at the same moment, at 00:00 (Figure 3), i.e. in the first second of the minute. When they all start at the same moment, they cause the CPU usage to jump immediately, causing those spikes.

Figure 3. Example scheduling for the periodic jobs

I changed the jobs schedules, so they start at different moments (Figure 4). I made a simple alteration for the jobs schedules: the first job continued to execute on the 00 second of each minute, the second job was rescheduled to start its running in the 5th (05) second of each minute, and the third job was rescheduled to start in the 10th (10) second of each minute.

Figure 4. The one-minute interval jobs rescheduled

This simple naming convention in Figure 4 for the periodic jobs helps me to know how to schedule the next one-minute periodic job that will be added in the future. I will also know what scheduling to set up for it instantly, so that some kind of planning and regulation is preventing us from having extremely high spikes caused by the jobs executions. Of course, the rescheduling can be done some other ways that would be good too, or even better on another instance.

After several minutes running I got the following figure for the CPU (Figure 5).

Figure 5. CPU usage after rescheduling the one-minute periodic jobs

You can see the different behavior of the CPU usage by reducing the spikes in the right half of Figure 5. Then I continued my rescheduling for the other schedules of the group of jobs running every five minutes. After that, I got even smoother execution of the CPU (Figure 6).

Figure 6. CPU spikes after rescheduling all periodic jobs

Ending

The scheduling of the jobs can be important for the CPU usage. As we’ve seen in Figure 1, there were quite high spikes caused by the not well thought through scheduling of the jobs. All the jobs were running quite frequently on every minute, so having spikes such frequently is not good for the instance or my clients.

This instance had low CPU usage so the spikes were not dangerous or alerting any triggers. However, in the near future we plan to up the average CPU usage significantly, and then we would have probably had those spikes hitting the ceiling, if not prevented ahead of time.