Course contents

14. Cloud and Infrastructure Lesson 1 of 8

Virtual Machines

See how a hypervisor splits one host into isolated VMs, how instance size caps throughput, why resizing and booting take time, and what a noisy neighbour does.

14 minIntermediate8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A hypervisor runs several VMs on one host; each has its own kernel and virtual hardware.
  • The VM boundary contains crashes and security faults, but not contention for shared hardware.
  • An instance type is a hard ceiling: vCPUs × work per vCPU-second.
  • Resizing a VM usually means a restart; scaling out avoids that but waits for boot and health checks.
  • Noisy neighbours show up as steal time and latency; route around them, then change placement.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

A virtual machine (VM) is a computer implemented in software on top of a real one. A hypervisor runs on the physical host and gives each VM virtual CPUs, memory, disks and network interfaces. Each VM boots its own operating system kernel and runs as if it had the hardware to itself. Cloud "instances" (EC2, Compute Engine, Azure VMs) are VMs; the provider's hypervisor and control plane decide which physical host each one lands on.

Why it exists

Physical servers are expensive, slow to buy and usually underused. Virtualisation lets one machine run many independent workloads safely: different customers, different operating systems, different kernels. You get a machine in minutes through an API, resize or replace it without touching hardware, and pay for what you run. For providers, packing many VMs onto large hosts is what makes the cloud economical.

How it works

Hypervisor. Type-1 hypervisors (KVM, Xen, Hyper-V, ESXi) run directly on the hardware, or as part of the host kernel in KVM's case. Hardware virtualisation extensions (Intel VT-x, AMD-V, and second-level address translation for memory) let guest code run directly on the CPU at near-native speed; the hypervisor steps in only for privileged operations. Major clouds offload networking and storage virtualisation to dedicated cards so that more host CPU is left for guests.

vCPU scheduling. A vCPU is a thread the hypervisor schedules onto a physical core (on x86 clouds usually one hardware thread, i.e. half a core with SMT). If the host has more runnable vCPUs than hardware threads, some wait. The guest sees that waiting as steal time (st in top, steal in /proc/stat).

Isolation. Each VM has its own kernel and address space; a guest kernel panic, a root compromise inside a guest, or a full guest disk stays inside that VM. This boundary is strong enough that providers put unrelated customers on the same host. It does not partition everything: last-level caches, memory bandwidth, local disks and network links are shared, which is where noisy neighbours come from.

Instance types. You pick a size from a catalogue: vCPUs, memory and sometimes local disk, network bandwidth and accelerators, grouped into families (general purpose, compute-, memory-, storage-optimised). Burstable families give a baseline CPU share plus credits for short bursts.

Lifecycle. Launching a VM means: allocate a host, attach a disk created from an image, boot the guest OS, run start-up scripts, start the application, pass health checks. Expect tens of seconds to a few minutes, depending on image size and what the app does at start-up. Changing the instance type on most clouds requires stopping the VM and starting it again.

When to use it

  • You need a full OS you control: kernel modules, specific kernels, system services, or software that expects a whole machine.
  • Strong isolation between tenants or trust levels.
  • Long-running, stateful services (databases, brokers) where boot time does not matter much.
  • As the host for containers: most Kubernetes nodes are VMs.

When not to

  • Short, bursty work where boot time dominates: containers or functions start much faster.
  • Many small services on one machine: a guest OS per service wastes memory and patching effort.
  • When you would rather not manage an OS at all: managed services or serverless.

Trade-offs

Choice You gain You pay
VM instead of container Own kernel, stronger isolation Boot time, memory per guest OS, OS patching per VM
Scale up (bigger type) One machine, no distribution Restart to resize; hard ceiling; single point of failure
Scale out (more VMs) No resize outage, survives a host failure Load balancer, boot time for each new VM, more to operate
Burstable type Cheap for mostly idle workloads Throttled when credits run out
Dedicated host No other tenants on your hardware Higher cost, you manage host capacity

Alternatives

  • Containers: share the host kernel, start in well under a second to a few seconds, much less overhead; weaker isolation boundary.
  • MicroVMs (Firecracker, Cloud Hypervisor): VM isolation with a minimal device model, booting in a fraction of a second; used under serverless platforms and sandboxed container runtimes.
  • Bare metal: no hypervisor overhead or neighbours; slowest to provision and resize.
  • Serverless functions: no machine to size at all, at the cost of limits on runtime and state.

