Distributed databases with Peter Mattis

The Pragmatic Engineer • • 1h42 → 5 min • #104
Distributed databases with Peter Mattis
Watch on YouTube

Summary

  • Peter Mattis, co-founder and CTO of Cockroach Labs, traces a 30-year arc from building GIMP in college to designing Google’s Gmail storage, Colossus distributed file system, and Spanner, then founding CockroachDB — a strongly consistent, horizontally scalable SQL database — and recently returning to high-volume production coding using AI agents that he says amplify expertise rather than replace it.

Early path into software

  • Gaming and a household with early Apple computers sparked Peter’s interest; he started college as a mechanical engineer but switched to CS after finding the coursework far easier and more engaging.
  • In college, he and his roommate built GIMP (GNU Image Manipulation Program) and the GTK graphics library from scratch, learning graphics, rendering, and data structures on the fly; a competing pre-announcement on Usenet nearly dissuaded them, but they shipped anyway and the other project vanished.
  • GIMP led to a Google interview in 2001 (Google was three years old); Peter declined due to the commute, joined a startup, then accepted a second offer in 2002.

Gmail and Google infrastructure

  • Joined Google on April 1, 2002; worked on “Caribou” (Gmail’s internal codename), building the backend threading, message storage, and indexing system — his second B-tree implementation.
  • Gmail launched April 1, 2004 with 1 GB free storage (vs. 4 MB from competitors), using Google’s distributed file system (GFS) and an inverted index for fast search; ads were added later by Paul Buchheit, creating a massive business.
  • Invite-based rollout controlled demand and generated buzz; Peter wasn’t directly involved in the invite mechanics.

Google’s build system and Colossus

  • Google’s monolithic repo strained under a single massive Makefile (Google2); Peter laid the foundation for Google3, introducing “build files” — a stripped-down Python DSL that generated Makefiles — which evolved into Blaze (internal), then Bazel (external) and Buck (Facebook).
  • GFS hit scalability limits (~1,000 machines, single master bottleneck); Peter joined the founding team of Colossus, the successor, which used a distributed metadata layer backed by Bigtable (creating a circular dependency resolved by a bootstrap Bigtable that didn’t depend on Colossus).
  • Colossus adopted Reed-Solomon erasure coding (~2x overhead vs. 3x replication) for storage efficiency and higher durability; latency was bounded by HDD speeds (~5–10 ms), later improved by SSDs and faster networks (intra-zone ~100 µs).

Spanner and the storage/database distinction

  • Spanner, a distributed database built on Colossus, required different primitives: small, typed rows with random access vs. large append-only files; this drove log-structured merge trees (LSMs) — popularized by LevelDB/RocksDB — which Peter later reimplemented as Pebble for CockroachDB.
  • The circular dependency (Bigtable on Colossus, Colossus metadata in Bigtable) was resolved by a foundational Bigtable instance that didn’t use Colossus, a pragmatic hack that lasted years.

Founding Cockroach Labs

  • After a brief stint on Google Goggles and a failed startup (Viewfinder, acquired by Square as an acqui-hire), Peter reunited with college roommate Spencer Kimball and Ben Darnell at Square; they revived a Viewfinder-era design for a distributed SQL database.
  • Raised VC immediately (unlike Viewfinder); named it CockroachDB for resilience (“unkillable like cockroaches”).
  • Early customers came post-outage (e.g., a major bank after a region-wide power loss) or when hitting single-node limits (RDS max instance size) and facing manual sharding burden.

Automatic sharding vs. manual sharding

  • Manual sharding: hash user ID into fixed shards; resharding requires remapping and moving data — painful at scale.
  • Consistent hashing reduces data movement when adding nodes.
  • CockroachDB uses range-based partitioning (like Bigtable/Spanner/HBase): contiguous key spans split and moved automatically; a meta-index (B-tree-like) maps ranges to nodes, enabling automatic rebalancing without application involvement.

