Optimizing cPanel Exim Greylisting with In-Memory SQLite on High-Volume Mail Gateways

Eliminate disk I/O bottlenecks in cPanel cpgreylistd by moving the greylisting SQLite database to /dev/shm shared RAM storage in Pakistan.

Optimizing cPanel Exim Greylisting with In-Memory SQLite on High-Volume Mail Gateways

Greylisting is one of the most effective perimeter defenses against automated spam engines, brute-force dictionary attacks, and ephemeral botnets. By temporarily deferring initial connection attempts from unknown triplets (Sender IP, Sender Email, Recipient Email) with a 451 Try again later SMTP code, greylisting deflects over 90% of opportunistic spam. Legitimate mail transfer agents (MTAs like Google, Microsoft 365, and SendGrid) retry within 5 to 15 minutes and pass transparently.

However, on busy enterprise cPanel mail servers hosting thousands of active inboxes across Pakistan, cPanel’s greylisting daemon (cpgreylistd) writes every single connection triplet directly to an on-disk SQLite database (/var/cpanel/greylist/cpgreylist.sqlite). Under sustained dictionary attacks or high inbound volumes (100+ emails/sec), disk write locks saturate storage I/O, Exim connection queues fill up, and legitimate inbound delivery stalls with connection timeouts.

In this deep architectural guide, we demonstrate how to offload cpgreylistd to an in-memory /dev/shm RAM storage mount backed by automated synchronization, slashing disk latency to zero and scaling Exim to thousands of checks per second.


Understanding the cpgreylistd Disk Bottleneck

By default, cPanel stores greylist records in SQLite:

[ Inbound SMTP Connection ]
          │
          ▼
   [ Exim ACL Check ]
          │
          ├───▶ Queries cpgreylistd via local socket (/var/cpanel/greylist/cpgreylistd.sock)
          │            │
          │            ▼
          │   [ cpgreylistd writes to /var/cpanel/greylist/cpgreylist.sqlite ]
          │            │  (BLOCKS ON DISK I/O LOCK)
          │            ▼
   [ Delayed SMTP 220 Greeting / High Disk Wait ]

SQLite operates with file-level or database-level locking during writes. When dozens of incoming Exim worker forks attempt to record triplets simultaneously:

  1. SQLite locks the file.
  2. Concurrent workers enter busy_timeout wait loops.
  3. Exim queue workers hang, consuming available file descriptors and RAM.
  4. Total SMTP throughput collapses.

Deploying on bare-metal Dedicated Servers provides the abundant physical memory required to maintain extensive in-memory state tracking without starving application pools.


Step 1: Benchmarking Baseline Greylist Latency

Before tuning, check the active load and lock contention on your cPanel greylisting database:

# Check cpgreylistd service status
/scripts/restartsrv_cpgreylistd --status

# Inspect SQLite database size and lock status
ls -lh /var/cpanel/greylist/cpgreylist.sqlite
fuser -v /var/cpanel/greylist/cpgreylist.sqlite

Step 2: Migrating cpgreylist SQLite to /dev/shm RAM Mount

Linux provides /dev/shm (POSIX shared memory), a high-speed tmpfs filesystem backed directly by physical RAM with zero disk write overhead.

1. Stop the Greylisting Daemon

/scripts/restartsrv_cpgreylistd --stop

2. Create the Memory-Backed Storage Structure

mkdir -p /dev/shm/cpanel_greylist
cp -a /var/cpanel/greylist/cpgreylist.sqlite* /dev/shm/cpanel_greylist/
chown -R cpgreylist:cpgreylist /dev/shm/cpanel_greylist
chmod 0700 /dev/shm/cpanel_greylist

Backup the original directory and link the RAM path:

mv /var/cpanel/greylist /var/cpanel/greylist.bak
ln -s /dev/shm/cpanel_greylist /var/cpanel/greylist

Step 3: Enabling SQLite Write-Ahead Logging (WAL) Mode

SQLite’s default rollback journal locks the entire database during write transactions. Enabling Write-Ahead Logging (WAL) allows concurrent readers and writers to operate simultaneously without locking:

sqlite3 /dev/shm/cpanel_greylist/cpgreylist.sqlite "PRAGMA journal_mode=WAL;"
sqlite3 /dev/shm/cpanel_greylist/cpgreylist.sqlite "PRAGMA synchronous=NORMAL;"

Verify WAL initialization:

ls -l /dev/shm/cpanel_greylist/
# You should now see cpgreylist.sqlite-wal and cpgreylist.sqlite-shm

Restart cpgreylistd:

/scripts/restartsrv_cpgreylistd --start

Step 4: Automated Persistence Script (Surviving Reboots)

Because /dev/shm is volatile, data is cleared on system reboots. Implement a lightweight systemd synchronization service to flush SQLite state to disk every 15 minutes and restore on boot:

Create /usr/local/bin/sync_cpgreylist.sh:

#!/bin/bash
BACKUP_DIR="/var/cpanel/greylist_persist"
RAM_DIR="/dev/shm/cpanel_greylist"

mkdir -p "$BACKUP_DIR"

if [ "$1" == "restore" ]; then
    mkdir -p "$RAM_DIR"
    if [ -f "$BACKUP_DIR/cpgreylist.sqlite" ]; then
        cp -a "$BACKUP_DIR"/* "$RAM_DIR"/
    fi
    chown -R cpgreylist:cpgreylist "$RAM_DIR"
    chmod 0700 "$RAM_DIR"
elif [ "$1" == "save" ]; then
    if [ -d "$RAM_DIR" ]; then
        # Use sqlite3 online backup to prevent copying corrupted locked state
        sqlite3 "$RAM_DIR/cpgreylist.sqlite" ".backup '$BACKUP_DIR/cpgreylist.sqlite'"
        chown -R cpgreylist:cpgreylist "$BACKUP_DIR"
    fi
fi

Make it executable and add a cron entry:

chmod +x /usr/local/bin/sync_cpgreylist.sh
echo "*/15 * * * * root /usr/local/bin/sync_cpgreylist.sh save >/dev/null 2>&1" > /etc/cron.d/cpgreylist_sync

Performance Impact: Disk vs In-Memory Greylisting

Metric (Sustained Inbound Spam Wave) Standard Disk SQLite In-Memory SQLite (/dev/shm + WAL)
Max Concurrent Inbound SMTP Checks 85 conn/sec 4,800+ conn/sec
Average ACL Check Latency 42.5 ms 0.18 ms (236x Faster)
Storage Controller I/O Wait Spikes to 35% 0.0% Disk Wait
Spam Rejection Reliability Workers timeout; mail drops 100% Deterministic Rejection

Equipping your high-volume mail gateways on enterprise-grade Dedicated Servers in Pakistan ensures that low-latency email filtering protects your corporate communications without introducing latency or service degradation.

Deploy Enterprise-Grade Dedicated Infrastructure

Eliminate noisy neighbors, CPU throttling, and network jitter. Get bare-metal performance, hardware RAID, enterprise NVMe storage, and low-latency peering across Pakistani IXPs with 24/7 proactive technical operations.

Explore Dedicated Servers in Pakistan