46
Roger Bodamer [email protected] @rogerb

Thoughts on Transaction and Consistency Models

Embed Size (px)

DESCRIPTION

Thoughts on Transaction and Consistency Models

Citation preview

Page 1: Thoughts on Transaction and Consistency Models

Roger  [email protected]

@rogerb

Page 2: Thoughts on Transaction and Consistency Models

Thoughts on Transaction and

Consistency Models

Page 3: Thoughts on Transaction and Consistency Models

RDBMS(Oracle,  MySQL)

Page 4: Thoughts on Transaction and Consistency Models

RDBMS(Oracle,  MySQL)

New Gen. OLAP

(vertica,  aster,  greenplum)

Page 5: Thoughts on Transaction and Consistency Models

RDBMS(Oracle,  MySQL)

New Gen. OLAP

(vertica,  aster,  greenplum)

Non-relationalOperational

Stores(“NoSQL”)

Page 6: Thoughts on Transaction and Consistency Models

The database world is changingDocument Datastores, Key Value, Graph databases

Page 7: Thoughts on Transaction and Consistency Models

The database world is changingTransactional model

Page 8: Thoughts on Transaction and Consistency Models

The database world is changingFull Acid

Page 9: Thoughts on Transaction and Consistency Models

The database world is changing

Page 10: Thoughts on Transaction and Consistency Models

depth  of  functionality

scalab

ility  &  perform

ance • memcached

• key/value

• RDBMS

Page 11: Thoughts on Transaction and Consistency Models

CAP It is impossible in the asynchronous network model to

implement a read/write data object that guarantees the following properties:• Availability• Atomic consistency in all fair executions (including those in which messages are lost).

Page 12: Thoughts on Transaction and Consistency Models

CAP It is impossible in the asynchronous network model to

implement a read/write data object that guarantees the following properties:• Availability• Atomic consistency in all fair executions (including those in which messages are lost).

Or: If the network is broken, your database won’t work

But we get to define “won’t work”

Page 13: Thoughts on Transaction and Consistency Models

Consistency Models - CAP

• Choices are Available-P (AP) or Consistent-P (CP)• Write Availability, not Read Availability, is the Main Question

• It’s not all about CAPScale, Reduced latency:

•Multi data center•Speed•Even load distribution

Page 14: Thoughts on Transaction and Consistency Models

Examples of Eventually Consistent Systems

• Eventual Consistency: •“The storage system guarantees that if no new updates are made to the object, eventually all accesses will return the last updated value”

•Examples:• DNS• Async replication (RDBMS, MongoDB)• Memcached (TTL cache)

Page 15: Thoughts on Transaction and Consistency Models

Eventual Consistency

Page 16: Thoughts on Transaction and Consistency Models

Eventual Consistency

Read(x)  :  1,  2,  2,  4,  4,  4,  4  …

Page 17: Thoughts on Transaction and Consistency Models

Could we get this?

Read(x)  :  1,  2,  1,  4,  2,  4,  4,  4  …

Page 18: Thoughts on Transaction and Consistency Models

Monotonic read consistency

• Appserver and slave on same box• Appserver only reads from Slave

• Eventually consistent• Guaranteed to see reads in order

Prevent  seeing  writes  out  of  order

Page 19: Thoughts on Transaction and Consistency Models

Monotonic read consistency

• Appserver and slave on same box• Appserver only reads from Slave

• Eventually consistent• Guaranteed to see reads in order

• Failover ?

Prevent  seeing  writes  out  of  order

Page 20: Thoughts on Transaction and Consistency Models

RYOW Consistency

Primary

Read  /    Has  Written

Replication

Read

Secondary

• Read Your Own Writes

• Eventually consistent for readers• Immediately consistent for writers

Page 21: Thoughts on Transaction and Consistency Models

Amazon Dynamo

•R: # of servers to read from•W: # servers to get response from•N: Replication factor

• R+W>N has nice properties

Page 22: Thoughts on Transaction and Consistency Models

ExampleExample  1

R  +  W  <=  N

R  =  1W  =  1N  =  5

Possibly  Stale  dataHigher  availability

Page 23: Thoughts on Transaction and Consistency Models

ExampleExample  1

R  +  W  <=  N

R  =  1W  =  1N  =  5

Possibly  Stale  dataHigher  availability

Example  2

R  +  W  >  N

R  =  2                                          R  =  1W  =  1                                        W  =  2N  =  2                                        N  =  2

Consistent  data

Page 24: Thoughts on Transaction and Consistency Models

R + W > N

