tmail.sh

Tern vs Cyrus IMAP

Cyrus IMAP is the baseline Tern is measured against, and the server its first mailbox came from. The same mailbox was loaded into both: 403,757 messages in 32 folders, 11.3 GiB as received. This page says what was measured, how, and what the figures leave out.

Where they differ

Cyrus IMAPTern
A message on diskOne file, as it arrivedA compressed row for its skeleton and small parts; large parts apart, by the hash of their content
An attachmentInside the message's file, encoded as it was sentDecoded, compressed, kept once however many messages carry it
The search indexXapian, built by squatter in a pass of its ownLists of words in the store's own engine, written in the same batch as the message
The native protocolIMAPgRPC, mail.v1. IMAP is served beside it
FoldersMailboxes: a message is in oneLabels: a message is in any number, and always in All mail
Rules for arriving mailSieve scriptsRules, which a Sieve script is read into
Written inCRust

On disk

Cyrus 3.10.2 and Stalwart 1.2.1 were each loaded on a volume of their own and measured after they settled. Sizes are GiB as allocated; the ratio is to the 11.3 GiB loaded.

Cyrus 3.10.2Stalwart 1.2.1Stalwart, no search indexTern
Message bodies12.07.47.43.2 + 0.9
Metadata, index1.22.62.20.14
Search index9.2in the storenonein the index: 0.06
Total22.310.19.74.2
Ratio to the mail1.970.900.860.37
Files403,90742331931,280

Tern is a little over a third of the mail, less than half of Stalwart's and under a fifth of Cyrus's. Most of the difference with Stalwart is probably base64: an attachment kept decoded is three quarters of what was sent, before compression and before it is kept once.

Not like for like. The search indexes of Cyrus and Stalwart do more than Tern's. Cyrus's holds where each word stands, for phrases, and both index headers. Tern's has the words of bodies and of subjects, and names; positions would add about 0.2 GiB. Tern has no replication and no backup copy yet. The Cyrus figures are from a second, reviewed run; the first had measured 26.8 GiB, with a search index of 13.6 GiB, and why that index was half as large again was not looked into.

Building the search index

Cyrus IMAPTern
Loading the mailbox37 min, by IMAP APPEND8 min 44 s, both at once
Indexing its words2 h 14 min, afterwards
When new mail can be found by its wordsOnce the indexer has reached itWhen the delivery is acknowledged

Both on the same pool of three spinning disks, but not the same way: Cyrus was loaded one message a command while two Stalwart servers were being loaded beside it, with disk syncs turned off for the load; Tern was delivered in batches, with syncs, while the Cyrus server was paused, its large parts already in the blob store. Other machines share the disks. And 2 h 14 min is the first build of a whole mailbox's index: a Cyrus in service indexes new mail in the background as it arrives. The figures say what each took here, and little more.

On the wire

The larger difference is not in any one command. It is in how many a screen needs. A mail API in front of Cyrus pays for IMAP's shape in round trips; Tern's API was laid out from what the screens show.

To showOver IMAP, as a mail API backend does itTern, over gRPC
The folders with their countsA LIST, then a STATUS for each folder: 33 commands for 32 foldersListFolders: 1 call
A page of a folderA SORT of the whole folder, then a FETCH of the pageListConversations: 1 call
A search across foldersUp to a hundred EXAMINE and UID SORT pairs, again from the first folder for every pageQuery: 1 call
A conversationA search of every folder for the thread's Message-IDsGetThread: 1 call
What changed since I was last hereA resynchronisation of each folderChanges: 1 call for the whole mailbox

Counts of commands, not timings: the two have not been timed against each other screen for screen. IMAP has extensions that fold some of these, LIST-STATUS among them, and Tern's own IMAP serves them. Cyrus also speaks JMAP, which was not measured here.

The same IMAP commands, to both

Tern serves IMAP too, so the same commands can be sent to both servers. The whole flow, median of 20 (10 for the heavier), in milliseconds.

FlowTernCyrus
Folder list: LIST, then STATUS of each of 323.589
Folder list in one command (LIST-STATUS)0.1372
Open the inbox (36,898 messages)147
A page of the inbox: open, STATUS, sort by arrival, fetch 502633
The same sorted by date sent4633
Ids of the inbox: sort all by date, 13 searches by flag102124
Search the inbox for a word of a subject, sorted109230
Resynchronise the inbox (QRESYNC), nothing changed208
A thread in the inbox: two HEADER searches88469
Open the largest folder (166,143)5129
A page of it, sorted by date sent183159
Ids of it: sort all by date, 13 searches by flag324586
Search it for a word of a subject, sorted6061,094
Resynchronise it, nothing changed4029
One message, whole0.2634
Flags of every message of the inbox19449

Read this table with care. The two columns are not from the same machine. Tern was on NVMe on the development machine, its store read from memory; Cyrus was on the benchmark machine's spinning disks, reached through a tunnel that adds 0.4 ms a command, while it was still building its search index. Cyrus's column shows where Tern stands, no more. The copy on Cyrus also had no flags set, which the rows that search and fetch flags should be read with. Tern's own figures on those spinning disks are on the storage page.

Where Cyrus is ahead in it, the reason is the layout. Tern keeps flags by message and not by folder, so the flags of a folder that is a tenth of the mailbox are read from among ten times as many. Why opening a folder and resynchronising it take Tern about twice as long has not been looked into.

The same answers

With the same mailbox in both, every message's ENVELOPE and BODYSTRUCTURE was asked of each server and compared. Flags could not be: the copy on Cyrus had none.

The sameDiffer
Structures403,74215
Envelopes403,457300

What differs is mail that breaks the standard, which the two read differently: address lists with brackets around the whole list, names with an @ and no quotes, eight attached messages sent base64-encoded, a few multiparts whose type Cyrus does not take for one.

Tern's IMAP also passes a program of 100 checks written with JavaMail 1.6.8 (folders with names outside ASCII, append, STATUS, SORT, searches, flags and keywords, copy and move, quota, resynchronising, IDLE), and Dovecot's imaptest with ten clients at once reports nothing in its plain stress run.

Moving a mailbox from Cyrus

cyrus-migrate reads a Cyrus spool as it lies on disk and appends its mail to Tern through the API. Cyrus's files are only read.

A trial on a copy of a spool: 37 folders, 403,917 live messages, 13.3 GB of files
First runSecond runverify
Wall time1 h 54 min1.5 s132.7 s
Peak memory599 MiB610 MiB639 MiB
Messages appended403,9110
Refused: file unreadable11
Missing; extra, labels, flags, bytes, arrivals, script, quota1; all 0

The one refused and the one missing are the same message: its index record is live, and its file is not in the copy. The store afterwards took 4.87 GB; the 13.3 GB of Cyrus's files include 2,124 expunged messages not yet cleaned up.

The first run made 59 messages a second, far below what appending alone does on generated mail (236 a second on one stream). The likely cause, not yet proven, is the migrator's own state file, which records each message in a transaction of its own. Batching that is not built.

Where Cyrus is ahead

Cyrus has carried mail for decades, and Tern is not released. Beyond that:

Back: searchtmail.sh