1 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "https://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, 24 which 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 45 you</a></h2> 46 47 <p> With SMTP connection caching, Postfix can deliver multiple 48 messages over the same SMTP connection. By default, Postfix 2.2 49 reuses a plaintext SMTP connection automatically when a destination has 50 high volume of mail in the <a href="QSHAPE_README.html#active_queue">active queue</a>. </p> 51 52 <p> SMTP Connection caching is a performance feature. Whether or not 53 it actually improves performance depends on the conditions: </p> 54 55 <ul> 56 57 <li> <p> SMTP Connection caching can greatly improve performance 58 when delivering mail to a destination with multiple mail servers, 59 because 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 63 impose rate limits on new connections. </p> 64 65 <li> <p> Otherwise, the benefits of SMTP connection caching are 66 minor: it eliminates the latency of the TCP handshake (SYN, SYN+ACK, 67 ACK), plus the latency of the SMTP initial handshake (220 greeting, 68 EHLO command, EHLO response). With TLS-encrypted connections, this 69 can save an additional two roundtrips that would otherwise be needed 70 to send STARTTLS and to resume a TLS session. </p> 71 72 <li> <p> SMTP Connection caching gives no gains with respect to 73 SMTP session tear-down. The Postfix <a href="smtp.8.html">smtp(8)</a> client normally does 74 not wait for the server's reply to the QUIT command, and it never 75 waits for the TCP final handshake to complete. </p> 76 77 <li> <p> SMTP Connection caching introduces some overhead: the 78 client needs to send an RSET command to find out if a connection 79 is still usable, before it can send the next MAIL FROM command. 80 This introduces one additional round-trip delay. </p> 81 82 </ul> 83 84 <p> For other potential issues with SMTP connection caching, see 85 the discussion of <a href="#limitations">limitations</a> at the end 86 of 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 91 architecture <a href="OVERVIEW.html">OVERVIEW</a> document. </p> 92 93 <p> The Postfix connection cache is shared among Postfix mail 94 delivering processes. This maximizes the opportunity to reuse an 95 open connection. Some MTAs such as Sendmail have a 96 non-shared connection cache. Here, a connection can be reused only 97 by the mail delivering process that creates the connection. To get 98 the same performance improvement as with a shared connection cache, 99 non-shared connections need to be kept open for a longer time. </p> 100 101 <p> The <a href="scache.8.html">scache(8)</a> server, introduced with Postfix version 2.2, 102 maintains the shared connection cache. With Postfix version 2.2, 103 only the <a href="smtp.8.html">smtp(8)</a> client has support to access this cache. </p> 104 105 <p> When SMTP connection caching is enabled (see next section), the 106 <a href="smtp.8.html">smtp(8)</a> client does not disconnect after a mail transaction, but 107 gives the connection to the <a href="scache.8.html">scache(8)</a> server which keeps the 108 connection open for a limited amount of time. </p> 109 110 <p> After handing over the open connection to the <a href="scache.8.html">scache(8)</a> server, 111 the <a href="smtp.8.html">smtp(8)</a> client continues with some other mail delivery request. 112 Meanwhile, any <a href="smtp.8.html">smtp(8)</a> client process can ask the <a href="scache.8.html">scache(8)</a> server 113 for 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" 120 colspan="3" bgcolor="#f0f0ff"> <a href="smtp.8.html">smtp(8)</a> </td> <td colspan="2"> <tt> 121 --> </tt> </td> <td> Internet </td> </tr> 122 123 <tr> <td align="center" bgcolor="#f0f0ff"> <a href="qmgr.8.html">qmgr(8)</a> </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" 128 colspan="4" bgcolor="#f0f0ff"> <a href="smtp.8.html">smtp(8)</a> </td> <td align="left"> 129 </td> </tr> 130 131 <tr> <td colspan="2"> </td> <td> </td> <td 132 align="center"><tt>^<br>|</tt></td> </tr> 133 134 <tr> <td colspan="2"> </td> <td align="center" colspan="3" 135 bgcolor="#f0f0ff"> <a href="scache.8.html">scache(8)</a> </td> </tr> 136 137 </table> 138 139 </blockquote> 140 141 <p> With TLS connection reuse (Postfix 3.4 and later), the Postfix 142 <a href="smtp.8.html">smtp(8)</a> client connects to a remote SMTP server and sends plaintext 143 EHLO and STARTTLS commands, then inserts a <a href="tlsproxy.8.html">tlsproxy(8)</a> process into 144 the connection as shown below. </p> 145 146 <p> After delivering mail, the <a href="smtp.8.html">smtp(8)</a> client hands over the open 147 <a href="smtp.8.html">smtp(8)</a>-to-<a href="tlsproxy.8.html">tlsproxy(8)</a> connection to the <a href="scache.8.html">scache(8)</a> server, and 148 continues with some other mail delivery request. Meanwhile, any 149 <a href="smtp.8.html">smtp(8)</a> client process can ask the <a href="scache.8.html">scache(8)</a> server for that cached 150 connection 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" 157 colspan="3" bgcolor="#f0f0ff"> <a href="smtp.8.html">smtp(8)</a> </td> <td colspan="2"> <tt> 158 --> </tt> </td> <td align="center"bgcolor="#f0f0ff"> <a href="tlsproxy.8.html">tlsproxy(8)</a> 159 </td> <td> <tt> --> </tt> </td> <td> Internet </td> </tr> 160 161 <tr> <td align="center" bgcolor="#f0f0ff"> <a href="qmgr.8.html">qmgr(8)</a> </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" 166 colspan="4" bgcolor="#f0f0ff"> <a href="smtp.8.html">smtp(8)</a> </td> <td align="left"> 167 </td> </tr> 168 169 <tr> <td colspan="2"> </td> <td> </td> <td 170 align="center"><tt>^<br>|</tt></td> </tr> 171 172 <tr> <td colspan="2"> </td> <td align="center" colspan="3" 173 bgcolor="#f0f0ff"> <a href="scache.8.html">scache(8)</a> </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 181 of the host at the other end of the connection. This allows Postfix 182 to reuse a connection even when the remote host is a mail server for 183 domains with different names. </p> 184 185 <h2><a name="configuration">Connection cache configuration </a></h2> 186 187 <p> The Postfix <a href="smtp.8.html">smtp(8)</a> client supports two connection caching 188 strategies: </p> 189 190 <ul> 191 192 <li> <p> On-demand connection caching. This is enabled by default, 193 and is controlled with the <a href="postconf.5.html#smtp_connection_cache_on_demand">smtp_connection_cache_on_demand</a> configuration 194 parameter. When this feature is enabled, the Postfix <a href="smtp.8.html">smtp(8)</a> client 195 automatically saves a connection to the connection cache when a 196 destination has a high volume of mail in the <a href="QSHAPE_README.html#active_queue">active queue</a>. </p> 197 198 <p> Example: </p> 199 200 <blockquote> 201 202 <pre> 203 /etc/postfix/<a href="postconf.5.html">main.cf</a>: 204 <a href="postconf.5.html#smtp_connection_cache_on_demand">smtp_connection_cache_on_demand</a> = yes 205 </pre> 206 207 </blockquote> 208 209 <li> <p> Per-destination connection caching. This is enabled by 210 explicitly listing specific destinations with the 211 <a href="postconf.5.html#smtp_connection_cache_destinations">smtp_connection_cache_destinations</a> configuration parameter. After 212 completing delivery to a selected destination, the Postfix <a href="smtp.8.html">smtp(8)</a> 213 client <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 217 or pseudo-destinations: </p> 218 219 <ul> 220 221 <li> <p> if mail is sent without a <a href="postconf.5.html#relayhost">relay host</a>: a domain name (the 222 right-hand side of an email address, without the [] around a numeric 223 IP address), </p> 224 225 <li> <p> if mail is sent via a <a href="postconf.5.html#relayhost">relay host</a>: a <a href="postconf.5.html#relayhost">relay host</a> name (without 226 the [] or non-default TCP port), as specified in <a href="postconf.5.html">main.cf</a> or in the 227 transport map, </p> 228 229 <li> <p> a /file/name with domain names and/or <a href="postconf.5.html#relayhost">relay host</a> names as 230 defined above, </p> 231 232 <li> <p> a "<a href="DATABASE_README.html">type:table</a>" with domain names and/or <a href="postconf.5.html#relayhost">relay host</a> names 233 on the left-hand side. The right-hand side result from "<a href="DATABASE_README.html">type:table</a>" 234 lookups is ignored. </p> 235 236 </ul> 237 238 <p> Examples: </p> 239 240 <blockquote> 241 242 <pre> 243 /etc/postfix/<a href="postconf.5.html">main.cf</a>: 244 <a href="postconf.5.html#smtp_connection_cache_destinations">smtp_connection_cache_destinations</a> = $<a href="postconf.5.html#relayhost">relayhost</a> 245 <a href="postconf.5.html#smtp_connection_cache_destinations">smtp_connection_cache_destinations</a> = hotmail.com, ... 246 <a href="postconf.5.html#smtp_connection_cache_destinations">smtp_connection_cache_destinations</a> = <a href="DATABASE_README.html#types">static</a>: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 252 connection reuse</a> to enable multiple deliveries over a TLS-encrypted 253 connection (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 260 keep an unused SMTP connection open for a significant amount of 261 time, and it is unwise to send huge numbers of messages through 262 the same connection. In order to avoid problems with SMTP connection 263 caching, Postfix implements the following safety mechanisms: </p> 264 265 <ul> 266 267 <li> <p> The Postfix <a href="scache.8.html">scache(8)</a> server keeps a connection open for 268 only a limited time. The time limit is specified with the 269 <a href="postconf.5.html#smtp_connection_cache_time_limit">smtp_connection_cache_time_limit</a> and with the <a href="postconf.5.html#connection_cache_ttl_limit">connection_cache_ttl_limit</a> 270 configuration parameters. This prevents anti-social behavior. </p> 271 272 <li> <p> The Postfix <a href="smtp.8.html">smtp(8)</a> client reuses a session for only a 273 limited number of times. This avoids triggering bugs in implementations 274 that do not correctly handle multiple deliveries per session. </p> 275 276 <p> As of Postfix 2.3 connection reuse is preferably limited with 277 the <a href="postconf.5.html#smtp_connection_reuse_time_limit">smtp_connection_reuse_time_limit</a> parameter. In addition, Postfix 278 2.11 provides <a href="postconf.5.html#smtp_connection_reuse_count_limit">smtp_connection_reuse_count_limit</a> to limit how many 279 times a connection may be reused, but this feature is unsafe as it 280 introduces a "fatal attractor" failure mode (when a destination has 281 multiple inbound MTAs, the slowest inbound MTA will attract most 282 connections from Postfix to that destination). </p> 283 284 <p> Postfix 2.3 logs the use count of multiply-used connections, 285 as shown in the following example: </p> 286 287 <blockquote> 288 <pre> 289 Nov 3 16:04:31 myname postfix/smtp[30840]: 19B6B2900FE: 290 to=<wietse (a] test.example.com>, orig_to=<wietse@test>, 291 relay=mail.example.com[1.2.3.4], <b>conn_use=2</b>, delay=0.22, 292 delays=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 297 with destination domain and IP address information. A connection 298 cache lookup succeeds only when the correct information is specified. 299 This 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 311 cache cannot be used with TLS, because an open TLS connection can 312 be reused only in the process that creates it. For this reason, 313 the Postfix <a href="smtp.8.html">smtp(8)</a> client historically always closed the connection 314 after completing an attempt to deliver mail over TLS.</p> 315 316 <li> <p> Postfix connection caching currently does not support 317 multiple SASL accounts per mail server. Specifically, Postfix 318 connection caching assumes that a SASL credential is valid for all 319 hostnames or domain names that deliver via the same mail server IP 320 address and TCP port, and assumes that the SASL credential does not 321 depend on the message originator. </p> 322 323 </ul> 324 325 326 <h2><a name="statistics">Connection cache statistics </a></h2> 327 328 <p> The <a href="scache.8.html">scache(8)</a> connection cache server logs statistics about the 329 peak cache size and the cache hit rates. This information is logged 330 every <a href="postconf.5.html#connection_cache_status_update_time">connection_cache_status_update_time</a> seconds, when the process 331 terminates after the maximal idle time is exceeded, or when Postfix 332 is reloaded. </p> 333 334 <ul> 335 336 <li> <p> Hit rates for connection cache lookups by domain will tell 337 you how useful connection caching is. </p> 338 339 <li> <p> Connection cache lookups by network address will always 340 fail, unless you're sending mail to different domains that share 341 the same MX hosts. </p> 342 343 <li> <p> No statistics are logged when no attempts are made to 344 access the connection cache. </p> 345 346 </ul> 347 348 349 </body> 350 351 </html> 352