![](https://csdnimg.cn/release/download_crawler_static/9432680/bg11.jpg)
Chapter 1 ■ IntroduCtIon to Graph databases
3
When you talk about a database transaction, it should be atomic, consistent, isolated, and durable, or
ACID. If a database transaction is truly ACID, then it works as it’s been explained here, in an all-or-nothing
fashion. The most important time for a transaction to abide by the ACID principles is when money is
involved. For instance, if you had a system in place to pay bills, which transferred money from one account
to another, and then made a payment, you’d want to ensure all of that happened without any errors. If the
bills account was empty, the money transfer from one account to the other would fail, but if the two actions
were run outside of a single transaction, you would still try to make the payment, even though no money
had been transferred. This is an example of when a query is executed, then subsequent queries depend
on the result of the first one, and in this case, you’d want both queries to be in one, ACID-compliant
transaction.
Principles used within ACID are relative to the CAP Theorem, also known as Brewer’s Theorem. Eric
Brewer (the theorem’s creator) stated that it is impossible for a distributed computer system (or database) to
simultaneously guarantee the following three conditions:
• Consistency (data is available to all nodes at the same time)
• Availability (each request receives a response about whether it was successful or
failed)
• Partition tolerance (the system can still operate despite losing contact with other
nodes due to network issues)
If a system of nodes (or databases) wants to be always available, and safe from failures, then it cannot
always have the most up-to-date data. For example, if you have a system of three nodes, each node would
be a copy of the last, so if one failed, you would have access to the other two. If you were to make a change
to one of the nodes, then the other two nodes would be unaware of the change. To combat this problem,
Eventual Consistency is implied, meaning that through some means, eventually, the change would be
mirrored across all three nodes. In relation to ACID, until a transaction has completed, the contents of
that transaction won’t be available to access within a database. Essentially, CAP, is ACID, but applied to a
distributed system.
Many database vendors rave about their software being fully ACID compliant, so this was just a quick
overview to show what that actually is. Although a lot of different systems support ACID, it’s not something
that just happens. In most cases you’ll need to show you want to start a transaction, which can be different
depending on the query language used, but the concept is the same. Once the transaction has been
initialized, the queries running within it are added. Then when it needs to be committed, this is added to the
query in the way the language requires it. There are cases when you simply don’t need transactions, so just
remember when you want to use a transaction, you’ll probably have to indicate it in the query, unless your
chosen database vendor has different rules.
Although transactions are used with the intention of rolling them back if they fail, this isn’t always the
desired outcome. In some cases, such as in MySQL, you need to explicitly say if you want to rollback a failed
transaction, and this can only be done before the transaction is committed. Each database vendor will have
its own rules when it comes to how transactions are handled, so if you want to use them just be sure to check
the official documentation to ensure you’re using them correctly.