Reference library

System design patterns

Sharding, load balancing, CAP tradeoffs, and scaling patterns — interview and production ready.

5 matches
System design patterns easy

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.

service-discovery registry dict
Python
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…
13 0 Open
System design patterns medium

How to Implement the Flyweight Pattern in Python

Implements the Flyweight design pattern to share immutable intrinsic state (character + font) across many document objects, reducing memory usage.

flyweight design-patterns memory-optimization
Python
class Character:
    """Flyweight - stores only intrinsic state (shared)."""

    def __init__(self, char: str, font: str):
        self.char = char
        self.font = font

    def render(self, size: int) -> str:
        return f"{self.char}_{self.font}_{size}"


class CharacterFactory:
    """Flyweight factory - ma…
15 0 Open
System design patterns easy

How to Implement the Repository Pattern in Python with an In-Memory Dict

Stores, retrieves, updates, and deletes user records in memory using a Repository abstraction over a plain dict, isolating data access from business logic.

repository-pattern design-patterns in-memory
Python
class UserRepository:
    def __init__(self):
        self._storage = {}
        self._next_id = 1

    def create(self, name, email):
        user_id = self._next_id
        self._next_id += 1
        self._storage[user_id] = {"id": user_id, "name": name, "email": email}
        return self._storage[user_id]

    def…
11 0 Open
System design patterns easy

How to mock the domain center in an onion architecture in Python

Define a repository interface and an in-memory mock to test domain services without touching infrastructure.

onion-architecture repository-pattern dependency-injection
Python
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Dict, List, Optional


@dataclass
class Order:
    id: int
    customer: str
    items: List[str]
    total: float


class OrderRepository(ABC):
    @abstractmethod
    def find_by_id(self, order_id: int) -> Optional[Order]:
     …
11 0 Open
System design patterns medium

Inbox pattern consumer dedupe mock in Python

Implements a mock inbox consumer that deduplicates incoming messages by ID, with automatic eviction of old seen IDs to prevent unbounded memory growth.

deduplication inbox-pattern dataclasses
Python
import json
from collections import deque
from dataclasses import dataclass, field
from hashlib import sha256
from typing import Any


@dataclass
class InboxConsumer:
    max_seen: int = 1000
    seen_ids: set = field(default_factory=set)
    seen_history: deque = field(default_factory=deque)

    def _mark_seen(self,…
13 0 Open

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.