Software engineers usually care about speed, scalability, and fancy interfaces. At the core of every reliable application is something working quietly behind the scenes: ACID
Regardless of whether you're working on a fintech project or a simple marketplace, knowing how your database manages transactions is not an option; it is what separates a solid application from a potentially corrupted one.
What Are ACID Properties?
ACID is an acronym representing the four key properties that guarantee database transactions are processed reliably. Think of it as a contract between the developer and the database.
| Property | Description | The Core Promise | |:---|:---|:---| | Atomicity | All or Nothing | If any operation within a transaction fails, the entire transaction fails and is cancelled | | Consistency | Valid All the time | Data must follow all predefined rules (constraints, triggers, cascades) | | Isolation | Stay Private | Data transactions don't interfere with each other while processing concurrently | | Durability | Stay Permanent | Once committed transactions stay in the database regardless of interruptions |
Why Does It Matter
Let's assume a user is making a reservation for a particular product. It entails the following actions:
- The payment from the user is charged.
- The schedule of an artisan who offers this product is updated.
Without the atomicity property, a failure in processing the second step after a transaction would lead to the user's money being withdrawn but no booking being registered. ACID helps us prevent this "half-done" scenario.
Atomicity: Consider an example where $100 needs to be transferred from Account A to Account B. Without Atomicity: An error occurs while subtracting from Account A, but not before the subtraction takes place. The $100 disappears into thin air. With Atomicity: The transaction will either succeed in completing both operations or fail in doing so.
Consistency: Suppose your database enforces a rule stating "Balance cannot be less than zero." Consistency ensures that transactions violating the rule will be aborted. The database transitions between states maintaining a valid state throughout.
Isolation: Think of two users booking the final available spot in a service at precisely the same time. Isolation allows us to process these transactions independently, even if they occur simultaneously. Either Transaction A or Transaction B will be successful in making the booking. In either case, Transaction B will know that the spot is unavailable.
Durability: Once the database responds with success, the data will be saved to non-volatile storage (e.g., disk). A sudden failure in the database can cause all running transactions to abort. However, upon recovery, the database will know about its previously committed transactions.
How to Use ACID in Code
Most Relational Database Management Systems (RDBMS) like PostgreSQL or MySQL handle ACID by default through Transactions.
Here is a practical example using SQL for a booking system:
\
BEGIN; -- Start the transaction
-- Step 1: Deduct funds from user
UPDATE accounts SET balance = balance - 5000 WHERE user_id = 123;
-- Step 2: Record the booking
INSERT INTO bookings (user_id, artisan_id, amount, status)
VALUES (123, 456, 5000, 'confirmed');
-- Step 3: Check if everything is okay
-- If any error occurred, we would run: ROLLBACK;
COMMIT; -- If we reach here, all changes are saved permanently
Potential Issues Encountered During ACID Implementation
Although ACID properties are mostly implemented by modern database management systems automatically, many problems can occur when implementing transactions.
Long transaction duration
When a transaction takes too much time to execute, there can be some row locking and table locking. This can have a negative effect on the database performance.
Improper isolation level
Each transaction has its own isolation level that defines the transaction behavior (Read Uncommitted, Read Committed, Repeatable Read, Serializable). The wrong choice can cause dirty, non-repeatable, and phantom reads.
Relying totally on application logic
It can happen that a developer tries to implement consistency only in the application logic and not in the database itself. In some cases, this approach does not work because many different services may work with the same database.
When to Skip the ACID Rule (BASE)
High scalability applications such as social media newsfeed and worldwide "likes counters" cannot work efficiently with full ACID compliance due to some restrictions. This is why BASE principles for NoSQL databases came into play:
- Basically Available
- Soft state
- Eventual consistency
For BASE, having a slightly inconsistent number of "likes" for several seconds is acceptable as long as performance and availability of the system remain high.
Conclusion
ACID principles are the unsaid custodians of your application reputation. Be it an SQL DB or distributed stateful systems, having these four principles on your mind will ensure that users trust your app even when something is going wrong on the back-end. Knowing how to leverage these rules is the main skill that differentiates juniors and seniors.