1<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "http://www.w3.org/TR/html4/loose.dtd"> 3 4<html> 5 6<head> 7 8<title>Postfix Connection Cache </title> 9 10<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 11<link rel='stylesheet' type='text/css' href='postfix-doc.css'> 12 13</head> 14 15<body> 16 17<h1><img src="postfix-logo.jpg" width="203" height="98" ALT="">Postfix Connection Cache </h1> 18 19<hr> 20 21<h2>Introduction</h2> 22 23<p> This document describes the Postfix connection cache implementation, 24which is available with Postfix version 2.2 and later. </p> 25 26<p> Topics covered in this document: </p> 27 28<ul> 29 30<li><a href="#summary"> What SMTP connection caching can do for you</a> 31 32<li><a href="#implementation"> Connection cache implementation</a> 33 34<li><a href="#configuration"> Connection cache configuration</a> 35 36<li><a href="#safety">Connection cache safety mechanisms </a> 37 38<li><a href="#limitations">Connection cache limitations</a> 39 40<li><a href="#statistics">Connection cache statistics</a> 41 42</ul> 43 44<h2><a name="summary">What SMTP connection caching can do for 45you</a></h2> 46 47<p> With SMTP connection caching, Postfix can deliver multiple 48messages over the same SMTP connection. By default, Postfix 2.2 49reuses a plaintext SMTP connection automatically when a destination has 50high volume of mail in the active queue. </p> 51 52<p> SMTP Connection caching is a performance feature. Whether or not 53it actually improves performance depends on the conditions: </p> 54 55<ul> 56 57<li> <p> SMTP Connection caching can greatly improve performance 58when delivering mail to a destination with multiple mail servers, 59because it can help Postfix to skip over a non-responding server. 60</p> 61 62<li> <p> SMTP Connection caching can also help with receivers that 63impose rate limits on new connections. </p> 64 65<li> <p> Otherwise, the benefits of SMTP connection caching are 66minor: it eliminates the latency of the TCP handshake (SYN, SYN+ACK, 67ACK), plus the latency of the SMTP initial handshake (220 greeting, 68EHLO command, EHLO response). With TLS-encrypted connections, this 69can save an additional two roundtrips that would otherwise be needed 70to send STARTTLS and to resume a TLS session. </p> 71 72<li> <p> SMTP Connection caching gives no gains with respect to 73SMTP session tear-down. The Postfix smtp(8) client normally does 74not wait for the server's reply to the QUIT command, and it never 75waits for the TCP final handshake to complete. </p> 76 77<li> <p> SMTP Connection caching introduces some overhead: the 78client needs to send an RSET command to find out if a connection 79is still usable, before it can send the next MAIL FROM command. 80This introduces one additional round-trip delay. </p> 81 82</ul> 83 84<p> For other potential issues with SMTP connection caching, see 85the discussion of <a href="#limitations">limitations</a> at the end 86of this document. </p> 87 88<h2><a name="implementation">Connection cache implementation</a></h2> 89 90<p> For an overview of how Postfix delivers mail, see the Postfix 91architecture OVERVIEW document. </p> 92 93<p> The Postfix connection cache is shared among Postfix mail 94delivering processes. This maximizes the opportunity to reuse an 95open connection. Some MTAs such as Sendmail have a 96non-shared connection cache. Here, a connection can be reused only 97by the mail delivering process that creates the connection. To get 98the same performance improvement as with a shared connection cache, 99non-shared connections need to be kept open for a longer time. </p> 100 101<p> The scache(8) server, introduced with Postfix version 2.2, 102maintains the shared connection cache. With Postfix version 2.2, 103only the smtp(8) client has support to access this cache. </p> 104 105<p> When SMTP connection caching is enabled (see next section), the 106smtp(8) client does not disconnect after a mail transaction, but 107gives the connection to the scache(8) server which keeps the 108connection open for a limited amount of time. </p> 109 110<p> After handing over the open connection to the scache(8) server, 111the smtp(8) client continues with some other mail delivery request. 112Meanwhile, any smtp(8) client process can ask the scache(8) server 113for that cached connection and reuse it for mail delivery. </p> 114 115<blockquote> 116 117<table> 118 119<tr> <td> </td> <td> <tt> /-- </tt> </td> <td align="center" 120colspan="3" bgcolor="#f0f0ff"> smtp(8) </td> <td colspan="2"> <tt> 121--> </tt> </td> <td> Internet </td> </tr> 122 123<tr> <td align="center" bgcolor="#f0f0ff"> qmgr(8) </td> <td> </td> 124<td align="center" rowspan="3"><tt>|<br>|<br>|<br>|<br>v</tt></td> 125</tr> 126 127<tr> <td> </td> <td> <tt> \-- </tt> </td> <td align="center" 128colspan="4" bgcolor="#f0f0ff"> smtp(8) </td> <td align="left"> 129 </td> </tr> 130 131<tr> <td colspan="2"> </td> <td> </td> <td 132align="center"><tt>^<br>|</tt></td> </tr> 133 134<tr> <td colspan="2"> </td> <td align="center" colspan="3" 135bgcolor="#f0f0ff"> scache(8) </td> </tr> 136 137</table> 138 139</blockquote> 140 141<p> With TLS connection reuse (Postfix 3.4 and later), the Postfix 142smtp(8) client connects to a remote SMTP server and sends plaintext 143EHLO and STARTTLS commands, then inserts a tlsproxy(8) process into 144the connection as shown below. </p> 145 146<p> After delivering mail, the smtp(8) client hands over the open 147smtp(8)-to-tlsproxy(8) connection to the scache(8) server, and 148continues with some other mail delivery request. Meanwhile, any 149smtp(8) client process can ask the scache(8) server for that cached 150connection and reuse it for mail delivery. </p> 151 152<blockquote> 153 154<table> 155 156<tr> <td> </td> <td> <tt> /-- </tt> </td> <td align="center" 157colspan="3" bgcolor="#f0f0ff"> smtp(8) </td> <td colspan="2"> <tt> 158--> </tt> </td> <td align="center"bgcolor="#f0f0ff"> tlsproxy(8) 159</td> <td> <tt> --> </tt> </td> <td> Internet </td> </tr> 160 161<tr> <td align="center" bgcolor="#f0f0ff"> qmgr(8) </td> <td> </td> 162<td align="center" rowspan="3"><tt>|<br>|<br>|<br>|<br>v</tt></td> 163</tr> 164 165<tr> <td> </td> <td> <tt> \-- </tt> </td> <td align="center" 166colspan="4" bgcolor="#f0f0ff"> smtp(8) </td> <td align="left"> 167 </td> </tr> 168 169<tr> <td colspan="2"> </td> <td> </td> <td 170align="center"><tt>^<br>|</tt></td> </tr> 171 172<tr> <td colspan="2"> </td> <td align="center" colspan="3" 173bgcolor="#f0f0ff"> scache(8) </td> </tr> 174 175</table> 176 177</blockquote> 178 179<p> The connection cache can be searched by destination domain name 180(the right-hand side of the recipient address) and by the IP address 181of the host at the other end of the connection. This allows Postfix 182to reuse a connection even when the remote host is a mail server for 183domains with different names. </p> 184 185<h2><a name="configuration">Connection cache configuration </a></h2> 186 187<p> The Postfix smtp(8) client supports two connection caching 188strategies: </p> 189 190<ul> 191 192<li> <p> On-demand connection caching. This is enabled by default, 193and is controlled with the smtp_connection_cache_on_demand configuration 194parameter. When this feature is enabled, the Postfix smtp(8) client 195automatically saves a connection to the connection cache when a 196destination has a high volume of mail in the active queue. </p> 197 198<p> Example: </p> 199 200<blockquote> 201 202<pre> 203/etc/postfix/main.cf: 204 smtp_connection_cache_on_demand = yes 205</pre> 206 207</blockquote> 208 209<li> <p> Per-destination connection caching. This is enabled by 210explicitly listing specific destinations with the 211smtp_connection_cache_destinations configuration parameter. After 212completing delivery to a selected destination, the Postfix smtp(8) 213client <i>always</i> saves the connection to the connection cache. 214</p> 215 216<p> Specify a comma or white space separated list of destinations 217or pseudo-destinations: </p> 218 219<ul> 220 221<li> <p> if mail is sent without a relay host: a domain name (the 222right-hand side of an email address, without the [] around a numeric 223IP address), </p> 224 225<li> <p> if mail is sent via a relay host: a relay host name (without 226the [] or non-default TCP port), as specified in main.cf or in the 227transport map, </p> 228 229<li> <p> a /file/name with domain names and/or relay host names as 230defined above, </p> 231 232<li> <p> a "type:table" with domain names and/or relay host names 233on the left-hand side. The right-hand side result from "type:table" 234lookups is ignored. </p> 235 236</ul> 237 238<p> Examples: </p> 239 240<blockquote> 241 242<pre> 243/etc/postfix/main.cf: 244 smtp_connection_cache_destinations = $relayhost 245 smtp_connection_cache_destinations = hotmail.com, ... 246 smtp_connection_cache_destinations = static:all (<i>not recommended</i>) 247</pre> 248 249</blockquote> 250 251<p> See <a href="TLS_README.html#client_tls_reuse">Client-side TLS 252connection reuse</a> to enable multiple deliveries over a TLS-encrypted 253connection (Postfix version 3.4 and later). </p> 254 255</ul> 256 257<h2><a name="safety">Connection cache safety mechanisms </a></h2> 258 259<p> Connection caching must be used wisely. It is anti-social to 260keep an unused SMTP connection open for a significant amount of 261time, and it is unwise to send huge numbers of messages through 262the same connection. In order to avoid problems with SMTP connection 263caching, Postfix implements the following safety mechanisms: </p> 264 265<ul> 266 267<li> <p> The Postfix scache(8) server keeps a connection open for 268only a limited time. The time limit is specified with the 269smtp_connection_cache_time_limit and with the connection_cache_ttl_limit 270configuration parameters. This prevents anti-social behavior. </p> 271 272<li> <p> The Postfix smtp(8) client reuses a session for only a 273limited number of times. This avoids triggering bugs in implementations 274that do not correctly handle multiple deliveries per session. </p> 275 276<p> As of Postfix 2.3 connection reuse is preferably limited with 277the smtp_connection_reuse_time_limit parameter. In addition, Postfix 2782.11 provides smtp_connection_reuse_count_limit to limit how many 279times a connection may be reused, but this feature is unsafe as it 280introduces a "fatal attractor" failure mode (when a destination has 281multiple inbound MTAs, the slowest inbound MTA will attract most 282connections from Postfix to that destination). </p> 283 284<p> Postfix 2.3 logs the use count of multiply-used connections, 285as shown in the following example: </p> 286 287<blockquote> 288<pre> 289Nov 3 16:04:31 myname postfix/smtp[30840]: 19B6B2900FE: 290to=<wietse@test.example.com>, orig_to=<wietse@test>, 291relay=mail.example.com[1.2.3.4], <b>conn_use=2</b>, delay=0.22, 292delays=0.04/0.01/0.05/0.1, dsn=2.0.0, status=sent (250 2.0.0 Ok) 293</pre> 294</blockquote> 295 296<li> <p> The connection cache explicitly labels each cached connection 297with destination domain and IP address information. A connection 298cache lookup succeeds only when the correct information is specified. 299This prevents mis-delivery of mail. </p> 300 301</ul> 302 303<h2><a name="limitations">Connection cache limitations</a></h2> 304 305<p> Postfix SMTP connection caching conflicts with certain applications: 306</p> 307 308<ul> 309 310<li> <p> With Postfix versions < 3.4, the Postfix shared connection 311cache cannot be used with TLS, because an open TLS connection can 312be reused only in the process that creates it. For this reason, 313the Postfix smtp(8) client historically always closed the connection 314after completing an attempt to deliver mail over TLS.</p> 315 316<li> <p> Postfix connection caching currently does not support 317multiple SASL accounts per mail server. Specifically, Postfix 318connection caching assumes that a SASL credential is valid for all 319hostnames or domain names that deliver via the same mail server IP 320address and TCP port, and assumes that the SASL credential does not 321depend on the message originator. </p> 322 323</ul> 324 325 326<h2><a name="statistics">Connection cache statistics </a></h2> 327 328<p> The scache(8) connection cache server logs statistics about the 329peak cache size and the cache hit rates. This information is logged 330every connection_cache_status_update_time seconds, when the process 331terminates after the maximal idle time is exceeded, or when Postfix 332is reloaded. </p> 333 334<ul> 335 336<li> <p> Hit rates for connection cache lookups by domain will tell 337you how useful connection caching is. </p> 338 339<li> <p> Connection cache lookups by network address will always 340fail, unless you're sending mail to different domains that share 341the same MX hosts. </p> 342 343<li> <p> No statistics are logged when no attempts are made to 344access the connection cache. </p> 345 346</ul> 347 348 349</body> 350 351</html> 352