1---
2name: backend
3description: Build reliable backend services with proper error handling, security, and observability.
4metadata:
5 source: https://skillmd.com/skills/modbender/backend
6 clawdbot:
7 emoji: ⚙️
8 os:
9 - linux
10 - darwin
11 - win32
12---
13
14## Error Handling
15
16- Never expose stack traces to clients—log internally, return generic message
17- Structured error responses: code, message, request ID—enables debugging without leaking
18- Fail fast on bad input—validate at entry point, not deep in business logic
19- Unexpected errors: 500 + alert—expected errors: appropriate 4xx
20
21## Input Validation
22
23- Validate everything from outside—query params, headers, body, path params
24- Whitelist valid input, don't blacklist bad—reject unknown fields
25- Validate early, before any processing—save resources, clearer errors
26- Size limits on all inputs—prevent memory exhaustion attacks
27
28## Timeouts Everywhere
29
30- Database queries: set timeout, typically 5-30s
31- External HTTP calls: connect timeout + read timeout—don't wait forever
32- Overall request timeout—gateway or middleware level
33- Background jobs: max execution time—prevent zombie processes
34
35## Retry Patterns
36
37- Exponential backoff: 1s, 2s, 4s, 8s...—prevents thundering herd
38- Add jitter: randomize delay—prevents synchronized retries
39- Idempotency keys for non-idempotent operations—safe to retry
40- Circuit breaker for failing dependencies—stop hammering, fail fast
41
42## Database Practices
43
44- Connection pooling: reuse connections—creating is expensive
45- Transactions scoped minimal—hold locks briefly
46- Read replicas for read-heavy workloads—separate read/write traffic
47- Prepared statements always—SQL injection prevention, query plan cache
48
49## Caching Strategy
50
51- Cache invalidation strategy decided upfront—TTL, event-based, or both
52- Cache at right layer: query result, computed value, HTTP response
53- Cache stampede prevention—lock or probabilistic early expiration
54- Monitor hit rate—low hit rate = wasted resources
55
56## Rate Limiting
57
58- Per-user/IP limits on expensive operations—login, signup, search
59- Different limits for different operations—read vs write
60- Return Retry-After header—tell clients when to retry
61- Rate limit early in request pipeline—save resources
62
63## Health Checks
64
65- Liveness: is process running—restart if fails
66- Readiness: can handle traffic—remove from load balancer if fails
67- Startup probe for slow-starting services—don't kill during init
68- Health checks fast and cheap—don't hit database on every probe
69
70## Graceful Shutdown
71
72- Stop accepting new requests first—drain load balancer
73- Wait for in-flight requests to complete—with timeout
74- Close database connections cleanly—prevent connection leaks
75- SIGTERM handling: graceful; SIGKILL after timeout
76
77## Logging
78
79- Structured logs (JSON)—parseable by log aggregators
80- Request ID in every log—trace request across services
81- Log level appropriate: debug for dev, info/error for prod
82- Sensitive data never logged—passwords, tokens, PII
83
84## API Design
85
86- Versioning strategy from day one—path (/v1/) or header
87- Pagination for list endpoints—cursor or offset; include total count
88- Consistent response format—same envelope everywhere
89- Meaningful status codes—201 for create, 204 for delete, 404 for not found
90
91## Security Hygiene
92
93- Secrets from environment or vault—never in code or config files
94- Dependencies updated regularly—automated with Dependabot/Renovate
95- Principle of least privilege—service accounts with minimal permissions
96- Authentication and authorization separated—who you are vs what you can do
97
98## Observability
99
100- Metrics: request count, latency percentiles, error rate—the RED method
101- Distributed tracing for microservices—follow request across services
102- Alerting on symptoms, not causes—high error rate, not CPU usage
103- Dashboards for operational visibility—know normal to spot abnormal