@@ -14,7 +14,7 @@ the :doc:`complete list of FAQs </faq>` or post your question to the
14
14
:backlinks: none
15
15
:local:
16
16
17
- What kind of Database is MongoDB?
17
+ What kind of database is MongoDB?
18
18
---------------------------------
19
19
20
20
MongoDB is :term:`document`-oriented DBMS. Think of MySQL but with
@@ -32,7 +32,7 @@ partitioning.
32
32
MongoDB uses :term:`BSON`, a binary object format similar
33
33
to, but more expressive than, :term:`JSON`.
34
34
35
- Do MongoDB Databases Have Tables ?
35
+ Do MongoDB databases have tables ?
36
36
---------------------------------
37
37
38
38
Instead of tables, a MongoDB database stores its data in
@@ -43,19 +43,17 @@ database table, and each document has
43
43
one or more fields, which corresponds to a column in a relational
44
44
database table.
45
45
46
- Collections are have some important differences from RDMS tables:
47
-
48
- - Documents in a single collection may have unique combination and set
49
- of fields. Documents need not have identical fields.
50
-
51
- - You can add a field to some documents in a collection without adding
52
- that field to all documents in the collection.
46
+ Collections have important differences from RDMS tables. Documents in a
47
+ single collection may have a unique combination and set of fields.
48
+ Documents need not have identical fields. You can add a field to some
49
+ documents in a collection without adding that field to all documents in
50
+ the collection.
53
51
54
52
.. see:: :doc:`/reference/sql-comparison`
55
53
56
54
.. _faq-schema-free:
57
55
58
- Do MongoDB Databases have Schemas ?
56
+ Do MongoDB databases have schemas ?
59
57
----------------------------------
60
58
61
59
MongoDB uses dynamic schemas. You can create collections without
@@ -147,26 +145,130 @@ as it can, swapping to disk as needed. Deployments with enough memory
147
145
to fit the application's working data set in RAM will achieve the best
148
146
performance.
149
147
148
+ Do I need a swap space?
149
+ -----------------------
150
+
151
+ You should always have a swap space in case you run into extreme memory
152
+ constraints, memory leaks, or another program stealing a lot of memory.
153
+ Think of the swap space as something like a steam release valve which
154
+ allows excess pressure to release without blowing the system up.
155
+
156
+ But you *do not* need swap for routine use. Database files are
157
+ :ref:`memory-mapped <faq-storage-memory-mapped-files>` and should
158
+ constitute most of your MongoDB memory use. Therefore, it is unlikely
159
+ that :program:`mongod` will ever use any swap space. The memory mapped
160
+ files can simply be released from memory without going to swap or can be
161
+ written back to the database files without needing to be swapped out to
162
+ disk, as they are already backed by files.
163
+
164
+ .. _faq-fundamentals-working-set:
165
+
166
+ Must my working set size fit RAM?
167
+ ---------------------------------
168
+
169
+ Your working set should stay in memory to achieve good performance.
170
+ Otherwise many random disk IO's will occur, and unless you are using
171
+ SSD, this can be quite slow.
172
+
173
+ One area to watch specifically in managing the size of your working set
174
+ is index access patterns. If you are inserting into indexes at random
175
+ locations (as would happen with id's that are randomly
176
+ generated by hashes), you will continually be updating the whole index.
177
+ If instead you are able to create your id's in approximately ascending
178
+ order (for example, day concatenated with a random id), all the updates
179
+ will occur at the right side of the b-tree and the working set size for
180
+ index pages will be much smaller.
181
+
182
+ It is fine if databases and thus virtual size are much larger than RAM.
183
+
184
+ .. todo Commenting out for now:
185
+
186
+ .. _faq-fundamentals-working-set-size:
187
+
188
+ How can I measure working set size?
189
+ -----------------------------------
190
+
191
+ Measuring working set size can be difficult; even if it is much
192
+ smaller than total RAM. If the database is much larger than RAM in
193
+ total, all memory will be indicated as in use for the cache. Thus you
194
+ need a different way to estimate the working set size.
195
+
196
+ One technique is to use the `eatmem.cpp
197
+ <https://github.com/mongodb/mongo-snippets/blob/master/cpp/eatmem.cpp>`_.
198
+ utility, which reserves a certain amount of system memory for itself.
199
+ You can run the utility with a certain amount specified and see if
200
+ the server continues to perform well. If not, the working set is
201
+ larger than the total RAM minus the consumed RAM. The test will eject
202
+ some data from the file system cache, which might take time to page
203
+ back in after the utility is terminated.
204
+
205
+ Running eatmem.cpp continuously with a small percentage of total RAM,
206
+ such as 20%, is a good technique to get an early warning if memory is
207
+ too low. If disk I/O activity increases significantly, terminate
208
+ eatmem.cpp to mitigate the problem for the moment until further steps
209
+ can be taken.
210
+
211
+ In :term:`replica sets <replica set>`, if one server is underpowered
212
+ the eatmem.cpp utility could help as an early warning mechanism for
213
+ server capacity. Of course, the server must be receiving
214
+ representative traffic to get an indication.
215
+
216
+ How do I calculate how much RAM I need for my application?
217
+ ----------------------------------------------------------
218
+
219
+ .. todo Improve this FAQ
220
+
221
+ The amount of RAM you need depends on several factors, including but not
222
+ limited to:
223
+
224
+ - The relationship between :doc:`database storage </faq/storage>` and working set.
225
+
226
+ - The operating system's cache strategy for LRU (Least Recently Used)
227
+
228
+ - The impact of :doc:`journaling </administration/journaling>`
229
+
230
+ - The number or rate of page faults and other MMS gauges to detect when
231
+ you need more RAM
232
+
233
+ MongoDB makes no choices regarding what data is loaded into memory from
234
+ disk. It simply :ref:`memory maps <faq-storage-memory-mapped-files>` all
235
+ its data files and relies on the operating system to cache data. The OS
236
+ typically evicts the least-recently-used data from RAM when it runs low
237
+ on memory. For example if indexes are accessed more frequently than
238
+ documents then indexes will more likely stay in RAM, but it depends on
239
+ your particular usage.
240
+
241
+ To calculate how much RAM you need, you must calculate your working set
242
+ size, i.e., the portion of your data that is frequently accessed. This
243
+ depends on your access patterns, what indexes you have, and the size of
244
+ your documents. To calculate working set size, see :ref:`faq-fundamentals-working-set`.
245
+
246
+ If page faults are infrequent, your
247
+ working set fits in RAM. If fault rates rise higher than that, you risk
248
+ performance degradation. This is less critical with SSD drives than
249
+ with spinning disks.
250
+
251
+ How do I read memory statistics in the UNIX ``top`` command
252
+ -----------------------------------------------------------
253
+
254
+ Because :program:`mongod` uses :ref:`memory-mapped files
255
+ <faq-storage-memory-mapped-files>`, the memory statistics in ``top``
256
+ require interpretation in a special way. On a large database, ``VSIZE``
257
+ (virtual bytes) tends to be the size of the entire database. If the
258
+ :program:`mongod` doesn't have other processes running, ``RSIZE``
259
+ (resident bytes) is the total memory of the machine, as this counts
260
+ file system cache contents.
261
+
262
+ The ``vmstat`` command is also useful for determining
263
+ memory use. On Macintosh computers, the command is ``vm_stat``.
264
+
150
265
How do I configure the cache size?
151
266
----------------------------------
152
267
153
268
MongoDB has no configurable cache. MongoDB uses all *free* memory on
154
269
the system automatically by way of memory-mapped files. Operating
155
270
systems use the same approach with their file system caches.
156
271
157
- Are writes written to disk immediately, or lazily?
158
- --------------------------------------------------
159
-
160
- Writes are physically written to the journal within 100
161
- milliseconds. At that point, the write is "durable" in the sense that
162
- after a pull-plug-from-wall event, the data will still be recoverable after
163
- a hard restart.
164
-
165
- While the journal commit is nearly instant, MongoDB writes to the data
166
- files lazily. MongoDB may wait to write data to the data files for as
167
- much as one minute. This does not affect durability, as the journal
168
- has enough information to ensure crash recovery.
169
-
170
272
.. _faq-database-and-caching:
171
273
172
274
Does MongoDB require a separate caching layer for application-level caching?
@@ -196,6 +298,20 @@ in RAM, MongoDB serves all queries from memory.
196
298
MongoDB does not implement a query cache: MongoDB serves all queries
197
299
directly from the indexes and/or data files.
198
300
301
+ Are writes written to disk immediately, or lazily?
302
+ --------------------------------------------------
303
+
304
+ Writes are physically written to the :doc:`journal </administration/journaling>` within 100
305
+ milliseconds. At that point, the write is "durable" in the sense that
306
+ after a pull-plug-from-wall event, the data will still be recoverable after
307
+ a hard restart.
308
+
309
+ While the journal commit is nearly instant, MongoDB writes to the data
310
+ files lazily. MongoDB may wait to write data to the data files for as
311
+ much as one minute by default. This does not affect durability, as the journal
312
+ has enough information to ensure crash recovery. To change the interval
313
+ for writing to the data files, see :setting:`syncdelay`.
314
+
199
315
What language is MongoDB written in?
200
316
------------------------------------
201
317
@@ -208,7 +324,7 @@ drivers use C extensions for better performance.
208
324
What are the limitations of 32-bit versions of MongoDB?
209
325
-------------------------------------------------------
210
326
211
- MongoDB uses memory-mapped files. When running a 32-bit build of
327
+ MongoDB uses :ref:` memory-mapped files <faq-storage-memory-mapped-files>` . When running a 32-bit build of
212
328
MongoDB, the total storage size for the server, including data and
213
329
indexes, is 2 gigabytes. For this reason, do not deploy MongoDB to
214
330
production on 32-bit machines.
0 commit comments