Databases
JSONB, arrays, and when a side table wins
A team adds a “metadata” JSONB column for “occasional extra fields.” Three years later the column has 30 fields that every query touches — none indexed, none constrained, all parsed on every read. The schema grew into exactly what they were trying to avoid.
JSONB vs JSON: always JSONB
Postgres has two JSON storage types. JSON stores text verbatim — it is re-parsed on every read, not indexable, and slightly smaller. JSONB stores a parsed binary structure — indexable, queryable with path operators, slightly larger. In production, always JSONB. The only reason to use JSON is if you need to preserve key order or duplicate keys, which legitimate schemas never do.
When JSONB is the right choice
JSONB fits three shapes well:
-
Genuinely heterogeneous data. Event logs where each event type has a different payload shape. Third-party API responses where the schema is controlled by someone else. Configuration objects where keys differ per tenant.
-
Long-tail metadata. A products table where 80% of products share 10 columns and 20% have 50 additional supplier-specific fields. The 10 columns are typed; the 50-field tail is JSONB.
-
Schema-first, query-last. Data you store but rarely query — file uploader metadata, per-row user preferences. If the only query is “show me this row’s metadata,” JSONB is fine.
The wrong shape: any field you GROUP BY, JOIN on, aggregate, enforce uniqueness on, or reference with a foreign key. Those fields must be typed columns.
When a side table wins
Typed arrays (TEXT[], INTEGER[]) and JSONB both lose to a side table when:
- You need to query “all rows with tag X” at scale — a GIN index helps, but a join table with a B-tree index on
(tag_id, row_id)is faster and lets you enforce FK integrity. - You need to rename tag X globally — one UPDATE on the tags table vs. scanning every row that contains the tag.
- You need to count rows per tag, or join tags to another table — SQL aggregation over a join table is an order of magnitude cheaper than GIN-indexed JSONB.
- You need uniqueness per row (no duplicate tags on one item) — easily enforced on a join table’s composite PK; impossible to enforce inside an array without a constraint function.
The decision rule: if you only ever read “the tags on this row,” an array column is fine. The moment you query from the tag direction, use a side table.
JSONB index strategies
JSONB has two index families. Choosing the wrong one makes queries 10-100x slower than needed.
GIN (Generalized Inverted Index). Indexes every key or path inside the JSONB. Default operator class indexes every key; jsonb_path_ops indexes whole paths (faster for @> containment queries, larger index). Supports @>, ?, ?|, ?& operators.
-- Default GIN: supports ?, ?|, ?& and @>
CREATE INDEX idx_events_payload ON events USING GIN (payload);
-- jsonb_path_ops: only @> (containment), but faster for it
CREATE INDEX idx_events_payload_paths ON events USING GIN (payload jsonb_path_ops);Expression B-tree. Indexes one specific path extracted as a typed value. Supports equality and range queries on that path. Much smaller than GIN.
-- Indexes the user_id field as text
CREATE INDEX idx_events_user_id ON events ((payload->>'user_id'));
-- Indexes user_id as integer (cast)
CREATE INDEX idx_events_user_id_int ON events ((( payload->>'user_id')::BIGINT));Use GIN for “does this JSONB contain key/value X?” Use expression B-tree for “find rows where data.field equals this specific value.”
| Index type | Best for | Size | Operators |
|---|---|---|---|
| GIN (default) | Key existence, containment over many paths | 5-20x larger than B-tree | ?, ?|, ?&, @> |
| GIN (jsonb_path_ops) | @> containment only, faster lookups | Smaller than default GIN | @> only |
| Expression B-tree | One specific path, equality/range | Similar to regular B-tree | =, <, >, BETWEEN |
Generated STORED columns
Postgres supports generated columns whose value is computed from other columns at write time and stored:
ALTER TABLE order_items
ADD COLUMN line_total_cents INTEGER
GENERATED ALWAYS AS (unit_price_cents * quantity) STORED;The column is queryable, indexable, and updated automatically on every write. Unlike a trigger, the computation is declared in the schema and is visible to any reader without knowing the trigger exists.
Use cases: derived values you query frequently (full_name, line_total, is_final from a status enum), audit flags, computed denormalization. Trade: writes are slightly slower (expression evaluated per write); migrations on generated columns can trigger table rewrites.
Why this works
Why not use a trigger for computed columns? Triggers work, but they are invisible at schema level — a reader examining the DDL does not know the trigger exists or what it does. Generated columns are self-documenting, enforced by the engine, and survive schema dumps cleanly. Use triggers when the computation depends on data from other rows or tables (which generated columns cannot access). Use generated columns when the computation is per-row arithmetic.
FK at scale: the PlanetScale pattern
Some hyperscale shops (PlanetScale on Vitess, several large Postgres deployments) recommend disabling foreign keys. The specific conditions where this is reasonable:
- Data is sharded and the relationship crosses shard boundaries — FKs cannot span shards.
- A cascade would create a multi-million-row transaction holding locks for minutes.
- The FK validation pass on a column-type-change DDL is the operational bottleneck.
None of those conditions apply to a typical SaaS schema below ~100M rows per table. For most teams, the FK constraint costs ~5-50 μs per row at write time and refuses every orphan-row bug forever. Disabling it pushes the integrity guarantee into application code, where it is implemented inconsistently and breaks during refactors.
Senior engineers should treat “we disabled FKs” as a signal of specific scale constraints — not a general best practice.
Add product tags: array column, JSONB, or side table?
1/3A query `WHERE payload @> '{"event_type": "purchase"}'` on a 50M-row events table runs in 200 ms with a full GIN index but you need it under 20 ms. Which index change to try first?
A new service stores 'product reviews' (one review per user per product, rating + text + optional structured tags). Which schema shape?
- 01State the decision rule for JSONB vs typed column and give one example where each is correct.
- 02What is the difference between a GIN index with the default operator class and one with jsonb_path_ops, and when do you choose each?
- 03Articulate the conditions under which disabling foreign keys is a reasonable engineering choice.
JSONB (always JSONB over JSON) is the right choice for heterogeneous schemas, long-tail metadata, and data you store but rarely query by field. The moment a field appears in a WHERE, GROUP BY, or JOIN, it needs a typed column. Side tables beat arrays and JSONB when you query from either direction, need to aggregate, or need to enforce uniqueness between a row and a tag. GIN indexes support key-existence and containment; expression B-tree indexes support one specific path for equality and range. Generated STORED columns replace triggers for per-row arithmetic — declared in the schema, visible to any reader. FK constraints cost ~5-50 μs per write and prevent orphan rows forever; disable them only under specific sharding or cascade constraints, not as a general practice.
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