Introducing Dragonfly Cloud! Learn More

Question: What is the maximum number of inserts per second in MongoDB?

Answer

The maximum number of inserts per second in MongoDB largely depends on various factors such as the hardware configuration, network speed, document size, whether the operations are bulk inserts or single document inserts, and the specific MongoDB server version in use.

Considerations for Maximizing Insert Throughput

  1. Hardware Configuration: Faster CPUs, more RAM, and SSDs can significantly increase insert throughput.
  2. Document Size: Smaller documents allow for faster inserts since more documents can fit in memory and less disk I/O is needed.
  3. Bulk Inserts: Using MongoDB's bulk insert operation can greatly improve insert speed compared to individual document inserts.
  4. Write Concern: A lower write concern level (e.g., { w: 0 }) can improve insert speed at the cost of durability guarantees.
  5. Indexing: Minimizing the number of indexes during high insert operations can improve performance, as each index requires additional writes.
  6. Sharding: Distributing data across multiple servers (shards) can parallelize write operations, increasing overall throughput.

Example of Bulk Insert Operation

const MongoClient = require('mongodb').MongoClient; const uri = "your_mongodb_uri"; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); async function run() { try { await client.connect(); const database = client.db("test"); const collection = database.collection("documents"); // Prepare an array of documents to insert const docs = [{ item: "card", qty: 15 }, { item: "envelope", qty: 20 }, { item: "stamps", qty: 30 }]; // Perform a bulk insert const result = await collection.insertMany(docs); console.log(`${result.insertedCount} documents were inserted`); } finally { await client.close(); } } run().catch(console.dir);

The actual limit is difficult to quantify universally due to the dependencies on the factors mentioned above. It's best to conduct performance testing tailored to your application's specific context and requirements for a precise understanding of what you can achieve with your MongoDB setup.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.