Core Concepts

MLambda.Data rests on a few principles. Knowing them makes the whole system predictable.


Everything is an actor

There is no database process in the traditional sense: there are actors, each responsible for one concern, each a class (a data type and its instance Actor) with a route:

Actor Route Responsibility
DataEngine engine Routes requests to tables’ stores, runs joins
TableStore store/{database}/{table} One table’s storage: log, memtable, SSTables, indexes
SchemaRegistry tables/schema Every table’s definition, versioned
TableActor tables/{database}/{table} One table’s directories and schema file
WalActor wal/{database}/{table} The write-ahead log
MemtableActor memtable/{database}/{table} Sorted writes in memory
Compactor compaction/{database}/{table} Merging SSTables in the background
BTreeIndexActor, HashIndexActor, InvertedIndexActor, BloomFilterActor index/{kind}/{table}/{column} Secondary indexes
NamespaceActor, FileActor, BlockActor namespace, file/{file}, block/{block} The filesystem
Replica replica/{node} A node’s share of the ring, for the other nodes
AuthActor auth Users, logins, lockouts
HubActor hub/{hub} A live channel’s subscribers

Each actor handles one message at a time from its mailbox, and shares no mutable state with the others.

Why this matters

  • No locks: an actor’s state is only ever touched by the actor.
  • No deadlocks from locks: actors do not hold locks while they wait.
  • Fault isolation: a failing actor is restarted, resumed or stopped by its supervisor; the rest keep running.
  • Natural concurrency: many actors run at once, each on its own fiber.

Values replace query text

Traditional databases accept text (SQL, CQL) and parse it on every request. MLambda.Data accepts values:

-- parsed every time; a parameter interpolated by mistake is an injection
SELECT * FROM users WHERE age > 30
-- a value the compiler checks: there is nothing to parse and nothing to inject
select users (selecting (Where (Filter "age" OpGt (toValue 30))))

A query is a Spec (filters joined by and, or, not), a write is an entity or a row, a schema is a TableDef. The messages the actors exchange are Aleph data types, checked when the program is compiled.


Routes replace addresses

Callers reach an actor by its route, not by where it runs. On a cluster the ring decides which nodes hold a key; the coordinator sends the request to those replicas (replica/<node>), and MLambda.Actor carries it, locally or over the network.


Files as blocks

The package has a block filesystem: a NamespaceActor catalogs files (their type, whether they are sealed, their size), a FileActor owns a file’s blocks, and a BlockActor writes one block to disk. A file is created, appended to, sealed, read and deleted.

Table storage does not go through it yet: the log, the SSTables and the snapshots are files under the data root (<root>/data/<database>/<table>/), written with the operating system’s file calls. The filesystem layer is the foundation for storing those files as replicated blocks.


Quorums, not a leader

Cluster membership spreads by gossip (MLambda.Actor’s Syn, Ack, Ack2 exchange) with a phi accrual failure detector, and each key’s replicas come from the ring. There is no leader election for data: a request needs the acknowledgement of as many replicas as its consistency level asks. At Linearizable (the default) a key’s reads and writes take two rounds over a majority of its owners (the ABD register), which is linearizable without a leader; only a conditional write (writeIf) runs a consensus round, and only among that key’s owners.


Recovery is automatic

An actor fails

Its guardian decides by the failure’s kind (Management.Failures): storage restarts on I/O, compaction, snapshot and memory failures; a restart replays what the log holds.

A node restarts

Each table recovers from its manifest, its latest snapshot, and the log entries after it. A torn or corrupt entry at the end of the log ends the replay there. The Docker topologies check this: they stop every server, start them again on their volumes, and find the data.

A replica falls behind

A read at Linearizable, Quorum or All answers the newest copy and writes it back to the replicas that were behind (at Linearizable, before it answers). Hints deliver what a replica missed while it was down. And every hour the ring’s first node repairs every table: digests compared by group of owners, the groups that differ copied newest-first.

Circuit breakers

Management.CompactionBreaker pauses compaction when the disk is busy (80% of its I/O, or the fastest of the last eight log syncs past 50 ms) and lets one merge through a minute later, so background work does not starve foreground writes.


Consistency is tunable

Each request on a cluster names how many replicas must answer:

Level Replicas
One 1
Quorum a majority
All every replica

Writing and reading at Quorum means a read always meets the latest acknowledged write. See Consistency Model.


Specifications replace WHERE clauses

Both (Where (Filter "age" OpGt (toValue 20)))
     (Negated (Where (Filter "name" OpPrefix (B.bytesFromString "D"))))

The engine narrows by the filters joined with Both (through an index when one fits), and the whole specification is checked on each row, so OneOf and Negated hold exactly. See the Developer Guide.