If R+W > N, we can’t have both fast local reads and writes at the same time if all the data centers are equal peers

Page 25: Thoughts on Transaction and Consistency Models

Consistency levels

• Strong: W + R > N• Weak / Eventual : W + R <= N• Optimized Read: R=1, W=N• Optimized Write: W=1, R=N

Page 26: Thoughts on Transaction and Consistency Models

Multi Datacenter Strategies

• DR• Failover

• Single Region, Multi Datacenter• Useful for Strong or Eventual Consistency

• Local Reads, Remote Writes• All writes to master on WAN, EC on Slaves

• Intelligent Homing• Master copy close to user location

Page 27: Thoughts on Transaction and Consistency Models

Network Partitions

Page 28: Thoughts on Transaction and Consistency Models

Network Write Possibilities•Deny all writes

• Still Read fully consistent data • Give up write Availability

Page 29: Thoughts on Transaction and Consistency Models

Network Write Possibilities•Deny all writes

• Still Read fully consistent data • Give up write Availability

•Allow writes on one side• Failover• Possible to allow reads on other side

Page 30: Thoughts on Transaction and Consistency Models

Network Write Possibilities•Deny all writes

• Still Read fully consistent data • Give up write Availability

•Allow writes on one side• Failover• Possible to allow reads on other side

•Allow writes on both sides• Available • Give up consistency

Page 31: Thoughts on Transaction and Consistency Models

Multiple Writer Strategies• Last one wins

• Use Vector clocks to decide latest

• Insert Inserts often really means if ( !exists(x)) then set(x) exists is hard to implement in eventually consistent systems

Page 32: Thoughts on Transaction and Consistency Models

Multiple Writer Strategies• Delete op1: set( { _id : 'joe', age : 40 } } op2: delete( { _id : 'joe' } ) op3: set( { _id : 'joe', age : 33 } )

• Consider switching 2 and 3• Tombstone: Remember delete, and apply last-operation-wins

• Update update users set age=40 where _id=’joe’

However: op1: update users set age=40 where _id='joe' op2: update users set state='ca' where _id='joe'

Page 33: Thoughts on Transaction and Consistency Models

Multiple Writer Strategies• Programatic Merge

• Store operations, instead of state• Replay operations • Did you get the last operation ? • Not immediate

•Communtative operations• Conflict free? • Fold-able• Example: add, increment, decrement

Page 34: Thoughts on Transaction and Consistency Models

Trivial Network Partitions

Page 35: Thoughts on Transaction and Consistency Models

MongoDB Options

Page 36: Thoughts on Transaction and Consistency Models

MongoDB Transaction Support• Single Master / Sharded

• MongoDB Supports Atomic Operations on Single Documents• But not Rollback

• $ operators• $set, $unset, $inc, $push, $pushall, $pull, $pullall

• Update if current• find followed by update

• Find and modify

Page 37: Thoughts on Transaction and Consistency Models

MongoDB

Primary

Read  /  Write

Page 38: Thoughts on Transaction and Consistency Models

MongoDB

Primary

Secondary

Read  /  Write

Secondary  for  Backup

Replication

Replication

Page 39: Thoughts on Transaction and Consistency Models

MongoDB

Primary

Secondary

Read  /  Write

Secondary  for  Backup

Read Replication

Replication

Page 40: Thoughts on Transaction and Consistency Models

MongoDB

Primary

Secondary

Read  /  Write

Secondary  for  Backup

Read Replication

Replication

Replicaset

Page 41: Thoughts on Transaction and Consistency Models

Write Scalability: Sharding

write

read

ReplicaSet  1

Primary

Secondary

Secondary

ReplicaSet  2

Primary

Secondary

Secondary

ReplicaSet  3

Primary

Secondary

Secondary

key  range  0  ..  30

key  range  31  ..  60

key  range  61  ..  100

Page 42: Thoughts on Transaction and Consistency Models
Page 43: Thoughts on Transaction and Consistency Models

Sometimes we need global state / more consistency

•Unique key constraints•User registration

•ACL changes

Page 44: Thoughts on Transaction and Consistency Models

Could it be the case that…

uptime( CP + average developer ) >= uptime( AP + average developer )

where uptime:= system is up and non-buggy?

Page 45: Thoughts on Transaction and Consistency Models

Thank You :-)@rogerb

Page 46: Thoughts on Transaction and Consistency Models

@mongodb

conferences,  appearances,  and  meetupshttp://www.10gen.com/events

http://bit.ly/mongoU  Facebook                    |                  Twitter                  |                  LinkedIn

http://linkd.in/joinmongo

download at mongodb.org