4月 072010
 

在开放源代码的世界里,电子邮件服务器最主流的目前有三种,分别是sendmail, qmail, postfix。而存储格式最流行的有两种,Mbox和Maildir,它们都是开放的存储格式,因此兼容性比较好。

而mbox和maildir的历史,却不为大多数用户所了解。今天和一个朋友聊到mbox和maildir哪个好,顺便搜集一下mbox和maildir的资料,谈谈这两者的原理和区别,以及应该如何做选择。

Mbox的历史较Maildir悠久,sendmail支持mbox,qmail和postfix都支持mbox,其主要特点就是“所有邮件都存放到一个文件里”。每个邮件之间以特定的标记分割。

而Maildir则相反,每一封邮件保存成一个文件,每个文件名称一般有一定的规律,例如会包含时间戳、pid及inode节点号等。

除了mbox和maildir格式外,还有一个叫mbx的格式,它是对mbox的改进版本。主要用在UW-IMAP server里。它最大的特点是有一个针对mbox文件的索引,能改善读/写性能。但依然需要file lock。

随着qmail/postfix的普及,以及mbox的一些问题暴露,maildir得到了越来越多的应用。其中mbox的最主要问题是文件缩定(file lock),其次是大多数update操作的效能问题。

以下是引自courier-mta.org上的mbox和maildir的测试对比文章,介绍了mbox和maildir:

mbox mail storage format

This is the traditional way to store mail on UNIX-based mail servers. Individual messages are simply concatenated together, and saved in a single file. A special marker is placed where one message ends and the next message begins. Only one process can access the mbox file in read/write mode. Concurrent access requires a locking mechanism. Anytime someone needs to update the mbox file, everyone else must wait for the update to complete.

maildir mail storage format

Maildirs were originally implemented in the Qmail mail server, supposedly to address the inadequacies of mbox files. Individual messages are saved in separate files, one file per message. There is a defined method for naming each file. There's a defined procedure for adding new messages to the maildir. No locking is required. Multiple processes can use maildirs at the same time.

mbx mail storage format

This is a slightly modified version of the original mbox format that's offered by the UW-IMAP server. mbx mailboxes still require locking. The main difference from the mbox format is that each message in the file is preceded by a record that carries some message-specific metadata. As such, certain operations that used to require the entire mbox file to be rewritten can now be implemented by updating the fixed-size header record.

This benchmark focuses mainly on the mbox and maildirs formats. In March of 2003 an unrelated party conducted a similar benchmark for mbx formats. See http://www.decisionsoft.com/pdw/mailbench.html

for more details.

Mbox vs Maildir之优缺点比较

这里给出一个基本的特性对比,读者很容易就清楚根据自己的应用到底应该选什么存储格式:

可靠性

优选是Maildir,因为mbox只有一个文件,一旦出问题之后,所有邮件都将损毁。

更新速度

这里主要指的是删除/增加邮件的能力,无疑Maildir完胜Mbox

搜索速度

这点Mbox因为是单文件,因此搜索的能力要强于maildir

并发访问能力

对于繁忙的邮件系统,多个进程同时访问同一封邮件是可能的事情,Mbox需要flock()的支持,而且如果某一个进程操作时间长,则其他所有进程都堵

塞了。Maildir没有这个问题。在NFS等网络文件系统上,Maildir相对安全,Mbox不能用于此类型环境

扩充能力

现在的邮箱已经不是5年前甚至10年前的1MB,2MB而是100,200甚至1G/2G,Mbox应付那么大的容量已力不从心,无疑Maildir是比较适合的。

文件系统依赖

Maildir较依赖文件系统,尤其是依赖对目录的索引能力,用ReiserFS会比较快,对于超大型的maildir,读写性能将受到考验。相对而言Mbox则不存在这个问题。

综合结论

推荐使用Maildir格式,安全可靠,绝大部分操作都远快于Mbox。而且现今支持Maildir的软件越来越多,qmail/Postfix 都支持。

一些有用的链接

原理标准

http://cr.yp.to/proto/maildir.html
http://www.qmail.org/qmail-manual-html/man5/mbox.html

Benchmark/比较

http://www.courier-mta.org/mbox-vs-maildir/
http://www.decisionsoft.com/pdw/mailbench.html

转换工具

http://www.qmail.org/mbox2maildir
http://batleth.sapienti-sat.org/projects/mb2md/

 回复

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>