Reclaiming memory from MongoDB indexes
Written by David Mytton
As of the current version of MongoDB (1.7.3), deleting data does not compact indexes and their use of RAM. Indeed, this was the root cause of the problem Foursquare experienced several months ago. This means that once your indexes exceed memory (or the working set exceeds memory), deleting data (or moving it to another shard) will not alieviate any memory problems you might be having.
MongoDB is fairly intelligent about what it puts into memory when there’s not enough to store everything, but this limitation means that you may still see performance issues even after pruning data.
The best way to avoid this situation is to properly monitor your database to ensure that you always have sufficient RAM. However, until online compacting functionality is implemented (expected in Q1 2011), if you hit this limit you can work around the problem by running a repair on the database. This will compact everything (data on disk and indexes) since it rebuilds everything from scratch. But this can take a very long time. Instead, you could re-sync your slaves and then when they are completed, have another server take over as master so you can compact that last remaining one.