| 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>Read your writes consistency</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_lease.html" title="Master Leases" /> |
| 12 | <link rel="next" href="rep_clock_skew.html" title="Clock Skew" /> |
| 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">Read your writes consistency</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="rep_lease.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_clock_skew.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_ryw"></a>Read your writes consistency</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <div class="toc"> |
| 42 | <dl> |
| 43 | <dt> |
| 44 | <span class="sect2"> |
| 45 | <a href="rep_ryw.html#gettoken">Getting a token</a> |
| 46 | </span> |
| 47 | </dt> |
| 48 | <dt> |
| 49 | <span class="sect2"> |
| 50 | <a href="rep_ryw.html#tokenhandling">Token handling</a> |
| 51 | </span> |
| 52 | </dt> |
| 53 | <dt> |
| 54 | <span class="sect2"> |
| 55 | <a href="rep_ryw.html#usingtoken">Using a token to check or wait for a transaction</a> |
| 56 | </span> |
| 57 | </dt> |
| 58 | </dl> |
| 59 | </div> |
| 60 | <p> |
| 61 | Some applications require the ability to read replicated data at a |
| 62 | client site, and determine whether it is consistent with data that |
| 63 | has been written previously at the master site. |
| 64 | </p> |
| 65 | <p> |
| 66 | For example, a web application may be backed by multiple database |
| 67 | environments, linked to form a replication group, in order to share |
| 68 | the workload. Web requests that update data must be served by the |
| 69 | replication master, but any site in the group may serve a read-only |
| 70 | request. Consider a work flow of a series of web requests from one |
| 71 | specific user at a web browser: the first request generates a |
| 72 | database update, but the second request merely reads data. If the |
| 73 | read-only request is served by a replication client database |
| 74 | environment, it may be important to make sure that the updated data |
| 75 | has been replicated to the client before performing the read (or to wait |
| 76 | until it has been replicated) in order to show this user a |
| 77 | consistent view of the data. |
| 78 | </p> |
| 79 | <p> |
| 80 | Berkeley DB supports this requirement through the use of transaction |
| 81 | "tokens". A token is a form of identification for a transaction |
| 82 | within the scope of the replication group. The application may |
| 83 | request a copy of the transaction's token at the master site during |
| 84 | the execution of the transaction. Later, the application running |
| 85 | on a client site can use a copy of the token to determine whether |
| 86 | the transaction has been applied at that site. |
| 87 | </p> |
| 88 | <p> |
| 89 | It is the application's responsibility to keep track of the token |
| 90 | during the interim. In the web example, the token might be sent to |
| 91 | the browser as a "cookie", or stored on the application server in |
| 92 | the user's session context. |
| 93 | </p> |
| 94 | <p> |
| 95 | The operations described here are supported both for Replication |
| 96 | Manager applications and for applications that use the replication |
| 97 | Base API. |
| 98 | </p> |
| 99 | <div class="sect2" lang="en" xml:lang="en"> |
| 100 | <div class="titlepage"> |
| 101 | <div> |
| 102 | <div> |
| 103 | <h3 class="title"><a id="gettoken"></a>Getting a token</h3> |
| 104 | </div> |
| 105 | </div> |
| 106 | </div> |
| 107 | <p> |
| 108 | In order to get a token, the application must supply a small |
| 109 | memory buffer, using the <a href="../api_reference/C/txnset_commit_token.html" class="olink">DB_TXN->set_commit_token()</a> method. |
| 110 | </p> |
| 111 | <p> |
| 112 | Note that a token is generated only upon a successful commit |
| 113 | operation, and therefore the token buffer content is valid |
| 114 | only after a successful commit. Also, if a transaction does |
| 115 | not perform any update operations it does not generate a useful |
| 116 | token. |
| 117 | </p> |
| 118 | <p> |
| 119 | In the Berkeley DB Java and C# API, getting a token is simpler. |
| 120 | The application need only invoke the <a class="ulink" href="../java/com/sleepycat/db/Transaction.html#getCommitToken()" target="_top">Transaction.getCommitToken()</a> method, |
| 121 | after the transaction has committed. |
| 122 | </p> |
| 123 | </div> |
| 124 | <div class="sect2" lang="en" xml:lang="en"> |
| 125 | <div class="titlepage"> |
| 126 | <div> |
| 127 | <div> |
| 128 | <h3 class="title"><a id="tokenhandling"></a>Token handling</h3> |
| 129 | </div> |
| 130 | </div> |
| 131 | </div> |
| 132 | <p> |
| 133 | The application should not try to interpret the content of the |
| 134 | token buffer, but may store and/or transmit it freely between |
| 135 | systems. However, since the buffer contains binary data it |
| 136 | may be necessary to apply some encoding for transmission (e.g., |
| 137 | base 64). |
| 138 | </p> |
| 139 | <p> |
| 140 | The data is resilient to differences in byte order between |
| 141 | different systems. It does not expire: it may be retained |
| 142 | indefinitely for later use, even across Berkeley DB version |
| 143 | upgrades. |
| 144 | </p> |
| 145 | </div> |
| 146 | <div class="sect2" lang="en" xml:lang="en"> |
| 147 | <div class="titlepage"> |
| 148 | <div> |
| 149 | <div> |
| 150 | <h3 class="title"><a id="usingtoken"></a>Using a token to check or wait for a transaction</h3> |
| 151 | </div> |
| 152 | </div> |
| 153 | </div> |
| 154 | <p> |
| 155 | The <a href="../api_reference/C/envtxn_applied.html" class="olink">DB_ENV->txn_applied()</a> method takes a copy of a token, and |
| 156 | determines whether the corresponding transaction is currently |
| 157 | applied at the local site. The timeout argument allows the |
| 158 | application to block for a bounded amount of time for cases |
| 159 | where the transaction has not yet been applied. |
| 160 | </p> |
| 161 | <p> |
| 162 | Depending on the transaction durability levels implemented or |
| 163 | configured by the application, it is sometimes possible for a |
| 164 | transaction to disappear from a replication group if an |
| 165 | original master site fails and a different site becomes the new |
| 166 | master without having received the transaction. When the |
| 167 | <a href="../api_reference/C/envtxn_applied.html" class="olink">DB_ENV->txn_applied()</a> method discovers this, it produces the |
| 168 | <code class="literal">DB_NOTFOUND</code> return code. |
| 169 | </p> |
| 170 | <p> |
| 171 | This means that the results of <a href="../api_reference/C/envtxn_applied.html" class="olink">DB_ENV->txn_applied()</a> are not guaranteed |
| 172 | forever. Even after a successful call to <a href="../api_reference/C/envtxn_applied.html" class="olink">DB_ENV->txn_applied()</a>, it is |
| 173 | possible that by the time the application tries to read the |
| 174 | data, the transaction and its data could have disappeared. |
| 175 | </p> |
| 176 | <p> |
| 177 | To avoid this problem the application should do the read |
| 178 | operations in the context of a transaction, and hold the |
| 179 | transaction handle open during the <a href="../api_reference/C/envtxn_applied.html" class="olink">DB_ENV->txn_applied()</a> call. The |
| 180 | <a href="../api_reference/C/envtxn_applied.html" class="olink">DB_ENV->txn_applied()</a> method itself does not actually execute in the |
| 181 | context of the transaction; but no rollbacks due to new master |
| 182 | synchronization ever occur while a transaction is active, even |
| 183 | a read-only transaction at a client site. |
| 184 | </p> |
| 185 | <p> |
| 186 | Note that the <a href="../api_reference/C/envtxn_applied.html" class="olink">DB_ENV->txn_applied()</a> method can return |
| 187 | <code class="literal">DB_LOCK_DEADLOCK</code>. The application should |
| 188 | respond to this situation just as it does for any other normal |
| 189 | operation: abort any existing transaction, and then pause |
| 190 | briefly before retrying. |
| 191 | </p> |
| 192 | </div> |
| 193 | </div> |
| 194 | <div class="navfooter"> |
| 195 | <hr /> |
| 196 | <table width="100%" summary="Navigation footer"> |
| 197 | <tr> |
| 198 | <td width="40%" align="left"><a accesskey="p" href="rep_lease.html">Prev</a> </td> |
| 199 | <td width="20%" align="center"> |
| 200 | <a accesskey="u" href="rep.html">Up</a> |
| 201 | </td> |
| 202 | <td width="40%" align="right"> <a accesskey="n" href="rep_clock_skew.html">Next</a></td> |
| 203 | </tr> |
| 204 | <tr> |
| 205 | <td width="40%" align="left" valign="top">Master Leases </td> |
| 206 | <td width="20%" align="center"> |
| 207 | <a accesskey="h" href="index.html">Home</a> |
| 208 | </td> |
| 209 | <td width="40%" align="right" valign="top"> Clock Skew</td> |
| 210 | </tr> |
| 211 | </table> |
| 212 | </div> |
| 213 | </body> |
| 214 | </html> |