Quality IMAP, Part 2: Mailbox Concurrency

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Quality IMAP, Part 2: Mailbox Concurrency

Quality IMAP, Part 2: Mailbox Concurrency Published on November 23, 2026 15 min reading time 2969 words In the past year, we have been significantly improving the reliability and correctness of Aerogramme’s IMAP implementation. This is the second part in a series discussing why implementing IMAP correctly is not so easy, and presenting our work on making Aerogramme’s IMAP code as high-quality as we can. Today we focus on challenges that arise when considering concurrent access to a mailbox.

高质量 IMAP,第二部分:邮箱并发。发布于 2026 年 11 月 23 日,阅读时长 15 分钟,共 2969 字。在过去的一年中,我们显著提升了 Aerogramme IMAP 实现的可靠性和正确性。这是该系列文章的第二部分,旨在探讨为何正确实现 IMAP 并非易事,并展示我们为使 Aerogramme 的 IMAP 代码达到尽可能高质量所做的工作。今天,我们将重点讨论在考虑邮箱并发访问时所面临的挑战。

Aerogramme is an experimental email server (& calendar, contacts), designed to be geo-distributed, hosted outside of datacenters, on refurbished hardware, and reliable even in presence of network issues and hardware failures. It is built on top of Garage and shares the same philosophy. Aerogramme is AI-free: we do not wish to use unethical technology and prefer to spend effort on careful craftmanship and community-building.

Aerogramme 是一个实验性的电子邮件服务器(及日历、联系人),旨在实现地理分布式部署,托管在数据中心之外的翻新硬件上,即使在网络问题和硬件故障的情况下也能保持可靠。它构建于 Garage 之上,并秉持相同的理念。Aerogramme 不含人工智能:我们不希望使用不道德的技术,更倾向于将精力投入到精湛的工艺和社区建设中。

This is the second post in a series in which we argue that implementing an IMAP server is not easy, essentially due to two classes of features that are fundamentally challenging to implement. In the previous post, we talked about the first class of features: fine-grained retrieval and querying of email contents. In this post, we talk about the second class of features that make implementing an IMAP server tricky: concurrent access to user mailboxes.

这是该系列文章的第二篇,我们认为实现 IMAP 服务器并不容易,这主要是因为有两类功能在实现上具有根本性的挑战。在上一篇文章中,我们讨论了第一类功能:电子邮件内容的细粒度检索与查询。在本文中,我们将讨论使 IMAP 服务器实现变得棘手的第二类功能:用户邮箱的并发访问。

Concurrent and distributed software are famously hard to get right; concurrency bugs are known to be hard to find. In our recent work on refining Aerogramme we have tried to approach the problem from different angles. This is the topic of this post: the different ways in which we have improved the correctness of Aerogramme in concurrent settings. This work was funded by NLNet under the “Aerogramme 1.0” grant, and could not have been possible without their support. Feel free to join the Aerogramme matrix channel for updates or if you just want to chat!

并发和分布式软件以难以正确实现而闻名;并发错误通常很难被发现。在我们近期完善 Aerogramme 的工作中,我们尝试从不同角度解决这一问题。这正是本文的主题:我们通过多种方式提升了 Aerogramme 在并发环境下的正确性。这项工作由 NLNet 的“Aerogramme 1.0”资助项目提供支持,没有他们的支持,这一切都不可能实现。欢迎加入 Aerogramme 的 Matrix 频道获取更新,或者仅仅是进行交流!

Concurrent mailbox access in IMAP A core feature of IMAP is that multiple email clients can simultaneously access the same mailbox. For example, we may want to access our emails both from our phone and our computer. Concurrent access to a mailbox can also happen when a single client is involved: some clients (including Thunderbird) open multiple concurrent connections to a same mailbox to speed up processing. In any case, clients accessing the same mailbox should be able to coordinate between themselves and avoid stepping on each other’s toes.

IMAP 中的并发邮箱访问。IMAP 的一个核心特性是多个电子邮件客户端可以同时访问同一个邮箱。例如,我们可能希望同时从手机和电脑访问邮件。当涉及单个客户端时,也可能发生对邮箱的并发访问:一些客户端(包括 Thunderbird)会打开多个并发连接到同一个邮箱以加快处理速度。无论如何,访问同一邮箱的客户端都应能够相互协调,避免相互干扰。

Why must clients be able to coordinate when accessing the same mailbox? Assume that we are an email client that sends two commands to read information from a mailbox. Since another client can modify the mailbox in between the two commands, the information we read may have been invalidated by the other client and is now inconsistent! Clients thus need to be able to detect concurrent changes made by other clients. Similarly, a client that reconnects to the server after being offline needs to fetch the changes that happened while it was disconnected. This is possible thanks to mechanisms defined by the IMAP specification and that specifically exist because concurrent access is allowed.

为什么客户端在访问同一邮箱时必须能够协调?假设我们是一个发送两条命令从邮箱读取信息的电子邮件客户端。由于另一个客户端可以在这两条命令之间修改邮箱,我们读取的信息可能已被对方失效,从而导致不一致!因此,客户端需要能够检测到其他客户端所做的并发更改。同样,在离线后重新连接到服务器的客户端需要获取其断开连接期间发生的更改。这得益于 IMAP 规范中定义的机制,这些机制的存在正是因为允许并发访问。