Consistency models and Raft

  • Strong consistency (linearizability/serializability) means reads always reflect the latest write, even from different replicas; weak/eventual consistency allows stale reads, pushing complexity to the application.
  • CockroachDB uses Raft (a Paxos variant) for consensus: minimum 3 replicas, writes go to all, reads typically from one; on failure, any 2 of 3 can recover state. Customers can configure 5 or 7 replicas for higher durability at latency cost (speed-of-light cross-region latency).
  • Design principle: minimize round-trips; batch reads then writes to avoid serial latency accumulation.

CockroachDB today

  • Mission-critical / tier-zero workloads: trading systems, banking, DoorDash logistics, shopping carts — outages cost millions/hour and reputational damage.
  • ~100–150 engineers in R&D; steady growth; focus on stability, performance, enterprise features; revenue growing with a clear path forward.

AI-assisted coding: return to the keyboard

  • Peter wrote ~100k lines/year pre-AI (peak CTO years); stepped back 2022–2024 to focus on management, but found it hard to guide engineers on AI tools without using them himself.
  • Started with GitHub Copilot/Cursor (glorified autocomplete); progressed to Claude Code, Opus, Sonnet, then o1/Fable/Astra; by late 2024 (Thanksgiving break) he was “agenting” — spinning up many parallel agents.
  • Example: built a massive benchmarking matrix tool (vertical scaling configs) in 4 days; implemented a highly optimized B-tree in Rust (~10k lines) in ~30 minutes.
  • Workflow: uses Claude desktop app + Codex desktop app; runs 5–10 concurrent sessions, each with many sub-agents (sometimes 100+); uses adversarial review (one model writes, another tears it down).
  • Moved from Emacs (20 yrs) → VS Code → terminal-based agents → graphical agent UIs (Claude/Codex desktop) for better session management.

Quality, testing, and security with AI

  • Ambition must increase: higher quality, security, performance. AI agents are “lazy” on testing — need firm guidance to use property-based, metamorphic, deterministic simulation testing.
  • Security: every commit can get adversarial AI review; guardrails (e.g., verifying decompiled assembly for zero-overhead abstractions) prevent regressions.
  • UX: designers now write HTML/CSS/JS directly and open PRs, eliminating waterfall handoffs.
  • Code review: scrutiny decreases as agents improve; moving toward trusting AI output like compiler assembly, with AI verifying AI (guardrails, not human eyeballs).

The “zone” and 100x engineers

  • Flow state differs: less intense typing, more cognitive orchestration — firing many experiments in parallel, reviewing progress updates, iterating rapidly.
  • Analogy 1: learning to drive a car (AI tools) instead of walking; early crashes, then F1-driver skill gap emerges.
  • Analogy 2: domain experts become “sorcerers” — knowing the right incantations (prompts, constraints, architecture) yields magical output; novices get hollow scaffolding.
  • 10x → 100x engineer: pre-existing expertise amplified; best results from strong engineers who lean into tools and know where they break.

Advice for engineers in the AI era

  • Use AI as an always-available tutor: dissect senior engineers’ code, ask for explanations at any level, generate diagrams, translate to familiar concepts.
  • Cultivate curiosity beyond your immediate area; explore adjacent systems (e.g., Peter studied Bigtable/Spanner while on Gmail).
  • Personal agency is essential: no one will teach you fast enough; you must use the tools daily and stay current — Peter learned more in the past year than the prior five combined.
  • Deep expertise remains the lever: AI amplifies what you know; hollow prompting produces hollow results.

Closing reflections

  • Peter is both excited and exhausted; the pace is unsustainable but transformative.
  • Cockroach Labs now has 500–1,000 internal apps built by non-engineers (HR, finance) on an AI platform — empowering custom software for everyone.
  • Core belief: asymptotically 100% of code will be AI-generated; humans supply agency and vision.
Back to The Pragmatic Engineer