databasemongodbmongodb-querydatabase-administrationlinearization

MongoDb linearizable read concern restrictions


Could someone explain to me some part of Mongodb linearizable read concern documentation:

Linearizable read concern guarantees only apply if read operations specify a query filter that uniquely identifies a single document.

Does it mean that I have to have unique index on fields that presented in query filter?

For example let's answer on 4 questions:

  1. I have collection test without unique index on A field. db.test.find({A:1}).readConcern("linearizable").maxTimeMS(10000)

    Is it linearizable and I can't get stale read? If answer yes, is it means that there no reason to use linearizable read concern in reads by fields which not presented in unique index?

  2. I have collection test with unique index on A field.
    db.test.ensureIndex({A:1}, {unique:true}); db.test.find({A:1}).readConcern("linearizable").maxTimeMS(10000);

    Is it linearizable and I can't get stale read?

  3. I have collection test with unique index on A field.
    db.test.ensureIndex({A:1}, {unique:true}); db.test.find({A:1, B:1}).readConcern("linearizable").maxTimeMS(10000);

    Is it linearizable and I can't get stale read?

  4. I have collection test without unique index on A field. But find method return only one document in result.
    db.test.find({A:1}).readConcern("linearizable").maxTimeMS(10000); //returned {_id:"someId", A:1}

    Is it linearizable and I can't get stale read?


Solution

  • Distributed database concepts can be quite hard to understand, let's cover some background before addressing the questions.

    Linearizable Read Concern introduced in MongoDB v3.4, is to ensure applications are always reading the most up-to-date data from the correct (current/legitimate) primary node. This means that during a network partition, applications will not read:

    Due to the complexity of tracking data in multiple states (i.e. propagated, committed) in multiple nodes (i.e. secondaries), the guarantee of linearizable read concern only apply if the read operation uniquely identifies a single document.

    Does it mean that i have to have unique index on fields that presented in query filter?

    Now to address your question, the query only have to return one unique document in a collection. It is not necessary for the collection to have a unique index, although using a unique index will help the query to return a single document. For example, specifying a query filter with _id. As field name _id is reserved for use as a primary key; its value must be unique in the collection.

    You may also be interested to read the following: