← All posts

Monolith vs Microservices: Decide with the Scalability Wall Test

Discover when a monolith turns into a bottleneck and how the Scalability Wall Test guides you to split into microservices—plus a Python demo.

A single application that grows out of control can feel like a single, clogged pipe: every new feature adds pressure until the flow stalls.
When the CPU of your monolith hits a hard ceiling under realistic traffic, that is the moment to consider a micro‑service split.
In this article we’ll walk through that decision with a simple test, show when it’s still worth staying monolithic, and provide a concrete Python example that moves a single function into its own service without breaking the original API.


TL;DR

  • Monolith: one codebase, one deployment, one database.
  • Microservice: small, independently deployable unit focused on a single business capability.
  • Scalability Wall Test: run realistic load; if CPU climbs to 100 % and latency spikes, you’ve hit the wall.
  • When to stay monolith: small teams, simple logic, fast shipping.
  • When to split: components need independent scaling, teams own domains, you want separate deployment cycles.
  • Common pitfalls: over‑splitting, data consistency, added operational complexity.

1. What is a Monolith and What is a Microservice?

Term Definition Typical Characteristics
Monolith A single codebase that contains all application logic, usually deployed as one unit. One repository, one build pipeline, one runtime, one database schema.
Microservice A small, independently deployable service that focuses on one business capability. Separate repository (often), its own runtime, its own database or data partition.

Both can use the same programming language; the difference lies in how they are packaged, scaled, and maintained. In a monolith, all code runs in the same process. In microservices, each service runs in its own process (often on its own container or VM) and communicates over a network protocol such as HTTP/REST or gRPC.


2. The Scalability Wall Test

  1. Deploy your monolith (any language, any stack).

  2. Generate realistic traffic – use a tool like wrk, ab, or a cloud load‑testing service that mimics real user behavior.

  3. Monitor key metrics:

    • CPU usage per process
    • Memory consumption
    • Average response time / latency
    • Error rates (timeouts, 5xx)
  4. Identify the wall:

    • CPU climbs steadily and eventually saturates at 100 %.
    • Latency starts to rise sharply after the CPU ceiling is hit.
    • The system no longer accepts new requests or returns timeouts.

When you observe this pattern, your monolith has reached its scalability wall. The analogy: a single water pipe can only carry so much flow before the pressure builds; microservices are separate pipes that can be opened independently to relieve the pressure.


3. When to Stay Monolith

Scenario Why it works
Small or medium teams Fewer people means less coordination overhead; shipping fast is easier with one deployment.
Simple business logic No need for independent scaling; a single process can handle all traffic.
Fewer operational concerns One deployment, one database schema, one monitoring stack reduce complexity.

Real‑world example: A startup building a minimal viable product (MVP) for a note‑taking app can keep everything in a single Flask app. The team can iterate quickly without worrying about service contracts or inter‑service communication.


4. When Microservices Make Sense

Scenario What changes
Different scaling needs One part (e.g., analytics) receives heavy traffic, another (e.g., user profile) is lightweight.
Large teams Teams can own distinct domains (orders, payments, inventory) and work independently.
Independent deployment cycles Fix a bug in the payment service without redeploying the whole app.

Real‑world example: An e‑commerce platform might split its checkout logic into a payment microservice. High‑volume order traffic can scale the payment service separately from the catalog service, which rarely needs extra compute.


5. Making the Split Decision

  1. Trigger with the scalability wall test – if you hit the wall, you have a concrete reason to split.
  2. Identify domain boundaries – look at the code that is hot (CPU‑intensive) and the business logic it implements.
  3. Create a minimal split – extract the hottest function into its own service while keeping the original endpoint unchanged.
  4. Iterate – keep the monolith for the rest of the code; the new service runs alongside it. Test end‑to‑end behavior before fully decommissioning the monolith portion.

6. Code Example: Moving a Function into a Microservice

Below is a minimal Flask application that exposes a /process endpoint. The endpoint performs a CPU‑intensive calculation (heavy_computation). We then split that calculation into a separate microservice (compute-service) and keep the original interface unchanged.

6.1 Monolith Version

# monolith_app.py
from flask import Flask, request, jsonify
import time

