Databases & Consistency
Database Relational databases : dominant information storage/retrieval system
Activity…
Consistency Consistent : no contradictions
Consistency Inconsistent : contradictions
How Do we Become Inconsistent? Only one row can be saved at a time… Make Rosina and Jingyi friends
How Do we Become Inconsistent? Only one row can be saved at a time… Make Rosina and Jingyi friends
How Do we Become Inconsistent? Only one row can be saved at a time… Make Rosina and Jingyi friends CRASH!!!!
Even Worse
Concurrency Issues Concurrency : two things happening at the same time Many things are hard to share: Printers Shared memory
Concurrency Issues Race Condition : unpredictable result based on timing of concurrent operations
Concurrency Issues X starts as 5, 3 possible answers: Case 1 Case 2 A runs, x = 15 B runs, x =16 B runs, x = 6 A runs, x = 16 A gets x (5) B gets x (5) A adds 10 (has 15) B adds 1 (has 6) A stores x = 15 B stores x = 6 B gets x (5) A adds 10 (has 15) B adds 1 (has 6) B stores x = 6 A stores x = 15
Locks Can prevent concurrency problems with locks:
Atomicity Transaction must LOCK all rows it will modify before starting Want to transfer money from Marie to Pedro
Deadlock Workers A and B both want to use locked resources X and Y:
Deadlock Busting Database can resolve: Roll back one transaction Give up its locks Make it restart AFTER other is complete
To Do List Trick Make a list of what you intend to do and save it:
To Do List Trick Make a list of what you intend to do and save it:
To Do List Trick Make a list of what you intend to do and save it:
To Do List Trick Delete list when you are done:
To Do List Trick A crash…
Idempotent Idempotent action : same effect no matter how many times you execute it "Change checking balance to $600" Non-idempotent "Subtract $200 from checking"
To Do List If every instruction is: Idempotent Reversible : Know how to restore original state Can resume or reverse any partial transaction
Atomicity Atomic operation : indivisible / one unit Never see results of half done transaction
Transactional Memory Current research area : Design regular software with transactional memory interactions
Replication Databases must be replicated Redundancy Speed Copies must remain consistent on the fly
Prepare Then Commit Trick Master DB initiates transaction
Prepare Then Commit Trick Master DB initiates transaction Asks each other DB to do same… waits for OK Prepare
Prepare Then Commit Trick Master DB initiates transaction Asks each other DB to do same… waits for OK Prepare Once everyone complete, sends signal to end transaction Commit
Prepare Then Commit Trick Master DB initiates transaction Asks each other DB to do same… waits for OK ERROR
Prepare Then Commit Trick Master DB initiates transaction Asks each other DB to do same… waits for OK ERROR Rollback instead of committing