| 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>Transaction tuning</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="transapp.html" title="Chapter 11. Berkeley DB Transactional Data Store Applications" /> |
| 11 | <link rel="prev" href="transapp_reclimit.html" title="Berkeley DB recoverability" /> |
| 12 | <link rel="next" href="transapp_throughput.html" title="Transaction throughput" /> |
| 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">Transaction tuning</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="transapp_reclimit.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 11. |
| 26 | Berkeley DB Transactional Data Store Applications |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="transapp_throughput.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="transapp_tune"></a>Transaction tuning</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>There are a few different issues to consider when tuning the performance |
| 42 | of Berkeley DB transactional applications. First, you should review |
| 43 | <a class="xref" href="am_misc_tune.html" title="Access method tuning">Access method tuning</a>, as the |
| 44 | tuning issues for access method applications are applicable to |
| 45 | transactional applications as well. The following are additional tuning |
| 46 | issues for Berkeley DB transactional applications:</p> |
| 47 | <div class="variablelist"> |
| 48 | <dl> |
| 49 | <dt> |
| 50 | <span class="term">access method</span> |
| 51 | </dt> |
| 52 | <dd>Highly concurrent applications should use the Queue access method, where |
| 53 | possible, as it provides finer-granularity of locking than the other |
| 54 | access methods. Otherwise, applications usually see better concurrency |
| 55 | when using the Btree access method than when using either the Hash or |
| 56 | Recno access methods.</dd> |
| 57 | <dt> |
| 58 | <span class="term">record numbers</span> |
| 59 | </dt> |
| 60 | <dd>Using record numbers outside of the Queue access method will often slow |
| 61 | down concurrent applications as they limit the degree of concurrency |
| 62 | available in the database. Using the Recno access method, or the Btree |
| 63 | access method with retrieval by record number configured can slow |
| 64 | applications down.</dd> |
| 65 | <dt> |
| 66 | <span class="term">Btree database size</span> |
| 67 | </dt> |
| 68 | <dd>When using the Btree access method, applications supporting concurrent |
| 69 | access may see excessive numbers of deadlocks in small databases. There |
| 70 | are two different approaches to resolving this problem. First, as the |
| 71 | Btree access method uses page-level locking, decreasing the database |
| 72 | page size can result in fewer lock conflicts. Second, in the case of |
| 73 | databases that are cyclically growing and shrinking, turning off reverse |
| 74 | splits (with <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_REVSPLITOFF" class="olink">DB_REVSPLITOFF</a>) can leave the database with enough |
| 75 | pages that there will be fewer lock conflicts.</dd> |
| 76 | <dt> |
| 77 | <span class="term">read locks</span> |
| 78 | </dt> |
| 79 | <dd>Performing all read operations outside of transactions or at |
| 80 | <a class="xref" href="transapp_read.html" title="Degrees of isolation">Degrees of isolation</a> can often |
| 81 | significantly increase application throughput. In addition, limiting |
| 82 | the lifetime of non-transactional cursors will reduce the length of |
| 83 | times locks are held, thereby improving concurrency.</dd> |
| 84 | <dt> |
| 85 | <span class="term"><a href="../api_reference/C/envset_flags.html#set_flags_DB_DIRECT_DB" class="olink">DB_DIRECT_DB</a>, <a href="../api_reference/C/envlog_set_config.html#log_set_config_DB_LOG_DIRECT" class="olink">DB_LOG_DIRECT</a></span> |
| 86 | </dt> |
| 87 | <dd> |
| 88 | On some systems, avoiding caching in the operating system can improve |
| 89 | write throughput and allow the creation of larger Berkeley DB caches.</dd> |
| 90 | <dt> |
| 91 | <span class="term"><a href="../api_reference/C/dbopen.html#dbopen_DB_READ_UNCOMMITTED" class="olink">DB_READ_UNCOMMITTED</a>, <a href="../api_reference/C/dbcget.html#dbcget_DB_READ_COMMITTED" class="olink">DB_READ_COMMITTED</a></span> |
| 92 | </dt> |
| 93 | <dd> |
| 94 | <p> |
| 95 | Consider decreasing the level of isolation of transaction using |
| 96 | the <a href="../api_reference/C/dbopen.html#dbopen_DB_READ_UNCOMMITTED" class="olink">DB_READ_UNCOMMITTED</a>, or <a href="../api_reference/C/dbcget.html#dbcget_DB_READ_COMMITTED" class="olink">DB_READ_COMMITTED</a> flags for |
| 97 | transactions or cursors or the <a href="../api_reference/C/dbopen.html#dbopen_DB_READ_UNCOMMITTED" class="olink">DB_READ_UNCOMMITTED</a> flag on |
| 98 | individual read operations. The <a href="../api_reference/C/dbcget.html#dbcget_DB_READ_COMMITTED" class="olink">DB_READ_COMMITTED</a> flag will |
| 99 | release read locks on cursors as soon as the data page is |
| 100 | nolonger referenced. This is also called |
| 101 | <span class="emphasis"><em> degree 2 isolation</em></span>. This will |
| 102 | tend to block write operations for shorter periods for |
| 103 | applications that do not need to have repeatable reads for |
| 104 | cursor operations. |
| 105 | </p> |
| 106 | <p> |
| 107 | The <a href="../api_reference/C/dbopen.html#dbopen_DB_READ_UNCOMMITTED" class="olink">DB_READ_UNCOMMITTED</a> flag will allow read operations to |
| 108 | potentially return data which has been modified but not yet |
| 109 | committed, and can significantly increase application |
| 110 | throughput in applications that do not require data be |
| 111 | guaranteed to be permanent in the database. This is also |
| 112 | called <span class="emphasis"><em>degree 1 isolation</em></span>, |
| 113 | or <span class="emphasis"><em>dirty reads</em></span>. |
| 114 | </p> |
| 115 | </dd> |
| 116 | <dt> |
| 117 | <span class="term"> |
| 118 | <a href="../api_reference/C/dbcget.html#dbcget_DB_RMW" class="olink">DB_RMW</a> |
| 119 | </span> |
| 120 | </dt> |
| 121 | <dd> If there are many deadlocks, consider |
| 122 | using the <a href="../api_reference/C/dbcget.html#dbcget_DB_RMW" class="olink">DB_RMW</a> flag to |
| 123 | immediately acquire write locks when reading data items that will |
| 124 | subsequently be modified. Although this flag may increase contention |
| 125 | (because write locks are held longer than they would otherwise be), it |
| 126 | may decrease the number of deadlocks that occur.</dd> |
| 127 | <dt> |
| 128 | <span class="term"><a href="../api_reference/C/envset_flags.html#set_flags_DB_TXN_WRITE_NOSYNC" class="olink">DB_TXN_WRITE_NOSYNC</a>, <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a></span> |
| 129 | </dt> |
| 130 | <dd>By default, transactional commit in Berkeley DB implies durability, that is, |
| 131 | all committed operations will be present in the database after recovery |
| 132 | from any application or system failure. For applications not requiring |
| 133 | that level of certainty, specifying the <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> flag will |
| 134 | often provide a significant performance improvement. In this case, the |
| 135 | database will still be fully recoverable, but some number of committed |
| 136 | transactions might be lost after application or system failure.</dd> |
| 137 | <dt> |
| 138 | <span class="term">access databases in order</span> |
| 139 | </dt> |
| 140 | <dd>When modifying multiple databases in a single transaction, always access |
| 141 | physical files and databases within physical files, in the same order |
| 142 | where possible. In addition, avoid returning to a physical file or |
| 143 | database, that is, avoid accessing a database, moving on to another |
| 144 | database and then returning to the first database. This can |
| 145 | significantly reduce the chance of deadlock between threads of |
| 146 | control.</dd> |
| 147 | <dt> |
| 148 | <span class="term">large key/data items</span> |
| 149 | </dt> |
| 150 | <dd>Transactional protections in Berkeley DB are guaranteed by before and after |
| 151 | physical image logging. This means applications modifying large |
| 152 | key/data items also write large log records, and, in the case of the |
| 153 | default transaction commit, threads of control must wait until those |
| 154 | log records have been flushed to disk. Applications supporting |
| 155 | concurrent access should try and keep key/data items small wherever |
| 156 | possible.</dd> |
| 157 | <dt> |
| 158 | <span class="term">mutex selection</span> |
| 159 | </dt> |
| 160 | <dd> |
| 161 | <p> |
| 162 | During configuration, Berkeley DB selects a mutex implementation |
| 163 | for the architecture. Berkeley DB normally prefers blocking-mutex |
| 164 | implementations over non-blocking ones. For example, Berkeley DB |
| 165 | will select POSIX pthread mutex interfaces rather than |
| 166 | assembly-code test-and-set spin mutexes because pthread mutexes are |
| 167 | usually more efficient and less likely to waste CPU cycles spinning |
| 168 | without getting any work accomplished. |
| 169 | </p> |
| 170 | <p> |
| 171 | For some applications and systems (generally highly concurrent |
| 172 | applications on large multiprocessor systems), Berkeley DB makes |
| 173 | the wrong choice. In some cases, better performance can be |
| 174 | achieved by configuring with the <a href="../installation/build_unix_conf.html#build_unix_conf.--with-mutex" class="olink">--with-mutex</a> |
| 175 | argument and selecting a different mutex implementation than the |
| 176 | one selected by Berkeley DB. When a test-and-set spin mutex |
| 177 | implementation is selected, it may be useful to tune the number of |
| 178 | spins made before yielding the processor and sleeping. For more |
| 179 | information, see the <a href="../api_reference/C/mutexset_tas_spins.html" class="olink">DB_ENV->mutex_set_tas_spins()</a> method. |
| 180 | </p> |
| 181 | <p> |
| 182 | Finally, Berkeley DB may put multiple mutexes on individual cache |
| 183 | lines. When tuning Berkeley DB for large multiprocessor systems, |
| 184 | it may be useful to tune mutex alignment using the <a href="../api_reference/C/mutexset_align.html" class="olink">DB_ENV->mutex_set_align()</a> |
| 185 | method. |
| 186 | </p> |
| 187 | </dd> |
| 188 | <dt> |
| 189 | <span class="term"> |
| 190 | <a href="../installation/build_unix_conf.html#build_unix_conf.--enable-posixmutexes" class="olink">--enable-posix-mutexes</a> |
| 191 | </span> |
| 192 | </dt> |
| 193 | <dd>By default, the Berkeley DB library will only select the POSIX pthread mutex |
| 194 | implementation if it supports mutexes shared between multiple processes. |
| 195 | If your application does not share its database environment between |
| 196 | processes and your system's POSIX mutex support was not selected because |
| 197 | it did not support inter-process mutexes, you may be able to increase |
| 198 | performance and transactional throughput by configuring with the |
| 199 | <a href="../installation/build_unix_conf.html#build_unix_conf.--enable-posixmutexes" class="olink">--enable-posix-mutexes</a> argument.</dd> |
| 200 | <dt> |
| 201 | <span class="term">log buffer size</span> |
| 202 | </dt> |
| 203 | <dd>Berkeley DB internally maintains a buffer of log writes. The buffer is |
| 204 | written to disk at transaction commit, by default, or, whenever it |
| 205 | is filled. If it is consistently being filled before transaction |
| 206 | commit, it will be written multiple times per transaction, costing |
| 207 | application performance. In these cases, increasing the size of the |
| 208 | log buffer can increase application throughput.</dd> |
| 209 | <dt> |
| 210 | <span class="term">log file location</span> |
| 211 | </dt> |
| 212 | <dd>If the database environment's log files are on the same disk as the |
| 213 | databases, the disk arms will have to seek back-and-forth between the |
| 214 | two. Placing the log files and the databases on different disk arms |
| 215 | can often increase application throughput.</dd> |
| 216 | <dt> |
| 217 | <span class="term">trickle write</span> |
| 218 | </dt> |
| 219 | <dd>In some applications, the cache is sufficiently active and dirty that |
| 220 | readers frequently need to write a dirty page in order to have space in |
| 221 | which to read a new page from the backing database file. You can use |
| 222 | the <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility (or the statistics returned by the |
| 223 | <a href="../api_reference/C/mempstat.html" class="olink">DB_ENV->memp_stat()</a> method) to see how often this is happening in your |
| 224 | application's cache. In this case, using a separate thread of control |
| 225 | and the <a href="../api_reference/C/memptrickle.html" class="olink">DB_ENV->memp_trickle()</a> method to trickle-write pages can often increase |
| 226 | the overall throughput of the application.</dd> |
| 227 | </dl> |
| 228 | </div> |
| 229 | </div> |
| 230 | <div class="navfooter"> |
| 231 | <hr /> |
| 232 | <table width="100%" summary="Navigation footer"> |
| 233 | <tr> |
| 234 | <td width="40%" align="left"><a accesskey="p" href="transapp_reclimit.html">Prev</a> </td> |
| 235 | <td width="20%" align="center"> |
| 236 | <a accesskey="u" href="transapp.html">Up</a> |
| 237 | </td> |
| 238 | <td width="40%" align="right"> <a accesskey="n" href="transapp_throughput.html">Next</a></td> |
| 239 | </tr> |
| 240 | <tr> |
| 241 | <td width="40%" align="left" valign="top">Berkeley DB recoverability </td> |
| 242 | <td width="20%" align="center"> |
| 243 | <a accesskey="h" href="index.html">Home</a> |
| 244 | </td> |
| 245 | <td width="40%" align="right" valign="top"> Transaction throughput</td> |
| 246 | </tr> |
| 247 | </table> |
| 248 | </div> |
| 249 | </body> |
| 250 | </html> |