Blog

Microservices and containers: how they differ, and when you need each

Microservices decide how an application is split; containers decide how each part is packaged and run. How they differ, and when a product needs either.

Microservices and containers answer two separate questions. Microservices are an architecture: the application is divided into small services, each deployed on its own. Containers are a packaging and runtime technology: a process ships with everything it needs and runs isolated from its neighbors. The two are often used together, but each works without the other.

That last point is where most of the confusion starts, and it matters when you are deciding how to build a product. This guide sets the two side by side, answers the questions people most often ask about them, and looks at when a new SaaS product should split into services at all.

Two different decisions

The usual definition of microservices comes from a 2014 article by James Lewis and Martin Fowler, who described an application built as a suite of small services, each running in its own process, organized around a business capability and deployable independently. Ship a change to billing and only the billing service is redeployed.

A container is defined by what it does at run time. NIST’s Application Container Security Guide calls containers “a form of operating system virtualization combined with application software packaging”: a way to package and run an application that is portable and easy to automate.

MicroservicesContainers
What it isAn architecture: how the application is dividedA packaging and runtime technology: how a process is bundled and run
What it decidesWhere the boundaries between parts sit, which part owns which data, how the parts talkWhat a running process carries with it, and how it is kept apart from others on the same machine
The unitA service built around one business capabilityAn image, run as one or more isolated processes
What it costsNetwork calls between services, data kept consistent across them, more to operateImages to build and patch, a registry to store them, something to run them
Works without the other?Yes: services can run as serverless functions or on plain virtual machinesYes: a whole application can run in one container

Microsoft’s architecture guide for .NET says it directly: containers are a great fit for microservices but not mandatory for them.

What is containerization, with an example?

Containerization means packaging an application’s code together with the runtime, libraries and settings it depends on into an image, then running that image as an isolated process. Take a small subscription invoicing product. It has a web API written in Node.js, a background worker that sends invoices and retries failed card payments, and a PostgreSQL database. Containerized, the API becomes an image that holds a fixed Node.js version and the exact libraries the code was tested with. The same image runs on a developer’s laptop, in staging and in production, so the version that passed testing is the version customers use. The worker gets an image of its own, and the database can run from the official PostgreSQL image or as a managed database outside the containers altogether.

The difference from a virtual machine is the operating system. As the Kubernetes documentation puts it, a virtual machine is a full machine with its own operating system, while containers share the host’s operating system with relaxed isolation. That is why they are considered lightweight next to virtual machines, and Microsoft’s guide notes that Docker images typically start in seconds.

Is a REST API a microservice?

No. A REST API is an interface: a set of URLs and rules through which one program asks another for data or actions. A microservice is a separately deployed piece of the application, and it usually exposes an API so the other services can reach it. Lewis and Fowler describe services that talk through HTTP requests to resource APIs or through lightweight messaging, and NIST’s guidance on microservices security notes that they generally communicate with each other through APIs.

The reverse does not hold. A single monolithic application can expose a REST API to its mobile app and its customers’ integrations without being split into services at all. Having an API tells you how a system is reached. It says nothing about how many deployable parts sit behind it.

The four combinations

Because the two choices are independent, a team can land in any of four places.

A two-by-two grid. Columns: no containers, containers. Rows: one deployable application, many services. One application without containers runs on a server or virtual machine, deployed as a whole. One application in a container is packaged once and scaled by running more copies. Many services without containers run as serverless functions or on virtual machines, each deployed on its own. Many services in containers run under an orchestrator such as Kubernetes. Below the grid: each choice works without the other.

  • One application, no containers. The traditional setup: the application runs on a server or a virtual machine and is deployed as a whole.
  • One application in a container. Microsoft’s guide describes a monolith shipped as one container, with capacity added by running more copies behind a load balancer. It gains faster rollouts and the same image in every environment without any change to the architecture. The limit arrives with growth: the whole application scales and redeploys together, even when only one part is under load.
  • Many services, no containers. The AWS whitepaper Implementing Microservices on AWS describes a serverless microservices architecture, with no infrastructure to provision or manage, as an alternative to running the services in containers. Services can also run on ordinary virtual machines.
  • Many services in containers. The common pairing, usually with an orchestrator. Kubernetes handles service discovery, load balancing, rollouts and rollbacks, restarting failed containers and scaling, though it does not build the application for you. On AWS the choice is usually between Amazon’s own orchestrator and managed Kubernetes, which our comparison of ECS vs EKS sets side by side.

What are the 12 principles of microservices?

There is no official list of twelve. The number almost always refers to The Twelve-Factor App, written by Adam Wiggins from the Heroku platform’s experience of running hosted web apps. It never mentions microservices. Its factors are written for any app run as a service, in any language, and say nothing about how many services the app is split into, though AWS notes that microservices often adopt its patterns.

The twelve are: codebase, dependencies, config, backing services, build-release-run, processes, port binding, concurrency, disposability, dev/prod parity, logs, and admin processes. A handful matter most when an application moves into containers. Configuration should live in the environment rather than in the image, so one image can serve staging and production. Processes should be stateless, so any copy can be stopped or added. Startup should be fast and shutdown graceful, because an orchestrator replaces containers freely. And logs should be written as a stream for the platform to collect, since a container’s own disk disappears with it.

When a SaaS product should split into services

For a new product, the people who defined the style are cautious about starting with it. In Monolith First, Fowler writes that almost all the successful microservice stories he had come across began with a monolith that grew too big and was broken up, and he advises against starting a new project with microservices. He also records that the view is not unanimous among the people he talks to.

The reasons sit in his account of the trade-offs. Calls between services travel over a network, so they are slower and can fail. Data held by different services cannot be updated in one transaction and has to be reconciled. And many small services, each redeployed often, need a mature operations team. He lists three prerequisites before a team should try it at all: provisioning new servers quickly, monitoring that catches serious problems fast, and deploying quickly to test and production. NIST’s guide lists the core features such a system then needs, each with its own security strategy: authentication and access management, service discovery, secure communication, security monitoring, circuit breakers, load balancing and throttling.

The benefits are real once a product is large. Strong module boundaries, independent releases and the freedom to use a different technology for one part all help when several teams work on one system. Microsoft’s guide describes where the pressure usually shows: a few parts of an application become the choke points that need to scale, while the rest is lightly used. That is the moment to move one part out, and the part that has to scale or ship on its own schedule is the one to move first.

One decision does not wait for any of this. How each customer’s data is kept apart from the next is a data-model question, and splitting the code into services does not answer it. A product that puts a tenant on every record from its first release keeps all of these options open later.

How we approach it

In our SaaS development work, the tenant model is settled in scoping, before the build starts, because it is one of the decisions that is most expensive to change later. Environments, deployment pipelines and monitoring are set up on AWS, in accounts your company owns. The handover documents record the architecture, and the tenant model with the reasons it was chosen, so whoever runs the product next can see how it was built, as one application or as several services, and why its customers’ data is kept apart the way it is.

The full service behind this post: SaaS Development Company & Services.

Ready to scope your project?

Send us two sentences about what you need. We reply within one business day with the next step and any questions we need answered first.

Get a Quote