Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Transactions

What is a Transaction?

A transaction is a sequence of operations performed as a single logical unit of work. In the context of databases, a transaction typically involves reading and writing data. Transactions are crucial for maintaining data integrity, especially in environments where multiple users or systems may be accessing or modifying the same data concurrently.

Properties of Transactions

Transactions are characterized by the ACID properties, which ensure reliable processing of database transactions:

  • Atomicity: This property ensures that all operations within a transaction are completed successfully. If any operation fails, the entire transaction is rolled back to maintain data integrity.
  • Consistency: A transaction must transition the database from one valid state to another valid state. This means that any data written to the database must be valid according to all defined rules, including constraints and cascades.
  • Isolation: Transactions should execute independently of one another. Even if transactions are occurring concurrently, the outcome should be as if they were executed sequentially.
  • Durability: Once a transaction has been committed, it will remain so, even in the event of a system failure. This ensures that the results of a completed transaction are permanently recorded in the database.

Examples of Transactions

Consider a simple banking application where a user wants to transfer money from one account to another. This operation can be broken down into two main actions:

Example:

1. Deduct amount from Account A

2. Add amount to Account B

Both actions need to be performed as a single transaction to ensure that the money is not lost. If the deduction from Account A succeeds but the addition to Account B fails, the transaction must be rolled back to maintain consistency.

Transaction Management in NoSQL Databases

Unlike traditional SQL databases, many NoSQL databases do not support ACID transactions. Instead, they often provide eventual consistency, which allows for better performance and scalability. However, some NoSQL databases, like MongoDB and Couchbase, have introduced features to manage transactions, allowing developers to perform multi-document transactions while maintaining some level of consistency.

For instance, in MongoDB, you can perform a transaction across multiple documents within a session. Here’s a simple example of how a transaction might be handled:

MongoDB Transaction Example:

const session = client.startSession();

session.startTransaction();

try {

await collection1.updateOne({ _id: idA }, { $inc: { balance: -amount } }, { session });

await collection2.updateOne({ _id: idB }, { $inc: { balance: amount } }, { session });

await session.commitTransaction();

} catch (error) {

await session.abortTransaction();

} finally {

session.endSession();

}

In this example, if either update operation fails, the transaction will be aborted, ensuring that no partial modifications are made to the database.

Conclusion

Understanding transactions is fundamental for database design and management. While NoSQL databases may not offer traditional transaction support, they have evolved to provide mechanisms for ensuring data integrity and consistency in distributed systems. By grasping the concepts of ACID properties and transaction management, developers can better design applications that handle complex data interactions safely and efficiently.