Hi Abhi!
MongoDB pre-allocates data files by default, unless the --noprealloc flag is set (preallocDatafiles in release 2.6), either on the command line or from the configuration file [1] [2].
MongoDB pre-allocates files on a per-database basis. Pre-allocation is done when the database is initially created: MongoDB will create a namespace file and two data files. These are called "foo.ns", "foo.0", and "foo.1", where "foo" is replaced with the name of the database you've created.
Subsequent data files are called "foo.n" where "n" is the next number in the sequence.
MongoDB will always start allocating new Records from the lowest-numbered data file. So the first allocations will come from the "foo.0" data file. The instant it goes to allocate a new record from the "foo.1" data file, it will pre-allocate the "foo.2" data file. This will continue: as soon as MongoDB starts allocating data from the highest-numbered file, it will then pre-allocate the next file. This behavior is designed to insure that no write operation has to wait for the data file to be allocated.
MongoDB will pre-allocate a file with the next file size, using either the default file sizes or the "smallfiles" file sizes.
The default data file sizes are:
- The default size of the namespace (.ns) file is 16 MB. You cannot change the size of an existing .ns file. You can control the size of new .ns files with the nsSize [3] option
- On a 64-bit system, the default size of the first data file is 64 MB, and the second data file is 128 MB
- Each subsequent data file is 2x the previous file. By default on a 64-bit system, the 3d data file is 256 MB, the 4th 512 MB, etc.
- If you are not using the smallfiles option, the largest data file size is 2 GB
The smallfiles data file sizes are:
- The size of the .ns file is unaffected
- On a 64-bit system the size of the first data file is 16 MB, and the second data file is 128 MB [4]
- On subsequent data files, the data file size doubles every other file: the 3d data file is also 128 MB, the 4th and 5th are 256 MB, and all subsequent files are 512 MB
NOTE: pre-allocation is on a per-database basis. MongoDB will separately pre-allocate files for each database; if you have 5 different databases each one will be pre-allocated according to the number of files that exist for that database.
Given this algorithm, and using the default data file sizes, you can have up to 4 GB of files allocated for each database that are only used for preallocation:
- 2 GB preallocated for the last file
- A 2 GB "previous" file with only a small portion in use -- say 512 bytes for the one and only document in that file
I hope this helps.
-William