Skip to content

Instantly share code, notes, and snippets.

@mohashari
mohashari / snippet-2.sh
Created March 14, 2026 23:37
eBPF for Backend Engineers: Deep Observability Without Instrumentation
#!/bin/bash
# Capture off-CPU stack traces for 30 seconds, then generate flame graph
# Requires: bpftrace, stackcollapse-bpftrace.pl, flamegraph.pl
bpftrace -e '
tracepoint:sched:sched_switch {
if (args->prev_state == TASK_INTERRUPTIBLE ||
args->prev_state == TASK_UNINTERRUPTIBLE) {
@offcpu[args->prev_pid, ustack] = nsecs;
}
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 23:38
ElasticSearch in Production: Indexing, Querying, and Tuning
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/elastic/go-elasticsearch/v8"
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 23:39
Feature Flags: Safe Deployments and Experimentation at Scale
package flags
import (
"crypto/sha256"
"encoding/binary"
"strings"
)
type Context struct {
UserID string
@mohashari
mohashari / snippet-2.dockerfile
Created March 14, 2026 23:40
Database Connection Pooling: PgBouncer and Beyond
FROM alpine:3.19
RUN apk add --no-cache pgbouncer
COPY pgbouncer.ini /etc/pgbouncer/pgbouncer.ini
COPY userlist.txt /etc/pgbouncer/userlist.txt
RUN chown -R nobody:nobody /etc/pgbouncer
USER nobody
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 23:41
Building Resilient Systems with the Circuit Breaker Pattern
func (cb *CircuitBreaker) Execute(fn func() error) error {
cb.mu.Lock()
state := cb.currentState()
if state == StateOpen {
cb.mu.Unlock()
return ErrCircuitOpen
}
cb.mu.Unlock()
err := fn()
@mohashari
mohashari / snippet-2.sh
Created March 14, 2026 23:42
Probabilistic Data Structures: Bloom Filters, HyperLogLog, and Count-Min Sketch
# Install Redis Stack (includes RedisBloom, RedisSearch, RedisJSON)
docker run -p 6379:6379 redis/redis-stack-server:latest
# Create a Bloom filter: 1M capacity, 0.1% false positive rate
redis-cli BF.RESERVE crawled_urls 0.001 1000000
# Add and test items
redis-cli BF.ADD crawled_urls "https://example.com/page-1"
redis-cli BF.EXISTS crawled_urls "https://example.com/page-1" # returns 1
redis-cli BF.EXISTS crawled_urls "https://never-seen.com" # returns 0
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 23:42
API Versioning Strategies: Evolving APIs Without Breaking Clients
func VersionMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
version := r.Header.Get("API-Version")
if version == "" {
version = "2024-01" // default to latest stable
}
supported := map[string]bool{
"2023-06": true,
"2024-01": true,
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 23:43
Multi-Tenancy Architecture: Designing SaaS Backends That Scale
func TenantMiddleware(store TenantStore) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
host := r.Host // e.g. "acme.app.io"
subdomain := strings.Split(host, ".")[0]
tenant, err := store.GetBySubdomain(r.Context(), subdomain)
if err != nil || tenant == nil {
http.Error(w, "tenant not found", http.StatusNotFound)
return
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 23:44
Zero Trust Security for Backend Services: Beyond the Perimeter
func serviceIdentityFromRequest(r *http.Request) (string, error) {
if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
return "", fmt.Errorf("no client certificate presented")
}
cert := r.TLS.PeerCertificates[0]
for _, uri := range cert.URIs {
if uri.Scheme == "spiffe" {
return uri.String(), nil
}
}
@mohashari
mohashari / snippet-2.yaml
Created March 15, 2026 04:55
Change Data Capture with Debezium: Real-Time Data Pipelines from Your Database
version: "3.8"
services:
zookeeper:
image: confluentinc/cp-zookeeper:7.5.0
environment:
ZOOKEEPER_CLIENT_PORT: 2181
kafka:
image: confluentinc/cp-kafka:7.5.0
depends_on: [zookeeper]