Bird
Raised Fist0
MongoDBquery~20 mins

Read concern and write concern in transactions in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Transaction Mastery: Read and Write Concerns
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the effect of setting readConcern: 'majority' in a MongoDB transaction?

Consider a MongoDB transaction started with readConcern: 'majority'. What does this setting guarantee about the data read inside the transaction?

MongoDB
session.startTransaction({ readConcern: { level: 'majority' } });
AThe transaction reads uncommitted data from other transactions.
BThe transaction reads data that has been acknowledged by a majority of replica set members.
CThe transaction reads only data from the primary node, ignoring secondaries.
DThe transaction reads data without any consistency guarantees.
Attempts:
2 left
💡 Hint

Think about how MongoDB ensures data consistency across replica sets.

❓ query_result
intermediate
2:00remaining
What does writeConcern: { w: 'majority' } ensure in a MongoDB transaction?

When a transaction is committed with writeConcern: { w: 'majority' }, what does this guarantee about the write operation?

MongoDB
session.commitTransaction({ writeConcern: { w: 'majority' } });
AThe write is acknowledged immediately without waiting for replication.
BThe write is acknowledged only by the primary node.
CThe write is not acknowledged and may be lost if the primary fails.
DThe write is acknowledged by a majority of replica set members before the commit returns success.
Attempts:
2 left
💡 Hint

Consider how MongoDB ensures durability of writes in a replica set.

📝 Syntax
advanced
2:00remaining
Identify the correct syntax to start a MongoDB transaction with readConcern: 'snapshot' and writeConcern: { w: 'majority' }.

Which of the following code snippets correctly starts a transaction with readConcern: 'snapshot' and writeConcern: { w: 'majority' }?

Asession.startTransaction({ readConcern: { level: 'snapshot' }, writeConcern: { w: 'majority' } });
Bsession.startTransaction({ readConcern: { level: snapshot }, writeConcern: { w: majority } });
Csession.startTransaction({ readConcern: 'snapshot', writeConcern: 'majority' });
Dsession.startTransaction({ readConcern: { level: 'majority' }, writeConcern: { w: 'snapshot' } });
Attempts:
2 left
💡 Hint

Remember the correct structure for specifying readConcern and writeConcern options.

🧠 Conceptual
advanced
2:00remaining
Why is readConcern: 'snapshot' recommended for multi-document transactions?

In MongoDB, what is the main advantage of using readConcern: 'snapshot' inside a multi-document transaction?

AIt provides a consistent view of data as it existed at the start of the transaction.
BIt allows reading uncommitted changes from other transactions.
CIt disables replication during the transaction.
DIt forces the transaction to read only from secondary nodes.
Attempts:
2 left
💡 Hint

Think about how transactions maintain data consistency during multiple operations.

🔧 Debug
expert
2:00remaining
What error occurs when committing a transaction with an invalid writeConcern value?

Consider the following commit code:

session.commitTransaction({ writeConcern: { w: 'invalidLevel' } });

What error will MongoDB raise?

MongoDB
session.commitTransaction({ writeConcern: { w: 'invalidLevel' } });
ATypeError: writeConcern must be a string
BSyntaxError: Invalid writeConcern syntax
CMongoServerError: Unknown write concern level 'invalidLevel'
DNo error; commit succeeds with default writeConcern
Attempts:
2 left
💡 Hint

Invalid write concern levels are not accepted by MongoDB.

Practice

(1/5)
1. What does readConcern control in a MongoDB transaction?
easy
A. How fresh or confirmed the data you read is during the transaction
B. How many servers confirm your data changes before commit
C. The speed of the transaction execution
D. The size of the data being written

Solution

  1. Step 1: Understand the role of readConcern

    Read concern defines the level of isolation and freshness of data read during a transaction.
  2. Step 2: Differentiate from writeConcern

    Write concern controls confirmation of writes, not reads.
  3. Final Answer:

    How fresh or confirmed the data you read is during the transaction -> Option A
  4. Quick Check:

    readConcern = data freshness [OK]
Hint: Read concern is about data freshness, write concern is about confirmation [OK]
Common Mistakes:
  • Confusing readConcern with writeConcern
  • Thinking readConcern controls write confirmations
  • Assuming readConcern affects transaction speed
