| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Transactional guarantees</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="rep.html" title="Chapter 12. Berkeley DB Replication" /> |
| 11 | <link rel="prev" href="rep_bulk.html" title="Bulk transfer" /> |
| 12 | <link rel="next" href="rep_lease.html" title="Master Leases" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Transactional guarantees</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="rep_bulk.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 12. |
| 26 | Berkeley DB Replication |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="rep_lease.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="rep_trans"></a>Transactional guarantees</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>It is important to consider replication in the context of the overall |
| 42 | database environment's transactional guarantees. To briefly review, |
| 43 | transactional guarantees in a non-replicated application are based on |
| 44 | the writing of log file records to "stable storage", usually a disk |
| 45 | drive. If the application or system then fails, the Berkeley DB logging |
| 46 | information is reviewed during recovery, and the databases are updated |
| 47 | so that all changes made as part of committed transactions appear, and |
| 48 | all changes made as part of uncommitted transactions do not appear. In |
| 49 | this case, no information will have been lost.</p> |
| 50 | <p>If a database environment does not require the log be flushed to |
| 51 | stable storage on transaction commit (using the <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> |
| 52 | flag to increase performance at the cost of sacrificing transactional |
| 53 | durability), Berkeley DB recovery will only be able to restore the system to |
| 54 | the state of the last commit found on stable storage. In this case, |
| 55 | information may have been lost (for example, the changes made by some |
| 56 | committed transactions may not appear in the databases after recovery).</p> |
| 57 | <p>Further, if there is database or log file loss or corruption (for |
| 58 | example, if a disk drive fails), then catastrophic recovery is |
| 59 | necessary, and Berkeley DB recovery will only be able to restore the system |
| 60 | to the state of the last archived log file. In this case, information |
| 61 | may also have been lost.</p> |
| 62 | <p>Replicating the database environment extends this model, by adding a |
| 63 | new component to "stable storage": the client's replicated information. |
| 64 | If a database environment is replicated, there is no lost information |
| 65 | in the case of database or log file loss, because the replicated system |
| 66 | can be configured to contain a complete set of databases and log records |
| 67 | up to the point of failure. A database environment that loses a disk |
| 68 | drive can have the drive replaced, and it can then rejoin the |
| 69 | replication group.</p> |
| 70 | <p>Because of this new component of stable storage, specifying |
| 71 | <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> in a replicated environment no longer sacrifices |
| 72 | durability, as long as one or more clients have acknowledged receipt of |
| 73 | the messages sent by the master. Since network connections are often |
| 74 | faster than local synchronous disk writes, replication becomes a way |
| 75 | for applications to significantly improve their performance as well as |
| 76 | their reliability.</p> |
| 77 | <p>The return status from the application's <span class="bold"><strong>send</strong></span> function must be |
| 78 | set by the application to ensure the transactional guarantees the |
| 79 | application wants to provide. Whenever the <span class="bold"><strong>send</strong></span> function |
| 80 | returns failure, the local database environment's log is flushed as |
| 81 | necessary to ensure that any information critical to database integrity |
| 82 | is not lost. Because this flush is an expensive operation in terms of |
| 83 | database performance, applications should avoid returning an error from |
| 84 | the <span class="bold"><strong>send</strong></span> function, if at all possible.</p> |
| 85 | <p>The only interesting message type for replication transactional |
| 86 | guarantees is when the application's <span class="bold"><strong>send</strong></span> function was called |
| 87 | with the <a href="../api_reference/C/reptransport.html#transport_DB_REP_PERMANENT" class="olink">DB_REP_PERMANENT</a> flag specified. There is no reason |
| 88 | for the <span class="bold"><strong>send</strong></span> function to ever return failure unless the |
| 89 | <a href="../api_reference/C/reptransport.html#transport_DB_REP_PERMANENT" class="olink">DB_REP_PERMANENT</a> flag was specified -- messages without the |
| 90 | <a href="../api_reference/C/reptransport.html#transport_DB_REP_PERMANENT" class="olink">DB_REP_PERMANENT</a> flag do not make visible changes to databases, |
| 91 | and the <span class="bold"><strong>send</strong></span> function can return success to Berkeley DB as soon as |
| 92 | the message has been sent to the client(s) or even just copied to local |
| 93 | application memory in preparation for being sent.</p> |
| 94 | <p>When a client receives a <a href="../api_reference/C/reptransport.html#transport_DB_REP_PERMANENT" class="olink">DB_REP_PERMANENT</a> message, the client |
| 95 | will flush its log to stable storage before returning (unless the client |
| 96 | environment has been configured with the <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> option). |
| 97 | If the client is unable to flush a complete transactional record to disk |
| 98 | for any reason (for example, there is a missing log record before the |
| 99 | flagged message), the call to the <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method on the client |
| 100 | will return <a href="../api_reference/C/repmessage.html#repmsg_DB_REP_NOTPERM" class="olink">DB_REP_NOTPERM</a> and return the LSN of this record |
| 101 | to the application in the <span class="bold"><strong>ret_lsnp</strong></span> parameter. |
| 102 | The application's client or master |
| 103 | message handling loops should take proper action to ensure the correct |
| 104 | transactional guarantees in this case. When missing records arrive |
| 105 | and allow subsequent processing of previously stored permanent |
| 106 | records, the call to the <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method on the client will |
| 107 | return <a href="../api_reference/C/repmessage.html#repmsg_DB_REP_ISPERM" class="olink">DB_REP_ISPERM</a> and return the largest LSN of the |
| 108 | permanent records that were flushed to disk. Client applications |
| 109 | can use these LSNs to know definitively if any particular LSN is |
| 110 | permanently stored or not.</p> |
| 111 | <p>An application relying on a client's ability to become a master and |
| 112 | guarantee that no data has been lost will need to write the <span class="bold"><strong>send</strong></span> |
| 113 | function to return an error whenever it cannot guarantee the site that |
| 114 | will win the next election has the record. Applications not requiring |
| 115 | this level of transactional guarantees need not have the <span class="bold"><strong>send</strong></span> |
| 116 | function return failure (unless the master's database environment has |
| 117 | been configured with <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a>), as any information critical |
| 118 | to database integrity has already been flushed to the local log before |
| 119 | <span class="bold"><strong>send</strong></span> was called.</p> |
| 120 | <p>To sum up, the only reason for the <span class="bold"><strong>send</strong></span> function to return |
| 121 | failure is when the master database environment has been configured to |
| 122 | not synchronously flush the log on transaction commit (that is, |
| 123 | <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> was configured on the master), the |
| 124 | <a href="../api_reference/C/reptransport.html#transport_DB_REP_PERMANENT" class="olink">DB_REP_PERMANENT</a> flag is specified for the message, and the |
| 125 | <span class="bold"><strong>send</strong></span> function was unable to determine that some number of |
| 126 | clients have received the current message (and all messages preceding |
| 127 | the current message). How many clients need to receive the message |
| 128 | before the <span class="bold"><strong>send</strong></span> function can return success is an application |
| 129 | choice (and may not depend as much on a specific number of clients |
| 130 | reporting success as one or more geographically distributed clients).</p> |
| 131 | <p>If, however, the application does require on-disk durability on the master, |
| 132 | the master should be configured to synchronously flush the log on commit. |
| 133 | If clients are not configured to synchronously flush the log, |
| 134 | that is, if a client is running with <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> configured, |
| 135 | then it is up to the application to reconfigure that client |
| 136 | appropriately when it becomes a master. That is, the |
| 137 | application must explicitly call <a href="../api_reference/C/envset_flags.html" class="olink">DB_ENV->set_flags()</a> to |
| 138 | disable asynchronous log flushing as part of re-configuring |
| 139 | the client as the new master.</p> |
| 140 | <p>Of course, it is important to ensure that the replicated master and |
| 141 | client environments are truly independent of each other. For example, |
| 142 | it does not help matters that a client has acknowledged receipt of a |
| 143 | message if both master and clients are on the same power supply, as the |
| 144 | failure of the power supply will still potentially lose information.</p> |
| 145 | <p>Configuring your replication-based application to achieve the proper |
| 146 | mix of performance and transactional guarantees can be complex. In |
| 147 | brief, there are a few controls an application can set to configure the |
| 148 | guarantees it makes: specification of <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> for the |
| 149 | master environment, specification of <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> for the |
| 150 | client environment, the priorities of different sites participating in |
| 151 | an election, and the behavior of the application's <span class="bold"><strong>send</strong></span> |
| 152 | function.</p> |
| 153 | <p>Applications using Replication Manager are free to use |
| 154 | <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> at the master and/or clients as they see fit. The |
| 155 | behavior of the <span class="bold"><strong>send</strong></span> function that Replication Manager provides |
| 156 | on the application's behalf is determined by an "acknowledgement |
| 157 | policy", which is configured by the <a href="../api_reference/C/repmgrset_ack_policy.html" class="olink">DB_ENV->repmgr_set_ack_policy()</a> method. |
| 158 | Clients always send acknowledgements for <a href="../api_reference/C/reptransport.html#transport_DB_REP_PERMANENT" class="olink">DB_REP_PERMANENT</a> |
| 159 | messages (unless the acknowledgement policy in effect indicates that the |
| 160 | master doesn't care about them). For a <a href="../api_reference/C/reptransport.html#transport_DB_REP_PERMANENT" class="olink">DB_REP_PERMANENT</a> |
| 161 | message, the master blocks the sending thread until either it receives |
| 162 | the proper number of acknowledgements, or the <a href="../api_reference/C/repset_timeout.html#set_timeout_DB_REP_ACK_TIMEOUT" class="olink">DB_REP_ACK_TIMEOUT</a> |
| 163 | expires. In the case of timeout, Replication Manager returns an error |
| 164 | code from the <span class="bold"><strong>send</strong></span> function, causing Berkeley DB to flush the |
| 165 | transaction log before returning to the application, as previously |
| 166 | described. The default acknowledgement policy is |
| 167 | <a href="../api_reference/C/repmgrset_ack_policy.html#ackspolicy_DB_REPMGR_ACKS_QUORUM" class="olink">DB_REPMGR_ACKS_QUORUM</a>, which ensures that the effect of a |
| 168 | permanent record remains durable following an election.</p> |
| 169 | <p>First, it is rarely useful to write and synchronously flush the log when |
| 170 | a transaction commits on a replication client. It may be useful where |
| 171 | systems share resources and multiple systems commonly fail at the same |
| 172 | time. By default, all Berkeley DB database environments, whether master or |
| 173 | client, synchronously flush the log on transaction commit or prepare. |
| 174 | Generally, replication masters and clients turn log flush off for |
| 175 | transaction commit using the <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> flag.</p> |
| 176 | <p>Consider two systems connected by a network interface. One acts as the |
| 177 | master, the other as a read-only client. The client takes over as |
| 178 | master if the master crashes and the master rejoins the replication |
| 179 | group after such a failure. Both master and client are configured to |
| 180 | not synchronously flush the log on transaction commit (that is, |
| 181 | <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> was configured on both systems). The |
| 182 | application's <span class="bold"><strong>send</strong></span> function never returns failure to the Berkeley DB |
| 183 | library, simply forwarding messages to the client (perhaps over a |
| 184 | broadcast mechanism), and always returning success. On the client, any |
| 185 | <a href="../api_reference/C/repmessage.html#repmsg_DB_REP_NOTPERM" class="olink">DB_REP_NOTPERM</a> returns from the client's <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method |
| 186 | are ignored, as well. This system configuration has excellent |
| 187 | performance, but may lose data in some failure modes.</p> |
| 188 | <p>If both the master and the client crash at once, it is possible to lose |
| 189 | committed transactions, that is, transactional durability is not being |
| 190 | maintained. Reliability can be increased by providing separate power |
| 191 | supplies for the systems and placing them in separate physical locations.</p> |
| 192 | <p>If the connection between the two machines fails (or just some number |
| 193 | of messages are lost), and subsequently the master crashes, it is |
| 194 | possible to lose committed transactions. Again, transactional |
| 195 | durability is not being maintained. Reliability can be improved in a |
| 196 | couple of ways:</p> |
| 197 | <div class="orderedlist"> |
| 198 | <ol type="1"> |
| 199 | <li> |
| 200 | <p> |
| 201 | Use a reliable network protocol (for example, TCP/IP instead of UDP). |
| 202 | </p> |
| 203 | </li> |
| 204 | <li> |
| 205 | <p> |
| 206 | Increase the number of clients and network paths to make it |
| 207 | less likely that a message will be lost. In this case, it is |
| 208 | important to also make sure a client that did receive the |
| 209 | message wins any subsequent election. If a client that did not |
| 210 | receive the message wins a subsequent election, data can still |
| 211 | be lost. |
| 212 | </p> |
| 213 | </li> |
| 214 | </ol> |
| 215 | </div> |
| 216 | <p>Further, systems may want to guarantee message delivery to the client(s) |
| 217 | (for example, to prevent a network connection from simply discarding |
| 218 | messages). Some systems may want to ensure clients never return |
| 219 | out-of-date information, that is, once a transaction commit returns |
| 220 | success on the master, no client will return old information to a |
| 221 | read-only query. Some of the following changes to a Base API application |
| 222 | may be used to address these issues:</p> |
| 223 | <div class="orderedlist"> |
| 224 | <ol type="1"> |
| 225 | <li> |
| 226 | <p> |
| 227 | Write the application's <span class="bold"><strong>send</strong></span> |
| 228 | function to not return to Berkeley DB until one or more clients |
| 229 | have acknowledged receipt of the message. The number of |
| 230 | clients chosen will be dependent on the application: you will |
| 231 | want to consider likely network partitions (ensure that a |
| 232 | client at each physical site receives the message) and |
| 233 | geographical diversity (ensure that a client on each coast |
| 234 | receives the message). |
| 235 | </p> |
| 236 | </li> |
| 237 | <li> |
| 238 | <p> |
| 239 | Write the client's message processing loop to not acknowledge |
| 240 | receipt of the message until a call to the <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method |
| 241 | has returned success. Messages resulting in a return of |
| 242 | <a href="../api_reference/C/repmessage.html#repmsg_DB_REP_NOTPERM" class="olink">DB_REP_NOTPERM</a> from the <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method mean the message |
| 243 | could not be flushed to the client's disk. If the client does |
| 244 | not acknowledge receipt of such messages to the master until a |
| 245 | subsequent call to the <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method returns |
| 246 | <a href="../api_reference/C/repmessage.html#repmsg_DB_REP_ISPERM" class="olink">DB_REP_ISPERM</a> and the LSN returned is at least as large as |
| 247 | this message's LSN, then the master's <span class="bold"><strong>send</strong></span> function will not return |
| 248 | success to the Berkeley DB library. This means the thread |
| 249 | committing the transaction on the master will not be allowed to |
| 250 | proceed based on the transaction having committed until the |
| 251 | selected set of clients have received the message and consider |
| 252 | it complete. |
| 253 | </p> |
| 254 | <p> |
| 255 | Alternatively, the client's message processing loop could |
| 256 | acknowledge the message to the master, but with an error code |
| 257 | indicating that the application's <span class="bold"><strong>send</strong></span> function should not return to |
| 258 | the Berkeley DB library until a subsequent acknowledgement from |
| 259 | the same client indicates success. |
| 260 | </p> |
| 261 | <p> |
| 262 | The application send callback function invoked by Berkeley DB |
| 263 | contains an LSN of the record being sent (if appropriate for |
| 264 | that record). When <a href="../api_reference/C/repmessage.html" class="olink">DB_ENV->rep_process_message()</a> method returns indicators that |
| 265 | a permanent record has been written then it also returns the |
| 266 | maximum LSN of the permanent record written. |
| 267 | </p> |
| 268 | </li> |
| 269 | </ol> |
| 270 | </div> |
| 271 | <p>There is one final pair of failure scenarios to consider. First, it is |
| 272 | not possible to abort transactions after the application's <span class="bold"><strong>send</strong></span> |
| 273 | function has been called, as the master may have already written the |
| 274 | commit log records to disk, and so abort is no longer an option. |
| 275 | Second, a related problem is that even though the master will attempt |
| 276 | to flush the local log if the <span class="bold"><strong>send</strong></span> function returns failure, |
| 277 | that flush may fail (for example, when the local disk is full). Again, |
| 278 | the transaction cannot be aborted as one or more clients may have |
| 279 | committed the transaction even if <span class="bold"><strong>send</strong></span> returns failure. Rare |
| 280 | applications may not be able to tolerate these unlikely failure modes. |
| 281 | In that case the application may want to:</p> |
| 282 | <div class="orderedlist"> |
| 283 | <ol type="1"> |
| 284 | <li> |
| 285 | <p> |
| 286 | Configure the master to do always local synchronous commits |
| 287 | (turning off the <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> configuration). This will |
| 288 | decrease performance significantly, of course (one of the |
| 289 | reasons to use replication is to avoid local disk writes.) In |
| 290 | this configuration, failure to write the local log will cause |
| 291 | the transaction to abort in all cases. |
| 292 | </p> |
| 293 | </li> |
| 294 | <li> |
| 295 | <p> |
| 296 | Do not return from the application's <span class="bold"><strong>send</strong></span> function under any conditions, |
| 297 | until the selected set of clients has acknowledged the message. |
| 298 | Until the <span class="bold"><strong>send</strong></span> function |
| 299 | returns to the Berkeley DB library, the thread committing the |
| 300 | transaction on the master will wait, and so no application will |
| 301 | be able to act on the knowledge that the transaction has |
| 302 | committed. |
| 303 | </p> |
| 304 | </li> |
| 305 | </ol> |
| 306 | </div> |
| 307 | </div> |
| 308 | <div class="navfooter"> |
| 309 | <hr /> |
| 310 | <table width="100%" summary="Navigation footer"> |
| 311 | <tr> |
| 312 | <td width="40%" align="left"><a accesskey="p" href="rep_bulk.html">Prev</a> </td> |
| 313 | <td width="20%" align="center"> |
| 314 | <a accesskey="u" href="rep.html">Up</a> |
| 315 | </td> |
| 316 | <td width="40%" align="right"> <a accesskey="n" href="rep_lease.html">Next</a></td> |
| 317 | </tr> |
| 318 | <tr> |
| 319 | <td width="40%" align="left" valign="top">Bulk transfer </td> |
| 320 | <td width="20%" align="center"> |
| 321 | <a accesskey="h" href="index.html">Home</a> |
| 322 | </td> |
| 323 | <td width="40%" align="right" valign="top"> Master Leases</td> |
| 324 | </tr> |
| 325 | </table> |
| 326 | </div> |
| 327 | </body> |
| 328 | </html> |