Microservices patterns
Service boundaries, discovery, inter-service calls, and decomposition patterns.
CQRS with Separate Read and Write Repositories in Python
Implement CQRS in Python with separate write and read repositories, using commands for mutations and frozen DTOs for queries.
from dataclasses import dataclass
from typing import Dict, List, Optional
# --- Write side: commands mutate state ---
@dataclass
class CreateUserCommand:
id: int
name: str
class UserWriteRepository:
def __init__(self) -> None:
self._store: Dict[int, Dict[str, object]] = {}
def create(self,…
How to Use the Adapter Pattern to Mock a Legacy System in Python
This code demonstrates the Adapter pattern, allowing a modern interface to interact with a legacy system by wrapping its outdated method.
class LegacySystem:
def legacy_method(self, data):
return f"Legacy processed: {data}"
class ModernInterface:
def process(self, data):
raise NotImplementedError
class Adapter(ModernInterface):
def __init__(self, legacy):
self.legacy = legacy
def process(self, data):
re…
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.