Blog

Monolithic vs microservices: how to choose an architecture

A monolith ships as one application; microservices split it into services released on their own. How the two differ, what each costs, and which to pick.

A monolithic application is built, tested and deployed as one unit: one codebase, usually one database, one release. A microservices application is split into small services, each with its own code and its own data, each deployed when its team is ready, and they reach each other over the network. The difference that matters most in practice is the unit of release. In a monolith every change ships with everything else; with microservices a change ships with only the service it touched.

Neither is the modern or the old-fashioned choice. Each one is cheaper for some products and more expensive for others, and many teams land on a third option, the modular monolith, in between. To keep the comparison concrete, this guide follows one product throughout: an invented rental system that self-storage operators use to let units, take card payments, issue gate codes and chase late rent. No such product exists, and its numbers are illustrative.

What is a monolithic architecture?

In the 2014 article that gave microservices their usual definition, James Lewis and Martin Fowler describe the alternative first. A typical business application has three parts: a user interface, a database, and a server-side application that is “a single logical executable”. All the business logic lives in that one program, and it runs as one process. You can run many copies of it behind a load balancer, but every copy is the same whole application.

Built as a monolith, the storage rental product is one codebase with four areas of code inside it:

  • Units: the sites, the units, their sizes and who rents each one.
  • Billing: monthly rent, card payments, late fees.
  • Gate access: the code each tenant types at the gate, and locking it when rent is overdue.
  • Reminders: emails and text messages before rent falls due and after it is missed.

All four read and write one database, and a single deployment pipeline ships them together. When a tenant pays an overdue balance, one database transaction can record the payment, clear the late fee and unlock the gate code. Either all three happen or none does.

“Monolith” describes how the software is packaged and deployed. Whether the code inside is tidy is a separate matter. A monolith can have clean, separate areas, as the storage product’s four do, or it can be tangled so that every part reaches into every other.

Can you give me an example of a monolithic application?

Most web applications built on one framework, such as Ruby on Rails, Django, Laravel or ASP.NET, start as monoliths, and many stay that way. A well-known large one is Shopify’s main application. In 2019 its engineering team described it as “one of the largest Ruby on Rails codebases in existence”, worked on for more than a decade by over a thousand developers. Rather than break it into services, the team chose to make it modular, a choice covered below.

What are microservices?

Lewis and Fowler define the microservice style as one application made of many small services, each in a process of its own, organized around a business capability and deployed independently by automated tooling. Microsoft’s Azure Architecture Center adds two points that do most of the work. Each service is small enough for one small team to write and maintain as its own codebase. And each service keeps its own data: there is no shared database, and other services reach that data only through the service’s API.

Built as microservices, the storage product becomes four services, one per area above. Billing has its own database of invoices and payments. Gate access has its own table of codes and their status. When rent is paid, billing records the payment and publishes a “payment received” event; gate access listens for that event and unlocks the code; reminders listens too and cancels the next overdue message. Each service can be released, scaled and restarted without the others.

Is Apache Kafka a microservice?

No. Kafka is infrastructure that services use to talk to each other. Its own documentation calls it an event streaming platform: programs called producers write events to it, others called consumers read them, and the events are kept in named topics. One of the uses it lists is serving as the foundation for microservices, and Microsoft names it, with Azure Service Bus, as the kind of messaging that lets services pass events without calling each other directly. In the storage example, the “payment received” event could travel through Kafka, through a managed queue, or through a simpler message broker. The broker carries messages between the services and is not one of them.

Monolithic vs microservices: the differences

The comparison below draws on Amazon Web Services’ own comparison of the two architectures and Microsoft’s architecture guide.

Monolith Microservices
What ships The whole application, every release One service at a time, each on its own schedule
Data Usually one database, shared by every part of the code Each service owns its data; others ask for it through an API
Calls between parts Function calls inside one process Requests or messages over the network, which are slower and can fail
Updating related records One database transaction Several steps in several services, reconciled afterwards
Scaling More copies of the whole application More copies of only the service under load
When one part fails An error in one part can take the whole application down The rest can keep working, if the services that depend on it are built to cope
Finding a bug Traced within one program and one log Traced across several services, which needs distributed tracing and joined-up logs
Technology One language and framework for everything Each service can use a different one
Cost Cheap to start; harder to change as it grows More to set up and run from the first day; AWS expects savings over the long term

