Skip to content

Instantly share code, notes, and snippets.

@mohashari
mohashari / snippet-2.sql
Created March 14, 2026 23:27
CQRS and Event Sourcing: A Practical Implementation Guide
-- event_store schema
CREATE TABLE events (
id BIGSERIAL PRIMARY KEY,
aggregate_id UUID NOT NULL,
aggregate_type TEXT NOT NULL,
event_type TEXT NOT NULL,
sequence_num INT NOT NULL,
payload JSONB NOT NULL,
occurred_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (aggregate_id, sequence_num)
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 23:28
Database Sharding Strategies: Horizontal Scaling Done Right
package sharding
import "fmt"
const shardCount = 4
const rangeSize = 1_000_000
// RangeSharder routes based on numeric ID ranges.
type RangeSharder struct {
shards []string // DSN per shard
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 23:29
Zero-Downtime Deployments: Blue-Green, Canary, and Rolling Strategies
func deepHealthHandler(db *sql.DB, cache *redis.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
type check struct {
name string
fn func() error
}
@mohashari
mohashari / snippet-2.hcl
Created March 14, 2026 23:30
HashiCorp Vault: Secrets Management for Production Systems
# policies/myapp.hcl
path "secret/data/myapp/*" {
capabilities = ["read", "list"]
}
path "auth/token/renew-self" {
capabilities = ["update"]
}
path "sys/leases/renew" {
@mohashari
mohashari / snippet-2.yaml
Created March 14, 2026 23:31
Service Mesh with Istio: Traffic Control, Security, and Observability
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-v1
namespace: production
spec:
replicas: 2
selector:
matchLabels:
app: backend
@mohashari
mohashari / snippet-2.py
Created March 14, 2026 23:32
Python Async Programming: asyncio Patterns for Backend Engineers
import asyncio
import httpx
async def fetch_user(client: httpx.AsyncClient, user_id: int) -> dict:
response = await client.get(f"https://api.internal/users/{user_id}")
response.raise_for_status()
return response.json()
async def fetch_permissions(client: httpx.AsyncClient, user_id: int) -> list:
response = await client.get(f"https://api.internal/permissions/{user_id}")
@mohashari
mohashari / snippet-2.sql
Created March 14, 2026 23:33
PostgreSQL Partitioning: Managing Billions of Rows Efficiently
CREATE TABLE events_2026_01 PARTITION OF events
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE events_2026_02 PARTITION OF events
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
CREATE TABLE events_2026_03 PARTITION OF events
FOR VALUES FROM ('2026-03-01') TO ('2026-04-01');
-- Each partition gets its own indexes. PostgreSQL 11+ propagates these
@mohashari
mohashari / snippet-2.sh
Created March 14, 2026 23:34
Chaos Engineering: Building Confidence Through Controlled Failure
#!/usr/bin/env bash
# inject-latency.sh — add 200ms ± 50ms jitter to egress on eth0
# Requires: iproute2, root or CAP_NET_ADMIN
IFACE="${1:-eth0}"
DELAY_MS="${2:-200}"
JITTER_MS="${3:-50}"
DURATION_S="${4:-60}"
echo "[chaos] Injecting ${DELAY_MS}ms ± ${JITTER_MS}ms on ${IFACE} for ${DURATION_S}s"
@mohashari
mohashari / snippet-2.yaml
Created March 14, 2026 23:35
SLOs, SLAs, and Error Budgets: The SRE Approach to Reliability
# prometheus/recording_rules.yml
groups:
- name: slo_availability
interval: 30s
rules:
- record: job:http_requests_total:success_rate5m
expr: |
sum(rate(http_requests_total{status!~"5.."}[5m])) by (job)
/
sum(rate(http_requests_total[5m])) by (job)
@mohashari
mohashari / snippet-2.sql
Created March 14, 2026 23:36
Saga Pattern: Managing Distributed Transactions Without Two-Phase Commit
CREATE TABLE saga_log (
id BIGSERIAL PRIMARY KEY,
saga_id UUID NOT NULL,
state TEXT NOT NULL,
payload JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_saga_log_saga_id ON saga_log(saga_id);