I am using mongodb in a 3-member replicaset, trying to read my own writes. However, I seem to be getting stale data back from my reads. According to the documentation, by doing read/write with “majority” concerns, it should guarantee that:
“Read operations reflect the results of write operations that precede them.”
The same is stated in this post from 2018:
The causal read R1 with read concern majority waits to see T1 majority committed before returning success.
However, I can’t seem to be so lucky. The code below inserts a user and instantly tries to find that same user by the ID. This is done in a loop and only takes 1-3 iterations before it fails with “User not found”.
IMongoCollection<User> collection = MongoDatabase.GetCollection<User>("UserCollection")
.WithReadConcern(ReadConcern.Majority)
.WithWriteConcern(WriteConcern.WMajority)
.WithReadPreference(ReadPreference.Secondary);
Random rnd = new Random();
while (true)
{
User newUser = new User
{
Email = $"{rnd.Next(int.MinValue, int.MaxValue)}@gg.com"
};
collection.InsertOne(newUser);
if (newUser.Id == ObjectId.Empty)
{
throw new Exception("Id is empty");
}
var findFluent = collection.Find(Builders<User>.Filter.Eq(x => x.Id, newUser.Id));
User foundUser = findFluent.FirstOrDefault();
if (foundUser == null)
{
throw new Exception("User not found");
}
}
I have specified “Majority” concerns for both read/write. And I specify “Secondary” as read preference for the sake of testing this. If I specify “Primary” as the read preference, this will never fail (obviously).
What am I doing wrong?