| 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>Memory-only or Flash configurations</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="program.html" title="Chapter 15. Programmer Notes" /> |
| 11 | <link rel="prev" href="program_namespace.html" title="Name spaces" /> |
| 12 | <link rel="next" href="program_cache.html" title="Disk drive caches" /> |
| 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">Memory-only or Flash configurations</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="program_namespace.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 15. |
| 26 | Programmer Notes |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="program_cache.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="program_ram"></a>Memory-only or Flash configurations</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>Berkeley DB supports a variety of memory-based configurations for systems |
| 42 | where filesystem space is either limited in availability or entirely |
| 43 | replaced by some combination of memory and Flash. In addition, Berkeley DB |
| 44 | can be configured to minimize writes to the filesystem when the |
| 45 | filesystem is backed by Flash memory.</p> |
| 46 | <p>There are four parts of the Berkeley DB database environment normally written |
| 47 | to the filesystem: the database environment region files, the database |
| 48 | files, the database environment log files and the replication internal |
| 49 | files. Each of these items can |
| 50 | be configured to live in memory rather than in the filesystem:</p> |
| 51 | <div class="variablelist"> |
| 52 | <dl> |
| 53 | <dt> |
| 54 | <span class="term">The database environment region files:</span> |
| 55 | </dt> |
| 56 | <dd> |
| 57 | <p> |
| 58 | Each of the Berkeley DB subsystems in a database environment is |
| 59 | described by one or more regions, or chunks of memory. The regions |
| 60 | contain all of the per-process and per-thread shared information |
| 61 | (including mutexes), that comprise a Berkeley DB environment. By |
| 62 | default, these regions are backed by the filesystem. In situations |
| 63 | where filesystem backed regions aren't optimal, applications can |
| 64 | create memory-only database environments in two different types of |
| 65 | memory: either in the application's heap memory or in system shared |
| 66 | memory. |
| 67 | </p> |
| 68 | <p> |
| 69 | To create the database environment in heap memory, specify the |
| 70 | <a href="../api_reference/C/envopen.html#envopen_DB_PRIVATE" class="olink">DB_PRIVATE</a> flag to the <a href="../api_reference/C/envopen.html" class="olink">DB_ENV->open()</a> method. Note that database |
| 71 | environments created in heap memory are only accessible to the |
| 72 | threads of a single process, however. |
| 73 | </p> |
| 74 | <p> |
| 75 | To create the database environment in system shared memory, specify |
| 76 | the <a href="../api_reference/C/envopen.html#envopen_DB_SYSTEM_MEM" class="olink">DB_SYSTEM_MEM</a> flag to the <a href="../api_reference/C/envopen.html" class="olink">DB_ENV->open()</a> method. Database |
| 77 | environments created in system memory are accessible to multiple |
| 78 | processes, but note that database environments created in system |
| 79 | shared memory do create a small (roughly 8 byte) file in the |
| 80 | filesystem, read by the processes to identify which system shared |
| 81 | memory segments to use. |
| 82 | </p> |
| 83 | <p> |
| 84 | For more information, see <a class="xref" href="env_region.html" title="Shared memory regions">Shared memory regions</a>. |
| 85 | </p> |
| 86 | </dd> |
| 87 | <dt> |
| 88 | <span class="term">The database files:</span> |
| 89 | </dt> |
| 90 | <dd> |
| 91 | <p> |
| 92 | By default, databases are periodically flushed from the |
| 93 | Berkeley DB memory cache to backing physical files in the |
| 94 | filesystem. To keep databases from being written to backing |
| 95 | physical files, pass the <a href="../api_reference/C/mempset_flags.html#mpool_set_flags_DB_MPOOL_NOFILE" class="olink">DB_MPOOL_NOFILE</a> flag to the |
| 96 | <a href="../api_reference/C/mempset_flags.html" class="olink">DB_MPOOLFILE->set_flags()</a> method. This flag |
| 97 | implies the application's databases must fit entirely in the |
| 98 | Berkeley DB cache, of course. To avoid a database file growing |
| 99 | to consume the entire cache, applications can limit the size of |
| 100 | individual databases in the cache by calling the |
| 101 | <a href="../api_reference/C/mempset_maxsize.html" class="olink">DB_MPOOLFILE->set_maxsize()</a> method. |
| 102 | </p> |
| 103 | </dd> |
| 104 | <dt> |
| 105 | <span class="term">The database environment log files:</span> |
| 106 | </dt> |
| 107 | <dd> |
| 108 | <p> |
| 109 | If a database environment is not intended to be transactionally |
| 110 | recoverable after application or system failure (that is, if it |
| 111 | will not exhibit the transactional attribute of "durability"), |
| 112 | applications should not configure the database environment for |
| 113 | logging or transactions, in which case no log files will be |
| 114 | created. If the database environment is intended to be |
| 115 | durable, log files must either be written to stable storage and |
| 116 | recovered after application or system failure, or they must be |
| 117 | replicated to other systems. |
| 118 | </p> |
| 119 | <p> |
| 120 | In applications running on systems without any form of stable |
| 121 | storage, durability must be accomplished through replication. |
| 122 | In this case, database environments should be configured to |
| 123 | maintain database logs in memory, rather than in the |
| 124 | filesystem, by specifying the <a href="../api_reference/C/envlog_set_config.html#log_set_config_DB_LOG_IN_MEMORY" class="olink">DB_LOG_IN_MEMORY</a> flag to the |
| 125 | <a href="../api_reference/C/envlog_set_config.html" class="olink">DB_ENV->log_set_config()</a> method. |
| 126 | </p> |
| 127 | </dd> |
| 128 | <dt> |
| 129 | <span class="term">The replication internal files:</span> |
| 130 | </dt> |
| 131 | <dd> |
| 132 | <p> |
| 133 | By default, Berkeley DB replication stores a small amount of |
| 134 | internal data in the filesystem. To store this replication |
| 135 | internal data in memory only and not in the filesystem, specify |
| 136 | the <a href="../api_reference/C/repconfig.html#config_DB_REP_CONF_INMEM" class="olink">DB_REP_CONF_INMEM</a> flag to the <a href="../api_reference/C/repconfig.html" class="olink">DB_ENV->rep_set_config()</a> method before |
| 137 | opening the database environment. |
| 138 | </p> |
| 139 | </dd> |
| 140 | </dl> |
| 141 | </div> |
| 142 | <p>In systems where the filesystem is backed by Flash memory, the number |
| 143 | of times the Flash memory is written may be a concern. Each of the |
| 144 | four parts of the Berkeley DB database environment normally written to the |
| 145 | filesystem can be configured to minimize the number of times the |
| 146 | filesystem is written:</p> |
| 147 | <div class="variablelist"> |
| 148 | <dl> |
| 149 | <dt> |
| 150 | <span class="term">The database environment region files:</span> |
| 151 | </dt> |
| 152 | <dd> |
| 153 | <p> |
| 154 | On a Flash-based filesystem, the database environment should be placed |
| 155 | in heap or system memory, as described previously. |
| 156 | </p> |
| 157 | </dd> |
| 158 | <dt> |
| 159 | <span class="term">The database files:</span> |
| 160 | </dt> |
| 161 | <dd> |
| 162 | <p> |
| 163 | The Berkeley DB library maintains a cache of database pages. |
| 164 | The database pages are only written to backing physical files |
| 165 | when the application checkpoints the database environment with |
| 166 | the <a href="../api_reference/C/txncheckpoint.html" class="olink">DB_ENV->txn_checkpoint()</a> method, when database handles are closed |
| 167 | with the <a href="../api_reference/C/dbclose.html" class="olink">DB->close()</a> method, or when the application explicitly |
| 168 | flushes the cache with the <a href="../api_reference/C/dbsync.html" class="olink">DB->sync()</a> or <a href="../api_reference/C/mempsync.html" class="olink">DB_ENV->memp_sync()</a> methods. |
| 169 | </p> |
| 170 | <p> |
| 171 | To avoid unnecessary writes of Flash memory due to checkpoints, |
| 172 | applications should decrease the frequency of their |
| 173 | checkpoints. This is especially important in applications |
| 174 | which repeatedly modify a specific database page, as repeatedly |
| 175 | writing a database page to the backing physical file will |
| 176 | repeatedly update the same blocks of the filesystem. |
| 177 | </p> |
| 178 | <p> |
| 179 | To avoid unnecessary writes of the filesystem due to closing a |
| 180 | database handle, applications should specify the <a href="../api_reference/C/dbclose.html#dbclose_DB_NOSYNC" class="olink">DB_NOSYNC</a> flag |
| 181 | to the <a href="../api_reference/C/dbclose.html" class="olink">DB->close()</a> method. |
| 182 | </p> |
| 183 | <p> |
| 184 | To avoid unnecessary writes of the filesystem due to flushing |
| 185 | the cache, applications should not explicitly flush the cache |
| 186 | under normal conditions – flushing the cache is rarely if |
| 187 | ever needed in a normally-running application. |
| 188 | </p> |
| 189 | </dd> |
| 190 | <dt> |
| 191 | <span class="term">The database environment log files:</span> |
| 192 | </dt> |
| 193 | <dd> |
| 194 | <p> |
| 195 | The Berkeley DB log files do not repeatedly overwrite the same |
| 196 | blocks of the filesystem as the Berkeley DB log files are not |
| 197 | implemented as a circular buffer and log files are not re-used. |
| 198 | For this reason, the Berkeley DB log files should not cause any |
| 199 | difficulties for Flash memory configurations. |
| 200 | </p> |
| 201 | <p> |
| 202 | However, as Berkeley DB does not write log records in |
| 203 | filesystem block sized pieces, it is probable that sequential |
| 204 | transaction commits (each of which flush the log file to the |
| 205 | backing filesystem), will write a block of Flash memory twice, |
| 206 | as the last log record from the first commit will write the |
| 207 | same block of Flash memory as the first log record from the |
| 208 | second commit. Applications not requiring absolute durability |
| 209 | should specify the <a href="../api_reference/C/envset_flags.html#set_flags_DB_TXN_WRITE_NOSYNC" class="olink">DB_TXN_WRITE_NOSYNC</a> or |
| 210 | <a href="../api_reference/C/envset_flags.html#envset_flags_DB_TXN_NOSYNC" class="olink">DB_TXN_NOSYNC</a> flags to the <a href="../api_reference/C/envset_flags.html" class="olink">DB_ENV->set_flags()</a> |
| 211 | method to avoid this overwrite of a block of Flash memory. |
| 212 | </p> |
| 213 | </dd> |
| 214 | <dt> |
| 215 | <span class="term">The replication internal files:</span> |
| 216 | </dt> |
| 217 | <dd> |
| 218 | <p> |
| 219 | On a Flash-based filesystem, the replication internal data |
| 220 | should be stored in memory only, as described previously. |
| 221 | </p> |
| 222 | </dd> |
| 223 | </dl> |
| 224 | </div> |
| 225 | </div> |
| 226 | <div class="navfooter"> |
| 227 | <hr /> |
| 228 | <table width="100%" summary="Navigation footer"> |
| 229 | <tr> |
| 230 | <td width="40%" align="left"><a accesskey="p" href="program_namespace.html">Prev</a> </td> |
| 231 | <td width="20%" align="center"> |
| 232 | <a accesskey="u" href="program.html">Up</a> |
| 233 | </td> |
| 234 | <td width="40%" align="right"> <a accesskey="n" href="program_cache.html">Next</a></td> |
| 235 | </tr> |
| 236 | <tr> |
| 237 | <td width="40%" align="left" valign="top">Name spaces </td> |
| 238 | <td width="20%" align="center"> |
| 239 | <a accesskey="h" href="index.html">Home</a> |
| 240 | </td> |
| 241 | <td width="40%" align="right" valign="top"> Disk drive caches</td> |
| 242 | </tr> |
| 243 | </table> |
| 244 | </div> |
| 245 | </body> |
| 246 | </html> |