Phase E: Advanced Backend Engineering (In-Repo Lessons)
Maps to: Blueprint Phase E
Goal: Handle money, files, background work, and failure modes without turning your codebase into copy-pasted routes.
Table of Contents
- Lesson 1: Layering routes, controllers, services
- Lesson 2: Idempotency and webhooks
- Lesson 3: File uploads and async processing
- Lesson 4: Query patterns at scale
- Lesson 5: Observability for backends
- Exercises
- Next step
Lesson 1: Layering routes, controllers, services
Route: HTTP parsing, status mapping
Controller: request/response shaping, authz checks
Service: business rules and DB transactions
This separation makes testing easier: services should not depend on Express-specific objects.
Lesson 2: Idempotency and webhooks
Payment providers retry webhooks. Your handler must be safe to run twice.
Patterns:
- Store provider event IDs you have processed
- Use DB constraints to prevent double-capture
- Return
2xxonly after durable state updates (or queue work transactionally)
Lesson 3: File uploads and async processing
Validate MIME type, size limits, and filename hygiene. Prefer scanning and virus scanning policies for real products.
Offload heavy transforms to a worker queue when response latency matters.
Lesson 4: Query patterns at scale
Avoid unbounded OFFSET for huge tables when you can use keyset pagination.
Add indexes that match real WHERE + ORDER BY clauses; verify with EXPLAIN.
Lesson 5: Observability for backends
Minimum viable production awareness:
- Structured logs with request IDs
- Error reporting (Sentry-class tools)
- Basic metrics: latency, error rate, queue depth
Exercises
- Implement a webhook endpoint with an idempotency table.
- Add a background job that runs every N minutes and cleans stale rows.
- Pick one slow query and document an index plan.