2. Which of the following is the correct way to specify writeConcern in a MongoDB transaction?
easy
A. { writeConcern: { w: 'majority' } }
B. { readConcern: { w: 'majority' } }
C. { writeConcern: { r: 'majority' } }
D. { writeConcern: 'readConcern' }

Solution

  1. Step 1: Recall writeConcern syntax

    Write concern uses the key w to specify acknowledgement level, e.g., 'majority'.
  2. Step 2: Check options for correctness

    { writeConcern: { w: 'majority' } } correctly uses writeConcern: { w: 'majority' }. Others misuse keys or mix readConcern and writeConcern.
  3. Final Answer:

    { writeConcern: { w: 'majority' } } -> Option A
  4. Quick Check:

    writeConcern uses w key [OK]
Hint: writeConcern uses w, readConcern uses level [OK]
Common Mistakes:
  • Using 'r' instead of 'w' for writeConcern
  • Mixing readConcern and writeConcern keys
  • Passing writeConcern as a string instead of object
3. Given this transaction code snippet, what is the effective read concern level?
const session = client.startSession();
session.startTransaction({ readConcern: { level: 'snapshot' }, writeConcern: { w: 'majority' } });
// transaction operations
await session.commitTransaction();
medium
A. local
B. majority
C. linearizable
D. snapshot

Solution

  1. Step 1: Identify readConcern in transaction options

    The transaction is started with readConcern: { level: 'snapshot' }.
  2. Step 2: Understand readConcern levels

    'snapshot' means the transaction reads a consistent snapshot of data at start time.
  3. Final Answer:

    snapshot -> Option D
  4. Quick Check:

    readConcern level = snapshot [OK]
Hint: Check readConcern.level in startTransaction options [OK]
Common Mistakes:
  • Confusing writeConcern level with readConcern level
  • Assuming default readConcern instead of specified
  • Mixing up 'majority' with readConcern levels
4. Identify the error in this transaction code snippet:
const session = client.startSession();
session.startTransaction({ readConcern: { level: 'majority' }, writeConcern: { w: 1 } });
// transaction operations
await session.commitTransaction();
medium
A. writeConcern w: 1 is invalid
B. Missing session.endSession() call
C. readConcern level 'majority' is invalid
D. startTransaction syntax is incorrect

Solution

  1. Step 1: Check for session lifecycle management

    The code does not call session.endSession() after commitTransaction(). This is necessary to properly release session resources and avoid leaks.
  2. Step 2: Verify other components

    readConcern level 'majority' is valid; writeConcern { w: 1 } is valid (acknowledgement from primary); syntax is correct.
  3. Final Answer:

    Missing session.endSession() call -> Option B
  4. Quick Check:

    endSession after commit/abort [OK]
Hint: Always call session.endSession() after commitTransaction or abortTransaction [OK]
Common Mistakes:
  • Thinking readConcern 'majority' is invalid for transactions
  • Assuming writeConcern w:1 is invalid
  • Believing session.endSession() is optional
5. You want a transaction that reads the most recent confirmed data and ensures all writes are acknowledged by a majority of servers. Which combination of readConcern and writeConcern should you use?
hard
A. readConcern: 'snapshot', writeConcern: { w: 0 }
B. readConcern: 'local', writeConcern: { w: 1 }
C. readConcern: 'majority', writeConcern: { w: 'majority' }
D. readConcern: 'linearizable', writeConcern: { w: 'majority' }

Solution

  1. Step 1: Understand readConcern 'majority'

    This level reads data confirmed by a majority of servers, ensuring recent confirmed data.
  2. Step 2: Understand writeConcern { w: 'majority' }

    This ensures writes are acknowledged by a majority of servers before commit.
  3. Step 3: Evaluate other options

    readConcern: 'local', writeConcern: { w: 1 } uses weaker writeConcern; C uses no write acknowledgement; D uses linearizable readConcern which is stricter than needed.
  4. Final Answer:

    readConcern: 'majority', writeConcern: { w: 'majority' } -> Option C
  5. Quick Check:

    Majority read and write concern = readConcern: 'majority', writeConcern: { w: 'majority' } [OK]
Hint: Use 'majority' for both read and write for confirmed data [OK]
Common Mistakes:
  • Choosing writeConcern w:1 for majority confirmation
  • Confusing 'snapshot' with 'majority' readConcern
  • Ignoring writeConcern w:0 means no acknowledgement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard