MBOX-Line: From tss at iki.fi  Fri Nov  9 14:16:19 2012
To: imap-protocol@u.washington.edu
From: Timo Sirainen <tss@iki.fi>
Date: Fri Jun  8 12:34:49 2018
Subject: [Imap-protocol] Efficiently handling sequence numbers
In-Reply-To: <CABa8R6tdej9ZrNW6EtBXQwuZupsSKP64Fq9fUhJLtJM4oA7NVQ@mail.gmail.com>
References: <CABa8R6v2-NW2L4itj30juL6CEFUC3v9pQG0D0zvqM04UNiB+vA@mail.gmail.com>
	<E121DCDC-75FA-43FB-87A7-EA0EEEEA13B0@iki.fi>
	<CABa8R6sSdHCK9ziPrwc=avaJiHjWtfVtgyBBs=kxSzSkkWJQCQ@mail.gmail.com>
	<8560FE55-3E60-4FCF-9B1A-FB0F66D04295@iki.fi>
	<CABa8R6tdej9ZrNW6EtBXQwuZupsSKP64Fq9fUhJLtJM4oA7NVQ@mail.gmail.com>
Message-ID: <9B0A7B09-AF1D-41E5-A87E-7BD3A6A05DB2@iki.fi>

On 10.11.2012, at 0.05, Brandon Long wrote:

> > So, do you load the full sequence into memory on SELECT?
> 
> Yes, first read/mmap the index snapshot into memory and then apply any expunges and other changes to it from the log. It could be optimized so that all the expunges are done in one O(n) scan of the index instead of multiple separate of memmove()s, but it hasn't been a real problem so far. I don't have the old/new index separation yet, but I think that would be a good idea to add when the mailbox size grows past maybe 100k messages (or maybe even sooner).
> 
> 
> Ah, ok.  If only loading the data was as simple as an mmap for us ;)  At this point, I think the data only moves through 5 separate servers on 4 machines, usually in the same data center though.  That, and of course we don't use UIDs as our primary message-id, so we're looking at 32 bits for the uid and 64 bits for the message-id, so 96 bits minimum per message, which means a 10M message folder is loading 120MB, probably another 30MB of overhead in protocol buffers (not really meant for millions of entries).

Sure, Dovecot has also similar extra metadata, more or less depending on the mailbox format. Keep the user typically assigned to the same backend server and you need to fetch the 120MB only once (or somewhat rarely). Send the data back in smaller diffs to avoid constantly re-uploading the whole 120MB file when it changes. I've been working on an object storage optimized backend for Dovecot these last few months. I think it could work well for GMail as well ;)

Anyway, like I mentioned several times already :), I think the most important optimization for huge mailboxes is the separation of old/new data. You most likely don't need to even download/read the old data normally. (Although many IMAP clients fetch 1:* flags after SELECT, but even if IMAP server had no problems with that, the IMAP client would probably be unusably slow so that's not a real problem.)


