tmail.sh

Storage: an engine made for seeks

Tern keeps mail in an engine written for it, not in files of messages and not in a database someone else made. The aim it was designed to: as few disk reads for each mailbox operation as can be, on spinning disks, with nothing cached.

The constraint

On a spinning disk a seek costs about 10 ms, and reading on from there is about 150 MB a second. Seeks are the scarce thing. A 256 KiB block of mail compresses about five-fold and takes under 2 ms to transfer, so it is cheap to read more than was asked for and dear to go anywhere else for it.

Everything below follows from that: what a screen shows together is laid out together, and a design whose speed depends on fast solid-state storage counts as a failure.

A message, taken apart

A message that arrives is split into a skeleton and its parts.

Before a delivery is acknowledged the message is put together from its pieces and compared with what arrived. A part whose encoding cannot be made again exactly is kept as it was sent.

The example mailbox: 403,757 messages, 11.3 GiB as received
WhatOn disk
Large parts: 31,267 blobs, decoded, once each, compressed3.2 GiB
Message rows: skeletons and small parts, compressed in blocks0.9 GiB
The index, with the words of every body0.14 GiB
Total4.2 GiB

Two keyspaces, one log

The engine knows keys, values, batches and merge rules. It does not know what mail is. On top of it the mail schema defines rows and operations.

Readers never block writers and writers never block readers. A view of the store holds references to immutable runs and to the tables in memory as they were.

A run

Each keyspace is a set of immutable sorted runs on disk and a table of recent writes in memory. A run file is:

The memory this takes is about 4.6 bytes a message: 1.86 MiB for the example mailbox.

The rows

Keys are built from fixed-width big-endian fields, so the order of bytes is the order that matters. In the index keyspace all of one user's rows lie together.

RowKey, after the userHolds
Labellabel idName and role: inbox, sent, trash
Counterslabel idMessages, unseen messages
Statemessage idThe message's labels, flags and thread
Listinglabel id, inverted message idWhat a list shows: sender, recipients, subject, dates, size
Postingstag, first idMessage ids, up to 1,024 a row
Messagemessage idThe record, the skeleton, the small parts

Three merge rules

The schema tells the engine which rule a key has. Rules are applied when reading and when merging, with the same result, so a counter is changed by writing one small operand and reading nothing.

RuleMeaningUsed for
ReplaceThe newest put or delete winsMost rows
SumOperands are added togetherCounters, quota, references to parts
SetOperands add or remove message ids from a setPostings

One batch, one sync

Each write is one batch, applied entirely or not at all, and it returns when it is on disk.

Delivering the example mailbox into a new store, every word indexed on the way: 149 to 158 seconds on NVMe, 524 seconds on three spinning disks. That is 770 messages a second on the disks, with the large parts in the blob store already: it is the engine's own cost, rows, lists and their syncs.

After a crash

Nothing is rebuilt by scanning. An acknowledged write that a crash loses is one of the three outcomes the design names as failure, with a message whose part is missing and a store that must be scanned to come back.

Merging, and giving space back

On spinning disks

The example mailbox on a ZFS raidz1 of three spinning disks, in a virtual machine with 8 cores, the host keeping none of its data in memory. From cold means the guest's caches were dropped and the server started anew before the flow, which then ran once. The flows are IMAP's, as a mail API backend sends them. Milliseconds.

FlowColdWarm
Folder list in one command0.80.8
Open the inbox (36,898 messages)9135
A page of the inbox: open, STATUS, sort by arrival, fetch 5020561
The same sorted by date sent230105
A word of a subject, sorted21082
Resynchronise the inbox, nothing changed9233
A thread in the inbox284132
One message, whole1151.4
Its text part2.00.8
A word in the text, inbox590125
Open the largest folder (166,143)22780
A page of it306136
A word in the text, the largest folder650181
Flags of every message of the inbox316313

Measured 2026-11-27, in two runs: the three rows that look for a word are from the second, after IMAP's searches were made to fold accents. The disks are shared with other machines, which moves the figures from one run to the next. Not measured: many users at once, and a mailbox being written while it is read.

What is slow, and why

Two things read headers, and headers are where this layout pays.

FlowColdWarm
Sizes, flags and 14 header fields of every message of the inbox12,300 to 25,0004,400 to 5,500
A word in text or header (IMAP's TEXT), inbox14,3003,800

A folder's messages lie among all of the user's messages, in the order they arrived. The inbox is a tenth of this mailbox, so each 256 KiB block has two or three of its messages among twenty or so. Reading the headers of the whole inbox decompresses nearly every block of the message rows, some 2.6 GB, to use a tenth of it.

A cache does not mend that, and one was tried: the remedy is in the layout. The 16 header fields mail clients ask for come to 34 MB compressed for the whole mailbox, a twenty-eighth of the message rows, and could be kept by folder. That is measured and not yet built.

A desktop IMAP client asks for those fields when it first meets a folder. The tmail apps never do: a list's row has what a list shows.

How it is checked

Back: the protocolNext: search