Failure modes

  • Undersized instance: CPU saturates, queues grow, then requests are rejected or time out.
  • Resize outage: a single VM resized in place is down for the restart.
  • Slow scale-out: new VMs arrive after allocation, boot and health checks; the existing fleet must survive that window.
  • Noisy neighbour: another tenant's load raises your latency through CPU steal or shared caches, memory bandwidth, disk or network. The guest's own CPU graph may look normal.
  • Host failure or maintenance: all VMs on a host are affected together. Clouds live-migrate many VMs for planned maintenance, but hardware failures still restart them elsewhere.
  • Burst credit exhaustion: a burstable VM is fine for hours, then is throttled to its baseline.

In production

Run at least two VMs per tier behind a balancer, spread across hosts and zones (placement groups or spread policies). Bake images (golden images, Packer) so boots do less work. Monitor steal time, per-instance latency and errors, not just fleet averages. Prefer replacing VMs to resizing them in place, and keep capacity headroom for the boot time of new instances. Treat VMs as replaceable: configuration in code, data on attached volumes or managed services.

Common misconceptions

  • "A vCPU is a core." On most x86 clouds it is one hardware thread; two vCPUs share a physical core.
  • "VMs are fully isolated." They isolate faults and security domains well, but share caches, memory bandwidth, disks and networks.
  • "Resizing is free." On most clouds it needs a stop and start.
  • "A new VM adds capacity immediately." Only after it has booted, started the app and passed health checks; often later still, once caches are warm.

Interview angle

Know what a hypervisor does and how a VM differs from a container (own kernel versus shared kernel). Size instances with arithmetic ("50 ms CPU per request, 8 vCPUs, about 160 req/s at 100%; plan for 60–70%"). Explain why you would scale out rather than up (no resize outage, host failure tolerance) and what limits scale-out speed (boot plus health checks). Mentioning steal time and noisy neighbours when asked about unexplained latency on cloud VMs shows practical experience.

Model assumptions

This lesson uses a simplified model, not a benchmark. A VM is a simulated server whose workers stand for vCPUs; each request needs 50 ms of CPU (lognormal, cv 0.35), so 4 vCPUs serve about 80 req/s and 8 about 160. Arrivals are Poisson. The hypervisor is a scripted illustration: its time-slice tokens are not simulated scheduling. The noisy neighbour is modelled as the Web VM running at half speed, not as cache or memory-bandwidth contention. Boots and resizes are timed actions on a compressed timeline (about a second instead of tens of seconds to minutes). Health checks run every 250 ms; 2 failures eject, 2 successes admit. Utilisation is the fraction of busy vCPUs.

