ACID Properties AC
ACID Properties AC
1. Scenario: Two friends, Alice and Bob, decide to send money to each
other. Alice sends money to Bob online, but due to an issue (e.g., a
server problem), the transaction fails. Alice’s account is debited, but
Bob's account is not credited.
2. Key Points:
1. Alice’s account is deducted the money.
2. Bob's account shows no change.
3. Both friends are puzzled, worried about the missing money.
Imagine you are buying a product from an online store. The transaction
involves two key operations:
2. Saga Pattern
•Description: A long-lived transaction is split into a series of smaller, isolated
steps, each with its own compensating transaction to undo the work if something
goes wrong. This is common in microservices architectures.
•Trade-Off: Complexity increases because developers must explicitly define
compensation logic for each step.
•Use Case: Useful in scenarios like order processing systems where various
services (inventory, payment, shipping) must be coordinated.
3. Two-Phase Commit (2PC)
•Description: A distributed transaction protocol ensuring that a transaction either commits
across all involved databases or rolls back if any one part fails.
•Trade-Off: It can be slower and more resource-intensive, especially in distributed systems,
due to the need for coordination between multiple nodes.
•Use Case: Ideal for systems requiring strict consistency across distributed databases.
5. Idempotency
•Description: Ensuring operations can be applied multiple times without changing the result
beyond the initial application. This can be used to achieve consistency in scenarios where
atomic transactions are not possible.
•Trade-Off: It requires careful design to ensure that operations are idempotent.
•Use Case: Common in distributed systems for operations like payment processing or API
calls, where retries can occur.
Each of these alternatives provides different trade-offs between consistency, availability, and
partition tolerance, tailored to the specific needs of different systems or applications.
OPTIMIZING ATOMICITY
•Grouping Operations:
•Batches multiple operations into a single transaction.
•Reduces overhead and improves performance.
•Database Tuning:
•Optimizes database performance through adjustments.
•Includes indexing, query optimization, and hardware tuning.
•Asynchronous Commits:
•Commits changes without waiting for the physical write to disk.
•Enhances performance by reducing commit time.
•Idempotent Operations:
•Operations that produce the same result even if executed multiple times.
•Useful in scenarios to ensure consistent outcomes, particularly in retries.
REAL-LIFE USE CASES
When a customer books a flight ticket, the system must ensure that:
1.The seat is available.
2.The booking is successfully completed, updating the seat's status as booked.
3.The customer's payment is processed.
Consistency Ensures:
•The seat inventory is correctly updated.
•No two customers can book the same seat simultaneously.
•The booking and payment processes are synchronized.
Process:
1.Before Booking:
1. Available seats for Flight XY123: Seat 1A, Seat 1B, Seat 1C.
2. Customer chooses Seat 1A.
2.During Booking:
1. Check if Seat 1A is available.
2. Reserve Seat 1A.
3. Process the payment.
4. Confirm the booking and update the seat status in the database.
• After Booking:
•Limits Growth:
•Issue: Strict rules can make it harder to handle more users.
•Solution: Use different consistency levels where possible, like eventual consistency.
2.BASE Model:
1. Stands for Basically Available, Soft state, Eventual consistency.
2. Focuses on availability and performance over immediate consistency.
3. Common in NoSQL databases like Cassandra and DynamoDB.
3.Causal Consistency:
1. Ensures related changes are seen in the correct order.
2. Useful in collaborative applications where the order of operations matters.
4.Read-Your-Writes Consistency:
1. Guarantees that once a write is completed, subsequent reads by the same
client will reflect that write.
2. Useful for user-specific data, like a shopping cart or profile updates.
These alternatives provide more flexibility and better performance in scenarios
where immediate consistency is not a strict requirement. Would you like to dive
deeper into any of these?
Use Cases of Consistency: