Design a VM Provisioning System
☁️ What is a VM provisioning system? It is the part of a cloud that answers "where should this virtual machine run?". The cloud owns a fleet of physical machines (hosts), each with some CPUs and some memory. When a customer asks for a VM of a certain size, the system picks a host with enough room, reserves that room, and gives it back when the VM is deleted.
This problem looks like the parking lot with one twist: a request has two sizes at once (CPUs and memory), and a host can hold many VMs until either one runs out. It tests the same core skills: clear rules for "does it fit?" and "which one wins?", a thin entry-point class, and a clean place for the rule the interviewer says will change. Here that rule is the placement policy.
Requirements
"Design a system that places virtual machines on physical hosts. Hosts have limited CPU and memory. VMs are created and deleted all the time."
Most interviewers stop there. The questions below turn it into rules we can code and test.
Clarifying Questions
You: What does a VM request look like? Are there fixed instance types like "small" and "large"?
Interviewer: Keep it raw: each request gives a number of CPUs and an amount of memory in GB. Instance types can be a follow-up.
You: When a host has room, can a VM use part of it? Can a host run many VMs?
Interviewer: Yes. A host runs as many VMs as fit. A VM fits when the host has at least its CPUs free and at least its memory free. We don't overcommit.
You: When several hosts fit, which one gets the VM?
Interviewer: That depends on the customer of this system. Sometimes we want to pack hosts tightly so empty ones can be powered down. Sometimes we want to spread load so one host failing hurts fewer VMs. Support first fit, best fit and spread, picked when the system starts.
You: What are the exact tie-breaking rules for each?
Interviewer: First fit takes the host added first. Best fit takes the host with the fewest free CPUs, then the least free memory. Spread takes the most free CPUs, then the most free memory. Any remaining tie goes to the host added first.
You: Do hosts ever go away?
Interviewer: They go into maintenance. An operator drains a host: it keeps its running VMs, but it never takes a new one. Moving the VMs off it is a follow-up.
You: What happens when nothing fits? Do we queue the request?
Interviewer: No queue. Reject it and let the caller retry.
You: Is this called from many threads at once?
Interviewer: In real life, yes. Design it single-threaded first and we'll talk about it at the end.
The answer about tie-breaking is the one people skip, and it decides every test. "Fewest free CPUs" and "smallest host" are different rules: a big host that is almost full is a better best-fit target than a small empty one. Pin it down before writing the search.
💡 Interview tip: When a request has two dimensions (CPU and memory), say out loud that a fit check needs both to fit. Candidates often compare only CPUs, and an interviewer will happily watch a 4 GB host accept a 64 GB VM.
Final Requirements
- Operators add hosts, each with a fixed number of CPUs and GB of memory. Host ids are unique.
- A VM request has an id, a CPU count and a memory size. A host can take it when the host is not draining and has at least that many CPUs and that much memory free.
- Among the hosts that can take a VM, the configured policy picks one: first fit, best fit or spread, with the tie-breaking rules above.
- Placing a VM reserves its CPUs and memory on the host. A VM id that is already running is rejected. If no host can take the VM, the request is rejected.
- Releasing a VM gives its resources back to its host. Releasing an unknown VM is an error. A released id can be used again.
- Anyone can ask for a host's free CPUs and free memory.
- Draining a host stops new placements on it and reports how many VMs are still running there.
- The placement policy will change and grow, so adding one must not touch the rest of the system.
Out of scope
- Moving (migrating) VMs between hosts. We sketch it in Extensibility.
- Networks, disks, GPUs, images and the hypervisor that actually boots the VM.
- Queuing requests that don't fit, quotas and billing.
- Several callers at the same time (we come back to it in Extensibility).