Microservices are often treated as the default architecture for modern web applications. However, splitting a application too early introduces massive operational overhead: distributed tracing, network latency, complex deployments, and data consistency headaches.
Vague justifications like "this codebase feels messy" or "we might need to scale later" are expensive excuses. Most teams that regret adopting microservices split too early, paying for infrastructure and on-call complexity long before receiving any structural benefit.
Here is a practical framework to evaluate your architecture and make the split decision systematically.
The 4-Question Diagnostic Filter
Before refactoring your infrastructure, evaluate your current architecture against four technical constraints:
- Deployment Contention: Are multiple engineering teams blocking each other from releasing code because of a shared release pipeline?
- Differential Scaling: Does one specific feature (like billing or image processing) require 10x to 100x the resources compared to the rest of the application?
- Data Isolation: Can you clearly draw a hard boundary around the data model without needing multi-table joins across domains?
- Failure Isolation: Do you need independent resilience so a crash in an auxiliary feature (like notifications) cannot crash critical business flows?
If you answered "yes" to fewer than three of these questions, microservices will likely harm your developer velocity. If you answered "yes" to three or more, you are ready to evaluate a structural change.
Step 1: Build a Modular Monolith First
Before distributing services over a network, enforce domain boundaries inside your single repository. The biggest mistake developers make is creating a "distributed monolith"—microservices that still share databases and tightly coupled function calls.
Enforce strict boundary contracts within your codebase using domain interfaces.
# domain/billing/service.py from abc import ABC, abstractmethod from dataclasses import dataclass @dataclass class Invoice: id: str user_id: str amount_cents: int class BillingService(ABC): @abstractmethod def create_invoice(self, user_id: str, amount_cents: int) -> Invoice: pass # In a modular monolith, core services call this local implementation directly. # No foreign keys or direct SQL queries from other modules allowed. class LocalBillingService(BillingService): def create_invoice(self, user_id: str, amount_cents: int) -> Invoice: # DB operations localized strictly to the billing domain tables return Invoice(id="inv_123", user_id=user_id, amount_cents=amount_cents) By enforcing module encapsulation at the code level, you verify that your data model is actually decoupled before introducing network boundaries.
Step 2: Extract One Service at a Time
If your diagnostic scores justify a microservice split, do not rewrite the entire system. Use the Strangler Fig pattern to extract a single domain.
Because you previously implemented interface boundaries, swapping a local call for a remote call requires minimal code modifications in your core application.
# domain/billing/remote_client.py import requests from domain.billing.service import BillingService, Invoice class RemoteBillingServiceClient(BillingService): def __init__(self, service_url: str, api_key: str): self.service_url = service_url self.headers = {"Authorization": f"Bearer {api_key}"} def create_invoice(self, user_id: str, amount_cents: int) -> Invoice: response = requests.post( f"{self.service_url}/v1/invoices", json={"user_id": user_id, "amount_cents": amount_cents}, headers=self.headers, timeout=3.0 ) response.raise_for_status() data = response.json() return Invoice( id=data["id"], user_id=data["user_id"], amount_cents=data["amount_cents"] ) Extracting one service at a time allows your team to establish observability, CI/CD pipelines, and alerting patterns for distributed systems without disrupting the primary application.
Summary
Keep your architecture as simple as possible for as long as possible:
- 0-2 Yes Answers: Keep a standard monolithic architecture. Clean up your code structure.
- 2-3 Yes Answers: Refactor into a modular monolith with strict domain boundaries and isolated database schemas.
- 4 Yes Answers: Safely extract one independent microservice, run it in production for a quarter, and evaluate the operational cost before extracting the next.
About me
Tanmay builds AI agents and automation for businesses and is open to freelance projects. Connect with him on LinkedIn.
