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.
Which option describes what a write concern of 'majority' ensures in a MongoDB replica set?
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.
What is the default read preference mode when connecting to a MongoDB replica set without specifying any preference?
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.
If you set write concern to { w: 1 }, what does this require from the database?
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.
What is a likely consequence of setting your application's read preference to 'secondary'?
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.
What does the 'wtimeout' field in a write concern configuration specify?
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'.
Which read preference option would you use if your application needs to read from secondary nodes but prefers the primary if available?
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.
What is the effect of using a write concern of { w: 0 } when performing writes?
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.
If your goal is to distribute read traffic among all replica set members regardless of their role, which read preference should you select?
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.
How can read preferences other than 'primary' potentially affect the consistency of data read by applications?
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.
Why is it generally recommended to use a write concern of 'majority' instead of 'w:1' for important data?
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.