5 min read
•Question 26 of 27mediumWhat is ACID vs BASE?
Database consistency models.
What You'll Learn
- ACID properties
- BASE properties
- When to use each
ACID (Traditional Databases)
code.jsJavaScript
A - Atomicity: All or nothing transactions
C - Consistency: Valid state after transaction
I - Isolation: Transactions don't interfere
D - Durability: Committed data persistsExample
code.jsJavaScript
// Bank transfer - must be atomic
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
// Either both succeed or both failBASE (NoSQL Approach)
code.jsJavaScript
BA - Basically Available: System always responds
S - Soft state: State may change over time
E - Eventual consistency: Eventually consistentExample
code.jsJavaScript
// Social media like count
// May show 999 likes briefly before updating to 1000
// Eventually correct, but not immediatelyComparison
| Property | ACID | BASE |
|---|---|---|
| Consistency | Immediate | Eventual |
| Availability | May block | Always available |
| Performance | Lower | Higher |
| Scaling | Harder | Easier |
When to Use
Use ACID
- Financial transactions
- Inventory management
- Booking systems
- Data integrity critical
Use BASE
- Social media feeds
- Analytics
- Caching
- High availability critical
MongoDB's Flexibility
code.jsJavaScript
// ACID for multi-document transactions
session.withTransaction(async () => {
// ACID guarantees here
});
// BASE for single documents (default)
db.metrics.insertOne({ views: 1 });