Databases & Consistency. Database Relational databases : dominant information storage/retrieval...

Preview:

Citation preview

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 Rosina 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• Make Rosina friend Jingyi• Check who Jingyi is friends with

none

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• Make Rosina friend Jingyi• Check who Jingyi is friends with

none

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• Make Rosina friend Jingyi• Check who Jingyi is friends with

none• Set Jingyi friend to none + Matt

Even Worse

• Zadie transfers $200 from savings to checking

Even Worse

• Zadie transfers $200 from savings to checking– 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

– Begin transaction– Change Zadie checking

from $600 to $400– Change Pedro checking

from $150 to $350– End transaction

• Pedro withdraws $100– Begin transaction– Change Pedro checking

from $150 to $50– Spit out money– End transaction

OK smart guy…• Zadie sends $200 to Pedro

– Begin transaction– Change Zadie checking

from $600 to $400– Change Pedro checking

from $150 to $350– End transaction

• Pedro withdraws $100– Begin transaction– Change Pedro checking

from $150 to $50– Spit out money– End transaction

$400

$350

OK smart guy…• Zadie sends $200 to Pedro

– Begin transaction– Change Zadie checking

from $600 to $400– Change Pedro checking

from $150 to $350– End transaction

• Pedro withdraws $100– Begin transaction– Change Pedro checking

from $150 to $50– Spit out money– End transaction

$400

$50

Pedro got cheated!!!

OK smart guy…• Zadie sends $200 to Pedro

– Begin transaction– Change Zadie checking

from $600 to $400– Change Pedro checking

from $150 to $350– End transaction

• Pedro withdraws $100– Begin transaction– Change Pedro checking

from $150 to $50– Spit out money– End transaction

$50

OK smart guy…• Zadie sends $200 to Pedro

– Begin transaction– Change Zadie checking

from $600 to $400– Change Pedro checking

from $150 to $350– End transaction

• Pedro withdraws $100– Begin transaction– Change Pedro checking

from $150 to $50– Spit out money– End transaction

$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

Atomicity

• Process A locks Marie• Process B Pedro

Atomicity

• Process A locks Marie… now needs Pedro• Process B Pedro… now needs Marie

Deadlock Busting

• Deadlock requires:– Mutual Exclusion, Hold & Wait, No Preemption

Deadlock Busting

• Deadlock requires:– Mutual Exclusion, Hold & Wait, No Preemption

• Database can preempt:– Roll back one transaction– Give up its locks– Make it restart AFTER other is complete

Replication

• Databases must be replicated– Redundancy– Speed

• Copies mustremain consistenton 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 OKPrepare

Prepare Then Commit Trick

• Master DB initiates transaction

• Asks each other DB to do same… waits for OKPrepare

• Once everyone complete, sends signal to end transactionCommit

Prepare Then Commit Trick

• Master DB initiates transaction

• Asks each other DB to do same… waits for OKERROR

Prepare Then Commit Trick

• Master DB initiates transaction

• Asks each other DB to do same… waits for OKERROR

• Rollback instead of committing

Transactional Memory

• Current research area : Design regular software with transactional memory interactions

Recommended