Using this one simple trick you can cut your GCP compute costs by as much as 80%!
Tags: devops, googlecloud, sre
Published: 2020-08-12 05:18:38

Okay! Now that I got your attention let me tell you how you can lower your compute costs with minimal change to your existing infrastructure or deployment scripts.

## Use Preemptible Instances!

Preemptible instances are type of compute engine resources that run at a much lower price than normal instances; they are ephemeral and are terminated (preempted) after 24 hours, if not sooner.

From the google documentation:

> Preemptible instances are excess Compute Engine capacity, so their availability varies with usage.

Preemptible instances offer us massive savings as compared to normal instances of the same machine and hardware specifications. Take a look at the N1 standard machine types pricing page and compare the Price and Preemptible price columns.

![](https://i.imgur.com/YLZ5abW.png)

## EIGHTY SEVEN PERCENT !!!

Now you’re probably thinking “_Where do I sign up? How do I move everything over to preemptible instances?_”

![](https://i.imgur.com/KytnEFe.png)

Slow down! They’re not great for everything and shouldn’t be used in systems where downtime is unacceptable. Here are some situations where using preemptible instances have an advantage and could cut down on operating costs.

* Scientific, medical, or engineering efforts that can tolerate loss and addition of computation nodes without disruption to the overall process.
* Batch image or video rendering workloads where processing power is needed on demand and where jobs that don’t exceed 24 hours.
* Machine Learning modeling that can be distributed across nodes
* Other applications that can restart from a saved state (file).

On Google Cloud Platform there are three ways to utilize preemptible instances on your compute resources:

* Single compute instance with preemptible option set.
* Managed Instance Groups - The configured compute engine template must have the preemptible option set.
* GKE Kubernetes clusters can also leverage preemptible instances as the underlying compute stack. 

This sounds like all positives, so let’s talk about the downside, being preempted, aka, having your machine terminated. When its been determined that your instance is headed for destruction you are given a 30 second window to execute any clean up functions, data synchronization, and get out of there before the machine is forcefully deleted.  You can accomplish this by configuring a shutdown script, which will automatically be executed when the system receives the signal.  The script as part of its execution triggers a shutdown of any services you want and copies files to a remote location, like a cloud bucket for the next restart. Below is a link to a great sample of a shutdown script and a python script catching the event for a graceful exit.

[Running shutdown scripts on Compute Engine ](https://cloud.google.com/compute/docs/shutdownscript)
[Sample Shutdown script for preemptible instances](https://cloud.google.com/compute/docs/instances/create-start-preemptible-instance#handle_preemption)

Preemptible instances aren’t a panacea for your billing woes; they aren’t ideal for all workloads; availability may become a concern. This is just one of many ways to cut your cloud costs and one of the first steps to knowing which direction to go in is to understand what you’re trying to do (knowing what direction to go in helps too).

***

Enjoyed the post? Let me know! 💛🦄🔖