2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
7 Licensed to the Apache Software Foundation (ASF) under one or more
8 contributor license agreements. See the NOTICE file distributed with
9 this work for additional information regarding copyright ownership.
10 The ASF licenses this file to You under the Apache License, Version 2.0
11 (the "License"); you may not use this file except in compliance with
12 the License. You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
23 <modulesynopsis metafile="event.xml.meta">
25 <description>A variant of the <module>worker</module> MPM with the goal
26 of consuming threads only for connections with active processing</description>
28 <sourcefile>event.c</sourcefile>
29 <identifier>mpm_event_module</identifier>
32 <p>The <module>event</module> Multi-Processing Module (MPM) is,
33 as its name implies, an asynchronous, event-based implementation
34 designed to allow more requests to be served simultaneously by
35 passing off some processing work to the listeners threads, freeing up
36 the worker threads to serve new requests.</p>
38 <p>To use the <module>event</module> MPM, add
39 <code>--with-mpm=event</code> to the <program>configure</program>
40 script's arguments when building the <program>httpd</program>.</p>
44 <seealso><a href="worker.html">The worker MPM</a></seealso>
46 <section id="event-worker-relationship"><title>Relationship with the Worker MPM</title>
47 <p><module>event</module> is based on the <module>worker</module> MPM, which implements a hybrid
48 multi-process multi-threaded server. A single control process (the parent) is responsible for launching
49 child processes. Each child process creates a fixed number of server
50 threads as specified in the <directive module="mpm_common">ThreadsPerChild</directive> directive, as well
51 as a listener thread which listens for connections and passes them to a worker thread for processing when they arrive.</p>
53 <p>Run-time configuration directives are identical to those provided by <module>worker</module>, with the only addition
54 of the <directive>AsyncRequestWorkerFactor</directive>.</p>
58 <section id="how-it-works"><title>How it Works</title>
59 <p>This original goal of this MPM was to fix the 'keep alive problem' in HTTP. After a client
60 completes the first request, it can keep the connection
61 open, sending further requests using the same socket and saving
62 significant overhead in creating TCP connections. However,
63 Apache HTTP Server traditionally keeps an entire child
64 process/thread waiting for data from the client, which brings its own disadvantages.
65 To solve this problem, this MPM uses a dedicated listener thread for each process
66 along with a pool of worker threads, sharing queues specific for those
67 requests in keep-alive mode (or, more simply, "readable"), those in write-
68 completion mode, and those in the process of shutting down ("closing").
69 An event loop, triggered on the status of the socket's availability,
70 adjusts these queues and pushes work to the worker pool.
73 <p>This new architecture, leveraging non-blocking sockets and modern kernel
74 features exposed by <glossary>APR</glossary> (like Linux's epoll),
75 no longer requires the <code>mpm-accept</code> <directive module="core">Mutex</directive>
76 configured to avoid the thundering herd problem.</p>
78 <p>The total amount of connections that a single process/threads block can handle is regulated
79 by the <directive>AsyncRequestWorkerFactor</directive> directive.</p>
81 <section id="async-connections"><title>Async connections</title>
82 <p>Async connections would need a fixed dedicated worker thread with the previous MPMs but not with event.
83 The status page of <module>mod_status</module> shows new columns under the Async connections section:</p>
86 <dd>While sending the response to the client, it might happen that the TCP write buffer fills up because the connection is too slow. Usually in this case a <code>write()</code> to the socket returns <code>EWOULDBLOCK</code> or <code>EAGAIN</code>, to become writable again after an idle time. The worker holding the socket might be able to offload the waiting task to the listener thread, that in turn will re-assign it to the first idle worker thread available once an event will be raised for the socket (for example, "the socket is now writable"). Please check the Limitations section for more information.
90 <dd>Keep Alive handling is the most basic improvement from the worker MPM.
91 Once a worker thread finishes to flush the response to the client, it can offload the
92 socket handling to the listener thread, that in turns will wait for any event from the
93 OS, like "the socket is readable". If any new request comes from the client, then the
94 listener will forward it to the first worker thread available. Conversely, if the
95 <directive module="core">KeepAliveTimeout</directive> occurs then the socket will be
96 closed by the listener. In this way the worker threads are not responsible for idle
97 sockets and they can be re-used to serve other requests.</dd>
100 <dd>Sometimes the MPM needs to perform a lingering close, namely sending back an early error to the client while it is still transmitting data to httpd.
101 Sending the response and then closing the connection immediately is not the correct thing to do since the client (still trying to send the rest of the
102 request) would get a connection reset and could not read the httpd's response. The lingering close is time bounded but it can take relatively long
103 time, so it's offloaded to a worker thread (including the shutdown hooks and real socket close). From 2.4.28 onward this is also the
104 case when connections finally timeout (the listener thread never handles connections besides waiting for and dispatching their events).
108 <p>These improvements are valid for both HTTP/HTTPS connections.</p>
110 <p>The above connection states are managed by the listener thread via dedicated queues, that up to 2.4.27 were checked every 100ms
111 to find which connections hit timeout settings like <directive module="mpm_common">Timeout</directive> and
112 <directive module="core">KeepAliveTimeout</directive>. This was a simple and efficient solution, but it presented a downside: the pollset was
113 forcing a wake-up of the listener thread even if there was no need (for example because completely idle), wasting resources. From 2.4.28
114 these queues are completely managed via an event based logic, not relying anymore on active polling.
115 Resource constrained environments, like embedded servers, may benefit from this improvement.</p>
119 <section id="graceful-close"><title>Graceful process termination and Scoreboard usage</title>
120 <p>This mpm showed some scalability bottlenecks in the past leading to the following
121 error: "<strong>scoreboard is full, not at MaxRequestWorkers</strong>".
122 <directive module="mpm_common">MaxRequestWorkers</directive>
123 limits the number of simultaneous requests that will be served at any given time
124 and also the number of allowed processes
125 (<directive module="mpm_common">MaxRequestWorkers</directive>
126 / <directive module="mpm_common">ThreadsPerChild</directive>), meanwhile
127 the Scoreboard is a representation of all the running processes and
128 the status of their worker threads. If the scoreboard is full (so all the
129 threads have a state that is not idle) but the number of active requests
130 served is not <directive module="mpm_common">MaxRequestWorkers</directive>,
131 it means that some of them are blocking new requests that could be served
132 but that are queued instead (up to the limit imposed by
133 <directive module="mpm_common">ListenBacklog</directive>). Most of the times
134 the threads are stuck in the Graceful state, namely they are waiting to
135 finish their work with a TCP connection to safely terminate and free up a
136 scoreboard slot (for example handling long running requests, slow clients
137 or connections with keep-alive enabled). Two scenarios are very common:</p>
139 <li>During a <a href="../stopping.html#graceful">graceful restart</a>.
140 The parent process signals all its children to complete
141 their work and terminate, while it reloads the config and forks new
142 processes. If the old children keep running for a while before stopping,
143 the scoreboard will be partially occupied until their slots are freed.
145 <li>When the server load goes down in a way that causes httpd to
146 stop some processes (for example due to
147 <directive module="mpm_common">MaxSpareThreads</directive>).
148 This is particularly problematic because when the load increases again,
149 httpd will try to start new processes.
150 If the pattern repeats, the number of processes can rise quite a bit,
151 ending up in a mixture of old processes trying to stop and new ones
152 trying to do some work.
155 <p>From 2.4.24 onward, mpm-event is smarter and it is able to handle
156 graceful terminations in a much better way. Some of the improvements are:</p>
158 <li>Allow the use of all the scoreboard slots up to
159 <directive module="mpm_common">ServerLimit</directive>.
160 <directive module="mpm_common">MaxRequestWorkers</directive> and
161 <directive module="mpm_common">ThreadsPerChild</directive> are used
162 to limit the amount of active processes, meanwhile
163 <directive module="mpm_common">ServerLimit</directive>
164 takes also into account the ones doing a graceful
165 close to allow extra slots when needed. The idea is to use
166 <directive module="mpm_common">ServerLimit</directive> to instruct httpd
167 about how many overall processes are tolerated before impacting
168 the system resources.
170 <li>Force gracefully finishing processes to close their
171 connections in keep-alive state.</li>
172 <li>During graceful shutdown, if there are more running worker threads
173 than open connections for a given process, terminate these threads to
174 free resources faster (which may be needed for new processes).</li>
175 <li>If the scoreboard is full, prevent more processes to finish
176 gracefully due to reduced load until old processes have terminated
177 (otherwise the situation would get worse once the load increases again).</li>
179 <p>The behavior described in the last point is completely observable via
180 <module>mod_status</module> in the connection summary table through two new
181 columns: "Slot" and "Stopping". The former indicates the PID and
182 the latter if the process is stopping or not; the extra state "Yes (old gen)"
183 indicates a process still running after a graceful restart.</p>
186 <section id="limitations"><title>Limitations</title>
187 <p>The improved connection handling may not work for certain connection
188 filters that have declared themselves as incompatible with event. In these
189 cases, this MPM will fall back to the behavior of the
190 <module>worker</module> MPM and reserve one worker thread per connection.
191 All modules shipped with the server are compatible with the event MPM.</p>
193 <p>A similar restriction is currently present for requests involving an
194 output filter that needs to read and/or modify the whole response body.
195 If the connection to the client blocks while the filter is processing the
196 data, and the amount of data produced by the filter is too big to be
197 buffered in memory, the thread used for the request is not freed while
198 httpd waits until the pending data is sent to the client.<br />
199 To illustrate this point we can think about the following two situations:
200 serving a static asset (like a CSS file) versus serving content retrieved from
201 FCGI/CGI or a proxied server. The former is predictable, namely the event MPM
202 has full visibility on the end of the content and it can use events: the worker
203 thread serving the response content can flush the first bytes until <code>EWOULDBLOCK</code>
204 or <code>EAGAIN</code> is returned, delegating the rest to the listener. This one in turn
205 waits for an event on the socket, and delegates the work to flush the rest of the content
206 to the first idle worker thread. Meanwhile in the latter example (FCGI/CGI/proxied content)
207 the MPM can't predict the end of the response and a worker thread has to finish its work
208 before returning the control to the listener. The only alternative is to buffer the
209 response in memory, but it wouldn't be the safest option for the sake of the
210 server's stability and memory footprint.
215 <section id="background"><title>Background material</title>
216 <p>The event model was made possible by the introduction of new APIs into the supported operating systems:</p>
218 <li>epoll (Linux) </li>
219 <li>kqueue (BSD) </li>
220 <li>event ports (Solaris) </li>
222 <p>Before these new APIs where made available, the traditional <code>select</code> and <code>poll</code> APIs had to be used.
223 Those APIs get slow if used to handle many connections or if the set of connections rate of change is high.
224 The new APIs allow to monitor much more connections and they perform way better when the set of connections to monitor changes frequently. So these APIs made it possible to write the event MPM, that scales much better with the typical HTTP pattern of many idle connections.</p>
226 <p>The MPM assumes that the underlying <code>apr_pollset</code>
227 implementation is reasonably threadsafe. This enables the MPM to
228 avoid excessive high level locking, or having to wake up the listener
229 thread in order to send it a keep-alive socket. This is currently
230 only compatible with KQueue and EPoll.</p>
236 <section id="requirements"><title>Requirements</title>
237 <p>This MPM depends on <glossary>APR</glossary>'s atomic
238 compare-and-swap operations for thread synchronization. If you are
239 compiling for an x86 target and you don't need to support 386s, or
240 you are compiling for a SPARC and you don't need to run on
241 pre-UltraSPARC chips, add
242 <code>--enable-nonportable-atomics=yes</code> to the
243 <program>configure</program> script's arguments. This will cause
244 APR to implement atomic operations using efficient opcodes not
245 available in older CPUs.</p>
247 <p>This MPM does not perform well on older platforms which lack good
248 threading, but the requirement for EPoll or KQueue makes this
253 <li>To use this MPM on FreeBSD, FreeBSD 5.3 or higher is recommended.
254 However, it is possible to run this MPM on FreeBSD 5.2.1, if you
255 use <code>libkse</code> (see <code>man libmap.conf</code>).</li>
257 <li>For NetBSD, at least version 2.0 is recommended.</li>
259 <li>For Linux, a 2.6 kernel is recommended. It is also necessary to
260 ensure that your version of <code>glibc</code> has been compiled
261 with support for EPoll.</li>
266 <directivesynopsis location="mpm_common"><name>CoreDumpDirectory</name>
268 <directivesynopsis location="mpm_common"><name>EnableExceptionHook</name>
270 <directivesynopsis location="mod_unixd"><name>Group</name>
272 <directivesynopsis location="mpm_common"><name>Listen</name>
274 <directivesynopsis location="mpm_common"><name>ListenBacklog</name>
276 <directivesynopsis location="mpm_common"><name>SendBufferSize</name>
278 <directivesynopsis location="mpm_common"><name>MaxRequestWorkers</name>
280 <directivesynopsis location="mpm_common"><name>MaxMemFree</name>
282 <directivesynopsis location="mpm_common"><name>MaxConnectionsPerChild</name>
284 <directivesynopsis location="mpm_common"><name>MaxSpareThreads</name>
286 <directivesynopsis location="mpm_common"><name>MinSpareThreads</name>
288 <directivesynopsis location="mpm_common"><name>PidFile</name>
290 <directivesynopsis location="mpm_common"><name>ScoreBoardFile</name>
292 <directivesynopsis location="mpm_common"><name>ServerLimit</name>
294 <directivesynopsis location="mpm_common"><name>StartServers</name>
296 <directivesynopsis location="mpm_common"><name>ThreadLimit</name>
298 <directivesynopsis location="mpm_common"><name>ThreadsPerChild</name>
300 <directivesynopsis location="mpm_common"><name>ThreadStackSize</name>
302 <directivesynopsis location="mod_unixd"><name>User</name>
306 <name>AsyncRequestWorkerFactor</name>
307 <description>Limit concurrent connections per process</description>
308 <syntax>AsyncRequestWorkerFactor <var>factor</var></syntax>
310 <contextlist><context>server config</context> </contextlist>
311 <compatibility>Available in version 2.3.13 and later</compatibility>
314 <p>The event MPM handles some connections in an asynchronous way, where
315 request worker threads are only allocated for short periods of time as
316 needed, and other connections with one request worker thread reserved per
317 connection. This can lead to situations where all workers are tied up and
318 no worker thread is available to handle new work on established async
321 <p>To mitigate this problem, the event MPM does two things:</p>
323 <li>it limits the number of connections accepted per process, depending on the
324 number of idle request workers;</li>
325 <li>if all workers are busy, it will
326 close connections in keep-alive state even if the keep-alive timeout has
327 not expired. This allows the respective clients to reconnect to a
328 different process which may still have worker threads available.</li>
331 <p>This directive can be used to fine-tune the per-process connection
332 limit. A <strong>process</strong> will only accept new connections if the current number of
333 connections (not counting connections in the "closing" state) is lower
336 <p class="indent"><strong>
337 <directive module="mpm_common">ThreadsPerChild</directive> +
338 (<directive>AsyncRequestWorkerFactor</directive> *
339 <var>number of idle workers</var>)
342 <p>An estimation of the maximum concurrent connections across all the processes given
343 an average value of idle worker threads can be calculated with:
347 <p class="indent"><strong>
348 (<directive module="mpm_common">ThreadsPerChild</directive> +
349 (<directive>AsyncRequestWorkerFactor</directive> *
350 <var>number of idle workers</var>)) *
351 <directive module="mpm_common">ServerLimit</directive>
354 <note><title>Example</title>
355 <highlight language="config">
359 AsyncRequestWorkerFactor = 2
360 MaxRequestWorkers = 40
362 idle_workers = 4 (average for all the processes to keep it simple)
364 max_connections = (ThreadsPerChild + (AsyncRequestWorkerFactor * idle_workers)) * ServerLimit
365 = (10 + (2 * 4)) * 4 = 72
370 <p>When all the worker threads are idle, then absolute maximum numbers of concurrent
371 connections can be calculared in a simpler way:</p>
373 <p class="indent"><strong>
374 (<directive>AsyncRequestWorkerFactor</directive> + 1) *
375 <directive module="mpm_common">MaxRequestWorkers</directive>
379 <note><title>Example</title>
380 <highlight language="config">
384 MaxRequestWorkers = 40
385 AsyncRequestWorkerFactor = 2
389 <p>If all the processes have all threads idle then: </p>
391 <highlight language="config">idle_workers = 10</highlight>
393 <p>We can calculate the absolute maximum numbers of concurrent connections in two ways:</p>
395 <highlight language="config">
397 max_connections = (ThreadsPerChild + (AsyncRequestWorkerFactor * idle_workers)) * ServerLimit
398 = (10 + (2 * 10)) * 4 = 120
400 max_connections = (AsyncRequestWorkerFactor + 1) * MaxRequestWorkers
406 <p>Tuning <directive>AsyncRequestWorkerFactor</directive> requires knowledge about the traffic handled by httpd in each specific use case, so changing the default value requires extensive testing and data gathering from <module>mod_status</module>.</p>
408 <p><directive module="mpm_common">MaxRequestWorkers</directive> was called
409 <directive>MaxClients</directive> prior to version 2.3.13. The above value
410 shows that the old name did not accurately describe its meaning for the event MPM.</p>
412 <p><directive>AsyncRequestWorkerFactor</directive> can take non-integer
413 arguments, e.g "1.5".</p>