The invented storage rental product drawn two ways. Top, a monolith: one deployable application holding four areas of code (units, billing, gate access, reminders) over one shared database, where the payment, the late fee and the gate code are saved in one transaction, all or nothing. Bottom, microservices: the same four areas as services, each deployed on its own and each owning its data; billing publishes a payment received event through a message broker, and gate access and reminders each act on it in their own time.

The row with the widest consequences is the one about related records. In the monolith, paying an overdue balance is one transaction. With microservices it becomes a sequence: billing saves the payment, the event is published, gate access unlocks the code a moment later. If gate access is down when the event arrives, the message has to wait and be retried, and the tenant may stand at a locked gate for a while with a paid receipt in hand. Microsoft’s guide is direct about this: when several services persist one change, it is unlikely to be a single ACID transaction, and it advises embracing eventual consistency where possible. Designing for that delay is real work, and it is work a monolith never asks for.

What are the drawbacks of microservices?

Microsoft’s guide lists the challenges plainly, and most of them follow from the network sitting between the parts:

  • More moving parts. Each service is simpler, but the system as a whole is more complex. Service discovery, data consistency and calls between services all have to be designed.
  • Harder testing. A service that depends on others needs those others, or stand-ins for them, before it can be tested properly, and refactoring across a service boundary is difficult.
  • Latency. A request that passes from service A to B to C adds network time at every step. Chatty APIs make it worse.
  • Versioning. A change to one service’s API must not break the services that call it, and several services may be changing at once.
  • Operations. The guide says success needs a mature DevOps culture, and that logging has to tie together every call made for a single user action.
  • Skills. These are distributed systems, and the guide tells teams to weigh carefully whether they have the skills and experience for them.

Fowler calls the sum of these costs the microservice premium: a premium on cost and risk that, in his words, often gets projects into serious trouble. His guideline, written in 2015: “don’t even consider microservices unless you have a system that’s too complex to manage as a monolith.”

For the storage product, the premium looks like this: four pipelines instead of one, a message broker to run and watch, a way to trace one payment through three services, and a plan for every event that arrives late, twice or out of order.

What are the drawbacks of a monolith?

The costs of a monolith arrive later, as it grows. Lewis and Fowler name two. Change cycles are tied together, so a small change to one part means rebuilding and redeploying the whole. And scaling means scaling all of it, even when the extra traffic reaches just one part. AWS adds that a small change can affect many functions at once, which is why releases of a large monolith tend to need wide retesting.

Shopify’s engineers described what their monolith felt like at size. A harmless-looking change could set off unrelated test failures. Their example: if the shipping rate code called into the tax code, a change to tax could alter shipping rates without the reason being obvious. Tests were slow to run, and a new developer had to understand many unrelated areas before shipping a first feature.

The cause of all this is parts of the code depending on each other without limits, which a single deployable unit allows but does not require. The modular monolith is built on that distinction.

The middle ground: a modular monolith

Shopify’s answer was to keep one application and enforce boundaries inside it. Their definition: “A modular monolith is a system where all of the code powers a single application and there are strictly enforced boundaries between different domains.” They reorganized the code around business areas, such as orders, shipping, inventory and billing, rather than around technical layers. Each component was to expose a public interface and own its data, and a tool run in their build pipeline reported calls that crossed a boundary without going through that interface.

For the storage product, a modular monolith means the four areas stay in one codebase and one deployment, but:

  • each area keeps its own tables, and no other area queries them directly;
  • areas call each other only through a small, documented set of functions;
  • a check in the build fails when code in one area imports the inside of another.

The product keeps its single transaction and its single pipeline. What it gains is a set of seams. If reminders one day needs to run on its own, the boundary already exists, and taking it out starts from there: a network call goes where a function call was, and the costs of the network come with it. Microsoft’s guidance points the same way from the other side: code that tends to change together belongs in the same deployable unit, because splitting it across services only couples those services.

The distributed monolith: the costs of both

The outcome to avoid is a system split into services that still behave like one program. People call it a distributed monolith. The signs are easy to check:

  • Two or more services have to be released together, because a change in one breaks the other.
  • Services share one database schema, so a column change in one service can break another.
  • One user action passes through a long chain of synchronous calls, and if any link fails, the action fails.
  • Nobody can say which service owns a given piece of data.

