Skip to content

Commit c7cd7b7

Browse files
author
Sam Kleinman
committed
merge: DOCS-491
2 parents 0cf137a + 54b54d6 commit c7cd7b7

File tree

2 files changed

+142
-24
lines changed

2 files changed

+142
-24
lines changed

source/faq/fundamentals.txt

Lines changed: 140 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ the :doc:`complete list of FAQs </faq>` or post your question to the
1414
:backlinks: none
1515
:local:
1616

17-
What kind of Database is MongoDB?
17+
What kind of database is MongoDB?
1818
---------------------------------
1919

2020
MongoDB is :term:`document`-oriented DBMS. Think of MySQL but with
@@ -32,7 +32,7 @@ partitioning.
3232
MongoDB uses :term:`BSON`, a binary object format similar
3333
to, but more expressive than, :term:`JSON`.
3434

35-
Do MongoDB Databases Have Tables?
35+
Do MongoDB databases have tables?
3636
---------------------------------
3737

3838
Instead of tables, a MongoDB database stores its data in
@@ -43,19 +43,17 @@ database table, and each document has
4343
one or more fields, which corresponds to a column in a relational
4444
database table.
4545

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.
5351

5452
.. see:: :doc:`/reference/sql-comparison`
5553

5654
.. _faq-schema-free:
5755

58-
Do MongoDB Databases have Schemas?
56+
Do MongoDB databases have schemas?
5957
----------------------------------
6058

6159
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
147145
to fit the application's working data set in RAM will achieve the best
148146
performance.
149147

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+
150265
How do I configure the cache size?
151266
----------------------------------
152267

153268
MongoDB has no configurable cache. MongoDB uses all *free* memory on
154269
the system automatically by way of memory-mapped files. Operating
155270
systems use the same approach with their file system caches.
156271

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-
170272
.. _faq-database-and-caching:
171273

172274
Does MongoDB require a separate caching layer for application-level caching?
@@ -196,6 +298,20 @@ in RAM, MongoDB serves all queries from memory.
196298
MongoDB does not implement a query cache: MongoDB serves all queries
197299
directly from the indexes and/or data files.
198300

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+
199315
What language is MongoDB written in?
200316
------------------------------------
201317

@@ -208,7 +324,7 @@ drivers use C extensions for better performance.
208324
What are the limitations of 32-bit versions of MongoDB?
209325
-------------------------------------------------------
210326

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
212328
MongoDB, the total storage size for the server, including data and
213329
indexes, is 2 gigabytes. For this reason, do not deploy MongoDB to
214330
production on 32-bit machines.

source/faq/storage.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ the :doc:`complete list of FAQs </faq>` or post your question to the
1515
:backlinks: none
1616
:local:
1717

18+
.. _faq-storage-memory-mapped-files:
19+
1820
What are memory mapped files?
1921
-----------------------------
2022

0 commit comments

Comments
 (0)