# Microservices: Splitting One App Into Small, Independent Services

> Learn what microservices are, how they differ from one big app, why teams choose them, and what they cost in network calls and complexity.

**In short:** Microservices is a way to build one application as many small programs that work together, each with one business job.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/microservices

## What you will learn

This lesson explains microservices, a way to build one application as many small programs that work together. By the end you should be able to say what a microservice is, compare it with a monolith, list why teams adopt the style, name its main costs, and follow one worked example. A short quiz at the end checks your understanding.

- **Microservice:** A small program that does one business job, such as taking payments, and that can be changed and run on its own.
- **Monolith:** An application where all its features live in one codebase and ship together as one unit.
- **Codebase:** The full set of source code files that make up one program.
- **Deploy:** Put a new version of a program onto the servers where it runs, so that users get the change.
- **API:** Application Programming Interface. A set of rules that one program follows when it asks another program for data or an action, such as 'give me this restaurant's menu'.
- **Network call:** One program sending a request to another program across a network. It can be slow or fail.
- **Business capability:** Something a business does, such as taking orders, storing customer accounts, or tracking a delivery.
- **Scale:** Add capacity so an app can serve more users or handle more work.

## What is a microservice?

Microservices is an architecture, which means a plan for how the parts of an application are split up and how they connect. In this plan, the application is a group of small services. Each service is self-contained, does one business job, and can be built, tested, and run without waiting for the others. Microsoft's architecture guide describes these services as small, independent, and loosely coupled. Loosely coupled means that one service does not depend on the inner details of another. It only depends on the public rules, called the API, that the other service offers.

Each service usually has its own codebase. A team that owns the service can change its code, test it, and deploy it without touching the rest of the app. Many teams also give each service its own data store, which is the place where that service keeps its data, such as a database. When the orders service keeps its own orders table, the menus service cannot quietly change those rows. The only way to get order data is to ask the orders service.

Business capability is the key phrase. A good service boundary follows what the business does, not how the code is layered. A 'payments' service is a business capability. A 'database helper' folder is not.

> **Tip:** A quick test for a service boundary: can one small team own this part from design to running in production? If the answer is no, the piece may be too big or too tangled.

## What is the difference between a monolith and microservices?

A monolith packs every feature into one application that is built and shipped as a single unit. Atlassian describes a monolith as one unified unit, while a microservices system is a collection of smaller services that can each be deployed on their own. AWS makes a similar point: a monolith uses one code base for many business functions, while in microservices each service handles a single feature or business logic.

- **Deployment:** A monolith is released as one big package. A microservices system releases each service separately, so a small fix to one service does not need a full app release.
- **Scaling:** A monolith is usually scaled by copying the whole application, even when only one feature is busy. Microservices let a team add copies of just the service under load.
- **Data:** A monolith often shares one database among all features. In microservices, each service can own its data, which keeps changes in one place from breaking others.

The two styles solve different problems. A monolith is simpler to build, run, and debug when one small team is working on the whole app. Microservices add moving parts, but they let many teams work on the same product without stepping on each other.

### Not a yes or no choice

Many teams do not jump straight from one app to many services. This makes a later split easier. Guidance from Build Beyond Backend points out that a split should follow a real need, such as team growth or uneven load, not fashion.

> **Tip:** Ask what problem you are solving before you choose the style. If the answer is 'we want to be modern', a simpler app is usually the better start.

## Why do teams choose microservices?

Teams pick microservices for a few main reasons. Each one solves a pain that is common in a large, growing monolith.

- **Independent deployment:** A team can ship a fix or a new feature in its own service on its own schedule. Azure's guide notes that a bad update can also be rolled back without undoing the whole application.
- **Scale only the busy parts:** If one part of the app gets heavy traffic, only that part needs more servers. The rest of the app keeps its current size.
- **Fault isolation:** Fault isolation means a failure stays inside one part instead of spreading. If one service crashes, the others can keep running, as long as they are designed to handle that failure.
- **Team autonomy:** Small teams can each own one service. They choose their own tools and set their own release pace, which often makes them faster.

