Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2811|回复: 1
打印 上一主题 下一主题

Mbox vs Maildir 两者的原理和区别

[复制链接]

388

主题

602

帖子

2218

积分

金牌会员

Rank: 6Rank: 6

积分
2218
跳转到指定楼层
楼主
发表于 2016-3-13 22:24:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

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

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

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

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

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

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/2GMbox应付那么大的容量已力不从心,无疑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/


回复

使用道具 举报

388

主题

602

帖子

2218

积分

金牌会员

Rank: 6Rank: 6

积分
2218
沙发
 楼主| 发表于 2016-3-13 22:24:20 | 只看该作者
MaildirMailbox 的区别
在开放源代码的世界里,电子邮件存储格式最流行的有两种,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操作的效能问题。
原理:
The Mbox Format
This is the traditional way of storing mail messages in the Unix world. In this format, a regular text file which serves as the mail user’s mailbox file is created.
Fig. 1: Mbox storage format
How Mbox works
Receiving and storing a mail
  • Lock the mailbox.
  • Append the header (usually “From [sender's email address] [date and time received]“) and the mail into the mailbox file.
  • Unlock the mailbox.
Retrieving a mail
  • Lock the mailbox.
  • Locate and read the mail.
  • Update the mail status flag.
  • Unlock the mailbox.
Deleting a mail
  • Lock the mailbox.
  • Move the contents of the mailbox, beginning from the position right after the mail to be deleted until the end of the mailbox, into the position of the mail to be deleted.
  • Reduce the size of the mailbox file by the size of the deleted mail.
  • Unlock the mailbox.
Searching a mail
  • Lock the mailbox.
  • Search the mailbox.
  • Unlock the mailbox.
Advantages
  • Format is universally supported.
  • Appending a new mail into the mailbox file is fast.
  • Searching text inside a single mailbox file is fast.
Disadvantages
  • Has file locking problems.
  • Has problems when used with network file systems.
  • Format is prone to corruption.
The Maildir Format
This is a new way of storing mail messages. In this format, a directory usually named Maildir is created for each mail user. Under this directory are three more directories named new, cur and tmp.
Fig. 2: Maildir storage format
How Maildir works
Receiving and storing a mail
  • Create a unique file in the tmp directory.
  • Write the mail into the newly created file.
  • Move the completely written mail into the new directory.
Retrieving a mail
  • Locate and read the mail.
  • Move the mail from new into the cur directory and append the mail status flag into the filename.
Deleting a mail
  • Delete the file containing the mail.
Searching a mail
  • Search each and every mail file.
Advantages
  • Locating, retrieving and deleting a specific mail is fast.
  • Minimal to no file locking needed.
  • Can be used on network file system.
  • Immune to mailbox corruption (assuming the hardware will not fail).
Disadvantages
  • Some filesystems may not efficiently handle a large number of small files.
  • Searching text, which requires all mail files to be opened is slow.
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/

参考文档:
http://www.daxigua.com/archives/117
http://www.linuxmail.info/mbox-maildir-mail-storage-formats/

Maildir 格式
Maildir邮箱格式不用于mbox之处,在于它使用目录结构来存贮邮件。Maildir的设计愿意是为了解决mbox格式的可靠性与文件锁定问题。例如:如果在邮件内容还没有完全写入mbox文件之前,系统就死机了,这时候可能只有部分内容在邮箱里。当系统恢复运行,MDA将邮件写入邮箱时,新的内容会接在前次残缺的内容的后面,造成问题。
Mbox格式的另一个缺点,是发生在POP/IMAP server 与 SMTP server 试图同时开启同一个邮箱时。如果双方没有使用相同的锁定机制,邮箱文件可能因此受损。保护文件的锁定机制有好几种,但是并非所有邮件程序都使用锁定机制。如果使用maildir格式,则可以不使用文件保护锁,因为每一封邮件都是存放在单独的一个文件里。不同的邮件程序,不能同时访问同一个文件。

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-3 04:16 , Processed in 0.060953 second(s), 18 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表