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