Databases
What a relation is: tables, rows, keys, and constraints
A team stores “user has many tags” as a comma-separated column. Six months later: “how many users have tag X?” — every row must be parsed. A relational design answers that question as a point lookup. The difference is the model you start with.
What a relation is
Edgar Codd’s 1970 paper defined the foundations of every SQL database since. The vocabulary is small:
- Relation — a set of tuples sharing the same shape. The table is the SQL implementation.
- Tuple — a single row; every tuple in a relation has the same attributes.
- Attribute — a column; each draws values from a domain (a type).
- Candidate key — a minimal subset of attributes that uniquely identifies every tuple. A table may have several candidate keys; one is designated the primary key.
| Concept | SQL term | What it means |
|---|---|---|
| Relation | Table | A set of rows sharing one shape |
| Tuple | Row | One record |
| Attribute | Column | One named typed value per row |
| Domain | Type | The set of valid values for an attribute |
| Candidate key | PRIMARY KEY / UNIQUE | A minimal row identifier |
Why constraints are the model’s enduring win
The relational model does not just store data — it refuses bad data. Constraints are declarative rules the engine checks before any insert, update, or delete:
- PRIMARY KEY — uniquely identifies each row; implies NOT NULL and UNIQUE.
- FOREIGN KEY — a column referencing a primary or unique key in another table; the engine refuses orphan rows.
- NOT NULL — this attribute must always have a value.
- UNIQUE — this column or column set has no duplicates across rows.
- CHECK — an arbitrary boolean expression evaluated on every write; e.g.
CHECK (amount >= 0).
Without constraints you have a key-value store with SQL syntax. Application bugs can commit bad data. With constraints, the engine refuses on behalf of every caller — application code does not have to remember the rules.
The metaphor
A relational schema is a library catalogue. Each table is a drawer (books, authors, loans). Each row is a card with the same fields. A loan card has member-id and book-id pointing at other drawers (foreign keys). The librarian (the engine) refuses to file a loan card if the book-id does not exist. The system stays coherent without humans re-checking.
A practical scenario
Sven wants a “favourites” feature for a marketplace. Otto asks the shape question. Sven says “user has many favourites.” Otto reaches for the model: users, items, favourites table with (user_id, item_id) as PK plus two FKs. Three DDL lines and the feature is structurally correct — no duplicates, no orphans, queryable both ways. A JSON-array shortcut breaks the moment you ask “who favourited this item.”
Another team stores addresses as comma-separated text on the user row. Six months later marketing asks “how many users in Oregon?” — every text field must be parsed. A year later finance asks “sales by state” — same problem on every order. A relational design (addresses table with structured columns) is two extra schema lines and three orders of magnitude cheaper to query.
Why this works
The cost of the relational model is the constraint check at write time and the discipline of designing the schema before you can write code. Senior engineers pay that cost knowingly; new teams either pay it accidentally (denormalize first, regret later) or skip it (treat the database as a key-value store and accumulate years of integrity debt). This lesson is about why the first path is almost always cheaper across the lifetime of a system.
Order the steps to design a schema for 'user has many addresses':
- 1 Identify the entities: User, Address
- 2 For each entity, pick a primary key (id BIGSERIAL or uuid)
- 3 Identify the relationship: one user has many addresses (1:N)
- 4 Add a user_id column to addresses with REFERENCES users(id)
- 5 Add NOT NULL on the FK (every address must belong to a user)
- 6 Add an index on addresses(user_id) so 'list addresses for user X' is fast
- 7 Decide ON DELETE: CASCADE or RESTRICT
What is a primary key in a relational table?
What does a foreign key declaration buy you?
Fill in the blank: the database engine refuses bad inserts because of declared _______ — rules like NOT NULL, UNIQUE, FOREIGN KEY, CHECK.
- 01In two sentences, why is storing 'user has many tags' as a JSON array column usually worse than a tags table plus a user_tags join table?
- 02Name the five constraint kinds a relational engine enforces and state what each one does.
- 03What is the difference between a candidate key and a primary key?
The relational model defines data as sets of typed tuples sharing a fixed shape (a relation). Tables, rows, and columns are the SQL implementations of relations, tuples, and attributes. Every row is identified by a candidate key — one is the primary key. Foreign keys link tables and let the engine refuse orphan rows. The five constraint kinds (PK, FK, NOT NULL, UNIQUE, CHECK) are the declarative guarantees that move correctness from every application to the database boundary. The cost is schema-design discipline before code; the payoff is correctness every caller gets for free.
appears again in164
- Why GraphQL gets N+1junior
- DataLoader mechanics: tick-boundary batchingmiddle
- Batch function contracts: ordering, shapes, errorsmiddle
- Federation and lookahead: batching beyond DataLoadermiddle
- Query complexity defences: depth, cost, persisted queriesmiddle
- Senior GraphQL API: scheduling contract, tenant isolation, observabilitysenior
- Why idempotency: making retries safejunior
- Server-side state machine: four states of an idempotency keymiddle
- Outbox and inbox: effectively-once across the dual-write boundarymiddle
- Concurrency and cache architecture for idempotency at scalesenior
- Observability, production failures, and global-scale designsenior
- The event loop: one thread, three queuesjunior
- Tasks, microtasks, and scheduler.yield()middle
- Microtask starvation, Long Tasks, and LoAFsenior
- Node.js event loop: phases, nextTick, and loop lagsenior
- React, Vue, and INP observability in productionsenior
- The render pipeline: six stages from bytes to pixelsjunior
- Stage costs and the renderer process modelmiddle
- Invalidation, dirty bits, and containmiddle
- Compositor layers: promotion, overlap, and GPU memorymiddle
- DevTools flame strip and the frame lifecyclemiddle
- Layout thrash: forced synchronous layoutsenior
- BeginMainFrame, compositor-driven animations, and GPU memorysenior
- Production observability: LoAF, INP, and the full attack surfacesenior
- What V8 is and why performance varies 100×junior
- V8''''s four-tier JIT pipeline and profile-guided tieringmiddle
- Hidden classes, transition trees, and memory layoutmiddle
- Inline caches, IC states, and deoptimizationmiddle
- Orinoco GC: parallel scavenger, concurrent marking, and write barriersmiddle
- TurboFan''''s speculative engine and the deopt-loop trapsenior
- V8 in production: isolates, pointer compression, and real failuressenior
- What workers are and why they existjunior
- Web worker mechanics: dedicated, shared, and OffscreenCanvasmiddle
- Structured clone and transferablesmiddle
- Service worker lifecycle and cache strategiesmiddle
- SharedArrayBuffer, Atomics, and cross-origin isolationsenior
- Service worker edge cases: version skew, durability, and navigation trapssenior
- Worker pools, Comlink, and production observabilitysenior
- What the reconciler does: render vs commitjunior
- The fiber object and the double-buffer treemiddle
- Render phase purity and commit phase sub-stepsmiddle
- Reconciliation: diffing heuristics and the key trapmiddle
- Priority lanes, time-slicing, and useTransitionmiddle
- Bailout, memoisation, and tearingsenior
- React Profiler, the Compiler, and production observabilitysenior
- Rendering strategies: SSG, SSR, ISR, streaming, and hydrationjunior
- SSG, SSR, ISR, streaming, and RSC — how each worksmiddle
- Hydration cost: selective, progressive, islands, resumabilitymiddle
- Hydration mismatch: causes, detection, and the determinism rulesenior
- RSC, per-route strategy, and production observabilitysenior
- Core Web Vitals: what LCP, INP, and CLS measurejunior
- CLS: why layout shifts happen and how to stop themmiddle
- Metric tradeoffs, RUM attribution, and the CI+field loopsenior
- The full picture: URL to LCP to INP as a relay racejunior
- Eight layers traced: from the service worker to the second navigationmiddle
- Five canonical breaks: where production reliably diessenior
- The three-track method: reading traces and building a monitored systemsenior
- What is a cache stampede and why it makes things worsejunior
- Lock and single-flight: bounding concurrent rebuildsmiddle
- XFetch: coordination-free probabilistic early expirationmiddle
- Stale-while-revalidate and CDN request coalescingmiddle
- Detecting stampedes and designing TTL for productionmiddle
- Metastable failure, fencing tokens, and production postmortemssenior
- Raft roles, terms, and why majority quorums prevent split brainjunior
- How Raft replicates a log entry and decides it is safe to commitmiddle
- Raft leader election: timeouts, voting rules, and the four safety propertiesmiddle
- Raft in the real world: partitions, slow disks, and client routingmiddle
- Raft extensions: pre-vote, learners, snapshots, and linearizable readssenior
- Raft in production: membership changes, Multi-Raft, and observabilitysenior
- Where data fetching happens — and why it decides LCPjunior
- Fetch waterfalls — diagnosis and the Promise.all curemiddle
- React Server Components and Suspense streamingmiddle
- Client-side cache: TanStack Query, SWR, and stale-while-revalidatemiddle
- LCP, prefetch, and race conditions in interactive fetchingmiddle
- Senior internals: RSC payload, caching layers, and production failure modessenior
- The IP envelopejunior
- Reading the IP headermiddle
- The three-way handshakejunior
- Sequence numbers and connection statemiddle
- DNS: what it does and why it existsjunior
- The resolver walk: referrals, record types, and gluemiddle
- TTL, caching, and DNS propagationmiddle
- What TLS does and why it existsjunior
- The 1-RTT handshake: key shares and ECDHEmiddle
- Session resumption and 0-RTTmiddle
- Key schedule, SNI, ALPN, and extensionssenior
- 0-RTT defenses, ECH, hybrid PQ, and production TLSsenior
- WebSocket: the HTTP upgrade handshakejunior
- WebSocket frame format: opcodes, masking, fragmentationmiddle
- WebSocket backpressure: when clients can''''t keep upmiddle
- Reconnection: jittered backoff, thundering herd, message resumptionsenior
- WebSocket at scale: HTTP/2 multiplexing, permessage-deflate, C10Msenior
- WebSocket in production: proxies, security, and distributed architecturesenior
- What reverse proxies dojunior
- Health checks, connection draining, and slow startmiddle
- Session affinity, consistent hashing, and the right fixmiddle
- Retry storms, circuit breakers, and load sheddingsenior
- Resilient LB architecture: anycast, zone-aware routing, and observabilitysenior
- Why QUIC and not TCP+TLSjunior
- Connection IDs and network migrationmiddle
- 0-RTT resumption and packet encryptionsenior
- DDoS: what it is and why it worksjunior
- Amplification attacks and state exhaustionmiddle
- Rate limiting: algorithms and architecturemiddle
- WAFs, firewalls, mTLS, and HSTSmiddle
- DNS cache poisoning and BGP hijackingsenior
- Defense-in-depth architecture and attack economicssenior
- The twelve layers: one URL, seven actorsjunior
- DNS, TCP, TLS in sequence: where the milliseconds gomiddle
- Proxy intercepts and security gates: rate limiters, WAF, mTLSmiddle
- Alternate paths: QUIC 0-RTT, WebSocket upgrade, connection migrationmiddle
- Observability: distributed traces, USE/RED, and samplingsenior
- Resilience: cascading retries, circuit breakers, and error budgetssenior
- What the three signals are: logs, metrics, and tracesjunior
- Why structured logs exist: the diary vs the spreadsheetjunior
- The production log schema: fields every line must carrymiddle
- PII redaction and log injectionsenior
- OTel Logs Data Model and audit logs as a subsystemsenior
- What is OpenTelemetry: API, SDK, Collector, OTLPjunior
- OTel signals, Semantic Conventions, and the OTLP wire formatmiddle
- The OTel Collector: receivers, processors, exporters, and deployment patternsmiddle
- Vendor neutrality, eBPF instrumentation, the Operator, and browser/serverless OTelsenior
- Operating the OTel Collector: reliability, version skew, failure modes, and governancesenior
- SLI, SLO, and the error budget: reliability by the numbersjunior
- Error budget policy, latency SLOs, and composite journeysmiddle
- Production SLO failures, self-observability, security, and the big picturesenior
- What is trace propagation and why broken propagation is worse than nonejunior
- traceparent and tracestate: the W3C header format in fullmiddle
- Baggage and async boundaries: carrying context across queues and callbacksmiddle
- Async context per language, service mesh, B3 migration, and securitysenior
- Production propagation failures, span links, and platform designsenior
- The debugging funnel: SLO → RED → trace → profilejunior
- OTel architecture: one SDK, four signals, one wire formatmiddle
- The incident loop: from pager to postmortem to preventionmiddle
- Scale, security, and the ROI of observable systemssenior
- Cache lines, struct layout, and false sharingmiddle
- SIMD, SoA vs AoS, and memory bandwidthmiddle
- Cache-oblivious algorithms, PGO, and production failuressenior
- GC in production: observability, security, edge cases, and fleet governancesenior
- Batching: amortize fixed cost per operationjunior
- The batching window: size and wait timemiddle
- Batching in Kafka and Postgresmiddle
- io_uring and observability of batchingmiddle
- From Nagle to io_uring: evolution of batchingmiddle
- Backpressure, failure isolation, and batch security in productionsenior
- CI enforcement and RUM: making budgets stickmiddle
- V8 JIT pipeline, HTTP priorities, and bundle securitysenior
- The performance loop: discipline, not a projectjunior
- Classify and fix: matching bottleneck families to remediesmiddle
- Observability stack and CI gates: catching regressions before they shipmiddle
- Incident to enforcement: SLO burn to verified fix in 35 minutesmiddle
- Culture, economics, and org-scale performancesenior
- At-most-once, at-least-once, exactly-once: the three delivery contractsjunior
- The three failure legs — where duplicates and losses actually happenmiddle
- Consumer-side dedup: the cheapest path to exactly-once processingmiddle
- Kafka exactly-once semantics: idempotent producer and transactionsmiddle
- SQS visibility timeout, DLQ, and the outbox patternmiddle
- Exactly-once in production: impossibility proof, hybrid patterns, and real incidentssenior
- What OAuth is and why passwords are not the answerjunior
- Authorization code flow with PKCEmiddle
- ID token validation and JWKS cache managementmiddle
- Refresh token rotation and scope-based least privilegemiddle
- Sender-constrained tokens: DPoP and mTLSsenior
- OAuth in production: audience attacks, observability, and real failuressenior