The overall consistency of the mailbox data must be taken seriously: an IMAP server exposing inconsistent views of a mailbox could result in a confused client deleting the wrong emails, for example. To our best knowledge, concurrent mailbox behaviors are tackled in the following (parts of) specifications: Base IMAP The base IMAP4 specification has few explicit mentions of concurrent mailbox access, but does talk about re-synchronizing disconnected clients, which can be seen as a restricted form of concurrent access. It defines mechanisms for assigning IDs to emails (UIDNEXT/UIDVALIDITY) and associated properties which can be extrapolated to concurrent contexts. This is relevant when concurrently adding new emails to a mailbox. After an email has been added to a mailbox it cannot be modified, so there are no issues with reading emails concurrently. The improvements that we will talk about in this post are related to these aspects of the base IMAP specification.

必须认真对待邮箱数据的整体一致性:例如,如果 IMAP 服务器暴露了邮箱的不一致视图,可能会导致困惑的客户端删除错误的邮件。据我们所知,并发邮箱行为在以下(部分)规范中得到了处理:基础 IMAP。基础 IMAP4 规范对并发邮箱访问的明确提及很少,但确实讨论了断开连接客户端的重新同步,这可以被视为一种受限的并发访问形式。它定义了为电子邮件分配 ID(UIDNEXT/UIDVALIDITY)及相关属性的机制,这些机制可以推演到并发上下文中。这在向邮箱并发添加新邮件时非常重要。邮件一旦添加到邮箱就无法修改,因此并发读取邮件不存在问题。我们将在本文中讨论的改进与基础 IMAP 规范的这些方面有关。

As an additional complication, IMAP defines in fact two (!) email-numbering systems. One of these has the drawback that deleting an email can invalidate email IDs used in concurrent commands. This is purely a quirk of a numbering system that can nowadays be considered legacy, but must still be supported. This issue is not acknowledged in the base IMAP specification, leaving a class of problematic behaviors completely unspecified, but is later addressed in an informational RFC which specifies what to do in such cases. Aerogramme’s latest release includes changes and bugfixes to make sure that we follow the recommendations of this informational RFC, but we won’t enter into the details of these legacy quirks in this post.

作为一个额外的复杂因素,IMAP 实际上定义了两种(!)电子邮件编号系统。其中一种系统的缺点是删除邮件可能会使并发命令中使用的邮件 ID 失效。这纯粹是编号系统的一种怪癖,虽然在今天可以被视为遗留问题,但仍必须予以支持。基础 IMAP 规范中并未承认这一问题,导致一类有问题的行为完全未被定义,但后来在一份信息性 RFC 中得到了解决,该 RFC 规定了在此类情况下应采取的措施。Aerogramme 的最新版本包含了更改和错误修复,以确保我们遵循该信息性 RFC 的建议,但我们不会在本文中深入探讨这些遗留怪癖的细节。

Extensions Email flags can be modified concurrently, but the base IMAP specification does not specify a mechanism for clients to coordinate around flag changes. This is done in the later CONDSTORE specification that extends IMAP with mechanisms to coordinate flag changes across concurrent connections. Aerogramme has preliminary support for CONDSTORE, but we disabled it in the last release, as we found out it needs a redesign to be nicer to email clients1. A later specification further extends IMAP and CONDSTORE by adding a more general mechanism (QRESYNC) for synchronizing all mailbox changes across concurrent sessions. Aerogramme does not supports QRESYNC at the moment2.

扩展。电子邮件标记可以被并发修改,但基础 IMAP 规范并未指定客户端协调标记更改的机制。这在后来的 CONDSTORE 规范中得到了解决,该规范通过在并发连接间协调标记更改的机制扩展了 IMAP。Aerogramme 对 CONDSTORE 有初步支持,但在上一个版本中我们禁用了它,因为我们发现它需要重新设计以对电子邮件客户端更友好。后来的一个规范通过添加一种更通用的机制(QRESYNC)进一步扩展了 IMAP 和 CONDSTORE,用于在并发会话间同步所有邮箱更改。Aerogramme 目前不支持 QRESYNC。

Context: Concurrency in Aerogramme On top of the complexity carried by the IMAP specification, Aerogramme’s distributed infrastructure comes with its own challenges. Not only we need to think about concurrency at the IMAP protocol level, but also internally in Aerogramme: how do we handle concurrent changes to the server internal data storage? Can we simply take a lock or a transaction over mailbox data each time we need to update it

背景:Aerogramme 中的并发。除了 IMAP 规范带来的复杂性外,Aerogramme 的分布式基础设施也有其自身的挑战。我们不仅需要在 IMAP 协议层面考虑并发,还需要在 Aerogramme 内部考虑:我们如何处理服务器内部数据存储的并发更改?每次需要更新邮箱数据时,我们是否可以直接加锁或进行事务处理?