Why does my multithreaded insert perform better th

2019-05-16 12:36发布

问题:

I looked into concurrency in MongoDB, and apparently it uses a database level locking system. I thought that would mean that multiple threads inserting into the same database would perform similarly or worse than a single thread inserting into the database.

I found that when I got to 4 threads concurrently inserting into the database, performance nearly doubled (in terms of inserts/sec).

Is there any reason for why performance is getting better? I don't understand why.

If it helps, I have one thread continually receiving packets from a server and inserting it into a queue. My 4 threads continually dequeue from that queue and insert into the database.

回答1:

When a write happens in MongoDB the actual write lock is only held for a small fraction of the total time it takes to complete.

As it turns out writing to data files in RAM only takes microseconds (micro, not milli). The rest of the time "other" stuff is going on - this is why you will get best performance if you have a lot more than one or a handful of threads - in most situations the limiting factor will be something like network bandwidth or disk IO or CPU (once you have enough threads) and not the lock.

If you have enough threads throwing writes at mongod and the documents are very small, the disk is fast (for flushing all the written data to disk) and there is enough RAM for handling related index updates then the database lock can become the limiting factor for your write throughput but it's not normally the first or second thing that becomes the bottleneck.

I encourage you to use tools like mongostat, iostat, and other system monitoring resources to see where the actual bottlenecks are. If you don't see any then your application is not throwing work at the DB fast enough to max out its capacity.