Microsoft’s guide names the causes: shared database schemas and rigid communication protocols couple services, and chatty traffic between two services can be a symptom of that coupling. Its list of antipatterns opens with drawing service lines before the business itself is well understood, because boundaries drawn in the wrong place produce exactly this. A distributed monolith pays the network costs of microservices and still loses the independent releases that justified them.

How to choose between monolith and microservices

The useful questions are about your teams, your load and your operations, more than about the technology.

  1. How many teams will work on it? If one team builds the product, it gains little from releasing parts separately, because the same people would coordinate every release anyway. Several teams that keep blocking each other’s releases is the strongest reason to split. This is Conway’s law at work: a system tends to mirror the structure of the organization that builds it, a point our post on building a development team takes further.
  2. Does one part have a very different load? If reminders sends thousands of messages on the first of each month while the rest of the product idles, that part may justify running separately. If load rises evenly across the product, more copies of the whole application usually do the job.
  3. Do you know where the boundaries are? Boundaries are easier to move inside one codebase than between services. A new product is still learning what its areas are, which is one reason AWS calls a monolith more suitable for a simple application or a prototype.
  4. Can you run it? Microservices assume automated deployment, monitoring that spots problems fast, and tracing across services. Our guide to microservices and containers lists the prerequisites and how containers fit in.
  5. What would one shared transaction save you? If the core of the product, like rent and gate access here, has to change together and stay consistent, keeping it in one process removes a whole class of problems.

For a new product with one team, the answers usually point to a modular monolith: one deployment, enforced boundaries, and the option to move a part out once a reason appears. A large system with several teams and parts under very different load is where microservices earn their premium.

Is Netflix monolithic or microservices?

Microservices, and it got there from a monolith. In Netflix’s own account of its cloud migration, published in February 2016, three of its vice presidents date the start to 2008, when a major database corruption left the company unable to ship DVDs for three days. Netflix chose to rebuild almost all of its technology for the cloud rather than move the old systems over as they were, and in the process it went “from a monolithic app to hundreds of micro-services.” The migration was finished in early January 2016, after what the authors call seven years of effort.

The story is often told as proof that microservices win. Read as Netflix tells it, it says something narrower. A very large company, with a reason to rebuild and years to do it, split its system into services. Shopify, also very large, chose to keep its monolith and make it modular. Both choices fit the organization that made them.

Moving from a monolith to microservices

When a monolith does need to be split, the safe way is piece by piece, with the old application still serving everything that has not moved yet. The best-known method is the Strangler Fig pattern. Fowler named it after the strangler figs he saw in the Queensland rain forest in 2001, which grow around a host tree until they replace it. Our guide to legacy system modernization places the pattern next to its alternatives; the parts that matter for a split into services are these.

Microsoft’s write-up of the Strangler Fig pattern puts a façade in front of the old application. At first it sends most requests to the monolith. As each piece is rebuilt as a service, the façade sends that piece’s requests to the new service instead, until the old code has nothing left to do and can be retired.

The data moves the same way, one area at a time. In Microsoft’s example the new service first uses the old shared database, then gets a database of its own, filled by a one-off copy and kept in step with the old one by change data capture until the two are checked against each other. Only then does the new database become the system of record, and only after that are the old tables removed. Until those tables are gone, going back is still possible.

For the storage product, reminders would be the natural first piece: it has the most uneven load, and it only needs to read billing’s data, not change it. Billing and gate access, which have to agree with each other on every payment, would move last, or not at all.

Microsoft also says when the pattern does not fit. For a small system that could just be replaced in one go, or when requests to the old system cannot be intercepted, it is the wrong tool.

Before choosing

The architecture is easier to get right when a few things are written down first:

  • Which areas does the product have, and which data does each one own?
  • How many teams will build it in the first year, and in the third?
  • Which parts must change together and stay consistent, like payments and access here?
  • Which part, if any, has a load unlike the rest?
  • Who will run deployments, monitoring and tracing, and have they run distributed systems before?

Whichever way you go, record the decision and the reasons for it, so the next team can see why the product is shaped the way it is. If you are planning a subscription product, our SaaS development page describes that service. Questions about your own product can go through the contact page, and the team answers within one business day.

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