Message-cache warming slows down auth for large mailboxes

Issue Description

We migrated mailboxes that have 10-100k emails in them. I connected via openssl and logged in and it just hangs. After a few minutes, I finally get the response.

openssl s_client -connect mail.example.com:993 -crlf


OK [CAPABILITY IMAP4rev2 IMAP4rev1 ENABLE SASL-IR LITERAL+ ID UTF8=ACCEPT AUTH=PLAIN AUTH=OAUTHBEARER AUTH=XOAUTH2] Stalwart IMAP4rev2 at your service.
a CAPABILITY
CAPABILITY IMAP4rev2 IMAP4rev1 ENABLE SASL-IR LITERAL+ ID UTF8=ACCEPT AUTH=PLAIN AUTH=OAUTHBEARER AUTH=XOAUTH2
a OK CAPABILITY completed
b LOGIN [email protected] password
(hangs for minutes)
b OK [CAPABILITY IMAP4rev2 IMAP4rev1 ENABLE SASL-IR LITERAL+ ID UTF8=ACCEPT JMAPACCESS IDLE NAMESPACE CHILDREN MULTIAPPEND BINARY UNSELECT ACL UIDPLUS ESEARCH WITHIN SEARCHRES SORT THREAD=REFERENCES LIST-EXTENDED LIST-STATUS ESORT SORT=DISPLAY SPECIAL-USE CREATE-SPECIAL-USE MOVE CONDSTORE QRESYNC UNAUTHENTICATE STATUS=SIZE OBJECTID+ PREVIEW RIGHTS=texk QUOTA QUOTA=RES-STORAGE] Authentication successful

Expected Behavior

The login should take less than 3 seconds.

Actual Behavior

The login takes minutes.

Reproduction Steps

  1. Have an inbox with 100k emails in it.
  2. Try logging in.

Stalwart Version

v0.16.x

Installation Method

Binary (Linux)

Database Backend

PostgreSQL

Blob Storage

S3-compatible

Search Engine

Elasticsearch

Directory Backend

Internal

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

Please set the log level to trace and check the logs to see what is taking all that time, data store and blob operations include the number of milliseconds they take to fetch.
Also check your PostgreSQL for any slow query reports.