Intermediate LevelMongoDB & Mongoose
What are MongoDB Read Concerns (local, majority, linearizable)?
❓ Interview Prompt: Explain Read Concerns (local, available, majority, linearizable, snapshot) and controlling dirty read visibility.
Read Concern controls the durability and consistency level of data read from MongoDB:
•
'local' (Default): Returns node's most recent data; risk of reading data that might be rolled back if primary crashes.
•
'majority': Returns data acknowledged by a majority of replica nodes (prevents dirty reads).
•
'linearizable': Guarantees real-time primary node consistency.
Code Implementation Example
typescript
import { AccountModel } from './models/Account';
async function getGuaranteedBalance(accountId: string) {
// Ensures read data cannot be rolled back!
return await AccountModel.findById(accountId)
.readConcern('majority')
.exec();
}💡Executing majority read concern to prevent reading rolled-back data.
🎯 Interview Key Takeaway
Use majority readConcern for financial reads to ensure queried data cannot be rolled back during failover.
#Read Concern#majority#Dirty Reads#Intermediate
Back to all MongoDB & Mongoose Questions→