Optimizing SMTP Server Performance: From 100 to 10,000 TPS
Lessons learned scaling mail servers. Connection pooling, queue management, TLS optimization, and hardware considerations for high-throughput email delivery.
Scaling an SMTP server from 100 transactions per second to 10,000 requires tuning at every layer. Here are the optimizations that made the biggest difference in production.
Connection Pooling
Opening a new TCP connection for every email is wasteful. We maintain persistent SMTP connections to major relays (AWS SES, SendGrid, Mailgun) and reuse them across workers. Connection pooling reduced our per-message latency from 180ms to 45ms.
TLS Session Resumption
TLS handshakes are expensive. Enable session tickets and session caching in your SMTP client. With OpenSSL, set `ssl_session_cache` and `ssl_session_timeout`. This cut our TLS overhead by 60%.
Queue Sharding
A single queue becomes a bottleneck. We shard by recipient domain, creating dedicated queues for Gmail, Outlook, Yahoo, and enterprise domains. Workers pull from queues with the highest priority and lowest current throughput. This prevents one slow domain from backing up the entire pipeline.
Kernel & Network Tuning
On Linux, increase `net.core.somaxconn`, `net.ipv4.tcp_tw_reuse`, and `fs.file-max`. Disable Nagle's algorithm for SMTP sockets with `TCP_NODELAY`. Use `epoll` instead of `select` or `poll` for the event loop.
Hardware Matters
SSD storage for mail queues is essential — spinning disks create I/O bottlenecks under burst loads. For high-volume relays, dedicate a NIC or use SR-IOV to reduce kernel networking overhead.
Performance optimization is about removing bottlenecks one by one. Profile first, then optimize.
Written by Irfan Naseem
Senior Software Engineer at Netcode. Building email infrastructure and scalable systems.