]> granicus.if.org Git - apache/blob - docs/manual/mod/mod_unique_id.html.en
update transformation
[apache] / docs / manual / mod / mod_unique_id.html.en
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
4         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
5               This file is generated from xml source: DO NOT EDIT
6         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7       -->
8 <title>mod_unique_id - Apache HTTP Server</title>
9 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
10 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
11 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" />
12 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
13 <body>
14 <div id="page-header">
15 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
16 <p class="apache">Apache HTTP Server Version 2.5</p>
17 <img alt="" src="../images/feather.gif" /></div>
18 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
19 <div id="path">
20 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.5</a> &gt; <a href="./">Modules</a></div>
21 <div id="page-content">
22 <div id="preamble"><h1>Apache Module mod_unique_id</h1>
23 <div class="toplang">
24 <p><span>Available Languages: </span><a href="../en/mod/mod_unique_id.html" title="English">&nbsp;en&nbsp;</a> |
25 <a href="../fr/mod/mod_unique_id.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
26 <a href="../ja/mod/mod_unique_id.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
27 <a href="../ko/mod/mod_unique_id.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
28 </div>
29 <table class="module"><tr><th><a href="module-dict.html#Description">Description:</a></th><td>Provides an environment variable with a unique
30 identifier for each request</td></tr>
31 <tr><th><a href="module-dict.html#Status">Status:</a></th><td>Extension</td></tr>
32 <tr><th><a href="module-dict.html#ModuleIdentifier">Module Identifier:</a></th><td>unique_id_module</td></tr>
33 <tr><th><a href="module-dict.html#SourceFile">Source File:</a></th><td>mod_unique_id.c</td></tr></table>
34 <h3>Summary</h3>
35
36
37     <p>This module provides a magic token for each request which is
38     guaranteed to be unique across "all" requests under very
39     specific conditions. The unique identifier is even unique
40     across multiple machines in a properly configured cluster of
41     machines. The environment variable <code>UNIQUE_ID</code> is
42     set to the identifier for each request. Unique identifiers are
43     useful for various reasons which are beyond the scope of this
44     document.</p>
45 </div>
46 <div id="quickview"><h3 class="directives">Directives</h3>
47 <p>This module provides no
48             directives.</p>
49 <h3>Topics</h3>
50 <ul id="topics">
51 <li><img alt="" src="../images/down.gif" /> <a href="#theory">Theory</a></li>
52 </ul></div>
53 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
54 <div class="section">
55 <h2><a name="theory" id="theory">Theory</a></h2>
56     
57
58     <p>First a brief recap of how the Apache server works on Unix
59     machines. This feature currently isn't supported on Windows NT.
60     On Unix machines, Apache creates several children, the children
61     process requests one at a time. Each child can serve multiple
62     requests in its lifetime. For the purpose of this discussion,
63     the children don't share any data with each other. We'll refer
64     to the children as <dfn>httpd processes</dfn>.</p>
65
66     <p>Your website has one or more machines under your
67     administrative control, together we'll call them a cluster of
68     machines. Each machine can possibly run multiple instances of
69     Apache. All of these collectively are considered "the
70     universe", and with certain assumptions we'll show that in this
71     universe we can generate unique identifiers for each request,
72     without extensive communication between machines in the
73     cluster.</p>
74
75     <p>The machines in your cluster should satisfy these
76     requirements. (Even if you have only one machine you should
77     synchronize its clock with NTP.)</p>
78
79     <ul>
80       <li>The machines' times are synchronized via NTP or other
81       network time protocol.</li>
82
83       <li>The machines' hostnames all differ, such that the module
84       can do a hostname lookup on the hostname and receive a
85       different IP address for each machine in the cluster.</li>
86     </ul>
87
88     <p>As far as operating system assumptions go, we assume that
89     pids (process ids) fit in 32-bits. If the operating system uses
90     more than 32-bits for a pid, the fix is trivial but must be
91     performed in the code.</p>
92
93     <p>Given those assumptions, at a single point in time we can
94     identify any httpd process on any machine in the cluster from
95     all other httpd processes. The machine's IP address and the pid
96     of the httpd process are sufficient to do this. A httpd process
97     can handle multiple requests simultaneously if you use a
98     multi-threaded MPM. In order to identify threads, we use a thread
99     index Apache httpd uses internally. So in order to
100     generate unique identifiers for requests we need only
101     distinguish between different points in time.</p>
102
103     <p>To distinguish time we will use a Unix timestamp (seconds
104     since January 1, 1970 UTC), and a 16-bit counter. The timestamp
105     has only one second granularity, so the counter is used to
106     represent up to 65536 values during a single second. The
107     quadruple <em>( ip_addr, pid, time_stamp, counter )</em> is
108     sufficient to enumerate 65536 requests per second per httpd
109     process. There are issues however with pid reuse over time, and
110     the counter is used to alleviate this issue.</p>
111
112     <p>When an httpd child is created, the counter is initialized
113     with ( current microseconds divided by 10 ) modulo 65536 (this
114     formula was chosen to eliminate some variance problems with the
115     low order bits of the microsecond timers on some systems). When
116     a unique identifier is generated, the time stamp used is the
117     time the request arrived at the web server. The counter is
118     incremented every time an identifier is generated (and allowed
119     to roll over).</p>
120
121     <p>The kernel generates a pid for each process as it forks the
122     process, and pids are allowed to roll over (they're 16-bits on
123     many Unixes, but newer systems have expanded to 32-bits). So
124     over time the same pid will be reused. However unless it is
125     reused within the same second, it does not destroy the
126     uniqueness of our quadruple. That is, we assume the system does
127     not spawn 65536 processes in a one second interval (it may even
128     be 32768 processes on some Unixes, but even this isn't likely
129     to happen).</p>
130
131     <p>Suppose that time repeats itself for some reason. That is,
132     suppose that the system's clock is screwed up and it revisits a
133     past time (or it is too far forward, is reset correctly, and
134     then revisits the future time). In this case we can easily show
135     that we can get pid and time stamp reuse. The choice of
136     initializer for the counter is intended to help defeat this.
137     Note that we really want a random number to initialize the
138     counter, but there aren't any readily available numbers on most
139     systems (<em>i.e.</em>, you can't use rand() because you need
140     to seed the generator, and can't seed it with the time because
141     time, at least at one second resolution, has repeated itself).
142     This is not a perfect defense.</p>
143
144     <p>How good a defense is it? Suppose that one of your machines
145     serves at most 500 requests per second (which is a very
146     reasonable upper bound at this writing, because systems
147     generally do more than just shovel out static files). To do
148     that it will require a number of children which depends on how
149     many concurrent clients you have. But we'll be pessimistic and
150     suppose that a single child is able to serve 500 requests per
151     second. There are 1000 possible starting counter values such
152     that two sequences of 500 requests overlap. So there is a 1.5%
153     chance that if time (at one second resolution) repeats itself
154     this child will repeat a counter value, and uniqueness will be
155     broken. This was a very pessimistic example, and with real
156     world values it's even less likely to occur. If your system is
157     such that it's still likely to occur, then perhaps you should
158     make the counter 32 bits (by editing the code).</p>
159
160     <p>You may be concerned about the clock being "set back" during
161     summer daylight savings. However this isn't an issue because
162     the times used here are UTC, which "always" go forward. Note
163     that x86 based Unixes may need proper configuration for this to
164     be true -- they should be configured to assume that the
165     motherboard clock is on UTC and compensate appropriately. But
166     even still, if you're running NTP then your UTC time will be
167     correct very shortly after reboot.</p>
168
169     
170     <p>The <code>UNIQUE_ID</code> environment variable is
171     constructed by encoding the 144-bit (32-bit IP address, 32 bit
172     pid, 32 bit time stamp, 16 bit counter, 32 bit thread index)
173     quadruple using the
174     alphabet <code>[A-Za-z0-9@-]</code> in a manner similar to MIME
175     base64 encoding, producing 24 characters. The MIME base64
176     alphabet is actually <code>[A-Za-z0-9+/]</code> however
177     <code>+</code> and <code>/</code> need to be specially encoded
178     in URLs, which makes them less desirable. All values are
179     encoded in network byte ordering so that the encoding is
180     comparable across architectures of different byte ordering. The
181     actual ordering of the encoding is: time stamp, IP address,
182     pid, counter. This ordering has a purpose, but it should be
183     emphasized that applications should not dissect the encoding.
184     Applications should treat the entire encoded
185     <code>UNIQUE_ID</code> as an opaque token, which can be
186     compared against other <code>UNIQUE_ID</code>s for equality
187     only.</p>
188
189     <p>The ordering was chosen such that it's possible to change
190     the encoding in the future without worrying about collision
191     with an existing database of <code>UNIQUE_ID</code>s. The new
192     encodings should also keep the time stamp as the first element,
193     and can otherwise use the same alphabet and bit length. Since
194     the time stamps are essentially an increasing sequence, it's
195     sufficient to have a <em>flag second</em> in which all machines
196     in the cluster stop serving and request, and stop using the old
197     encoding format. Afterwards they can resume requests and begin
198     issuing the new encodings.</p>
199
200     <p>This we believe is a relatively portable solution to this
201     problem. The identifiers
202     generated have essentially an infinite life-time because future
203     identifiers can be made longer as required. Essentially no
204     communication is required between machines in the cluster (only
205     NTP synchronization is required, which is low overhead), and no
206     communication between httpd processes is required (the
207     communication is implicit in the pid value assigned by the
208     kernel). In very specific situations the identifier can be
209     shortened, but more information needs to be assumed (for
210     example the 32-bit IP address is overkill for any site, but
211     there is no portable shorter replacement for it). </p>
212 </div>
213 </div>
214 <div class="bottomlang">
215 <p><span>Available Languages: </span><a href="../en/mod/mod_unique_id.html" title="English">&nbsp;en&nbsp;</a> |
216 <a href="../fr/mod/mod_unique_id.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a> |
217 <a href="../ja/mod/mod_unique_id.html" hreflang="ja" rel="alternate" title="Japanese">&nbsp;ja&nbsp;</a> |
218 <a href="../ko/mod/mod_unique_id.html" hreflang="ko" rel="alternate" title="Korean">&nbsp;ko&nbsp;</a></p>
219 </div><div id="footer">
220 <p class="apache">Copyright 2011 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
221 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>
222 </body></html>