app = Flask(__name__)

def heavy_computation(data):
    """
    Simulate a CPU‑intensive task.
    """
    # Dummy workload: sum numbers up to a large limit
    total = 0
    for i in range(10_000_000):
        total += i
    return total

@app.route('/process', methods=['POST'])
def process():
    """
    Accept JSON payload, run heavy computation, and return the result.
    """
    payload = request.get_json()
    result = heavy_computation(payload.get('value', 0))
    return jsonify({'result': result})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Explanation:

  • The heavy_computation function is the CPU bottleneck.
  • The /process endpoint calls it directly, so the entire request lives in one process.

6.2 Microservice Version – Compute Service

# compute_service.py
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/compute', methods=['POST'])
def compute():
    """
    Perform the heavy computation and return the result.
    """
    payload = request.get_json()
    value = payload.get('value', 0)

    # Simulate heavy work
    total = 0
    for i in range(10_000_000):
        total += i

    return jsonify({'result': total})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=6000)

Explanation:

  • This service exposes a /compute endpoint that does the same work as heavy_computation.
  • It runs on its own port (6000) and can be scaled independently.

6.3 Updated Monolith Endpoint (Proxying)

# monolith_app.py (updated)
from flask import Flask, request, jsonify
import requests

app = Flask(__name__)

COMPUTE_SERVICE_URL = 'http://compute-service:6000/compute'

@app.route('/process', methods=['POST'])
def process():
    """
    Forward the payload to the compute service and return its response.
    """
    payload = request.get_json()
    try:
        resp = requests.post(COMPUTE_SERVICE_URL, json=payload, timeout=5)
        resp.raise_for_status()
    except requests.RequestException as e:
        return jsonify({'error': str(e)}), 502

    return jsonify(resp.json())

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Explanation:

  • The monolith’s /process endpoint now acts as a thin proxy.
  • It forwards the request to the compute service, waits for the response, and returns it to the client.
  • The client’s interface (/process) remains unchanged.

6.4 Running the Split

  1. Build two Docker images or run two separate Python processes.
  2. Use a reverse proxy or Docker Compose to expose compute-service to the monolith.
  3. Test the original /process endpoint; it should behave identically but now offloads CPU work to a separate process.

7. Common Mistakes and Trade‑offs

Mistake Impact Mitigation
Over‑splitting Every feature becomes a service → network latency, higher failure surface. Split only the truly hot or independent parts; keep the monolith for unrelated logic.
Data consistency Shared data across services can lead to complex transaction handling or eventual consistency bugs. Use domain‑owned databases or event‑driven patterns; avoid cross‑service transactions.
Deployment complexity More CI/CD pipelines, more containers to monitor, more versioning. Adopt a single pipeline that builds all services; use a service mesh for observability.
Team coordination Communication overhead grows; teams may clash over API contracts. Define clear contracts, use OpenAPI specs, and enforce versioning.

Key takeaways

  • A monolith is a single codebase and deployment; a microservice is an independently deployable unit focused on one capability.
  • The Scalability Wall Test is a practical trigger: CPU saturates, latency spikes → consider splitting.
  • Stay monolithic for small teams, simple logic, and fast iteration; split when components need independent scaling, teams own domains, or you need separate deployment cycles.
  • Start with a minimal split: move the hottest function into its own service and keep the original API unchanged.
  • Avoid over‑splitting, watch data consistency, manage deployment complexity, and keep team communication clear.

FAQ

What is the Scalability Wall Test?

The Scalability Wall Test deploys the monolith, generates realistic traffic, and monitors CPU, memory, latency, and error rates to detect when CPU reaches 100 % and latency spikes.

When should I stay monolithic?

If your team is small to medium, the business logic is simple, and you value rapid shipping with minimal operational overhead, staying monolithic is advantageous.

What triggers a microservice split?

A 100 % CPU ceiling combined with rising latency during load testing signals that the monolith has hit its scalability wall, making a microservice split justified.

How do I split a CPU‑intensive function into a microservice in Python?

Extract the function into a separate Flask app, expose it via a REST endpoint, and have the monolith call this new service while keeping the original /process endpoint unchanged.