transactions and concurrency control patterns

58
Transactions and Concurrency Control Patterns VLAD MIHALCEA

Upload: vlad-mihalcea

Post on 12-Apr-2017

742 views

Category:

Internet


7 download

TRANSCRIPT

Page 1: Transactions and Concurrency Control Patterns

Transactions and Concurrency Control

Patterns

VLAD MIHALCEA

Page 2: Transactions and Concurrency Control Patterns

About me

• @Hibernate Developer

• vladmihalcea.com

• @vlad_mihalcea

• vladmihalcea

Page 3: Transactions and Concurrency Control Patterns

Google Developers

“We believe it is better to have application

programmers deal with performance problems

due to overuse of transactions as bottlenecks arise,

rather than always coding around

the lack of transactions.”

Spanner: Google’s Globally-Distributed Database - https://static.googleusercontent.com/media/research.google.com/en//archive/spanner-osdi2012.pdf

Page 4: Transactions and Concurrency Control Patterns

Database transactions

• Every statement is executed in the context of a transaction

• There are two types of transactions:

• Implicit – auto-commit

• Explicit – BEGIN, COMMIT or ROLLBACK

Page 5: Transactions and Concurrency Control Patterns

History of ACID

In SQL-86, there were only three transaction properties:

• Consistency

• Atomicity

• Durability

The Transaction Concept: Virtues and Limitations http://research.microsoft.com/en-us/um/people/gray/papers/theTransactionConcept.pdf

Page 6: Transactions and Concurrency Control Patterns

Atomicity

Page 7: Transactions and Concurrency Control Patterns

Atomicity

• Atomicity requires rolling back

• Rolling back requires preventing dirty writes

Page 8: Transactions and Concurrency Control Patterns

Dirty Write Anomaly

Page 9: Transactions and Concurrency Control Patterns

Consistency

• Moving the database from one valid state to another.

• Constraint checks

• column types

• column length

• column nullability

• foreign key constraints

• unique key constraints

Page 10: Transactions and Concurrency Control Patterns

Consistency in CAP – Linearizability

Page 11: Transactions and Concurrency Control Patterns

Durability

• Durability ensures that all committed transaction changes become permanent.

Page 12: Transactions and Concurrency Control Patterns

Durability

Page 13: Transactions and Concurrency Control Patterns

SQL-92 ACID

• Atomicity

• Consistency

• Isolation

• Durability

Page 14: Transactions and Concurrency Control Patterns

Serializability

• Interleaving concurrent transaction statements so that the outcome is equivalent to a serial execution

Page 15: Transactions and Concurrency Control Patterns

Serial execution

Page 16: Transactions and Concurrency Control Patterns

Conflict

Page 17: Transactions and Concurrency Control Patterns

Dealing with Concurrency

• Avoid it• VoltDB (in-memory, single-threaded, guarantees Serializability)

• Control it• RDBMS (Concurrency Control and isolation levels)

Page 18: Transactions and Concurrency Control Patterns

Dealing with conflicts in RDBMS

• Conflict avoidance• 2PL (Two-Phase Locking)

• Conflict detection• MVCC (Multi-Version Concurrency Control)

Page 19: Transactions and Concurrency Control Patterns

Lock types

Compatibility matrix Shared Lock Exclusive lock

Shared Lock Allow Prevent

Exclusive lock Prevent Prevent

Page 20: Transactions and Concurrency Control Patterns

2PL

• Simple to reason about:• Reads acquire shared locks

• Writes acquire exclusive locks

• The 2PL protocol:

• expanding phase (acquire locks, release no lock)

• shrinking phase (release all locks, no further lock acquisition)

Page 21: Transactions and Concurrency Control Patterns

Cost of locking

Page 22: Transactions and Concurrency Control Patterns

Challenges have changed

“At present [1981], the largest airlines and

banks have about 10,000 terminals and about

100 active transactions at any instant.

These transactions live for a second or two and are

gone forever.”

The Transaction Concept: Virtues and Limitations - http://research.microsoft.com/en-us/um/people/gray/papers/theTransactionConcept.pdf

Page 23: Transactions and Concurrency Control Patterns

MVCC

• Readers don’t block writers and writers don’t block readers.

• Writers block writers to prevent Dirty Writes.

Page 24: Transactions and Concurrency Control Patterns

MVCC – Insert row (PostgreSQL)

Page 25: Transactions and Concurrency Control Patterns

MVCC – Delete row (PostgreSQL)

Page 26: Transactions and Concurrency Control Patterns

MVCC – Update row (PostgreSQL)

Page 27: Transactions and Concurrency Control Patterns

MVCC

• Two types of snapshots:• Query-level: Read Committed

• Transaction-level: Snapshot Isolation

• Watch out for long-running transactions

Page 28: Transactions and Concurrency Control Patterns

Phenomena

• The SQL-92 standard introduced three phenomena:

• dirty read

• non-repeatable read

• phantom read.

• In reality, there are more:• dirty write

• read skew

• write skew

• lost update.

Page 29: Transactions and Concurrency Control Patterns

Dirty Read

Page 30: Transactions and Concurrency Control Patterns

Non-Repeatable Read

Page 31: Transactions and Concurrency Control Patterns

Phantom Read

Page 32: Transactions and Concurrency Control Patterns

Read Skew

Page 33: Transactions and Concurrency Control Patterns

Write Skew

Page 34: Transactions and Concurrency Control Patterns

Lost Update

Page 35: Transactions and Concurrency Control Patterns

2PL – Phantom Read

Page 36: Transactions and Concurrency Control Patterns

