Production deployment patterns
Graceful shutdown, prod config, rollouts, readiness probes, and ship-with-confidence checks.
How to Mock Canary Deployment Traffic Split in Python
Simulate a canary deployment's stable/canary traffic split using deterministic request hashing to mock rollout behavior with precise percentage control.
class CanaryDeployment:
def __init__(self, stable_weight: float = 0.9, canary_weight: float = 0.1):
self.stable_weight = stable_weight
self.canary_weight = canary_weight
self.total_weight = stable_weight + canary_weight
def route_request(self, request_id: int) -> str:
"""Route …
How to Mock Image Signing Cost in Python
Create a deterministic mock signing cost calculator that predicts resource usage for image signatures before real signing infrastructure is staged.
import math
import struct
def sign_image_cost(image_signature: bytes) -> int:
"""Deterministic mock signing cost based on image signature bytes."""
if not image_signature:
raise ValueError("Empty image signature")
digest = 0
for byte in image_signature:
digest = (digest * 31 + byte) &…
How to Mock a Feature Flag Rollout Percentage in Python
Simulate a percentage-based feature flag rollout by hashing a user ID to deterministically enable features for a subset of users.
import random
from dataclasses import dataclass
@dataclass
class FeatureFlag:
name: str
rollout_percentage: int
def is_feature_enabled(feature_flag: FeatureFlag, user_id: str) -> bool:
hashed_id = hash(user_id) % 100
return hashed_id < feature_flag.rollout_percentage
if __name__ == "__main__":
…
How to Mock a Slow Startup Probe for Fast Testing in Python
This code shows how to replace a slow startup probe's initialization with a mock to make tests run fast and reliably.
import time
from unittest.mock import Mock, patch
class StartupProbe:
def __init__(self, init_time):
self.init_time = init_time
self.ready = False
def initialize(self):
time.sleep(self.init_time)
self.ready = True
return self.ready
def run_startup_probe(probe):
…
How to build a maintenance mode page in Python
Mock a service maintenance status page that computes remaining downtime and lists affected features from a simple class.
from datetime import datetime
class MaintenanceMode:
"""Mock a maintenance mode status page for a service."""
def __init__(self, service_name: str, scheduled_end: str):
self.service_name = service_name
self.scheduled_end = datetime.fromisoformat(scheduled_end)
self.affected_featur…
How to simulate a database migration init container mock in Python
A mock init container that runs environment checks and a staged database migration job before the main application starts, printing progress to stdout.
```python
class MigrationJob:
def __init__(self, name, steps):
self.name = name
self.steps = steps
self.current_step = 0
self.status = "pending"
def run(self):
print(f"Initializing migration job: {self.name}")
for step in self.steps:
self.current_ste…
Browse by section
Each section groups closely related Python snippets.
Production deployment patterns — Python code examples
What you will find here
This page collects production deployment 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.