Bird
Raised Fist0
MongoDBquery~20 mins

Causal consistency concept in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Causal Consistency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding causal consistency in MongoDB

Which statement best describes causal consistency in MongoDB?

AOperations that are causally related are seen by clients in the same order, but unrelated operations may be seen in different orders.
BOperations are only visible to the client that performed them until a manual refresh.
COperations are seen by all clients in the exact order they were performed globally.
DAll operations are immediately visible to all clients without delay.
Attempts:
2 left
💡 Hint

Think about how related changes should appear in order to maintain logical flow.

❓ query_result
intermediate
2:00remaining
Effect of causal consistency on read operations

Given a MongoDB replica set with causal consistency enabled, if a client writes a document and then reads it immediately, what will the read operation return?

AThe read will return null until the write is acknowledged by all replicas.
BThe read may return an older version if the write has not propagated yet.
CThe read will fail with a consistency error.
DThe read will always return the latest written document.
Attempts:
2 left
💡 Hint

Consider what causal consistency guarantees about visibility of your own writes.

📝 Syntax
advanced
2:00remaining
Enabling causal consistency in MongoDB sessions

Which of the following code snippets correctly enables causal consistency for a MongoDB session in Node.js?

MongoDB
const session = client.startSession({ /* options here */ });
Aconst session = client.startSession({ enableCausalConsistency: true });
Bconst session = client.startSession({ causal_consistency: true });
Cconst session = client.startSession({ causalConsistency: true });
Dconst session = client.startSession({ causalConsistencyEnabled: true });
Attempts:
2 left
💡 Hint

Check the exact option name for enabling causal consistency in the MongoDB Node.js driver.

❓ optimization
advanced
2:00remaining
Optimizing read preference with causal consistency

When using causal consistency in MongoDB, which read preference setting optimizes for reading your own writes while allowing reads from secondary replicas?

ARead preference set to <code>primary</code> only.
BRead preference set to <code>primaryPreferred</code>.
CRead preference set to <code>secondaryPreferred</code>.
DRead preference set to <code>secondary</code> only.
Attempts:
2 left
💡 Hint

Think about a setting that prefers primary but can fall back to secondary.

🔧 Debug
expert
3:00remaining
Diagnosing causal consistency violation in MongoDB

You observe that a client sometimes reads stale data that does not reflect its previous writes, despite using causal consistency sessions. Which is the most likely cause?

AThe client is not using the same session for the write and subsequent read operations.
BThe MongoDB cluster does not support causal consistency.
CThe write concern is set to <code>w: 0</code> causing writes to be unacknowledged.
DThe client is using read preference <code>primary</code> instead of <code>secondary</code>.
Attempts:
2 left
💡 Hint

Consider how sessions relate to causal consistency guarantees.

Practice

(1/5)
1. What does causal consistency ensure in MongoDB sessions?
easy
A. Data is automatically backed up every hour
B. Data is always encrypted during transmission
C. Data changes are seen in the order they happened
D. Data is compressed to save storage space

Solution

  1. Step 1: Understand causal consistency meaning

    Causal consistency means users see data changes in the order they occurred.
  2. Step 2: Match with options

    Only Data changes are seen in the order they happened describes this behavior correctly.
  3. Final Answer:

    Data changes are seen in the order they happened -> Option C
  4. Quick Check:

    Causal consistency = ordered data changes [OK]
Hint: Remember: causal consistency means order matters [OK]
Common Mistakes:
  • Confusing causal consistency with encryption
  • Thinking it means automatic backups
  • Assuming it compresses data
2. Which is the correct way to enable causal consistency in a MongoDB session?
easy
A. session = client.startSession({ causalConsistency: true })
B. session = client.startSession({ consistency: 'strong' })
C. session = client.startSession({ causal: 'enabled' })
D. session = client.startSession({ consistencyLevel: 'causal' })

Solution

  1. Step 1: Recall MongoDB session syntax

    To enable causal consistency, use the option causalConsistency: true when starting a session.
  2. Step 2: Check options for exact syntax

    Only session = client.startSession({ causalConsistency: true }) uses the correct option name and value.
  3. Final Answer:

    session = client.startSession({ causalConsistency: true }) -> Option A
  4. Quick Check:

    Enable causalConsistency: true in startSession [OK]
Hint: Look for exact option name 'causalConsistency' set to true [OK]
Common Mistakes:
  • Using wrong option names like 'consistency' or 'causal'
  • Setting values as strings instead of boolean
  • Confusing with other consistency levels
3. Given the following code snippet, what will the output be if causal consistency is enabled?
const session = client.startSession({ causalConsistency: true });
session.startTransaction();
await collection.insertOne({ name: 'Alice' }, { session });
await session.commitTransaction();
const doc = await collection.findOne({ name: 'Alice' });
console.log(doc);
medium
A. null
B. { name: 'Alice' }
C. SyntaxError
D. undefined

Solution

  1. Step 1: Understand causal consistency effect

    With causal consistency, the read after commit sees the inserted document.
  2. Step 2: Analyze code behavior

    After committing the transaction, the findOne query returns the inserted document.
  3. Final Answer:

    { name: 'Alice' } -> Option B
  4. Quick Check:

    Committed insert visible with causal consistency [OK]
Hint: Committed writes are visible immediately with causal consistency [OK]
Common Mistakes:
  • Expecting null because of delayed visibility
  • Thinking a syntax error occurs
  • Assuming undefined means no document
4. Identify the error in this code snippet that tries to enable causal consistency:
const session = client.startSession({ causalConsistency: 'true' });
await collection.insertOne({ item: 'book' }, { session });
medium
A. The option value should be boolean true, not string 'true'
B. The session object is missing a required parameter
C. The insertOne method does not accept a session option
D. The collection name is invalid

Solution

  1. Step 1: Check option type for causalConsistency

    The option causalConsistency must be a boolean, not a string.
  2. Step 2: Identify error cause

    Using 'true' as a string causes the option to be ignored or error.
  3. Final Answer:

    The option value should be boolean true, not string 'true' -> Option A
  4. Quick Check:

    Boolean true required for causalConsistency [OK]
Hint: Use true without quotes for boolean options [OK]
Common Mistakes:
  • Passing 'true' as a string instead of boolean
  • Assuming insertOne can't use session
  • Thinking collection name affects causal consistency
5. You want to ensure users always see the latest updates in the correct order across multiple MongoDB clients. Which approach best applies causal consistency to achieve this?
hard
A. Disable causal consistency and rely on eventual consistency
B. Use standalone queries without sessions for faster reads
C. Enable causal consistency only on the primary node
D. Start sessions with { causalConsistency: true } and share session tokens between clients

Solution

  1. Step 1: Understand causal consistency across clients

    To keep updates ordered for users on different clients, sessions with causal consistency must be used and shared.
  2. Step 2: Evaluate options

    Only Start sessions with { causalConsistency: true } and share session tokens between clients correctly uses sessions with causalConsistency enabled and shares session tokens to maintain order.
  3. Final Answer:

    Start sessions with { causalConsistency: true } and share session tokens between clients -> Option D
  4. Quick Check:

    Share causal consistent sessions for ordered updates [OK]
Hint: Share sessions with causalConsistency true for ordered multi-client views [OK]
Common Mistakes:
  • Thinking standalone queries guarantee order
  • Assuming enabling only on primary is enough
  • Confusing causal consistency with eventual consistency
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