### Scaling only the busy parts

Consider an online shop. During a holiday sale, the checkout code gets many more visits than the page that lists products. In a monolith, the team must add capacity to the whole app. In microservices, the team can add copies of the checkout service and leave the rest alone. Capacity is added only where the load is.

## What are the trade-offs?

Microservices are not free. Each benefit above has a matching cost. The main costs are listed below, and they are the reason many teams wait before they split an app.

- **Network calls:** Inside a monolith, one function calls another in the same process, and that call almost never fails. Between services, every call crosses a network, so it can be slow, lost, or refused. Build Beyond Backend points out that every network call can fail, and that function calls inside one program do not time out in the same way.
- **Added complexity:** The system has more moving parts. Each service may be simple, but the whole system is harder to understand. Azure's guide lists service discovery, data consistency, transaction handling, and communication between services as design challenges.
- **Harder debugging:** When something goes wrong, a developer may need to follow one user request through several services and their logs. A monolith keeps that trail inside one program, so it is easier to follow.
- **Data consistency:** When each service keeps its own data, a single business action that changes several services can leave them out of step if one step fails. Teams must plan how to handle that case.

### Why does debugging get harder?

Suppose a customer reports that an order was charged but never shown. The bug may be in the orders service, the payments service, or the link between them. The team must look at logs from each service and match them by request ID. This takes extra tools for tracing and monitoring, which is a cost in time and money.

> **Tip:** Plan for failure from day one. Set time limits on each network call, decide what to show when another service does not answer, and keep logs that carry one shared request ID.

## How services work together

Services need a way to talk. The two most common ways are APIs and messages. An API is a direct request and reply, like a question to another service that waits for an answer. A message is a note sent to a queue or topic, which the other service reads later. Here is a simple path a user request might follow.

1. **The client sends a request:** A phone app or website sends one request to the entry point of the system, not to each service on its own.
2. **The entry point sends it on:** A gateway or router looks at the request and forwards it to the service that owns that job.
3. **Services call each other through APIs:** If the job needs data from another service, the first service makes an API call and waits for the reply, or it gives up after a set time.
4. **Services send messages for later work:** Some jobs, such as sending a receipt email, can be queued as a message so the user does not wait for them.
5. **Services find each other:** Each service needs to know where the others are running. The gateway and the lookup of service locations are big topics, so they get their own lessons later in this course.

### Routing and finding services

For that reason, systems add a front door that routes requests, and they add a way for services to locate one another. You only need to know that these pieces exist now. Service discovery, load balancers, and reverse proxies are covered in later lessons.

## Worked example: a food-delivery app

Take an online food-delivery app, similar to Uber Eats. This example is an illustration of the idea, not a description of how any real company builds its app. The team splits the app into separate services for user accounts, restaurant menus, orders, payments, and delivery tracking. A reviews service sits beside them. Here is how the split plays out.

1. **Split by business job:** Accounts handles sign-up and login. Menus holds each restaurant's dishes and prices. Orders creates and tracks a customer's order. Payments charges the card. Delivery tracking shows where the driver is. Reviews stores star ratings and comments.
2. **Give each service its own data:** Menus keeps its own dish list. Orders keeps its own order records. If Orders needs a price, it asks Menus through an API rather than reading Menus' database directly.
3. **Connect the services:** When a customer taps Order, the Orders service asks Menus for the current price, asks Payments to take the money, and then sends the order status to Delivery tracking.
4. **Handle a surge in orders:** At dinner time, order traffic jumps. The team adds more copies of the Orders service only. Accounts, Menus, and Reviews keep their current size, so the cost of the surge stays small.
5. **Survive a crash:** Suppose the Reviews service crashes. The app can hide the rating section, and customers can still place orders. The rest of the app stays up because the failure stayed inside one service.
6. **Ship a small change:** The delivery team changes how the driver's location is shown. It deploys only the Delivery tracking service. No other team has to rebuild or redeploy the whole app.

