.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto/core/containerization/spot_instances.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_core_containerization_spot_instances.py: Using Spot/Preemptible Instances -------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 7-32 What Are Spot/Preemptible Instances? ==================================== Spot instances are unused EC2 capacity in AWS. `Spot instances `_ can result in up to 90% savings on on-demand prices. The caveat is that these instances can be preempted at any point and no longer be available for use. This can happen due to: * Price – The spot price is greater than your maximum price. * Capacity – If there are not enough unused EC2 instances to meet the demand for spot instances, Amazon EC2 interrupts spot instances. Amazon EC2 determines the order in which the instances are interrupted. * Constraints – If your request includes a constraint such as a launch group or an Availability Zone group, these spot instances are terminated as a group when the constraint can no longer be met. Generally, most spot instances are obtained for around 2 hours (median), with the floor being about 20 minutes and the ceiling of unbounded duration. .. note:: Spot Instances are called ``Preemptible Instances`` in the GCP terminology. Setting up Spot Instances ^^^^^^^^^^^^^^^^^^^^^^^^^ - AWS: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html - GCP: https://cloud.google.com/compute/docs/instances/create-start-preemptible-instance If an auto-scaling group (ASG) is set up, you may want to isolate the tasks you want to trigger on spot/preemptible instances from the regular workloads. This can be done by setting taints and tolerations using the `config `__ available at ``flyteorg/flyteplugins`` repo. .. admonition:: What's an ASG for a spot/preemptible instance? When your spot/preemptible instance is terminated, ASG attempts to launch a replacement instance to maintain the desired capacity for the group. .. GENERATED FROM PYTHON SOURCE LINES 34-48 What Are Interruptible Tasks? ============================= If specified, the ``interruptible flag`` is added to the task definition and signals to the Flyte engine that it may be scheduled on machines that may be preempted, such as AWS spot instances. This is low-hanging fruit for any cost-savings initiative. Setting Interruptible ^^^^^^^^^^^^^^^^^^^^^ To run your workload on a spot/preemptible instance, you can set interruptible to ``True``. For example: .. code-block:: python @task(cache_version='1', interruptible=True) def add_one_and_print(value_to_print: int) -> int: return value_to_print + 1 .. GENERATED FROM PYTHON SOURCE LINES 50-65 By setting this value, Flyte will schedule your task on an auto-scaling group (ASG) with only spot instances. .. note:: If your task gets preempted, Flyte will retry your task on a non-spot (regular) instance. This retry will not count towards a retry that a user sets. Which Tasks Should Be Set To Interruptible? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Most Flyte workloads should be good candidates for spot instances. If your task does NOT exhibit the following properties, you can set ``interruptible`` to true. * Time-sensitive: It needs to run now and can not have any unexpected delays. * Side Effects: The task is not idempotent, and retrying will cause issues. * Long-Running Tasks: The task takes > 2 hours. Having an interruption during this time frame could potentially waste a lot of computation. In a nutshell, you should use spot/preemptible instances when you want to reduce the total cost of running jobs at the expense of potential delays in execution due to restarts. .. GENERATED FROM PYTHON SOURCE LINES 67-68 .. TODO: Write "How to Recover From Interruptions?" section .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_core_containerization_spot_instances.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: spot_instances.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: spot_instances.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_