Backend From First Principles

Detailed lecture notes for the Backend From First Principles series β€” built from first principles, covering everything from HTTP internals to production-grade scaling.`

πŸ“Ί Playlist: YouTube

πŸ“‹ Best Practices Reference: BEST_PRACTICES.md β€” a distilled, actionable guide synthesized from all lectures.


Lecture Index

#TopicKey ConceptsNotes
1–4What is a Backend?Request flow (DNS β†’ Firewall β†’ Nginx β†’ Process), frontend vs backend separation, three-layer architecture, full topic roadmapLEC1-4
5HTTP ProtocolStatelessness, request/response anatomy, headers, CORS, status codes, caching, content negotiation, TLSLEC5
6HTTP RoutingStatic vs dynamic routes, path params, query params, nested routes, versioning, catch-allLEC6
7Serialization & DeserializationJSON, Protobuf, text vs binary formats, client-server data exchangeLEC7
8Authentication & AuthorizationSessions, JWTs, cookies, OAuth 2.0, OIDC, API keys, RBAC, timing attacksLEC8
9Validations & TransformationsType/syntactic/semantic/cross-field validation, transformation pipeline, backend vs frontend validationLEC9
10Controllers, Services, Repositories, MiddlewaresThree-layer architecture, middleware chain, request context, auth propagation, distributed tracing IDsLEC10
11REST API DesignREST constraints, URL structure, idempotency, CRUD patterns, pagination/sorting/filtering, custom actionsLEC11
12Mastering Databases with PostgreSQLData types, migrations, schema design, relationships, referential integrity, parameterized queries, indexes, triggersLEC12
13CachingCDN, DNS caching, Redis, lazy caching, write-through, eviction policies, session/rate-limit/query cachingLEC13
14Task Queues & Background JobsProducer/broker/consumer, acknowledgements, exponential backoff, one-off/recurring/chain/batch tasksLEC14
15Full Text Search & ElasticsearchInverted index, BM25 relevance, field boosting, fuzzy search, type-ahead, PostgreSQL FTS vs ElasticsearchLEC15
16Error Handling & Fault ToleranceError types, global error handler, custom error classes, health checks, graceful degradation, log securityLEC16
17Production-Grade Configuration ManagementConfig types, env vars, secrets management, feature flags, startup validation, least privilegeLEC17
18Logging, Monitoring & ObservabilityThree pillars (logs/metrics/traces), log levels, structured JSON logs, OpenTelemetry, Grafana stackLEC18
19Graceful ShutdownSIGTERM/SIGINT/SIGKILL, connection draining, resource cleanup, reverse-order teardown, zero-downtime deploysLEC19
20Backend SecuritySQL/command injection, password hashing (bcrypt/Argon2id), sessions, JWT security, BOLA, BFLA, XSS, CSRF, defense in depthLEC20
21.1Scaling & Performance β€” Part 1Latency percentiles, throughput, utilization, N+1 queries, indexes, connection pooling, caching patterns, vertical vs horizontal scalingLEC21.1
21.2Scaling & Performance β€” Part 2Statelessness, load balancers, read replicas, sharding, CDN, edge computing, async processing, microservices vs monolith, serverlessLEC21.2
22Concurrency & ParallelismIO-bound vs CPU-bound, event loop, OS threads, goroutines, race conditions, mutexes, Go channelsLEC22
23(TBD)β€”LEC23

Topics at a Glance

Foundation

  • LEC1–4 – What is a Backend?: How a request travels from browser to backend (DNS, firewall, reverse proxy). Why backend logic cannot live in the frontend. The three-layer architecture (Presentation β†’ Business Logic β†’ Data Access). Full topic roadmap for the series.
  • LEC5 – HTTP: The complete HTTP mental model β€” statelessness, request/response structure, all status codes, CORS preflight, caching headers, HTTP/1.1 β†’ HTTP/3 evolution.
  • LEC6 – Routing: How servers map (method, path) pairs to handlers. Path params identify resources; query params filter/paginate/sort them.
  • LEC7 – Serialization: Why JSON exists, how data crosses language boundaries, text vs binary format tradeoffs.

Auth & Data Integrity

  • LEC8 – Auth: Every authentication pattern from sessions to OAuth 2.0 + OIDC. Why timing attacks work and how to prevent them.
  • LEC9 – Validation: The four validation types, the transformation-first pipeline, and why backend validation is mandatory even when you have frontend validation.
  • LEC10 – Architecture: The Controller β†’ Service β†’ Repository separation and the middleware chain. Request context for passing auth data without coupling.

API Design & Databases

  • LEC11 – REST: Roy Fielding’s constraints, URL conventions, idempotency, the list API pattern, custom actions.
  • LEC12 – PostgreSQL: Production-ready schema design β€” UUID keys, JSONB, TIMESTAMPTZ, parameterized queries, N+1 prevention, indexes, triggers, migrations.

Infrastructure & Reliability

  • LEC13 – Caching: CDN β†’ DNS β†’ hardware β†’ Redis. Lazy caching, write-through, eviction, rate limiting with Redis, session storage.
  • LEC14 – Background Jobs: Async task patterns β€” one-off, recurring (cron), chained, batch. Acknowledgements, visibility timeout, exponential backoff.
  • LEC15 – Search: Why ILIKE '%term%' breaks at scale. Inverted index, BM25, Elasticsearch vs PostgreSQL FTS decision framework.
  • LEC16 – Error Handling: The global error handler pattern, custom error types, health checks, graceful degradation, log security rules.
  • LEC17 – Config Management: Environment-specific configuration, secrets management, startup validation, feature flags, principle of least privilege.
  • LEC18 – Observability: The three pillars (logs, metrics, traces), structured JSON logging, OpenTelemetry, Grafana/Prometheus/Loki/Jaeger stack, debugging workflow.
  • LEC19 – Graceful Shutdown: Unix signals, connection draining, reverse-order resource cleanup, zero-downtime deployments.

Security & Performance

  • LEC20 – Security: The full attack surface β€” SQL injection, command injection, password hashing evolution, session security, JWT pitfalls, BOLA, BFLA, XSS, CSRF, defense in depth.
  • LEC21.1 – Performance Part 1: Latency percentiles over averages, finding bottlenecks (not guessing), N+1 queries, indexes deep dive, connection pooling, cache invalidation strategies.
  • LEC21.2 – Performance Part 2: Stateless horizontal scaling, load balancing algorithms, read replicas + replication lag, sharding, CDN physics, edge computing, async processing, microservices vs monolith, serverless tradeoffs.
  • LEC22 – Concurrency: Event loop vs OS threads vs goroutines. Why most backends are IO-bound. Race conditions in both multi-threaded and async/await code.