Consistency vs Atomicity
Fast recall
Quick revision
Core answer
Whether the candidate can distinguish between the two most commonly confused ACID properties
Conflating consistency with eventual consistency in distributed systems, or describing consistency only in terms of data replication rather than constraint enforcement
Mention that consistency is partly enforced by the database (foreign keys, triggers, check constraints) and partly the application's responsibility (business logic)
Interview-ready guide
Interview answer
Consistency and atomicity solve different problems. Consistency means a successful transaction preserves the rules that the schema and application actually enforce. For example, a foreign key can reject an order for a missing customer, and a CHECK can reject a negative balance. Atomicity means the transaction has one committed outcome, so a debit cannot commit while its matching credit is left out. Atomicity helps preserve consistency but cannot detect a missing rule or wrong calculation. In Spring, @Transactional declares the atomic boundary. JPA annotations describe mappings and may generate constraints only when schema generation is enabled, so production migrations should define and verify the real database rules.
Consistency vs Atomicity
Direct Answer
Consistency means a successful transaction preserves the invariants that the database schema and application actually enforce. Atomicity means the transaction's changes commit as one unit or have no committed effect. Atomicity can help preserve consistency, but it cannot repair missing or incorrect business rules.
Code Example
-- A CHECK constraint expresses a consistency rule
CREATE TABLE accounts (
id INT PRIMARY KEY,
balance DECIMAL(12,2) NOT NULL CHECK (balance >= 0) -- consistency rule
);
START TRANSACTION; -- atomicity begins
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- If id=1 would go negative, the CHECK fails, the statement errors,
-- and ROLLBACK leaves the database in a valid (consistent) state.
COMMIT; -- both updates persist together
Atomicity (all-or-nothing) is how the database avoids leaving a half-applied transfer that would violate consistency.
Common Mistakes & Pitfalls
- Confusing consistency with eventual consistency: The C in ACID means constraint enforcement and data validity, not the distributed systems concept where replicas converge over time. These are completely different meanings of consistency.
- Thinking consistency is only about constraints: Consistency also includes application-level business rules that the database can't validate, like discount logic or approval workflows.
- Not realizing consistency is shared responsibility: The database enforces foreign keys and check constraints, but the application must implement complex business rules that can't be expressed as SQL constraints, like cross-entity invariants.
Check your understanding
Answer these without looking back at the article.
- Can you give an example of a constraint that enforces consistency in PostgreSQL?
- What happens if an application bypasses consistency checks by directly modifying the database?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.