Course contentsShow
← C#
Lesson 1863 of 1,90817. Deployment and Cloud ArchitectureFree lesson

Cost Optimization using Spot Instances

Configure node pools and pod priority to run background .NET tasks on cheaper cloud compute.

In this lesson, you will learn how to reduce your cloud infrastructure bill by scheduling fault-tolerant, background .NET tasks on volatile but inexpensive Spot Instances.

Understanding Spot Instances

Cloud providers offer "Spot" or "Preemptible" instances at a massive discount (often 60–90%) compared to on-demand pricing. The trade-off is that the provider can reclaim these resources with very little warning (typically 30 seconds to two minutes) if they need the capacity back for full-price customers. For a .NET developer, this means your background workers—like data processing, report generation, or batch queue consumers—must be designed to handle sudden termination.

Configuring Node Pools and Scheduling

To leverage this, you create a specific "Spot" node pool in your Kubernetes cluster. You then use Node Affinity and Tolerations to ensure only specific, non-critical pods land on these nodes.

In your .NET deployment manifest, you define tolerations to allow your pods to be scheduled on the tainted Spot nodes, and nodeAffinity to prefer them:

spec:
  tolerations:
  - key: "cloud.google.com/gke-preemptible"
    operator: "Exists"
    effect: "NoSchedule"
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        preference:
          matchExpressions:
          - key: "cloud.google.com/gke-preemptible"
            operator: In
            values: ["true"]

Pod Priority and Graceful Handling

Because Spot instances can be reclaimed, you must combine this with the Graceful Shutdown techniques we discussed earlier. When the cloud provider signals a termination, Kubernetes sends a SIGTERM to your container. Your .NET application must catch this signal to stop accepting new work, finish the current unit of work, and persist its state before the process is killed.

Furthermore, set a priorityClassName for your background tasks. If you have critical tasks, give them higher priority so that if the cluster is squeezed for resources, the scheduler ensures the most important work stays alive on stable nodes while pushing lower-priority tasks to the volatile Spot pool.

Key Takeaway: Spot instances enable massive cost savings for background .NET workloads, provided you implement robust graceful shutdown logic and use tolerations to isolate these tasks from your mission-critical services.