System design patterns
Sharding, load balancing, CAP tradeoffs, and scaling patterns — interview and production ready.
How to Build a Simple Service Discovery Registry in Python
A lightweight in-memory service registry class using a dict — register, deregister, and discover services with host, port, and version.
class ServiceRegistry:
def __init__(self):
self._services = {}
def register(self, name, host, port, version="1.0"):
self._services[name] = {
"host": host,
"port": port,
"version": version
}
def deregister(self, name):
return self._servic…
How to Mock the Ambassador Pattern Retry Client in Python
This code demonstrates the ambassador pattern for API clients by simulating a flaky request and retrying with exponential backoff, useful for testing resilience in system design.
import time
import random
class RetryingClient:
"""Retry wrapper simulating a flaky ambassador-style API client."""
def __init__(self, max_attempts=3, base_delay=0.1):
self.max_attempts = max_attempts
self.base_delay = base_delay
self.attempts = 0
def _flaky_request(self):
…
Browse by section
Each section groups closely related Python snippets.
System design patterns — Python code examples
What you will find here
This page collects system design 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.