The lesson as textEvery step of the simulation, written out.
  1. 1. One physical machine, several virtual ones

    Physical host A runs a hypervisor (KVM, Xen, Hyper-V or ESXi; cloud providers run their own builds of these). It carves the machine into virtual machines: each gets virtual CPUs, memory, disks and a network card, boots its own kernel and believes it owns the hardware.

    The gold tokens are the hypervisor handing out CPU time slices: a vCPU only runs while the hypervisor has scheduled it on a real core. Our Web VM has 4 vCPUs and serves 40 req/s at about half its capacity; a second customer’s VM shares the same host. Web VM 2 on host B is not running yet.

    Scene: Physical host A runs a hypervisor and two virtual machines: the 4-vCPU Web VM serving 40 requests per second at about half capacity, and Tenant B’s VM. The hypervisor alternately hands CPU time slices to both. Host B is empty.

    Key idea: A VM is a full computer in software: own kernel, own virtual hardware, time-sliced onto a real machine by the hypervisor.

  2. 2. A neighbour’s kernel panics; yours keeps running

    One second in, Tenant B’s VM crashes (a kernel panic inside the guest). Every one of Tenant B’s requests now fails.

    Our Web VM does not notice: it has its own kernel, its own memory and its own virtual devices, and the hypervisor keeps scheduling it. It keeps serving its 40 req/s with zero errors. This strong boundary is why clouds can put strangers on the same machine.

    Scene: Tenant B’s VM crashes one second in and all its requests fail. The Web VM on the same host keeps serving with no errors.

    Key idea: The hypervisor boundary contains crashes: a guest kernel failure stays inside that guest.

  3. 3. Picking an instance size

    You choose a VM by instance type: a fixed amount of vCPUs and memory (general purpose, compute-optimised, memory-optimised families with different ratios). On x86 clouds one vCPU is usually one hardware thread, not a whole core.

    Traffic grows to 100 req/s. Four vCPUs at 50 ms of CPU per request serve about 80 per second, so the VM saturates: all vCPUs busy, its queue full, and the excess is rejected with 503 (red packets). (Tenant B has rebooted meanwhile.)

    Scene: Traffic rises to 100 requests per second. The 4-vCPU Web VM can do about 80, so it is saturated and rejects some requests.

    Key idea: An instance size is a hard ceiling: vCPUs × requests per vCPU-second.

  4. 4. Vertical resize: stop, change type, start

    We move the VM to an 8-vCPU type. On most clouds that means stop → change instance type → start: the VM is down while it stops and boots again (here 0.5 s to 1.5 s, compressed; in reality usually tens of seconds to a few minutes).

    During that window every request fails with connection refused. Afterwards the bigger VM handles 100 req/s at about 60% and the rejections are gone. Scaling up works, but a single VM means a resize is an outage.

    Scene: The Web VM is stopped at 0.5 seconds, resized to 8 vCPUs and started again at 1.5 seconds. Every request fails while it is down; afterwards it serves 100 requests per second at about 60% with no errors.

    Key idea: Scaling a single VM up is simple but usually needs a restart, and it stays one machine.

  5. 5. Horizontal: a second VM, and its boot time

    Traffic jumps to 200 req/s, more than the 8-vCPU VM can do (about 160). Instead of a bigger machine we add Web VM 2 on host B behind a load balancer.

    A new VM is not instant: the hypervisor allocates it, the guest OS boots, the app starts. Here it starts at 0.3 s and the balancer admits it only after two good health checks. Until then all traffic still lands on the first VM, which rejects the excess. Once VM 2 joins, each VM is about 60% busy and the errors stop. Putting it on a different host also means one host failure no longer takes everything down.

    Scene: A load balancer now fronts the Web VM and a second 8-vCPU VM on host B. Traffic is 200 requests per second. Until VM 2 has booted and passed two health checks, the first VM rejects the excess; afterwards both are about 60% busy with no errors.

    Key idea: Horizontal scaling avoids resize outages and single-host risk, but new capacity arrives only after boot plus health checks.

  6. 6. The noisy neighbour

    At 0.8 s Tenant B starts a heavy batch job on host A. Its VM is isolated from ours for correctness, not for performance: both share the host’s cores, caches, memory bandwidth, disk and network. The hypervisor now hands Tenant B most of the time slices (gold tokens) and our vCPUs wait; inside the guest this shows up as CPU steal time.

    Each request on the Web VM now takes twice as long. Round robin still sends it half the traffic, about 100 req/s against a reduced capacity of about 80, so it saturates, P95 jumps and requests are rejected, while VM 2 on host B has spare room.

    Scene: Tenant B starts a heavy job on host A at 0.8 seconds. The hypervisor gives most CPU time to Tenant B’s VM, the Web VM runs at half speed, saturates and rejects requests while Web VM 2 on host B has spare capacity.

    Key idea: VMs isolate faults, not performance: shared hardware means a neighbour can slow you down.

  7. 7. Route around it, then move away

    First aid: switch the balancer to least connections. The slowed VM holds requests longer, so it gets fewer new ones; VM 2 takes the rest. Rejections stop and P95 falls, but the Web VM is still slow and the fleet has little headroom left.

    The real fixes are about placement: stop and start the VM (on most clouds it then lands on another host), choose a non-burstable or larger instance family that owns more of the host, or pay for a dedicated host. And watch steal time on every VM, because the guest’s own CPU graph does not show the cause.

    Scene: With least connections, the slowed Web VM receives fewer requests and Web VM 2 more. Rejections stop and P95 drops, though the Web VM is still running at half speed.

    Key idea: Detect contention (steal time, per-instance latency), route around slow instances, then change placement.

  8. 8. What VMs give you, and what they cost

    VMs give you a strong isolation boundary and a full OS you control, sized by instance type. They cost boot time (tens of seconds to minutes), a guest OS per VM to patch, and memory for every kernel.

    Scale up with a bigger type (a restart, still one machine) or out with more VMs behind a balancer (boot time, more pieces). Expect shared-hardware effects: noisy neighbours, steal time, burst credits. Containers, next, trade some of that isolation for much faster starts.

    Scene: Two web VMs on two hosts behind a least-connections balancer; the Web VM on host A is still slowed by its noisy neighbour.

    Key idea: VMs: strong isolation and full control, paid for with boot time, per-VM OS overhead and shared-hardware surprises.