Reference library

Microservices patterns

Service boundaries, discovery, inter-service calls, and decomposition patterns.

5 matches
Microservices patterns easy

Correlation ID HTTP header mock in Python

A lightweight HTTP server that echoes or generates correlation IDs to help test distributed systems.

correlation-id http-server mock
Python
import json
import uuid
from http.server import BaseHTTPRequestHandler, HTTPServer


class CorrelationHandler(BaseHTTPRequestHandler):
    CORRELATION_HEADER = "X-Correlation-ID"

    def do_GET(self):
        correlation_id = self.headers.get(self.CORRELATION_HEADER) or str(uuid.uuid4())
        response = {
        …
13 0 Open
Microservices patterns easy

How to Mock Service Versioning URI in Python

Run a minimal HTTP server in Python that routes requests to different versions of a service URI like /v1/users vs /v2/users.

http-server versioning mock
Python
from http.server import HTTPServer, BaseHTTPRequestHandler
import json


class VersionedHandler(BaseHTTPRequestHandler):
    def _send_json(self, payload, status=200):
        body = json.dumps(payload).encode("utf-8")
        self.send_response(status)
        self.send_header("Content-Type", "application/json")
    …
11 0 Open
Microservices patterns easy

How to Mock an API Gateway Router in Python

Create a lightweight HTTP server that routes requests to mock microservice responses, simulating an API gateway for local development and testing.

api-gateway mock-server http
Python
from http.server import HTTPServer, BaseHTTPRequestHandler
import json


class SimpleGateway(BaseHTTPRequestHandler):
    def do_GET(self):
        routes = {
            "/users": {"service": "user-service", "status": "ok", "count": 42},
            "/orders": {"service": "order-service", "status": "ok", "count": 17}…
14 0 Open
Microservices patterns easy

How to Mock an Ambassador Edge Proxy in Python

Build a lightweight mock Ambassador edge proxy with Python's http.server that responds to health and user endpoint requests for local development and testing.

ambassador mock http-server
Python
import http.server
import json
import urllib.parse
import threading

class AmbassadorProxyHandler(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        parsed = urllib.parse.urlparse(self.path)
        if parsed.path == "/health":
            self.send_response(200)
            self.send_header("Content-T…
13 0 Open
Microservices patterns easy

Retry idempotent GET requests in Python

A Python function that retries an idempotent GET request a fixed number of times with a delay between attempts, raising a RuntimeError only after all retries fail.

retry idempotent urllib
Python
import time
import urllib.error
import urllib.request
from http.client import HTTPException

def fetch_with_retry(url, max_retries=3, delay=1.0):
    for attempt in range(1, max_retries + 1):
        try:
            with urllib.request.urlopen(url, timeout=5) as response:
                return response.read().decode…
14 0 Open

Browse by section

Each section groups closely related Python snippets.

Microservices patterns — Python code examples

What you will find here

This page collects microservices patterns snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.