shaledb architecture

Sections

A SlateDB-style log-structured merge tree on Cloudflare

How shaledb works

Each database has one writer and many readers. shaledb stores immutable SSTs in R2 and versions each database's manifest with compare-and-swap, or CAS. The writer is a Durable Object whose SQLite table is both the write-ahead log and the memtable. A Cloudflare Workflow runs compaction. Click the diagrams to step through them.

Workers
5
Durable Object classes
4
Public endpoints
23
Timers and crons
4

01

Topology

One Alchemy Stack deploys every part. Each database has its own writer and compactor Durable Object, and stores all of its data under db/<id>/. Pick a flow to animate it, or click a node to see what it does.

Scroll sideways to see the whole diagram.

02

Abstractions, layer by layer

Each layer calls only the layers below it. lsm-core has no Cloudflare imports. Point at a service, or tab to it, to see what it does.

03

Every endpoint and its code path

DbApiWorker, GcWorker, and ShardRouterWorker serve the public routes under /v1. The internal APIs carry HTTP requests between Workers and Durable Objects. Open a row to see its call tree.

    04

    Write path and flush

    A write is durable once SQLite commits it. A flush uploads the unflushed rows as one L0 SST and commits a new manifest by CAS. Only then does the writer mark the rows flushed. The rows stay in SQLite for transactions until a trim removes them.

    Crash and fencing cases

    DbWriter.flush, step by step

    flushLock
      flushOnce
        cut = memTable.meta.lastSeq
        ssts.write(keepReachableVersions(rows))
        exportChangelog                    # only with a changelog
        manifests.updateFrom(commitL0Flush)
          on WriterFencedError, markFenced
          on FlushedSeqStaleError, markFlushed and retry once
        memTable.markFlushed(cut), then trim
        refreshUnflushedCounters
        wakeCompactor if L0 has 4 SSTs     # in the background

    05

    Read path

    Reads run in the API Worker's isolate, not in the writer. The consistency level decides whether a read calls the writer. Block bytes come from two caches in front of R2.

    Consistency decides the path

      Where block bytes come from

      06

      Timers, crons, and background work

      Nothing runs a polling loop. Every periodic process is a Durable Object alarm, a cron trigger, a Workflow, or waitUntil. An idle database has no alarm set. The simulation below plays ten minutes of steady writes. The write rate and the job durations are made up. The timers use the default settings.

      t = 0:00

      Event log

        Every trigger

        TriggerKindWhereWhenDoes

        07

        Compaction

        The compactor plans jobs, and a Workflow runs them. The compactor records each job in its state before it launches the job. A job's commit replaces exactly its inputs and records its job id, so a retried commit changes nothing.

        The compactor Durable Object, as a state machine

        Click an event to move the compactor to its next state. The buttons show only the events that the current state accepts.

        08

        Garbage collection and the maintenance sweep

        Each database's compactor runs that database's garbage collection, one collection at a time. The cron sweep handles the cases that no compactor handles: removed databases, lost wakes, and holds that forks keep on their sources.

        GarbageCollector.collect, per database

          MaintenanceSweep.sweepPage(50), every five minutes

            09

            Database lifecycle and sharding

            Every lifecycle change is a manifest CAS. A fork copies no data. It references the source's SSTs and holds the source until the fork's compactions have rewritten them.

            ShardSplit.split, step by step

            10

            Storage layout

            Select an entry to see which processes write it, read it, and delete it.