Databases & Consistency
Database Relational databases : dominant information storage/retrieval system
Database Data stored in tables Each row is a record Columns define attributes
Consistency Consistent : no contradictions
Consistency Inconsistent : contradictions
How Do we Become Inconsistent? Only one row can be saved at a time…
How Do we Become Inconsistent? Only one row can be saved at a time…
How Do we Become Inconsistent? Only one row can be saved at a time… CRASH!!!!
How Do we Become Inconsistent? Two programs interact Program 1: Make Rosina & Jingyi friends Program 2: Make Matt & Jingi friends
How Do we Become Inconsistent? Two programs interact Program 1: Make Rosina & Jingyi friends Make Rosina friend Jingyi Program 2: Make Matt & Jingi friends
How Do we Become Inconsistent? Two programs interact Program 1: Make Rosina & Jingyi friends Make Rosina friend Jingyi Program 2: Make Matt & Jingi friends Make Matt friend Jingyi
How Do we Become Inconsistent? Two programs interact Program 1: Make Rosina & Jingyi friends Make Rosina friend Jingyi Check who Jingyi is friends with none Program 2: Make Matt & Jingi friends
How Do we Become Inconsistent? Two programs interact Program 1: Make Rosina & Jingyi friends Make Rosina friend Jingyi Check who Jingyi is friends with none Set Jingyi friend to none + Rosina Program 2: Make Matt & Jingi friends
How Do we Become Inconsistent? Two programs interact Program 1: Make Rosina & Jingyi friends Make Rosina friend Jingyi Check who Jingyi is friends with none Set Jingyi friend to none + Rosina Program 2: Make Matt & Jingi friends Set Jingyi friend to none + Matt
Even Worse Zadie transfers $200 from checking to savings
Even Worse Zadie transfers $200 from checking to savings Look at checking… decide it should be $600 Look at savings… decide it should be $500 Write change to checking BOOM
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
OK smart guy… Zadie sends $200 to Pedro Pedro withdraws $100 Begin transaction Change Zadie checking from $600 to $400 Change Pedro checking from $150 to $350 End transaction Pedro withdraws $100 Change Pedro checking from $150 to $50 Spit out money
OK smart guy… Zadie sends $200 to Pedro Pedro withdraws $100 Begin transaction Change Zadie checking from $600 to $400 Change Pedro checking from $150 to $350 End transaction Pedro withdraws $100 Change Pedro checking from $150 to $50 Spit out money $400 $350
OK smart guy… Zadie sends $200 to Pedro Pedro withdraws $100 Begin transaction Change Zadie checking from $600 to $400 Change Pedro checking from $150 to $350 End transaction Pedro withdraws $100 Change Pedro checking from $150 to $50 Spit out money $400 $50 Pedro got cheated!!!
OK smart guy… Zadie sends $200 to Pedro Pedro withdraws $100 Begin transaction Change Zadie checking from $600 to $400 Change Pedro checking from $150 to $350 End transaction Pedro withdraws $100 Change Pedro checking from $150 to $50 Spit out money $50
OK smart guy… Zadie sends $200 to Pedro Pedro withdraws $100 Begin transaction Change Zadie checking from $600 to $400 Change Pedro checking from $150 to $350 End transaction Pedro withdraws $100 Change Pedro checking from $150 to $50 Spit out money $400 $350 Pedro got $100 free!!!
Atomicity Atomic operation : indivisible / one unit Never see results of half done transaction
Atomicity Transaction must LOCK all rows it will modify before starting Want to transfer money from Marie to Pedro
Deadlock Two Programs both trying to transfer money Process A locks Marie Process B Pedro
Deadlock Process A locks Marie… now needs Pedro Process B Pedro… now needs Marie
Deadlock Busting Database can resolve: Roll back one transaction Give up its locks Make it restart AFTER other is complete
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
Transactional Memory Current research area : Design regular software with transactional memory interactions