Introduction to Microservices
Introduction
What Are Microservices?
Monolith vs Microservices
The Monolithic Approach
# Monolithic structure - everything in one codebase
# app/
# ├── models/
# │ ├── user.py
# │ ├── order.py
# │ └── payment.py
# ├── services/
# │ ├── user_service.py
# │ ├── order_service.py
# │ └── payment_service.py
# ├── api/
# │ └── routes.py
# └── main.py
# Everything shares the same database and process
from sqlalchemy.orm import Session
from app.models import User, Order, Payment
from app.database import get_db
class OrderService:
"""Order logic tightly coupled with user and payment."""
def __init__(self, db: Session):
self.db = db
def create_order(self, user_id: int, items: list[dict]) -> Order:
# Direct database access to user table
user = self.db.query(User).filter(User.id == user_id).first()
if not user:
raise ValueError("User not found")
# Create order in same transaction
order = Order(user_id=user_id, items=items)
self.db.add(order)
# Payment processing in same transaction
payment = Payment(order=order, amount=order.total)
self.db.add(payment)
self.db.commit()
return orderThe Microservices Approach
When to Use Microservices
Good Candidates
Factor
Indicator
Poor Candidates
The Modular Monolith Alternative
Conway's Law
Inverse Conway Maneuver
Team Ownership Model
The Distributed Systems Reality
Fallacies of Distributed Computing
CAP Theorem
Microservices Trade-offs Summary
Aspect
Monolith
Microservices
Practical Exercise
Exercise 1: Assess Your Application
Exercise 2: Identify Service Boundaries
Key Takeaways
What's Next?
Last updated