|
7 | 7 | <title>What are Persistent Connections?</title> |
8 | 8 | <simpara> |
9 | 9 | Persistent connections are links that do not close when the |
10 | | - execution of your script ends. When a persistent connection is |
11 | | - requested, PHP checks if there's already an identical persistent |
12 | | - connection (that remained open from earlier) - and if it exists, it |
13 | | - uses it. If it does not exist, it creates the link. An 'identical' |
14 | | - connection is a connection that was opened to the same host, with |
15 | | - the same username and the same password (where applicable). |
| 10 | + execution of the script ends. When a persistent connection is |
| 11 | + requested, PHP checks whether an identical persistent |
| 12 | + connection (that remained open from earlier) already exists; if one does, |
| 13 | + it is reused, and if not, a new link is created. An 'identical' |
| 14 | + connection is one opened to the same host with |
| 15 | + the same username and password (where applicable). |
16 | 16 | </simpara> |
17 | 17 | <simpara> |
18 | | - There's no method of requesting a specific connection, or guaranteeing |
19 | | - whether you get an existing connection or a brand new one (if all existing |
20 | | - connections are in use, or the request is being served by a different worker, |
21 | | - which has a separate pool of connections). |
| 18 | + There is no way to request a specific connection, or to guarantee |
| 19 | + whether the returned connection will be an existing one or a brand new |
| 20 | + one (if all existing connections are in use, or the request is being |
| 21 | + served by a different worker, which has a separate pool of connections). |
22 | 22 | </simpara> |
23 | 23 | <simpara> |
24 | | - This means that you cannot use PHP's persistent connections to, for example: |
| 24 | + PHP's persistent connections therefore cannot be used to, for example: |
25 | 25 | </simpara> |
26 | 26 | <simplelist> |
27 | 27 | <member>assign a specific database session to a specific web user</member> |
28 | 28 | <member>create a large transaction across multiple requests</member> |
29 | 29 | <member>initiate a query on one request and collect the results on another</member> |
30 | 30 | </simplelist> |
31 | 31 | <simpara> |
32 | | - Persistent connections do not give you <emphasis>any</emphasis> |
33 | | - functionality that wasn't possible with non-persistent connections. |
| 32 | + Persistent connections do not provide <emphasis>any</emphasis> |
| 33 | + functionality that was not possible with non-persistent connections. |
34 | 34 | </simpara> |
35 | 35 | </simplesect> |
36 | 36 |
|
37 | 37 | <simplesect xml:id="persistent-connections.web"> |
38 | 38 | <title>Web Requests</title> |
39 | 39 | <simpara> |
40 | | - There are two ways in which your web server can utilize PHP to generate |
| 40 | + There are two ways in which a web server can utilize PHP to generate |
41 | 41 | web pages: |
42 | 42 | </simpara> |
43 | 43 | <simpara> |
44 | 44 | The first method is to use PHP as a CGI "wrapper". When run this |
45 | 45 | way, an instance of the PHP interpreter is created and destroyed |
46 | | - for every page request (for a PHP page) to your web server. |
| 46 | + for every page request (for a PHP page) to the web server. |
47 | 47 | Because it is destroyed after every request, any resources that it |
48 | 48 | acquires (such as a link to an SQL database server) are closed when |
49 | | - it is destroyed. In this case, you do not gain anything from trying |
50 | | - to use persistent connections - they simply don't persist. |
| 49 | + it is destroyed. In this case, there is nothing to be gained from |
| 50 | + using persistent connections - they simply do not persist. |
51 | 51 | </simpara> |
52 | 52 | <simpara> |
53 | 53 | The second, and most popular, method is to run PHP-FPM, or PHP as a module |
54 | | - in a multiprocess web server, which currently only includes Apache. |
| 54 | + in a multiprocess web server (currently only Apache). |
55 | 55 | These setups typically have one process (the parent) which |
56 | | - coordinates a set of processes (its children) who actually do the |
| 56 | + coordinates a set of processes (its children) that actually do the |
57 | 57 | work of serving up web pages. When a request comes in from a |
58 | 58 | client, it is handed off to one of the children that is not already |
59 | 59 | serving another client. This means that when the same client makes |
60 | 60 | a second request to the server, it may be served by a different |
61 | | - child process than the first time. When opening a persistent connection, |
62 | | - every following page requesting SQL services can reuse the same |
63 | | - established connection to the SQL server. |
| 61 | + child process than the first time. Once a persistent connection has been |
| 62 | + opened, any subsequent page served by the same child process can reuse the |
| 63 | + already established connection to the SQL server. |
64 | 64 | </simpara> |
65 | 65 | <note> |
66 | 66 | <para> |
67 | | - You can check which method your web requests use by checking the value of |
| 67 | + The method in use can be checked by looking at the value of |
68 | 68 | "Server API" in the output of <function>phpinfo</function> or the value of |
69 | 69 | <constant>PHP_SAPI</constant>, run from a web request. |
70 | 70 | </para> |
|
82 | 82 | As command-line PHP uses a new process for each script, persistent |
83 | 83 | connections are not shared between command-line scripts, so there is no |
84 | 84 | value in using them in transient scripts such as crons or commands. |
85 | | - However, they may be useful if, for example, you're writing a long-running |
86 | | - application server that serves many requests or tasks and each may need |
87 | | - their own database connection. |
| 85 | + However, they may be useful, for example, in a long-running application |
| 86 | + server that serves many requests or tasks, each of which may need its |
| 87 | + own database connection. |
88 | 88 | </simpara> |
89 | 89 | </simplesect> |
90 | 90 |
|
91 | 91 | <simplesect xml:id="persistent-connections.why"> |
92 | 92 | <title>Why Use Them?</title> |
93 | 93 | <simpara> |
94 | | - Persistent connections are good if the overhead to create a link to your |
95 | | - SQL server is high. Whether or not this overhead is really high depends |
96 | | - on many factors. Like, what kind of database it is, whether or not |
97 | | - it sits on the same computer on which your web server sits, how |
98 | | - loaded the machine the SQL server sits on is and so forth. The |
99 | | - bottom line is that if that connection overhead is high, persistent |
100 | | - connections help you considerably. They cause the child process to |
101 | | - simply connect only once for its entire lifespan, instead of every |
102 | | - time it processes a page that requires connecting to the SQL |
103 | | - server. This means that for every child that opened a persistent |
104 | | - connection will have its own open persistent connection to the |
105 | | - server. For example, if you had 20 different child processes that |
106 | | - ran a script that made a persistent connection to your SQL server, |
107 | | - you'd have 20 different connections to the SQL server, one from |
108 | | - each child. |
| 94 | + Persistent connections are beneficial when the overhead of creating a link |
| 95 | + to an SQL server is high. Whether this overhead is significant depends on |
| 96 | + many factors, such as the type of database, whether it resides on the same |
| 97 | + machine as the web server, and how loaded that machine is. When the |
| 98 | + connection overhead is high, persistent connections can help considerably: |
| 99 | + each child process connects only once for its entire lifespan, rather than |
| 100 | + every time it processes a page that requires a connection to the SQL |
| 101 | + server. This means every child that opens a persistent connection will |
| 102 | + maintain its own connection to the server. For example, if 20 different |
| 103 | + child processes each run a script that makes a persistent connection to |
| 104 | + the SQL server, there will be 20 separate connections to that server, one |
| 105 | + from each child. |
109 | 106 | </simpara> |
110 | 107 | </simplesect> |
111 | 108 |
|
112 | 109 | <simplesect xml:id="persistent-connections.drawbacks.conn-limits"> |
113 | 110 | <title>Potential Drawbacks: Connection Limits</title> |
114 | 111 | <simpara> |
115 | | - Note, however, that this can have some drawbacks if you are using a |
| 112 | + Note, however, that this can have drawbacks when using a |
116 | 113 | database with connection limits that are exceeded by persistent |
117 | | - child connections. If your database has a limit of 16 simultaneous |
118 | | - connections, and in the course of a busy server session, 17 child |
119 | | - threads attempt to connect, one will not be able to. If there are |
120 | | - bugs in your scripts which do not allow the connections to shut |
121 | | - down (such as infinite loops), the database with only 16 connections |
| 114 | + child connections. If the database has a limit of 16 simultaneous |
| 115 | + connections, and during a busy server session 17 child |
| 116 | + processes attempt to connect, one of them will fail. If there are |
| 117 | + bugs in the scripts that prevent connections from shutting |
| 118 | + down (such as infinite loops), a database with only 16 connections |
122 | 119 | may be rapidly swamped. |
123 | 120 | </simpara> |
124 | 121 | <simpara> |
125 | 122 | Persistent connections will usually increase the number of connections open |
126 | | - at any given time because idle workers will still hold the connections for |
127 | | - the previous requests they served. If a large number of workers is spun up to |
128 | | - handle an influx of requests, the connections they opened will remain until |
129 | | - the worker is killed or the database server closes the connection. |
| 123 | + at any given time, because idle workers still hold on to the connections they |
| 124 | + opened for previous requests. If a large number of workers are spun up to |
| 125 | + handle a spike in traffic, the connections they opened will remain until |
| 126 | + the worker is terminated or the database server closes the connection. |
130 | 127 | </simpara> |
131 | 128 | <simpara> |
132 | 129 | Ensure that the maximum number of connections allowed by the database server |
133 | 130 | is greater than the maximum number of web request workers (plus any other |
134 | 131 | usage such as crons or administrative connections). |
135 | 132 | </simpara> |
136 | 133 | <simpara> |
137 | | - Check your database documentation for information on handling abandoned or |
| 134 | + Check the database documentation for information on handling abandoned or |
138 | 135 | idle connections (timeouts). Long timeouts may significantly increase the |
139 | 136 | number of persistent connections open at any one time. |
140 | 137 | </simpara> |
|
171 | 168 | to recreate the same table. |
172 | 169 | </simpara> |
173 | 170 | <simpara> |
174 | | - You can implement cleanup using class destructors or |
175 | | - <function>register_shutdown_function</function>. You may also want to |
176 | | - consider dedicated connection pooling proxies that include this as part of |
177 | | - their functionality. |
| 171 | + Cleanup can be implemented using class destructors or |
| 172 | + <function>register_shutdown_function</function>. Dedicated connection |
| 173 | + pooling proxies that include this as part of their functionality may |
| 174 | + also be considered. |
178 | 175 | </simpara> |
179 | 176 | </simplesect> |
180 | 177 |
|
181 | 178 | <simplesect xml:id="persistent-connections.final-words"> |
182 | 179 | <title>Final Words</title> |
183 | 180 | <simpara> |
184 | | - Given their behavior and potential drawbacks described above, you should not |
185 | | - use persistent connections without careful consideration. They should not be |
186 | | - used without implementing additional changes to your application and careful |
187 | | - configuration of your database server and web server and/or PHP-FPM. |
| 181 | + Given their behavior and potential drawbacks described above, persistent |
| 182 | + connections should not be used without careful consideration. They should |
| 183 | + not be used without implementing additional changes to the application and |
| 184 | + careful configuration of the database server and web server and/or PHP-FPM. |
188 | 185 | </simpara> |
189 | 186 | <simpara> |
190 | 187 | Consider alternative solutions such as investigating and fixing the causes of |
|
0 commit comments