Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database transactions - How do they work?

I'm trying to learn more about database transactions, I found the ACID rule of thumb for writing transactions and thought of a few questions.

The ACID rule of thumb:

A transaction must be:

  1. Atomic - it is one unit of work and does not dependent on previous and following transactions.
  2. Consistent - data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t.
  3. Isolated - no transaction sees the intermediate results of the current transaction.
  4. Durable - the values persist if the data had been committed even if the system crashes right after.

I was wondering how they work under the hood, so I can better understand the factors that need to be considered when writing such a transaction. I guess the specific details will vary between the database implementations that are avaliable, but certain rules will always be in place.

  1. How does the database handle concurrent transactions whilst still supporting the Atomic rule?
    • Is there a queue of transactions that is processed in order?
    • How is a lengthy transaction that is holding up all others handled?
  2. Are updates to tables done in memory so if a crash does occur before commit, there is no alteration to the database?
    • Or are there some intermediate tables that are updated to survive such a crash?
  3. Whilst a transaction is in progress, is all read and write access to the affected tables prevented?
    • Or would the database allow writes but the transaction would overwrite all changes upon commit?
like image 341
fletcher Avatar asked Aug 12 '10 10:08

fletcher


People also ask

What is transaction and how it works?

A transaction is a logical unit of work (comprising one or more SQL statements) performed on the database to complete a common task and maintain data consistency. Transaction statements are closely related and perform interdependent actions.

How are transactions implemented?

Steps in a TransactionLocate the record to be updated from secondary storage. Transfer the block disk into the memory buffer. Make the update to tuple in the buffer buffer. Write the modified block back out to disk.


1 Answers

  1. There are many different ways, including transaction queueing, optimistic concurrency control etc. This is actually a very complex question, there are books written about it:

    http://www.amazon.co.uk/Databases-Transaction-Processing-Application-Oriented-Approach/dp/0201708728/ref=sr_1_3?ie=UTF8&s=books&qid=1281609705&sr=8-3

  2. It depends on the level of logging in the database. If strict write-ahead logs are kept then in the case of a system crash, the database can be wound back to a consistent state.

  3. It depends on the type of concurrency. Optimistic concurrency involves no locks, but if the state of the db has changed once the transaction has finished, it is abandoned and restarted. This can speed up dbs where collisions are rare. There are also different levels of locking: row,table, or even the entire db.

These are very complex questions, I'd advise buying a book, or attending a concurrent systems lecture series if you want to be able to fully answer them :-)

like image 67
fredley Avatar answered Oct 02 '22 22:10

fredley