MVCC – Phantom Read

Page 37: Transactions and Concurrency Control Patterns

MVCC – Phantom Read?

Page 38: Transactions and Concurrency Control Patterns

SQL Standard Isolation Levels

Isolation Level Dirty Read

Non-Repeatable Read

Phantom Read

Read Uncommitted Yes Yes YesRead Committed No Yes YesRepeatable Read No No Yes

Serializable No No No

Page 39: Transactions and Concurrency Control Patterns

Oracle Isolation Levels

Isolation Level Dirty Read

Non-Repeatable Read

Phantom Read Read Skew Write Skew Lost Update

Read Committed No Yes Yes Yes Yes Yes

Serializable No No No No Yes No

Page 40: Transactions and Concurrency Control Patterns

SQL Server Isolation Levels

Isolation level Dirty Read

Non-Repeatable Read

Phantom Read Read Skew Write Skew Lost Update

Read Uncommitted Yes Yes Yes Yes Yes YesRead Committed No Yes Yes Yes Yes YesRepeatable Read No No Yes No No No

Serializable No No No No No NoRead Committed SI No Yes Yes Yes Yes YesSnapshot Ioslation No No No No Yes No

Page 41: Transactions and Concurrency Control Patterns

PostgreSQL Isolation Levels

Isolation level Dirty Read

Non-Repeatable Read

Phantom Read Read Skew Write Skew Lost Update

Read Uncommitted No Yes Yes Yes Yes YesRead Committed No Yes Yes Yes Yes YesRepeatable Read No No No No Yes No

Serializable No No No No No No

Page 42: Transactions and Concurrency Control Patterns

MySQL Isolation Levels

Isolation level Dirty Read

Non-Repeatable Read

Phantom Read Read Skew Write Skew Lost Update

Read Uncommitted Yes Yes Yes Yes Yes YesRead Committed No Yes Yes Yes Yes YesRepeatable Read No No No No Yes Yes

Serializable No No No No No No

Page 43: Transactions and Concurrency Control Patterns

Beyond ACID

• ACID is not sufficient

• What about multi-request logical transactions?

Page 44: Transactions and Concurrency Control Patterns

Stateless conversation lost update

Page 45: Transactions and Concurrency Control Patterns

Stateful conversation lost update

Page 46: Transactions and Concurrency Control Patterns

Version-based optimistic locking

@Version

private int version;

UPDATE post

SET

name = ‘Name’, version = 1

WHERE

id = 1 AND version = 0

Page 47: Transactions and Concurrency Control Patterns

Stateful versioned conversation

Page 48: Transactions and Concurrency Control Patterns

Write concerns

Page 49: Transactions and Concurrency Control Patterns

False positives

Page 50: Transactions and Concurrency Control Patterns

Write-based schema mapping

Page 51: Transactions and Concurrency Control Patterns

Non-overlapping writes

Page 52: Transactions and Concurrency Control Patterns

Versionless optimistic locking

@Entity @Table(name = "post") @DynamicUpdate@OptimisticLocking(type = OptimisticLockType.DIRTY)public class Post {

@Idprivate Long id;

private String title;

private long views;

private int likes;}

Page 53: Transactions and Concurrency Control Patterns

Non-overlapping writes

Page 54: Transactions and Concurrency Control Patterns

Explicit optimistic locking modesLock Mode Type Description

NONE In the absence of explicit locking, the application will use implicit locking (optimistic or pessimistic)

OPTIMISTICAlways issues a version check upon transaction commit, therefore ensuring optimistic locking repeatable reads.

READ Same as OPTIMISTIC.

OPTIMISTIC_FORCE_INCREMENTAlways increases the entity version (even when the entity doesn’t change) and issues a version check upon transaction commit, therefore ensuring optimistic locking repeatable reads.

WRITE Same as OPTIMISTIC_FORCE_INCREMENT.

PESSIMISTIC_READA shared lock is acquired to prevent any other transaction from acquiring a PESSIMISTIC_WRITE lock.

PESSIMISTIC_WRITEAn exclusive lock is acquired to prevent any other transaction from acquiring a PESSIMISTIC_READ or a PESSIMISTIC_WRITE lock.

PESSIMISTIC_FORCE_INCREMENTA database lock is acquired to prevent any other transaction from acquiring a PESSIMISTIC_READ or a PESSIMISTIC_WRITE lock and the entity version is incremented upon transaction commit.

Page 55: Transactions and Concurrency Control Patterns

LockModeType.OPTIMISTIC_FORCE_INCREMENT

Page 56: Transactions and Concurrency Control Patterns

LockModeType.OPTIMISTIC_FORCE_INCREMENT

Repository repository = entityManager.find(Repository.class, 1L,LockModeType.OPTIMISTIC_FORCE_INCREMENT);

executeSync(() -> {doInJPA(_entityManager -> {

Repository _repository = _entityManager.find(Repository.class, 1L,LockModeType.OPTIMISTIC_FORCE_INCREMENT);

Commit _commit = new Commit(_repository);_commit.getChanges().add(new Change("Intro.md", "0a1,2..."));_entityManager.persist(_commit);

});});Commit commit = new Commit(repository);commit.getChanges().add(new Change("FrontMatter.md", "0a1,5..."));commit.getChanges().add(new Change("HibernateIntro.md", "17c17..."));entityManager.persist(commit);

Page 57: Transactions and Concurrency Control Patterns

LockModeType.OPTIMISTIC_FORCE_INCREMENT

Page 58: Transactions and Concurrency Control Patterns

Thank you

Questions and Answers