Virtual machines are one of the core technologies that have improved our lives as developers for the better. Before virtual machines were common it wasn’t unheard of to purchase a new server for thousands of dollars and then host the server in a data center for hundreds of dollars a month. This process could take weeks or even months to set up a new server and god help us if we had to get quotes. Now we can cheaply and effortlessly rent a virtual machine to get ourselves started for USD 5 a month.

In this article we’ll discuss what a virtual machine is and some advantages and disadvantages to using them.

What is a Virtual Machine?

A virtual machine (VM) is essentially a file that a special piece of software, known as a hypervisor, uses to create a process on a computer that behaves like another computer. The hypervisor treats computer resources (CPU, memory, and storage) as a pool of resources that can quickly and easily be allocated to new or existing VMs. Hypervisors allow us to run multiple VMs on the same computer to maximize the utilization of the physical hardware.

When a VM is running and an instruction is issued that requires physical resources the hypervisor will “schedule” the request. This allows the VMs hosted on the same system to access the shared pool of physical resources.

There are several hypervisors (see https://en.wikipedia.org/wiki/Comparison_of_platform_virtualization_software for a partial list) that we can choose from but some of the more popular options are Xen, Linux KVM, Microsoft’s Hyper-V, VMware, and VirtualBox. We recommend VirtualBox to get started as it has several features that we need as developers, is free, runs under most OSs, and is easy to get started with.

A Couple Definitions

The physical machine that the hypervisor is installed on is known as the host machine, host computer, or just host.

The VMs that use the host machine’s resources can be known as the guest computer or simply the guest.

Virtualization is the act of creating a virtual version of a physical resource. This can be an entire computer as well as storage and networking components.

Types of Hypervisors

There are 2 different types of hypervisors that we may encounter.

Type 1 hypervisors are installed directly on the “bare metal” of their physical resources. Because there are fewer layers between the hardware and the VM the performance overhead of virtualization in a type 1 hypervisor is small. This is because the hypervisor can “schedule” the VMs against the physical hardware much more efficiently. The downside to this type is that we have to dedicate hardware for its use. VMware’s ESXi is an example of a type 1 hypervisor.

Type 2 hypervisors are hosted inside an OS. This type of hypervisor requires the VM’s resources to be scheduled against the operating system which then executes them against the physical hardware. This additional layer causes a small to moderate performance hit so it’s important to keep this in mind. Examples of a type 2 hypervisor are VirtualBox and VMware Workstation.

Type 1 hypervisors should be used for production workloads as we’re going to want the additional performance. Type 2 hypervisors are ideal for development as they can be used alongside our editors and browser.

Benefits of Using a Virtual Machine

Due to the extra layer of abstraction, it’s possible to mix and match operating systems by running a Windows VM inside Linux OS or a Linux VM on macOS. There are even some hypervisors, like QEMU, designed to also run different architectures which will allow us to use software applications and operating systems written for different CPU architectures.

If we weren’t using virtualization we would have to purchase a physical server for each of our workloads. Virtualization allows us to consolidate our computing resources which allows us to reduce space and maintenance costs. It’s also easier for us to go out and “rent” the computing resources from providers like Digital Ocean or Amazon Web Services when we don’t need a lot of them.

Disadvantages of Using a Virtual Machine

Because there is at least one level of abstraction between our VMs workload and the physical resources it’s using there will always be at least a small performance hit. In cases where this loss of performance is unacceptable (database servers, applications with lots of CPU-bound operations, real-time application) virtualization is a poor choice. In these cases, it’s best to purchase a dedicated computer for the workload.

There is a limit to the number of VMs a single host can handle and we must monitor any system that has multiple VMs. The risk we run is that because the hypervisor abstracts the physical hardware away from the guest operating system it’s possible to “oversubscribe” the physical resources of the host computer. For example, we might have 8 GB of physical RAM but we can allocate 4 VMs with 4 GB each. When we do we must make sure the VMs aren’t using the total 16 GB we’ve assigned. If this happens, it will cause performance problems for all 4 VMs as they’re forced to use a swap file.

Try It Out

With the advent of VirtualBox, it is easy to try out VM technology for yourself. If you want to try it out with a production workload you can get a $100 credit for DigitalOcean using our affiliate link (<www.digitalocean.com/?refcode=4f051fe2bd21>).