MongoDB Write Concerns u0026 Read Preferences Essentials Quiz Quiz

Explore key concepts in MongoDB write concerns and read preferences with practical questions designed for beginners. Strengthen your understanding of data durability, consistency, and replica set behavior in document-oriented databases.

  1. Basic Understanding of Write Concern

    Which option describes what a write concern of 'majority' ensures in a MongoDB replica set?

    1. The write is stored in cache but not persisted
    2. The write is sent to all clients
    3. The write is acknowledged by most replica set members
    4. The write is acknowledged by the primary only

    Explanation: A write concern of 'majority' means that a write operation must be acknowledged by more than half of the nodes in a replica set before it is considered successful. 'The write is acknowledged by the primary only' refers to default or 'acknowledged' concern. 'The write is sent to all clients' is incorrect, as write concerns are about server replication, not client distribution. 'The write is stored in cache but not persisted' is unrelated to write concerns.

  2. Default Read Preference

    What is the default read preference mode when connecting to a MongoDB replica set without specifying any preference?

    1. secondary
    2. nearest
    3. primary
    4. primaryPreferred

    Explanation: The default read preference is 'primary', meaning all reads are directed to the primary node by default. 'Secondary' would send reads to secondary nodes, which is not the default. 'Nearest' chooses the node with the lowest network latency, but this is not standard behavior. 'PrimaryPreferred' is an option, but not the default setting.

  3. Write Concern Values

    If you set write concern to { w: 1 }, what does this require from the database?

    1. The write must be acknowledged by all nodes
    2. The write will succeed even if no nodes acknowledge
    3. The write must be acknowledged by the primary node
    4. The write must be acknowledged by half of the nodes

    Explanation: Setting write concern to { w: 1 } means the write must be acknowledged by the primary node only. 'All nodes' is not required unless w is set to the total number of nodes. Option 'no nodes acknowledge' would only be true for w: 0. Half of the nodes is incorrect unless specifically setting w: majority.

  4. Read Preference Impact

    What is a likely consequence of setting your application's read preference to 'secondary'?

    1. Reads may return slightly stale data compared to the primary
    2. Reads are blocked until all nodes sync
    3. Reads will occur on the nearest node regardless of state
    4. Reads will always reflect the latest data

    Explanation: Reading from secondaries can return stale data because replication is asynchronous, and changes may not yet be applied. 'Reads always reflect the latest data' is incorrect since only the primary guarantees this. 'Nearest node regardless of state' is the behavior of 'nearest', not 'secondary'. Reads are not blocked until all nodes sync, so that is incorrect.

  5. Write Concern 'wtimeout' Option

    What does the 'wtimeout' field in a write concern configuration specify?

    1. The interval to retry failed writes
    2. The time to wait for write acknowledgment before an error is returned
    3. The amount of time before a write is deleted
    4. The frequency of data synchronization

    Explanation: 'wtimeout' sets the maximum time to wait for the write concern to be fulfilled before timing out. It does not control deletion of writes or synchronization frequency. The retry interval for failed writes is not managed by 'wtimeout'.

  6. Read Preference and Consistency

    Which read preference option would you use if your application needs to read from secondary nodes but prefers the primary if available?

    1. primary
    2. secondaryPreferred
    3. nearest
    4. primaryOnly

    Explanation: 'secondaryPreferred' first tries to read from secondaries, but falls back to the primary if none are available. 'Primary' restricts all reads to the primary node. There is no 'primaryOnly' setting. 'Nearest' chooses any member with the lowest latency, not prioritizing primary or secondary.

  7. Behavior of Write Concern w:0

    What is the effect of using a write concern of { w: 0 } when performing writes?

    1. The write operation is not allowed
    2. The write is guaranteed to be durable on disk
    3. The write is sent only to secondary nodes
    4. The application receives no acknowledgment of write success or failure

    Explanation: With { w: 0 }, the server does not acknowledge the write, so the application cannot know if it succeeded or failed. Durability is not guaranteed, as acknowledgment is not required. Writes go to the primary, not just secondaries. Write operations are allowed, they just aren't confirmed.

  8. Choosing a Read Preference for Balanced Load

    If your goal is to distribute read traffic among all replica set members regardless of their role, which read preference should you select?

    1. secondary
    2. nearest
    3. primary
    4. majority

    Explanation: The 'nearest' read preference helps balance load by directing reads to the member with the lowest network latency, regardless of whether it is primary or secondary. 'Primary' sends all reads to the primary only. 'Secondary' restricts reads to secondary nodes. 'Majority' refers to write concern, not read preference.

  9. Impact of Read Preferences on Consistency

    How can read preferences other than 'primary' potentially affect the consistency of data read by applications?

    1. They may result in reading stale or out-of-date data
    2. They force all nodes to synchronize immediately
    3. They prevent reading any data
    4. They always cause data loss

    Explanation: Non-primary read preferences can result in stale data because secondary nodes might not have the latest updates. Data loss is not a direct consequence of read preferences. Forcing all nodes to synchronize instantly does not happen due to read preferences. They do not prevent data from being read.

  10. Write Concern Acknowledgment and Data Safety

    Why is it generally recommended to use a write concern of 'majority' instead of 'w:1' for important data?

    1. It allows unacknowledged writes
    2. It prevents secondaries from replicating data
    3. It makes writes faster but less durable
    4. It increases the likelihood that writes survive node failures

    Explanation: A 'majority' write concern ensures more nodes confirm the write, making data more resilient to failures. 'Faster but less durable' is the opposite of the intended effect. 'Unacknowledged writes' is related to w: 0, not 'majority'. Preventing replication to secondaries is not accurate, as 'majority' requires replication to several members.