Sometimes we observe the next case:
- We insert a document to MongoDB with write concern
w: 1
viamongos
router that writes toprimary
replica of shard A. - We read this document from MongoDB with default
local
read concern viamongos
router that should read fromprimary
replica of shard A. - This
findOne
returnsnull
as if the document is not found. - (Actually it is Python driver
pymongo - find_one()
and it returnsNone
). - We sleep 0.1 seconds and retry the read.
- On step 2-5 the document is finally read successfully.
We found:
- Such cases are observed more often when a lot of documents are inserted to this collection quickly.
- We suspected such inserts lead to chunking and migration of chunk from shard A to shard B.
- While a document is migrating, could it be unavailable for reads for a short time?
- But there is only one insert to only one shard in
db.oplog.rs
, nofromMigrate
flags.
Why does it happen? How to deal with it correctly?
These false-positive "not found"-s may lead to faulty atomic-find-or-create via $setOnInsert
?