tmail.sh

The protocol: mail.v1 over gRPC

Tern's native protocol is gRPC. One file, mail/v1/mail.proto, holds everything a client can do: 48 calls in five services, over HTTP/2. The tmail apps speak nothing else.

Native means the store was built for these calls. IMAP is served too, beside the API and on the same store. It is not what the API is made of, and no gRPC call is turned into IMAP commands anywhere.

Why gRPC

Two doors

The public doorThe services' door
Who callsA person's deviceA trusted service
PresentsThe access token the device got at sign-in, as authorization: Bearer, over TLSThe services' token
The userThe token's. A request need not name one; if it does, it must be that oneNamed in each request, and may be any
AllowedA call needs a scope: mail.read, mail.write, mail.send or settings. Only WhoAmI needs noneEvery call, those marked "services only" among them
WhereOpen to the network, HTTP/2 onlyLoopback

Ids, folders and the state

The five services

Mail, 21 calls

CallKindWhat it does
ListFoldersunaryFolders, with their counts, in one request
CreateFolderunaryMakes a folder, or returns the one there is with that name
RenameFolderunaryRenames one
DeleteFolderunaryDeletes a folder, for a trusted service. Its messages stay in All mail
ListMessagesunaryA page of a folder, or of All mail, by arrival or sorted
ListConversationsunaryA page of a folder's conversations, newest first, each with what a list's row shows. One state of the mailbox answers the page
GetMessageunaryOne message: what a list shows, and as asked its header, its structure and its text. Never fetches a large part
GetMessagesunarySeveral messages in one call, answered in the order asked, as many as fit a reply
GetPartserver streamOne part's content, decoded
GetRawserver streamThe message as it was received
GetThreadunaryThe messages of a thread, oldest first, whole or in pages
Queryserver streamAnswers a query of the query language: the columns, the rows, then what it read
SetFlagsunaryAdds and removes flags, optionally only on messages unchanged since a state
RelabelunaryPuts messages in folders and takes them out of others, as one change: move, copy, archive
DeleteunaryDeletes messages for good
Appendclient streamStores a message the caller has: a sent message, a draft
Sendclient streamSends a message as the user, and keeps a copy in Sent
Uploadclient streamKeeps a file for a message to refer to. An upload that broke goes on from what the server holds
ChangesunaryWhat changed after a position
Watchserver streamThe same, and then each change as it is made
GetQuotaunaryWhat the user holds, and their limit

Accounts, 12 calls

Users and their addresses, the rules for arriving mail, passwords and quota. CreateUser, ResolveUser, SetPassword, SetQuota, AddAddress and ImportVacationMemory are for services only. A person reads and sets their own rules with GetRules and PutRules: rules are checked whole, every search and action, and refused whole if something is wrong, naming the rule and what. AskAddress and ConfirmAddress let a person add an address by a code sent to it. ImportVacationMemory brings what Cyrus remembered of the vacation replies it sent, so nobody is answered twice after a move.

Settings, 4 calls

The applications' settings, kept for them: names and values the server does not read. Each was last changed at a revision, which counts every change, and ListSettings with a revision gives only what changed since. PutSetting can be made to succeed only if the setting is still at the revision the device read, so two devices do not overwrite each other.

Push, 4 calls

Push notifications through Apple's service: each device's token, kept with its session and forgotten with it, and the labels whose new mail the account is told of.

Sessions, 7 calls

A person's own devices: who am I, my sessions, end one, make a password for a device that can only give one, change my password, list and remove passkeys.

Keeping a copy: Changes and Watch

A client that keeps a copy of the mailbox needs to know one thing: what happened after the point it has got to. That point is a position, a state and, where one change touched many messages, the message a reply ended at.

The whole of synchronising, from the proto file
// How far a reader of changes has got.
message Position {
  uint64 state = 1;
  optional uint64 after_message = 2;
}

message ChangesResponse {
  // New and changed messages as they are now, in the order of
  // their last change. A message that changed often is here once.
  repeated MessageSummary changed = 1;
  repeated uint64 deleted = 2;
  // How far this reply goes. Pass it as `after` to read on.
  Position position = 3;
  bool more = 4;
  bool folders_changed = 5;
  uint64 state = 6;
  uint64 settings_revision = 7;
  repeated uint64 changed_ids = 8;
}
What the clients' core does on a mailbox of 403,757 messages, counted at the wire
ScenarioCallsReceivedTime, p50
From nothing to the first page of the inbox in the client's copy4 to 5229 KiB288 ms
Back after 160 changes, caught up and following Watch741 KiB463 ms
A page of the inbox's conversations, with no copy16.4 KiB11 ms
A page of All mail's conversations, with no copy16.7 KiB14 ms

Measured 2026-12-04 on NVMe with a warm server, on a machine that was busy with other builds, so the times show the shape of each scenario more than they are a benchmark. The core was timed in a test; what an app takes to draw is not in it. Of the 463 ms, 286 are the core opening its own copy before it asks the server anything. Before ListConversations a page was 3 calls and 35 KiB.

Reading

Changing

A refusal says why in a word

A gRPC status has a code and a sentence. One code answers for several things a client does different things about, and the sentence is for a person. So a refusal that a client may decide on carries a word beside them, in the metadata tern-reason: not-found, too-large, over-quota, too-many, changed, busy and so on. The words are part of the API as its fields are, and none changes its meaning.

Old and new together

A client may be newer than its server. A call the server does not have answers UNIMPLEMENTED, and the proto file says for each newer call what gives the same: ListMessages and a Query for ListConversations, a GetMessage each for GetMessages. Replies are compressed for the callers that ask for it.

What a call costs

The cost of a request is part of the contract, so it is measured in what a spinning disk charges for: seeks. With nothing cached, on the 403,757-message mailbox as first delivered, a store of 2 index runs and 7 message runs:

RequestSeeks, averageWhat it reads
Folders with counts and the state2the user's first rows
A page of a folder, newest 504.0listing rows and the flags
A message's structure and text1the message's row
A thread5.3
Changes, the last 50 messages4

Counted 2026-11-26. Since then, rows that lie close together are read together, which these counts do not show.

And through the server, warm, on NVMe, one request at a time, 300 of each (100 of the query):

RequestMedian99th percentile
Folders with counts0.23 ms0.39 ms
A message with its structure and text0.32 ms0.53 ms
A page of a folder, newest 503.64 ms5.55 ms
A page of All mail, somewhere in the past3.75 ms5.95 ms
The thread of a message3.97 ms7.59 ms
Changes since the state before last5.22 ms9.57 ms
A query: a subject word in all folders, 50 results5.9 ms11.3 ms

Measured 2026-11-26. The first run of these had every request at 41 ms, the folder list included: replies were waiting for the caller's delayed acknowledgement. The server now sends without delay.

Trying it

Any gRPC tool that reads a proto file can call the API. At the services' door of a development server, with grpcurl:

Folders with their counts
grpcurl -plaintext \
  -import-path crates/mailapi/proto -proto mail/v1/mail.proto \
  -H "authorization: Bearer $(cat token)" \
  -d '{"user": {"address": "ann@example.org"}}' \
  127.0.0.1:2500 mail.v1.Mail/ListFolders
A query, streamed: the columns, the rows, then what it read
grpcurl -plaintext \
  -import-path crates/mailapi/proto -proto mail/v1/mail.proto \
  -H "authorization: Bearer $(cat token)" \
  -d '{"user": {"address": "ann@example.org"},
       "query": "is:unread | stats by (folder) count() as unread"}' \
  127.0.0.1:2500 mail.v1.Mail/Query

Back: Tern in one pageNext: storage