Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Getting Started

What Are Microservices?

Learn what a microservice is, how it differs from a monolith, why independent deployment and ownership matter, and try a tiny service registry in your browser.

A microservice architecture builds one application out of many small programs that run separately and talk to each other over a network. Each program, called a service, is responsible for one slice of the business: orders, payments, stock, notifications. Together they behave like a single product, but each can be written, deployed and scaled on its own.

The alternative is the monolith: one program, one codebase and usually one database, deployed as a single unit. Monoliths are not a mistake. Most successful products began as one. This tutorial explains what changes when you split a system apart, what you gain, and what you pay. This first lesson gives the vocabulary and a small runnable model; the next one, When (and When Not) to Use Microservices, tells you whether you should.

A monolith is one unit

One codebase Every feature lives together.
One deploy Change a line, ship everything.
One database All modules share the same tables.

Simple to build, hard to change in pieces.

1 / 2

What makes a service "micro"

There is no official size. A service is not micro because it has fewer than 500 lines. These properties matter far more than size.

  • Single business purpose. The service answers one question well, such as "what is in the cart?" or "has this payment been taken?".
  • Independently deployable. You can release a new version without releasing any other service. If two services must always ship together, they are really one service.
  • Owns its data. The service keeps its own database or schema and nobody else touches it directly. Others must ask through its API. See Database per Service and Data Ownership.
  • Communicates through contracts. Other services depend only on a published API or message format, never on internal classes.
  • Owned by one team. A team can understand the whole service, run it in production and decide how to build it.

A good test: can one team change, test and release this service on a Tuesday afternoon without asking anyone else? If the answer is "no, we need three other teams", you have a distributed monolith, which is the worst of both worlds. See When (and When Not) to Use Microservices.

Monolith versus microservices

Picture an online shop. In a monolith, the "place order" function calls the stock module, the payment module and the email module as ordinary function calls, inside one process and usually inside one database transaction.

flowchart LR
    subgraph M["Monolith (one process)"]
        direction TB
        O1["Orders module"] --> S1["Stock module"]
        O1 --> P1["Payment module"]
        O1 --> N1["Notification module"]
    end
    M --> DB1[("One database")]

All modules share one process and one database, so calls are cheap and transactions are easy.

With microservices the same functions become network requests or messages between separate processes, each with its own store.

flowchart LR
    G["API gateway"] --> O2["Orders service"]
    O2 -->|"HTTP"| S2["Stock service"]
    O2 -->|"HTTP"| P2["Payment service"]
    O2 -->|"event"| N2["Notification service"]
    O2 --- D1[("Orders DB")]
    S2 --- D2[("Stock DB")]
    P2 --- D3[("Payments DB")]

Each service has a private database. Calls cross the network and can fail.

The two designs make different things easy.

Concern Monolith Microservices
Deploying One artifact, one pipeline Many artifacts, many pipelines
Scaling Scale everything together Scale only the busy service
Failure A bug can take down everything A failure can stay contained, if you design for it
Data consistency One ACID transaction Eventual consistency, sagas (Sagas)
Technology One stack Each service can choose, within reason
Debugging One stack trace Requests span many processes (Distributed Tracing)
Operations One thing to monitor Dozens of things to monitor

A modular monolith is not a "bad" monolith

A monolith can be organized into clearly separated modules with strict rules about who may call whom. That is a modular monolith, and it is often the best place to start, because you can later pull a module out into a service if you ever need to. Microservices are best understood as a deployment choice layered on top of good modular design. Splitting a tangled codebase into services does not untangle it; it only adds a network between the tangles.

A tiny service registry

Services need a way to find each other. In production that is DNS, a service mesh or a registry such as Consul (see Service Discovery and Load Balancing). The stand-in below keeps the idea in a few lines: services register themselves, and a caller looks up an address by name. Nothing here touches a network; the "services" are plain functions held in a map, and a call is an ordinary function call.

JS A service registry in memory

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The third call asks for a service that nobody registered and gets a 503 back. In a monolith that mistake would be a compile error. In a system of services it is a runtime condition you must handle, and that single difference explains a large part of this tutorial.

Services that own their data

The registry above hides the second big idea: ownership. In the next stand-in, an orders service needs the price of an item. It does not read the pricing table. It asks the pricing service and keeps a snapshot of what it was told.

JS Asking instead of reading

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the order stores unitCents at the moment of purchase. If the price changes tomorrow, old orders must not change. Copying a little data, rather than sharing a table, is a normal and deliberate trade in this architecture.

What is real and what is simulated

Be clear about the limits of what you just ran.

  • Real: the ideas. Registration, lookup by name, a missing service producing an error, and a service keeping private state behind a small API all work the same way in production.
  • Simulated: everything physical. These "services" share one JavaScript process, one memory space and one clock. A call cannot be lost, slow, duplicated or reordered. Real services run in separate processes on separate machines, and every one of those things can happen. The Fallacies of Distributed Computing explores them.
  • Not shown at all: containers, orchestration, gateways, brokers, tracing and deployment pipelines. They arrive in later chapters.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which property best defines a microservice?

    Size and language are incidental. Independent deployment and private data ownership are what give microservices their benefits.
  2. 2. Two services must always be released together, or one of them breaks. What does this suggest?

    If services cannot be deployed independently, you pay for a network between them but get none of the freedom.
  3. 3. What is a modular monolith?

    It keeps one deployment but enforces clear internal boundaries, which makes a later split into services much easier.
  4. 4. In the registry stand-in, a call to an unregistered service returns 503. Why does this matter more with real services?

    In a monolith a missing module is caught at build time. Across a network, services can be down, slow or not yet started, so callers must expect that.

Summary

  • A microservice architecture is many small, separately deployed services, each owning one business area and its data.
  • The defining traits are independent deployment, private data and communication through contracts, not size.
  • Microservices trade simple calls and transactions for flexibility in deploying and scaling, and bring the problems of distributed systems.
  • A modular monolith is a strong starting point and often the right one.
  • The stand-ins here run in one process, so they cannot show latency, loss or partial failure; later lessons add those.

Next: When (and When Not) to Use Microservices.