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