Using Spot Instances For Cloud Databases
Published: 2022-01-09 22:47:40


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ff3uo3yw682pxvryvvje.jpg) 

## What Are Spot Instances?
A [spot instance](https://spot.io/what-are-ec2-spot-instances/) is an unused cloud-based virtual machine (VM). Cloud vendors, such as Amazon Web Services (AWS), Google Cloud, and Microsoft Azure offer spot instances at significantly low rates. The goal is to incentivize customers to purchase spare cloud capacity. 
 
You can bid on spot instances and get a discount of up 90% off the on-demand price and significantly lower your cloud costs. However, spot instances are subject to interruptions. A cloud provider may reclaim a spot instance at short notice of 30-180 seconds.

## Which Cloud Data Services are Suitable For Spot Instances?
Spot instances are ideal for stateless services that you can scale out. Here are key use cases for Spot Instances:

- **High-performance computing** (HPC)—[HPC clusters](https://www.run.ai/guides/hpc-clusters/) usually need high compute capabilities, fast storage, high network performance, and massive amounts of memory. You can leverage spot instances to support HPC operations, either by using bursting or by utilizing spot instances as the primary compute infrastructure. 
- **Distributed [cloud databases](https://cloud.netapp.com/blog/cloud-based-database-challenges-and-advantages)**—these databases can handle interruptions without affecting the service or losing any data. Notable distributed databases include MongoDB and Elasticsearch.
- **Batch-oriented processing**—you can leverage spot instances for jobs that you usually schedule to run with very little end-user interaction, including testing, big data analysis, and extraction transformation and load (ETL) operations. 

## Which Cloud Provider Should You Choose for Database Spot Instances?
All three major cloud providers offer spot instances, which you can use to run a cloud database. The relevant product offerings are:

- AWS Spot Instances
- Azure Spot VMs
- Google Preemptible VMs

**Pricing is roughly the same across all three cloud providers:** 

- All providers promise a discount of up to 90% for spot instances compared to on-demand rates. 
- In Amazon, spot prices are variable depending on market rates, and are updated every 5 minutes. 
- In Azure and Google Cloud, prices are fixed, advertised via VM instance price lists. 

**Limitations differ between the three clouds:**

- AWS Spot Instances are terminated with advance notice of 2 minutes, the longest notice of all three providers. There is a hard limit of 20 spot instances per Amazon region.
- Azure Spot VMs are terminated with advance notice of 30 seconds. They do not support certain sizes, including low-cost B-series VMs, and any promo sizes.
- Google preemptible VMs also provide an advance termination notice of 30 seconds. They do not allow migration from a preemptible VM to a regular VM. The maximum time you are allowed to use a preemptible VM is limited to 6-24 hours.

## Tutorial: Requesting a Spot Instance on AWS
A Spot Instance request allows you to define the parameters of the desired instance and the price. AWS uses this information to find spare capacity that meets your needs, Here is what you can specify in your request:

- number of instances
- instance type
- availability zone
- maximum price per instance hour 

AWS fulfills a Spot Instance requests if the following conditions are met:

- the max desired price exceeds the current Spot price
- the requested capacity is available

If the above conditions cannot be met, the system keeps your request waiting until AWS can fulfill the request or until you cancel it.

Here are the possible states of a Spot Instance request:

- `open`—pending requests.
- `active`—fulfilled requests with an associated Spot Instance.
- `failed`—requests with one or multiple bad parameters.
- `closed`—the request was active, but AWS terminated or interrupted the associated Spot Instance.
- `disabled`—the request was active, but you stopped the associated Spot Instance.
- `cancelled`—either you canceled your request or it expired.

**Here is how to create a Spot Instance request:**

1. Go to the Amazon EC2 console, using this URL: https://console.amazonaws.cn/ec2/.

2. Go to the navigation bar and choose a Region.

3. Go to the Amazon EC2 console dashboard and select Launch Instance.

4. Go to the Choose an Amazon Machine Image (AMI) page, and select an AMI.

5. Go to the Choose an Instance Type page, and choose hardware configurations and size for the Spot instance. Then, select Next: Configure Instance Details.

6. Go to the Configure Instance Details page, and configure your Spot Instance request using the following instructions:

- **Number of instances**—enter the desired number of instances.
- **Purchasing option**—select Request Spot instances.
- **Current price**—the dashboard displays the current Spot price applicable in each Availability Zone for your chosen instance type.
- **Maximum price**—(optional) either leave the field empty or specify a maximum bid.
- **Persistent request**—select the Persistent request option to resubmit a Spot Instance request. You can do this if AWS interrupts your Spot Instance.
- **Interruption behavior**—the default configuration is set to terminate an interrupted Spot Instance. When selecting the **Persistent request**, you can configure the service to stop or hibernate an interrupted Spot Instance.

7. Go to the **Add Tags** page, and specify key and value combinations to define tags.

8. Go to the **Configure Security Group** page to define firewall rules for your Spot instance. Each security group uses rules to specify the incoming network traffic delivered to the instance.

9. Go to the **Review Instance Launch** page to review the details of your Spot instance. You can use this page to make changes by choosing the relevant Edit link. Once you are ready, select **Launch**.

10. You can launch your instance by selecting the acknowledgment check box and choosing the **Launch Instances** option.
##Conclusion
In this article I explained the basics of using spot instances for cloud databases. Spot instances are mainly applicable to distributed, fault tolerant databases such as ElasticSearch, MongoDB, and Cassandra. I explained how spot instances work on the big three cloud providers, and key limitations you should be aware of, including termination notice and supported instances. Finally, I showed how to set up a spot instance on AWS, step by step.

In hope this will be of help as you evaluate the use of innovative cost optimization options for your cloud databases.