Thursday, November 5, 2009

Mbox vs Maildir: Mail Storage Formats

SkyHi @ Thursday, November 05, 2009
The Unix world has two ways of storing mail messages, the traditional mbox format and the newer maildir format. Postfix and Dovecot supports the two mail storage format so you can use any format, but I highly recommend you use the maildir format.
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.
Mbox storage format

Fig. 1: Mbox storage format
How Mbox works
Receiving and storing a mail

1. Lock the mailbox.
2. Append the header (usually “From [sender's email address] [date and time received]“) and the mail into the mailbox file.
3. Unlock the mailbox.

Retrieving a mail

1. Lock the mailbox.
2. Locate and read the mail.
3. Update the mail status flag.
4. Unlock the mailbox.

Deleting a mail

1. Lock the mailbox.
2. 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.
3. Reduce the size of the mailbox file by the size of the deleted mail.
4. Unlock the mailbox.

Searching a mail

1. Lock the mailbox.
2. Search the mailbox.
3. 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.
Maildir storage format

Fig. 2: Maildir storage format
How Maildir works
Receiving and storing a mail

1. Create a unique file in the tmp directory.
2. Write the mail into the newly created file.
3. Move the completely written mail into the new directory.

Retrieving a mail

1. Locate and read the mail.
2. Move the mail from new into the cur directory and append the mail status flag into the filename.

Deleting a mail

1. Delete the file containing the mail.

Searching a mail

1. 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.

Visit the forum to ask for help or to give a comment.

Reference: http://www.linuxmail.info/mbox-maildir-mail-storage-formats/