Skip to main content

🏘️ Microservices

A food court instead of one restaurant

The Restaurant Team Analogy

One-person restaurant (Monolith):

  • Cook takes orders, cooks, serves, cleans
  • Can usually do one main thing at a time
  • Cook gets sick = Restaurant closes

Specialized team (Microservices):

  • Host takes orders
  • Chef cooks
  • Server serves
  • Busser cleans
  • Each focuses on one job
  • One person sick? Others cover

Microservices split your app into specialized teams.


What Are Microservices?

Small, independent services that do ONE thing well.

Monolith:
  ┌────────────────────────────────────┐
  │           BIG APPLICATION          │
  │  Users + Orders + Payments + ...   │
  └────────────────────────────────────┘

Microservices:
  ┌──────────┐  ┌──────────┐  ┌──────────┐
  │  Users   │  │  Orders  │  │ Payments │
  │ Service  │  │ Service  │  │ Service  │
  └──────────┘  └──────────┘  └──────────┘

Each service:
  - Has its own codebase
  - Has its own database
  - Deploys independently
  - Communicates via API

Monolith vs Microservices

AspectMonolithMicroservices
DeploymentAll or nothingEach service separately
ScalingScale everythingScale what needs it
TechnologyOne stackDifferent per service
TeamOne big teamSmall, focused teams
FailureOne bug can crash allIsolated failures
ComplexityIn the codeIn the infrastructure

Key Principles

Single Responsibility

Each service does ONE thing.

User Service: Manage users
Order Service: Handle orders
Payment Service: Process payments

Not: User-Order-Payment-Everything Service

Database per Service

Each service owns its data.

User DB ─ User Service
Order DB ─ Order Service
Payment DB ─ Payment Service

Avoid sharing databases between services when possible.
Prevents tight coupling.

API First

Services communicate via API.

REST, gRPC, GraphQL, or messaging.

Clear contracts between services.

Independently Deployable

Deploy User Service without touching Order Service.

Changes are isolated.
Releases are safer.

Communication Patterns

Synchronous (REST/gRPC)

Order Service → User Service: "Get user 123"
User Service → Order Service: { user data }

Direct call. Wait for response.
Simple. But creates dependency.

Asynchronous (Messaging)

Order Service → Message Queue: "Order placed"
User Service ← (subscribed): React to order

No waiting. Decoupled.
More resilient. More complex.

Which to Use

Sync: Need immediate response
Async: Can process later, fire-and-forget

Often: Mix of both.

Benefits

Independent Deployment

Deploy 10× a day.
Often, you can deploy just the service that changed.
Fast, lower-risk releases.

Technology Freedom

User Service: Python
Order Service: Java
Analytics: Go

A good tool for each job.

Scaling Precision

Payment service under load?
Scale just the payment service.

Don't waste resources on others.

Fault Isolation

Payment service crashes.
Users can still browse, add to cart.
Graceful degradation.

Team Autonomy

User team owns User Service.
Order team owns Order Service.

Clear ownership. Faster decisions.

Challenges

Distributed Complexity

Network calls can fail.
Services can be slow.
Data consistency is hard.

More moving parts = More failure modes.

Data Consistency

Order in Order DB.
Inventory in Inventory DB.

How to keep in sync?
Eventual consistency, sagas, events.

Debugging

Request spans 5 services.
Where did it fail?

Need distributed tracing.

Operational Overhead

10 services to monitor.
10 deployments to manage.
10 databases to maintain.

Containerization and orchestration help.

When to Use

Good Fit

✓ Large teams (Amazon's "two-pizza rule")
✓ Complex domains
✓ Different scaling needs
✓ Need technology flexibility
✓ Frequent deployments

Start with Monolith

Small team? Start monolithic.
Easier to develop, deploy, debug.

Extract microservices when:
  - Team grows
  - Parts need different scaling
  - Deploy frequency differs
  - Clear domain boundaries emerge

"Modular monolith" → Microservices later.

Architecture Components

                ┌─────────────┐
                │   Clients   │
                └──────┬──────┘
                       │
                ┌──────▼──────┐
                │ API Gateway │
                └──────┬──────┘
                       │
         ┌─────────────┼─────────────┐
         │             │             │
    ┌────▼────┐  ┌─────▼────┐  ┌─────▼────┐
    │  User   │  │  Order   │  │ Payment  │
    │ Service │  │ Service  │  │ Service  │
    └────┬────┘  └────┬─────┘  └────┬─────┘
         │            │             │
    ┌────▼────┐  ┌────▼─────┐  ┌────▼─────┐
    │ User DB │  │ Order DB │  │Payment DB│
    └─────────┘  └──────────┘  └──────────┘

Essential Tools

Containers: Docker
Orchestration: Kubernetes
API Gateway: Kong, AWS API Gateway
Messaging: Kafka, RabbitMQ
Tracing: Jaeger, Zipkin
Monitoring: Prometheus, Grafana
Service Mesh: Istio, Linkerd

Common Mistakes

1. Too Many Services Too Fast

100 nano-services for 5 developers?
Operational nightmare.
Start with fewer, larger services.

2. Sharing Databases

Services sharing a database = tight coupling.
Change schema → Break multiple services.
Each service needs its own data store.

3. Synchronous Everything

Service A → B → C → D (all sync)
D slow = the whole request feels slow.
Use async where possible.

4. Ignoring Operations

Microservices need:
  - Containerization
  - Orchestration
  - Monitoring
  - Logging
  - Tracing

Build platform before services.

FAQ

Q: How small should a microservice be?

Small enough for one team to own. Big enough to be meaningful. "Two-pizza team" can handle it.

Q: How do microservices share data?

API calls or events. Avoid direct database access.

Q: What about transactions across services?

Saga pattern. Choreography or orchestration.

Q: Microservices for a startup?

Usually no. Start with monolith. Extract when needed.


Summary

Microservices split applications into small, independently deployable services that communicate via API.

Key Takeaways:

  • Each service does one thing well
  • Independent deployment and scaling
  • Database per service
  • Communicate via API or messaging
  • Operational complexity increases
  • Start monolith, extract when needed
  • Need containerization and orchestration

Microservices: Big systems from small, focused pieces!

Leave a Comment

Comments (0)

Be the first to comment on this concept.

Comments are approved automatically.