Understanding isolation levels

HieuNguyenTrung 942 views 36 slides Jun 04, 2018
Slide 1
Slide 1 of 36
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36

About This Presentation

Isolation (one of ACID) is an important attribute of Database. Understanding it helps us understand the database behavior and trade-offs.


Slide Content

Understanding Isolation Levels By Hieu Nguyen

Which isolation level our database currently is?

ACID

Vestibulum congue Vestibulum congue Vestibulum congue ACID Durable Isolated Consistent Atomic

Vestibulum congue Vestibulum congue Vestibulum congue ACID Durable Isolated Consistent Atomic

Isolation A transaction is invisible to other transactions before it is completed

Isolation A transaction is invisible to other transactions before it is completed The statement above is usually true

Isolation Levels

Serializable 04 Repeatable Read (Snapshot Isolation) 03 Read committed 02 Read uncommitted (Dirty Read) 01

Query 1 BEGIN TRANSACTION SET balance = balance - 100 FROM users WHERE users.name = 'A'; SET balance = balance + 100 FROM users WHERE users.name = 'B'; END

Query 2 BEGIN TRANSACTION SELECT name, balance FROM users WHERE users.name IN ('A', 'B'); END

Serializable 04 Repeatable Read (Snapshot Isolation) 03 Read committed 02 Read uncommitted (Dirty Read) 01 Has no concurrency control No overhead Bad data consistency

Serializable 04 Use locks for concurrency control High overhead Good consistency Repeatable Read (Snapshot Isolation) 03 Read committed 02 Read uncommitted (Dirty Read) 01 Has no concurrency control No overhead Bad data consistency

Serializable 04 Use locks for concurrency control High overhead Good consistency Repeatable Read (Snapshot Isolation) 03 Read committed 02 Read uncommitted (Dirty Read) 01 Has no concurrency control No overhead Bad data consistency ?

Serializable 04 Use locks for concurrency control High overhead Good consistency Repeatable Read (Snapshot Isolation) 03 Read committed 02 Use committed/uncommitted state for CC Very low overhead Prevent Dirty Read Has Non-repeatable Read issue Read uncommitted (Dirty Read) 01 Has no concurrency control No overhead Bad data consistency

Query 1 BEGIN TRANSACTION INSERT INTO mails (content, read) VALUE (content, FALSE); SET notifications_count = (SELECT COUNT(*) FROM mails WHERE read IS FALSE) FROM mailboxes; END

Query 2 BEGIN TRANSACTION SELECT notifications_count FROM mailboxes; SELECT * FROM mails WHERE read IS FALSE; END

Multiversion Concurrency Control (MVCC) When a record is created (using INSERT), it has created_id as the current transaction_id and deleted_id as nil. When a record is updated, a new row is created with created_id as the current transaction_id and deleted_id as nil. The old row is updated with deleted_id as the current transaction_id. When a record is deleted, deleted_id of the row is updated with current transaction_id. When reading, we filter all versions of the row using 2 criterias: row must have created_id <= current transaction id row must have deleted_id > current transaction id or nil

Serializable 04 Use locks for concurrency control High overhead Good consistency Repeatable Read (Snapshot Isolation) 03 Use MVCC Low overhead Prevent Dirty Read & Non-repeatable Read Prevent certain Phantom Read issues Read committed 02 Use committed/uncommitted state for CC Very low overhead Prevent Dirty Read Has Non-repeatable Read issue Read uncommitted (Dirty Read) 01 Has no concurrency control No overhead Bad data consistency

Prevent data inconsistency for non-serialization Application level of concurrency control Scheduled audit to check bad data

Usage

Read uncommitted Not recommend Should only be used in very special cases

Read committed Not recommend Used in application which required very fast read/write

Repeatable Read Recommended for most OLTP application

Serializable Recommended for data-critical application like banking

Conclusion

Conclusion Isolation Level is level of invisibility of a transaction to another transaction There are 4 levels defined by SQL Each level has trade-off in consistency and concurrency Aware of data inconsistency problem of non-serialization isolation levels

Thank you for listening!