10 min read

    11 - Machine Types and Cost Optimization

    gcpcloudcomputecost-optimization

    Welcome to Day 11 of Learn GCP in 30 Days! Yesterday, you launched your first Virtual Machine and served a live web page. Today, we learn how to pick the right computer size and save up to 90% on cloud costs using GCP Machine Types & Spot VMs.

    🎯

    Today's Goal Today, you will learn how to select the perfect virtual computer size for your application, understand the difference between cheap HDDs and fast SSD disks, and use Google's secret 90% discount trick (Spot VMs) to run servers for pennies!


    The Core Problem: Overpaying for Giant Computers

    Imagine you are starting a small online shop. On day one, you expect 10 visitors a day.

    What Existed Previously:

    In old physical offices, if a company needed a server for a small task (like testing a new app or running a personal blog), hardware stores only sold expensive, high-power server towers.

    Or, if a company needed a super-fast computer for just 3 days a month to calculate employee salaries, they had to buy a massive physical computer that sat completely idle for the remaining 27 days of the month.

    Problems Faced:

    • πŸ’Έ Wasted Money (Over-provisioning): Companies paid hundreds of dollars a month for giant servers when their application only used 5% of the computer's power.
    • 🚚 One Size Doesn't Fit All: Different jobs need different strengths (some apps need raw CPU calculation speed, some need massive RAM memory, some need graphics GPUs for AI).
    • πŸ“‰ Zero Discounts: Traditional hardware vendors charged full price regardless of whether you used the computer 1 hour or 24 hours a day.

    How Present Technology Solves It:

    Google Cloud provides Smart Machine Families & Cost-Optimization Tools:

    1. Pre-built Machine Families: Choose pre-sized virtual computers tailored to your specific job (General Purpose for web apps, Memory-Optimized for large databases, Compute-Optimized for gaming/math).
    2. Custom Machine Types: Pick exact CPU cores and RAM gigabytes (e.g. 3 vCPUs and 7 GB RAM) so you pay only for what you use.
    3. Spot VMs (Preemptible VMs): Rent Google's idle, unused data center servers at a 60% to 90% discount!
    mermaid

    Real-World Analogy: The Vehicle Rental Agency

    Imagine walking into a Vehicle Rental Agency. You don't rent a 10-ton semi-truck just to buy groceries!

    mermaid

    πŸ”‘ Everyday Cloud Terms Explained in Plain English

    Let's break down 5 essential machine sizing & cost terms into everyday concepts:


    1. Machine Families (The 4 Categories)

    Google groups virtual computers into 4 main families so you can pick the right tool for the job:

    Machine FamilyReal-World EquivalentBest Used ForExample Codes
    General PurposeπŸš— Standard SedanWeb apps, blogs, small databases, beginner testinge2-micro, e2-medium, n2
    Compute-Optimized🏎️ Sports CarGaming servers, video encoding, high-speed mathc2, c3
    Memory-Optimized🚚 Moving TruckGiant in-memory databases (SAP HANA, Redis)m2, m3
    Accelerator-Optimized🚜 Heavy CraneAI model training, machine learning, 3D graphicsa2, g2 (with GPUs)
    🌟

    The Beginner's Default Choice: E2 Family 95% of the time as a beginner or web developer, you will use the E2 General Purpose family (like e2-micro or e2-medium). It is flexible, fast, and extremely cheap!


    2. Standard Disks (HDD) vs. SSD Disks

    When you attach a hard drive to your VM, GCP gives you 2 main disk storage choices:

    • 🐒 Standard Persistent Disk (HDD): Slow spinning magnetic hard drive. Very cheap! Great for background backups and file archives.
    • ⚑ SSD Persistent Disk: Fast solid-state flash drive. High-speed reading and writing! Great for databases and active web servers.

    3. Spot VMs (Save Up to 90%!)

    Google operates millions of physical servers worldwide. At any given moment, thousands of CPUs sit idle waiting for customers.

    Instead of letting those servers sit empty, Google offers Spot VMs:

    Anatomy Breakdown:

    • πŸ’° 33% to 90% Discount: Depending on machine size and region, renting idle capacity drops costs drastically! (Standard provisioning model has zero discount; Spot model unlocks instant discounts).
    • ⚠️ The One Catch (Preemption): If a full-paying customer needs that server, Google gives you a 30-second advance warning and reclaims the server.
    • 🎯 Best Used For: Non-critical background jobs, batch data processing, software testing, and personal learning labs. Never use Spot VMs for main customer production databases!

    4. Custom Machine Types (Build Your Own Size)

    If GCP's pre-built sizes don't fit your needs (e.g. you need 3 vCPUs and 11 GB RAM), you can select Custom Machine Type to dial in the exact CPU cores and RAM memory you wantβ€”saving money by not paying for extra unused capacity!


    5. Resizing a VM (Upgrading CPU & RAM in Seconds)

    What if your small e2-micro website suddenly gets popular next month?

    You don't need to rebuild your website from scratch! You simply Stop the VM, change the machine type dropdown to e2-standard-4, and Start the VM back up. Your server now has 4 vCPUs and 16 GB RAM in under 30 seconds!


    πŸ§ͺ Hands-on Lab Activity: Launching a Discount Spot VM

    You can create a Spot VM using either the Web Console Website (UI) or Cloud Shell (CLI)!


    Activity 1: Launch a Spot VM

    Method A: Web Console Website (UI Click-by-Click)

    1. Log into console.cloud.google.com.
    2. Open the left menu ☰ β†’\rightarrow Click Compute Engine β†’\rightarrow Click VM instances.
    3. Click Create Instance at the top bar.
    4. Fill in basic details:
      • Name: cost-saver-vm
      • Region: asia-south1 (Mumbai) | Zone: asia-south1-a
      • Machine Configuration: Series E2, Machine type e2-micro.
    5. Select Spot Provisioning Model:
      • Scroll down to VM Provisioning Model.
      • Change the radio button from Standard (no discount) to Spot.
      • Notice the estimated price on the right side instantly shows a discount badge (e.g. 33% off for e2-micro, up to 90% for larger instances)!
    6. Click Create!

    Method B: Cloud Shell CLI (Command Terminal)

    1. Click the Activate Cloud Shell (>_) icon at the top right of your GCP Console.
    2. Run this command to launch the Spot VM:
      bash
      gcloud compute instances create cost-saver-vm \
          --zone=asia-south1-a \
          --machine-type=e2-micro \
          --provisioning-model=SPOT \
          --instance-termination-action=STOP \
          --description="My cost-optimized Spot VM lab instance"
      

    Output:

    text
    Creating instance cost-saver-vm...done.
    NAME           ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
    cost-saver-vm  asia-south1-a  e2-micro      true         10.160.0.3   34.93.200.10   RUNNING
    

    Activity 2: Inspect Machine Types

    • Via Web Console UI: Click Create Instance β†’\rightarrow Under Machine Configuration, open the Machine type dropdown menu to browse sizes and prices live.
    • Via Cloud Shell CLI:
      bash
      gcloud compute machine-types list --filter="zone:asia-south1-a AND name:e2*"
      

    Activity 3: Log into your Spot VM using SSH

    πŸ’‘

    Cloud Shell CLI vs. SSH into VM (What's the Difference?)

    • Cloud Shell Terminal (>_): Like running cmd on your management computer to manage Google Cloud (creating VMs, creating firewalls, deleting projects).
    • SSH Terminal: Like running cmd INSIDE the remote virtual server you rented from GCP (running Linux software, checking system logs, managing files).

    Method A: Web Console Website (UI 1-Click)

    1. Go to Compute Engine β†’\rightarrow VM instances.
    2. Next to cost-saver-vm, click the SSH button under the Connect column.
    3. A popup browser window opens connected directly inside your Linux server!

    Method B: Cloud Shell CLI

    1. Run gcloud compute ssh cost-saver-vm --zone=asia-south1-a.
    2. Type hostname inside the VM prompt to confirm you are logged into cost-saver-vm, then type exit to disconnect.

    🧹 Clean Up Step (Credit Safety Guarantee)

    Delete your test Spot VM to keep your learning environment clean and costs at $0.00:

    • Via Web Console UI: Check cost-saver-vm β†’\rightarrow Click Delete at the top bar.
    • Via Cloud Shell CLI:
      bash
      gcloud compute instances delete cost-saver-vm --zone=asia-south1-a --quiet
      

    Signal vs. Noise: Key Concepts & Noise Filter

    🧠

    Good to Know (Key Concepts)

    • E2 General Purpose: The standard default machine family for beginners and web apps.
    • Spot VMs: Rent idle Google servers at a 33%–90% discount with a 30-second reclamation warning.
    • HDD vs. SSD: HDDs are cheap for backups; SSDs are fast for active databases and web servers.
    • Custom Machine Types: Pick exact CPU and RAM numbers to avoid paying for unused capacity.
    • Cloud Shell vs SSH: Cloud Shell manages GCP; SSH opens the terminal inside your VM.
    • VM Resizing: Stop VM β†’\rightarrow change size β†’\rightarrow Start VM to upgrade specs in 30 seconds.
    ℹ️

    Noise Filter (Don't Memorize)

    • Do NOT try to memorize exact hourly pricing cents (e.g. $0.0084/hr)β€”prices adjust dynamically and are calculated automatically in the GCP console pricing calculator.
    • Do NOT memorize hardware processor clock speeds (GHz)β€”GCP handles physical CPU allocation automatically.

    Common Doubts & Interview Traps

    Q1: Should I run my main company database on a Spot VM to save money?

    • Answer: Absolutely NOT! Spot VMs can be reclaimed by Google with a 30-second warning whenever demand spikes. Use Spot VMs for background jobs, testing, and batch processingβ€”never for critical databases!

    Q2: What is the difference between Cloud Shell and SSH into a VM?

    • Answer: Cloud Shell is your management terminal to command GCP (creating/deleting VMs). SSH is the terminal doorway into your rented virtual server to run Linux software.

    Q3: Can I upgrade a running VM's CPU and RAM without deleting it?

    • Answer: Yes! You stop the VM, edit its machine type (e.g. from e2-micro to e2-medium), and start it again. You do NOT need to delete or recreate the VM!

    Daily Practice Drill & Self-Check

    Test your understanding of today's lesson:

    text
    // Try answering these:
    1. Which machine family is best suited for everyday web apps, blogs, and beginner learning labs: General Purpose (E2), Compute-Optimized (C2), or Memory-Optimized (M2)?
    2. What is the difference between Cloud Shell terminal and SSH terminal?
    3. True or False: Standard provisioning model offers a Spot discount automatically.
    
    πŸ’‘ Click for Solutions
    1. General Purpose (E2)! It is balanced, flexible, and cost-effective for general web workloads.
    2. Cloud Shell manages GCP resources (like cmd on your laptop); SSH logs into your rented remote server to run Linux commands inside it!
    3. False! Standard provisioning model is regular full-price billing. You must select Spot to unlock discounts!

    πŸŽ‰ Awesome job! You have completed Day 11.

    You now understand machine families, HDD vs. SSD disks, Spot VM savings, Cloud Shell vs SSH, and how to resize virtual computers using both the Web Console and Cloud Shell. Take a break, let today's concepts sink in, and come back tomorrow fresh!

    Tomorrow on Day 12, we will explore Managed Instance Groups and Autoscaling so your application can automatically add or remove servers when traffic spikes!


    ← 10 - Compute Engine VMs - Launching Virtual Servers | Next Topic β†’ 12 - Managed Instance Groups and Autoscaling