Understanding T2 CPU credits

Introduction

The t2 instance family provides inexpensive instances with moderate performance level and the ability to burst. This is accomplished using CPU credits which determines how much CPU you can use and for how long time. This way, AWS can cram more instances onto the same hardware and make them more inexpensive. How much CPU and for how long is determined by the instance type you are using. Depending on your workload type, t2 instances can work very well and be cost effective. But for high loads, they can perform really bad as CPU utilization will be pushed down if you eat up your credits. Let’s look at how this mechanism works.

What are CPU credits?

It’s actually quite simple – a CPU credit gives you the right to run a vCPU at 100% for one minute. So whenever your CPU utilization goes high, you start eating CPU credits. You will also continuously earn new credits. The level of when you start burning CPU credits is called the base performance level. If you use up all your credits, the CPU utilization will be pushed back down to the base performance level. Instances can only accumulate a certain amount of CPU credits and they will also start with some credits when launched. So to summarize:

The level for when instances burn credits is called the base performance level. When above, CPU credits are consumed. You will slowly but surely earn new credits. The instance will launch with an initial amount of credits and can only buffer up a certain amount. Bellow is AWS’ table for the different instance types.

cpucredits

Let’s look at a t2.small as an example. It will burn credits when above 20% CPU. It will earn 12 CPU credits per hour, meaning it can run 100% 12 minutes per hour. If idle for long time enough, it can accumulate a maximum of 288 CPU credits. It will also launch with 30 credits, giving it a little bit of room to boot up and come alive properly.

How does this work in real life?

You can actually see your CPU credits in the AWS Console. Highlight your instance, click the Monitor tab and scroll down to CPU Credit Balance or CPU Credit Usage. You can also create a CloudWatch Dashboard to see this. Bellow is a real world example for my Minecraft server. The blue line is my CPU credit balance, and the yellow line is the CPU utilization. As you can see, I start with a high balance. I then play some Minecraft causing my CPU utilization to go up and credits down. When I’ve finished playing the credits starts to build up again giving me some more credits for the next game. As long as I don’t drop to 0 credits, my server runs perfectly well.

cpu-buffers

Here is another example where I launch a new t2.micro instance. The instance is idle to begin with so it starts building up more credits slowly but surely. I then kick off a process on the machine that flats out the CPU. It starts burning credits and once all credits are burned the CPU is forced down to 10% which is the base performance level for this instance type.

cpu-cred-test

Conclusion

In order to use t2 instances successfully you should know your CPU utilization. Use the build in graphs to keep track of this. Your average utilization should not go above the base performance level for any longer periods of time. The opposite is also true – if you have instances from other instance families that are constantly bellow a t2 base performance level, consider a t2 instance. As a comparison, a t2.large with Windows costs $0.142 per Hour and m4.large costs $0.244 per Hour and they are comparable apart from CPU credits for the t2. Changing instance type is just a reboot and right click away.

Leave a comment