The crash example only works if the calling service is written to handle failure. The sketch below shows one service that keeps working when another is down. It is a simplified model, not production code.

```python
class MenuService:
    def __init__(self, up):
        self.up = up

    def get_price(self, item):
        if not self.up:
            raise ConnectionError('menu service is down')
        prices = {'noodles': 12, 'salad': 8}
        return prices[item]


class OrderService:
    def __init__(self, menu):
        self.menu = menu

    def place_order(self, item):
        try:
            price = self.menu.get_price(item)
        except ConnectionError as error:
            return 'order held: ' + str(error)
        return 'order placed for ' + item + ' at ' + str(price)


menu = MenuService(up=False)
orders = OrderService(menu)
print(orders.place_order('noodles'))
menu.up = True
print(orders.place_order('noodles'))

```

Output:

```text
order held: menu service is down
order placed for noodles at 12

```

The first order is held because the menu service is down, and the order service does not crash. Once the menu service is back, the same order goes through. The order service handled the failure itself, which is the habit microservices need.

## Check your understanding

Try these two short scenarios. Each one needs the ideas above, not just a memory of the words.

**Quick check:** A shop runs catalog, cart, and checkout in one codebase. During a sale only checkout is overloaded. Each small change means scaling and redeploying the whole shop. The team splits the shop into three services. What is the main gain for the sale, and what new problem must the team handle?

A. Checkout can scale on its own, but calls between the services can fail or slow down, so the team must plan for that.
B. Every service must share one database, so nothing can fail, and that shared database is the main gain.
C. The team no longer needs tests, because each service is small enough to skip them.
D. The catalog becomes faster because the cart is removed from the system.

**Answer:** A. Splitting lets the team add copies of checkout alone and ship small changes separately. The new problem is that services now talk across a network, which can fail. Sharing one database removes the independence that microservices are meant to give.

**Quick check:** A four-person team is building a new recipe app with a few features. They want the benefits of microservices, but their app is small and their team is small. What is the wisest first step?

A. Split the app into ten services on the first day, so the design is future-proof.
B. Build one application with clear internal modules, and split out a service later only if a real need appears.
C. Give every function its own server and database from the start.
D. Remove all APIs so the parts can share memory directly.

**Answer:** B. A small team with a small app gains little from many network-connected services, and it pays the costs of complexity and debugging. Clear modules keep the option open. A later split can follow a real need, such as team growth or uneven load.

## Key takeaways

Microservices split an application into small services, each with one business job, its own codebase, and ideally its own data. They can be deployed and scaled separately, which helps large teams and uneven loads. The price is network calls that can fail, more moving parts, harder debugging, and harder data consistency. Services talk through APIs or messages and rely on a front door and a way to find one another. The next lessons in this course explain those pieces in more detail.

## Sources

- [Microservices architecture style, Azure Architecture Center (Microsoft Learn)](https://learn.microsoft.com/en-us/azure/architecture/guide/architecture-styles/microservices)
- [Microservices vs. monolithic architecture, Atlassian](https://www.atlassian.com/microservices/microservices-architecture/microservices-vs-monolith)
- [Monolithic vs Microservices Architecture, AWS](https://aws.amazon.com/compare/the-difference-between-monolithic-and-microservices-architecture/)
- [Advantages and Disadvantages of Microservices Architecture, GeeksforGeeks](https://www.geeksforgeeks.org/system-design/what-are-the-advantages-and-disadvantages-of-microservices-architecture/)
- [Monolithic vs Microservices: What's the Difference?, Kong](https://konghq.com/blog/learning-center/monolith-vs-microservices)
- [Microservices vs Monolith: Which Architecture Should You Build?, Build Beyond Backend](https://buildbeyondbackend.com/blog/microservices-vs-monolith/)
- [Microservices Architecture Explained, DEV Community](https://dev.to/mattylantz/microservices-architecture-explained-benefits-challenges-scalability-best-practices-1gmm)
- [What Is a Microservice? Architecture, Benefits, and Trade-offs, API7.ai](https://api7.ai/blog/what-are-microservices)
