SlideShare a Scribd company logo
1 of 54
Download to read offline
<Insert Picture Here>

Locking and Concurrency Control
Morgan Tocker, MySQL Community Manager
Safe Harbor Statement
The	
  following	
  is	
  intended	
  to	
  outline	
  our	
  general	
  product	
  direction.	
  
It	
  is	
  intended	
  for	
  information	
  purposes	
  only,	
  and	
  may	
  not	
  be	
  
incorporated	
  into	
  any	
  contract.	
  It	
  is	
  not	
  a	
  commitment	
  to	
  deliver	
  
any	
  material,	
  code,	
  or	
  functionality,	
  and	
  should	
  not	
  be	
  relied	
  upon	
  
in	
  making	
  purchasing	
  decisions.	
  



The	
  development,	
  release,	
  and	
  timing	
  of	
  any	
  features	
  or	
  
functionality	
  described	
  for	
  Oracle’s	
  products	
  remains	
  at	
  the	
  sole	
  
discretion	
  of	
  Oracle.
4 Years of MySQL Innovation
MySQL Cluster 7.3

MySQL Migration Wizard

MySQL Workbench 6.0

MySQL 5.6

MySQL 5.5

Windows installer & Tools MySQL 5.7 M y S Q L
Cluster
MySQL
MySQL Enterprise Monitor 2.3 & e r
M a n a g 3.0
Applier for
Hadoop

MySQL Enterprise Backup
Security
MySQL Utilities
MySQL Workbench 5.2 & 6.0
Scalability
MySQL Cluster 7.2
HA
MySQL Enterprise
MySQL Cluster 7.1
O r a c l e C e r t i f i c a t i o n s Audit
Agenda
1.Table Locks, Row Locks and MVCC
2.Live Demo
3.Deadlocks and Lock Waits Discussion
4.ACID Discussion
5.Summary
Table	
  Locks,	
  Row	
  Locks	
  and	
  MVCC

Copyright	
  ©	
  2012	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.
Customer Accounting System
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

Replacement
Acorn

$20

$12.50
$17.50
Start Backup
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

Replacement
Acorn

$20

$12.50
$17.50
Continues…
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

Replacement
Acorn

$20

$12.50
$17.50
Concurrent Access System - New
Customer Added
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

4

Savvy Squirrel Global

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
Backup Continues
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

4

Savvy Squirrel Global

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
Backup Continues…
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

4

Savvy Squirrel Global

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
Backup Complete
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

4

Savvy Squirrel Global

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
What was really backed up
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
Next Operation
customer
customerid

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

4

X

name

Acorn Intolerant Squirrels Inc

itemid

invoice
invoiceid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

invoice_item

...

X

invoiceid

Description

Amount

1

1

$10

2

2

1 hrs Professional
Services
Acorn Parts

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
Solution
• Operation needs to represent a single point in time
• When does not matter.
• This race condition can exist in all database
operations - not just backups.
MyISAM Implementation
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

4

Savvy Squirrel Global

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
MyISAM Implementation
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

4

Savvy Squirrel Global

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
MyISAM Implementation
customer
customerid

name

1

Acorn Enterprises

2

First National Squirrel Bank

3

Squirrels 5000 Inc

4

Savvy Squirrel Global

invoice_item
itemid

customerid

1

1 # Acorn Enterprises

2

1 # Acorn Enterprises

3

3 # Squirrels 5000 Inc

4

4 # Savvy Squirrels

...

Amount

1
2

1 hrs Professional
Services
Acorn Parts

$10

2

invoiceid

Description

1

invoice

invoiceid

3

2

$50

4

3

5 hrs Professional
Services
Delivery Fee

5

3

$20

6

4

Replacement
Acorn
Premium Acorn

$12.50
$17.50
$999
MyISAM
•
•
•
•

Readers may co-exist.
Writers are forbidden.
Locks a table by default.
Programs like mysqldump responsibly lock all tables
they need access to before starting.
InnoDB Implementation
customer
Trx ID

Rollback Ptr

customerid

name

123

##

1

123

##

2

Acorn
Enterprises
First National

123

##

3

401

##

4

Squirrel Bank
Squirrels 5000
Inc
Savvy Squirrel

invoice_item
Rollback Ptr

Trx ID

Rollback Ptr

invoiceid

customerid

123

##

1

123

##

2

1 # Acorn
Enterprises
1 # Acorn

123

##

3

401

##

4

Enterprises
3 # Squirrels
5000 Inc
4 # Savvy

...

itemid

invoiceid

Description

Amount

123 ##

1

1

2

2

123 ##

3

2

123 ##

4

3

123 ##

5

3

401 ##

6

4

1 hrs
Profess
Acorn
Parts
5 hrs
Profess
Deliver
y Fee
Replac
ement
Premiu
m

$10

123 ##

invoice

Trx ID

$12.50
$50
$17.50
$20
$999
InnoDB
• Hidden meta data attached to each row:
• Row version
• Rollback pointer to older versions if needed.
• Every transaction gets allocated a monotonic
Transaction ID.
InnoDB Implementation
Backup process is TRX ID #394.
Remembers it is not supposed to see
anything newer than itself.
New changes are permitted to continue.

customer
Trx ID

Rollback Ptr

customerid

name

123

##

1

123

##

2

Acorn
Enterprises
First National

123

##

3

401

##

4

Squirrel Bank
Squirrels 5000
Inc
Savvy Squirrel

invoice_item
Rollback Ptr

Trx ID

Rollback Ptr

invoiceid

customerid

123

##

1

123

##

2

1 # Acorn
Enterprises
1 # Acorn

123

##

3

401

##

4

Enterprises
3 # Squirrels
5000 Inc
4 # Savvy

...

itemid

invoiceid

Description

Amount

123 ##

1

1

2

2

123 ##

3

2

123 ##

4

3

123 ##

5

3

401 ##

6

4

1 hrs
Profess
Acorn
Parts
5 hrs
Profess
Deliver
y Fee
Replac
ement
Premiu
m

$10

123 ##

invoice

Trx ID

$12.50
$50
$17.50
$20
$999
MVCC Algorithm
• Stands for “Multi-version concurrency control” - http://
en.wikipedia.org/wiki/
Multiversion_concurrency_control
• Readers don’t block writers!
• And actually writers don’t block readers, because
they can read older versions.
MVCC (cont.)
• Multiple versions of the same row may exist.
• What you will see depends on the context of your
transaction. Even the number of rows in a table
depends on your context.
Live	
  Demo

Copyright	
  ©	
  2012	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.
Deadlocks	
  &	
  Lock	
  Waits

Copyright	
  ©	
  2012	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.
What is a deadlock?
• A situation when two sessions are trying to acquire
each-other’s respective locks.
• Neither can proceed until the other backs down.
• It’s no different to when two colleagues get in an
irrational argument and neither will back down :)
What is a lock wait?
• One step short of a deadlock.
• You keep trying to do something, but it’s not in the
budget this quarter?
• Not quite sure of the analogy here, but you are
free to retry.
• But.. a fair amount of the time it still won’t work
Deadlock Detection
• InnoDB as a storage engine will detect deadlocks.
• Algorithm is Graph detection http://en.wikipedia.org/
wiki/Cycle_detection - happens virtually instantly.
• Kills the transaction that modified the least rows.
• All changes are rolled back - application developer
must now handle/retry.
Preventing Deadlocks
• Deadlock is a concurrency issue.
• Same solutions as when you have colleagues
arguing!
Solution #1
• Don’t make them work-together.
• Reduce concurrency / run known conflicting workers
in serial execution.

X
Solution #2
• Don’t make them work on big projects together.
• aka lower the stakes!
• Locks are held for the duration of a transaction:
• Put people who don’t work together only on small
projects as needed (smaller transactions).
• Reduce locking required for transactions (more
efficient queries, better indexing).
Preventing Deadlocks (cont.)
• Also, we should accept that some deadlocks are a
fact of life. Continuing co-worker analogy:
• Essential for business growth that people work
together.
• Have to be prepared to handle disputes.
!
• MyISAM as a storage engine does not actually need
deadlock detection (table locking).
• Where does this leave concurrency?
Preventing Deadlocks (cont.)
• You actually want databases to be optimistic.
• That is set as few locks as possible, knowing that
sometimes oopses will happen.
• Alternative is more locks and less concurrency.
Handing Deadlocks
• Database only owns responsibility of alerting
application
• It may not reapply transaction, since state has
changed.
• Application owns responsibility of handling errors.
• Yet maybe 5% of applications do this :(
Lock Wait Timeout
• Will proceed immediately if it can acquire the lock.
• Since most transactions are less than 1 second, at
50 seconds seems unlikely retrying will work.
• May need to voluntarily rollback and try again or give
a user error.
“Requires a replay of the transaction”
•
•
•
•

The problems probably won’t happen often.
May be easier to kick back to the user:
“try again - no changes have been applied”.
Exception may be asynchronous workers?
• Usually you’ve made the promise of work without
opportunity to back-out.
ACID

Copyright	
  ©	
  2012	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.
The “ACID” Contract
• Atomicity - All or none.
• Consistency - Generic term to say all constraints etc
must be honoured (primary key, unique key).
• Isolation - Changes in one transaction to not
interfere with another transaction.
• Durability - When you said that the atomic
transaction was committed, you will honour it. Even
if the server were to crash.
Atomicity
• Most people know this one:
• Transfer $20 from Account A --> Account B.
• Multiple failure states...
• The money may be subtracted from A, but has
not reached B yet.
• Wrap your code between START TRANSACTION
and COMMIT and you get this solved for free.
Consistency
• Not too much more to add on this one.
• InnoDB has foreign key constraints
• MySQL does not feature CHECK constraints where
another database may allow more domain specific
data validation here as well.
Isolation
• Actually a very expensive feature for a database to
implement (may require lots of locking, stale data
kept around etc.)
• Many databases make this somewhat tunable.
• There are four levels of isolation describing what
potential unsafe conditions are possible.
Isolation Levels
• READ-UNCOMMITTED - No isolation
• READ-COMMITTED - Can see other session’s
committed rows.
• REPEATABLE-READ - Won’t see other session’s
committed rows once your session starts (default).
• SERIALIZABLE - Reads actually lock. Can’t modify
any data another session has changed.
Durable
• This one’s actually tunable as well.
• InnoDB will not lie by default.
• It means on COMMIT everything should survive a
power loss not be stuck in a buffer in memory.
Summary

Copyright	
  ©	
  2012	
  Oracle	
  and/or	
  its	
  affiliates.	
  All	
  rights	
  reserved.
Application Responsibilities
• Wrap every logical unit of work in START
TRANSACTION and COMMIT.
• Expect failure because failure is a database allowing
concurrency.
• LOCK WAIT TIMEOUT = Retry “may” be possible.
• DEADLOCK = Everything must be retried.
<Insert Picture Here>
Mutexes, Latches & Semaphores
• Common names given to database “internal locking”.
• Today we are not going to differentiate between
them.
• Not unique to databases, required to provide “thread
safety” in multi-threaded programs.
Example #1
• InnoDB buffer pool is full
• Free space needs to be created in order to be able to
load your desired page into memory.
• In between freeing and loading, someone else fills
desired slot.
Example #2
• When a page becomes too full, it may need to split
data into other pages.
• It is possible that there could be a thread deadlock if
two pages left and right of each-other reach this
condition at once.
• Can’t decide who should proceed first.
Internal Locking (cont.)
• When mutexes have high contention other tasks are
not runable.
• Which means that CPUs can appear idle, but nothing
is happening.
Fixes in newer versions
• Responsibility to fix is on MySQL Engineering Team.
• High Priority for Us.
• Some DBAs will have peripheral knowledge - know
situations when it’s time to upgrade.
Time to Upgrade?
• Kernel mutex split (5.6)
• Lock open mutex split (5.6)
• Buffer pool mutex contention reduced w/8 buffer
pool instances (5.6)
• Multiple rollback segments (5.5)
• Log-sys mutex split (5.5)
• Flush-list mutex split (5.5)

More mentioned at:
http://www.tocker.ca/2013/11/27/what-is-a-mutex-anyway.html

More Related Content

What's hot

More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexingYoshinori Matsunobu
 
MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바NeoClova
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index TuningManikanda kumar
 
MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용I Goo Lee
 
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)Jean-François Gagné
 
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2021] 2. Porting Oracle UDF and OptimizationPgDay.Seoul
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index CookbookMYXPLAIN
 
How to use histograms to get better performance
How to use histograms to get better performanceHow to use histograms to get better performance
How to use histograms to get better performanceMariaDB plc
 
MySQL Architecture and Engine
MySQL Architecture and EngineMySQL Architecture and Engine
MySQL Architecture and EngineAbdul Manaf
 
My sql failover test using orchestrator
My sql failover test  using orchestratorMy sql failover test  using orchestrator
My sql failover test using orchestratorYoungHeon (Roy) Kim
 
PostgreSql query planning and tuning
PostgreSql query planning and tuningPostgreSql query planning and tuning
PostgreSql query planning and tuningFederico Campoli
 
ProxySQL High Availability (Clustering)
ProxySQL High Availability (Clustering)ProxySQL High Availability (Clustering)
ProxySQL High Availability (Clustering)Mydbops
 
MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바NeoClova
 
MySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptxMySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptxNeoClova
 
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...Jean-François Gagné
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLMorgan Tocker
 

What's hot (20)

More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexing
 
InnoDB Locking Explained with Stick Figures
InnoDB Locking Explained with Stick FiguresInnoDB Locking Explained with Stick Figures
InnoDB Locking Explained with Stick Figures
 
Query logging with proxysql
Query logging with proxysqlQuery logging with proxysql
Query logging with proxysql
 
MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용
 
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
MySQL Parallel Replication: All the 5.7 and 8.0 Details (LOGICAL_CLOCK)
 
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
 
Percona toolkit
Percona toolkitPercona toolkit
Percona toolkit
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index Cookbook
 
How to use histograms to get better performance
How to use histograms to get better performanceHow to use histograms to get better performance
How to use histograms to get better performance
 
MySQL Architecture and Engine
MySQL Architecture and EngineMySQL Architecture and Engine
MySQL Architecture and Engine
 
MySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB StatusMySQL 5.5 Guide to InnoDB Status
MySQL 5.5 Guide to InnoDB Status
 
My sql failover test using orchestrator
My sql failover test  using orchestratorMy sql failover test  using orchestrator
My sql failover test using orchestrator
 
PostgreSql query planning and tuning
PostgreSql query planning and tuningPostgreSql query planning and tuning
PostgreSql query planning and tuning
 
ProxySQL High Availability (Clustering)
ProxySQL High Availability (Clustering)ProxySQL High Availability (Clustering)
ProxySQL High Availability (Clustering)
 
MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바MySQL Administrator 2021 - 네오클로바
MySQL Administrator 2021 - 네오클로바
 
MySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptxMySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptx
 
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
MySQL Parallel Replication (LOGICAL_CLOCK): all the 5.7 (and some of the 8.0)...
 
The InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQLThe InnoDB Storage Engine for MySQL
The InnoDB Storage Engine for MySQL
 

Viewers also liked

MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server DefaultsMorgan Tocker
 
MySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsMySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsTuyen Vuong
 
Understanding MySql locking issues
Understanding MySql locking issuesUnderstanding MySql locking issues
Understanding MySql locking issuesOm Vikram Thapa
 
MariaDB: Connect Storage Engine
MariaDB: Connect Storage EngineMariaDB: Connect Storage Engine
MariaDB: Connect Storage EngineKangaroot
 
InnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevInnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevFuenteovejuna
 
MySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big DataMySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big DataMorgan Tocker
 
MySQL Query Optimization
MySQL Query OptimizationMySQL Query Optimization
MySQL Query OptimizationMorgan Tocker
 
Dbms ii mca-ch10-concurrency-control-2013
Dbms ii mca-ch10-concurrency-control-2013Dbms ii mca-ch10-concurrency-control-2013
Dbms ii mca-ch10-concurrency-control-2013Prosanta Ghosh
 
Locking unit 1 topic 3
Locking unit 1 topic 3Locking unit 1 topic 3
Locking unit 1 topic 3avniS
 
MySQL For Linux Sysadmins
MySQL For Linux SysadminsMySQL For Linux Sysadmins
MySQL For Linux SysadminsMorgan Tocker
 
protocols of concurrency control
protocols of concurrency controlprotocols of concurrency control
protocols of concurrency controlMOHIT DADU
 
MySQL 5.7: Core Server Changes
MySQL 5.7: Core Server ChangesMySQL 5.7: Core Server Changes
MySQL 5.7: Core Server ChangesMorgan Tocker
 
Transaction and concurrency control
Transaction and concurrency controlTransaction and concurrency control
Transaction and concurrency controlAnil Shrestha
 
Database management system chapter16
Database management system chapter16Database management system chapter16
Database management system chapter16Md. Mahedi Mahfuj
 

Viewers also liked (20)

Optimizing MySQL
Optimizing MySQLOptimizing MySQL
Optimizing MySQL
 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server Defaults
 
MySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsMySQL Atchitecture and Concepts
MySQL Atchitecture and Concepts
 
Understanding MySql locking issues
Understanding MySql locking issuesUnderstanding MySql locking issues
Understanding MySql locking issues
 
MariaDB: Connect Storage Engine
MariaDB: Connect Storage EngineMariaDB: Connect Storage Engine
MariaDB: Connect Storage Engine
 
InnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter ZaitsevInnoDB Architecture and Performance Optimization, Peter Zaitsev
InnoDB Architecture and Performance Optimization, Peter Zaitsev
 
MySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big DataMySQL: From Single Instance to Big Data
MySQL: From Single Instance to Big Data
 
Concurrency control
Concurrency control Concurrency control
Concurrency control
 
Ch16
Ch16Ch16
Ch16
 
MySQL Query Optimization
MySQL Query OptimizationMySQL Query Optimization
MySQL Query Optimization
 
Dbms ii mca-ch10-concurrency-control-2013
Dbms ii mca-ch10-concurrency-control-2013Dbms ii mca-ch10-concurrency-control-2013
Dbms ii mca-ch10-concurrency-control-2013
 
Locking unit 1 topic 3
Locking unit 1 topic 3Locking unit 1 topic 3
Locking unit 1 topic 3
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
MySQL For Linux Sysadmins
MySQL For Linux SysadminsMySQL For Linux Sysadmins
MySQL For Linux Sysadmins
 
protocols of concurrency control
protocols of concurrency controlprotocols of concurrency control
protocols of concurrency control
 
MySQL 5.7: Core Server Changes
MySQL 5.7: Core Server ChangesMySQL 5.7: Core Server Changes
MySQL 5.7: Core Server Changes
 
MySQL NoSQL APIs
MySQL NoSQL APIsMySQL NoSQL APIs
MySQL NoSQL APIs
 
Transaction and concurrency control
Transaction and concurrency controlTransaction and concurrency control
Transaction and concurrency control
 
Database management system chapter16
Database management system chapter16Database management system chapter16
Database management system chapter16
 
Locking And Concurrency
Locking And ConcurrencyLocking And Concurrency
Locking And Concurrency
 

Similar to Locking and Concurrency Control

Sierra Manufacturing Introduction Presentation: Special Cutting Tools Fabrica...
Sierra Manufacturing Introduction Presentation: Special Cutting Tools Fabrica...Sierra Manufacturing Introduction Presentation: Special Cutting Tools Fabrica...
Sierra Manufacturing Introduction Presentation: Special Cutting Tools Fabrica...Andrew Michael Martin
 
3DConsulting_Presentation
3DConsulting_Presentation3DConsulting_Presentation
3DConsulting_PresentationJoseph Baca
 
Event Sourcing with Microservices
Event Sourcing with MicroservicesEvent Sourcing with Microservices
Event Sourcing with MicroservicesRalph Winzinger
 
Information Technology - Discover the Root Cause and Develop a solution throu...
Information Technology - Discover the Root Cause and Develop a solution throu...Information Technology - Discover the Root Cause and Develop a solution throu...
Information Technology - Discover the Root Cause and Develop a solution throu...John Hudson
 
Latin America tour 2019 - Flashback
Latin America tour 2019 -  FlashbackLatin America tour 2019 -  Flashback
Latin America tour 2019 - FlashbackConnor McDonald
 
DownUnder Dreaming - 5 steps to dreamy data
DownUnder Dreaming - 5 steps to dreamy dataDownUnder Dreaming - 5 steps to dreamy data
DownUnder Dreaming - 5 steps to dreamy dataClive Astbury
 
How to Fail at Kafka
How to Fail at KafkaHow to Fail at Kafka
How to Fail at Kafkaconfluent
 
Massive Continuous Integration and Light-speed Iterations
Massive Continuous Integration and Light-speed IterationsMassive Continuous Integration and Light-speed Iterations
Massive Continuous Integration and Light-speed IterationsTechWell
 
Kscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsKscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsConnor McDonald
 
Flashback features in Oracle - UKOUG 2017
Flashback features in Oracle - UKOUG 2017Flashback features in Oracle - UKOUG 2017
Flashback features in Oracle - UKOUG 2017Connor McDonald
 
Flashback features in Oracle - UKOUG 2017
Flashback features in Oracle - UKOUG 2017Flashback features in Oracle - UKOUG 2017
Flashback features in Oracle - UKOUG 2017Connor McDonald
 
Inspec one tool to rule them all
Inspec one tool to rule them allInspec one tool to rule them all
Inspec one tool to rule them allKimball Johnson
 
Latin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingLatin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingConnor McDonald
 
DevConf OpenSanca 2018 - How does your application behaves when everything go...
DevConf OpenSanca 2018 - How does your application behaves when everything go...DevConf OpenSanca 2018 - How does your application behaves when everything go...
DevConf OpenSanca 2018 - How does your application behaves when everything go...Emerson Macedo
 
Half way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
Half way to clean architecture - Dmytro Voronkevych - Droidcon BerlinHalf way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
Half way to clean architecture - Dmytro Voronkevych - Droidcon BerlinBadoo
 
S2A Investment Opportunity Webinar Powerpoint_4.5.20
S2A Investment Opportunity Webinar Powerpoint_4.5.20S2A Investment Opportunity Webinar Powerpoint_4.5.20
S2A Investment Opportunity Webinar Powerpoint_4.5.20S2A Modular
 
Melbourne Groundbreakers Tour - Hints and Tips
Melbourne Groundbreakers Tour - Hints and TipsMelbourne Groundbreakers Tour - Hints and Tips
Melbourne Groundbreakers Tour - Hints and TipsConnor McDonald
 
OOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASOOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASKyle Hailey
 

Similar to Locking and Concurrency Control (20)

Sierra Manufacturing Introduction Presentation: Special Cutting Tools Fabrica...
Sierra Manufacturing Introduction Presentation: Special Cutting Tools Fabrica...Sierra Manufacturing Introduction Presentation: Special Cutting Tools Fabrica...
Sierra Manufacturing Introduction Presentation: Special Cutting Tools Fabrica...
 
3DConsulting_Presentation
3DConsulting_Presentation3DConsulting_Presentation
3DConsulting_Presentation
 
Event Sourcing with Microservices
Event Sourcing with MicroservicesEvent Sourcing with Microservices
Event Sourcing with Microservices
 
Information Technology - Discover the Root Cause and Develop a solution throu...
Information Technology - Discover the Root Cause and Develop a solution throu...Information Technology - Discover the Root Cause and Develop a solution throu...
Information Technology - Discover the Root Cause and Develop a solution throu...
 
Latin America tour 2019 - Flashback
Latin America tour 2019 -  FlashbackLatin America tour 2019 -  Flashback
Latin America tour 2019 - Flashback
 
DownUnder Dreaming - 5 steps to dreamy data
DownUnder Dreaming - 5 steps to dreamy dataDownUnder Dreaming - 5 steps to dreamy data
DownUnder Dreaming - 5 steps to dreamy data
 
How to Fail at Kafka
How to Fail at KafkaHow to Fail at Kafka
How to Fail at Kafka
 
Massive Continuous Integration and Light-speed Iterations
Massive Continuous Integration and Light-speed IterationsMassive Continuous Integration and Light-speed Iterations
Massive Continuous Integration and Light-speed Iterations
 
Are we there yet?
Are we there yet?Are we there yet?
Are we there yet?
 
Kscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAsKscope19 - Flashback: Good for Developers as well as DBAs
Kscope19 - Flashback: Good for Developers as well as DBAs
 
Flashback features in Oracle - UKOUG 2017
Flashback features in Oracle - UKOUG 2017Flashback features in Oracle - UKOUG 2017
Flashback features in Oracle - UKOUG 2017
 
Flashback features in Oracle - UKOUG 2017
Flashback features in Oracle - UKOUG 2017Flashback features in Oracle - UKOUG 2017
Flashback features in Oracle - UKOUG 2017
 
Inspec one tool to rule them all
Inspec one tool to rule them allInspec one tool to rule them all
Inspec one tool to rule them all
 
Latin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matchingLatin America Tour 2019 - pattern matching
Latin America Tour 2019 - pattern matching
 
DevConf OpenSanca 2018 - How does your application behaves when everything go...
DevConf OpenSanca 2018 - How does your application behaves when everything go...DevConf OpenSanca 2018 - How does your application behaves when everything go...
DevConf OpenSanca 2018 - How does your application behaves when everything go...
 
Half way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
Half way to clean architecture - Dmytro Voronkevych - Droidcon BerlinHalf way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
Half way to clean architecture - Dmytro Voronkevych - Droidcon Berlin
 
S2A Investment Opportunity Webinar Powerpoint_4.5.20
S2A Investment Opportunity Webinar Powerpoint_4.5.20S2A Investment Opportunity Webinar Powerpoint_4.5.20
S2A Investment Opportunity Webinar Powerpoint_4.5.20
 
Melbourne Groundbreakers Tour - Hints and Tips
Melbourne Groundbreakers Tour - Hints and TipsMelbourne Groundbreakers Tour - Hints and Tips
Melbourne Groundbreakers Tour - Hints and Tips
 
MySQL under the siege
MySQL under the siegeMySQL under the siege
MySQL under the siege
 
OOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AASOOUG - Oracle Performance Tuning with AAS
OOUG - Oracle Performance Tuning with AAS
 

More from Morgan Tocker

Introducing Spirit - Online Schema Change
Introducing Spirit - Online Schema ChangeIntroducing Spirit - Online Schema Change
Introducing Spirit - Online Schema ChangeMorgan Tocker
 
MySQL Usability Guidelines
MySQL Usability GuidelinesMySQL Usability Guidelines
MySQL Usability GuidelinesMorgan Tocker
 
My First 90 days with Vitess
My First 90 days with VitessMy First 90 days with Vitess
My First 90 days with VitessMorgan Tocker
 
FOSDEM MySQL and Friends Devroom
FOSDEM MySQL and Friends DevroomFOSDEM MySQL and Friends Devroom
FOSDEM MySQL and Friends DevroomMorgan Tocker
 
Introducing TiDB - Percona Live Frankfurt
Introducing TiDB - Percona Live FrankfurtIntroducing TiDB - Percona Live Frankfurt
Introducing TiDB - Percona Live FrankfurtMorgan Tocker
 
TiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - Boston MySQL Meetup GroupTiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - Boston MySQL Meetup GroupMorgan Tocker
 
TiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction - San Francisco MySQL MeetupTiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction - San Francisco MySQL MeetupMorgan Tocker
 
MySQL 8.0 Optimizer Guide
MySQL 8.0 Optimizer GuideMySQL 8.0 Optimizer Guide
MySQL 8.0 Optimizer GuideMorgan Tocker
 
MySQL Cloud Service Deep Dive
MySQL Cloud Service Deep DiveMySQL Cloud Service Deep Dive
MySQL Cloud Service Deep DiveMorgan Tocker
 
Using MySQL in Automated Testing
Using MySQL in Automated TestingUsing MySQL in Automated Testing
Using MySQL in Automated TestingMorgan Tocker
 
Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Morgan Tocker
 
MySQL Performance Metrics that Matter
MySQL Performance Metrics that MatterMySQL Performance Metrics that Matter
MySQL Performance Metrics that MatterMorgan Tocker
 
MySQL 5.6 - Operations and Diagnostics Improvements
MySQL 5.6 - Operations and Diagnostics ImprovementsMySQL 5.6 - Operations and Diagnostics Improvements
MySQL 5.6 - Operations and Diagnostics ImprovementsMorgan Tocker
 
My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2Morgan Tocker
 
Mysql 57-upcoming-changes
Mysql 57-upcoming-changesMysql 57-upcoming-changes
Mysql 57-upcoming-changesMorgan Tocker
 

More from Morgan Tocker (17)

Introducing Spirit - Online Schema Change
Introducing Spirit - Online Schema ChangeIntroducing Spirit - Online Schema Change
Introducing Spirit - Online Schema Change
 
MySQL Usability Guidelines
MySQL Usability GuidelinesMySQL Usability Guidelines
MySQL Usability Guidelines
 
My First 90 days with Vitess
My First 90 days with VitessMy First 90 days with Vitess
My First 90 days with Vitess
 
FOSDEM MySQL and Friends Devroom
FOSDEM MySQL and Friends DevroomFOSDEM MySQL and Friends Devroom
FOSDEM MySQL and Friends Devroom
 
Introducing TiDB - Percona Live Frankfurt
Introducing TiDB - Percona Live FrankfurtIntroducing TiDB - Percona Live Frankfurt
Introducing TiDB - Percona Live Frankfurt
 
TiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - Boston MySQL Meetup GroupTiDB Introduction - Boston MySQL Meetup Group
TiDB Introduction - Boston MySQL Meetup Group
 
TiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction - San Francisco MySQL MeetupTiDB Introduction - San Francisco MySQL Meetup
TiDB Introduction - San Francisco MySQL Meetup
 
TiDB Introduction
TiDB IntroductionTiDB Introduction
TiDB Introduction
 
MySQL 8.0 Optimizer Guide
MySQL 8.0 Optimizer GuideMySQL 8.0 Optimizer Guide
MySQL 8.0 Optimizer Guide
 
MySQL Cloud Service Deep Dive
MySQL Cloud Service Deep DiveMySQL Cloud Service Deep Dive
MySQL Cloud Service Deep Dive
 
MySQL 5.7 + JSON
MySQL 5.7 + JSONMySQL 5.7 + JSON
MySQL 5.7 + JSON
 
Using MySQL in Automated Testing
Using MySQL in Automated TestingUsing MySQL in Automated Testing
Using MySQL in Automated Testing
 
Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7Upcoming changes in MySQL 5.7
Upcoming changes in MySQL 5.7
 
MySQL Performance Metrics that Matter
MySQL Performance Metrics that MatterMySQL Performance Metrics that Matter
MySQL Performance Metrics that Matter
 
MySQL 5.6 - Operations and Diagnostics Improvements
MySQL 5.6 - Operations and Diagnostics ImprovementsMySQL 5.6 - Operations and Diagnostics Improvements
MySQL 5.6 - Operations and Diagnostics Improvements
 
My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2My sql 5.7-upcoming-changes-v2
My sql 5.7-upcoming-changes-v2
 
Mysql 57-upcoming-changes
Mysql 57-upcoming-changesMysql 57-upcoming-changes
Mysql 57-upcoming-changes
 

Locking and Concurrency Control

  • 1. <Insert Picture Here> Locking and Concurrency Control Morgan Tocker, MySQL Community Manager
  • 2. Safe Harbor Statement The  following  is  intended  to  outline  our  general  product  direction.   It  is  intended  for  information  purposes  only,  and  may  not  be   incorporated  into  any  contract.  It  is  not  a  commitment  to  deliver   any  material,  code,  or  functionality,  and  should  not  be  relied  upon   in  making  purchasing  decisions.   
 The  development,  release,  and  timing  of  any  features  or   functionality  described  for  Oracle’s  products  remains  at  the  sole   discretion  of  Oracle.
  • 3. 4 Years of MySQL Innovation MySQL Cluster 7.3 MySQL Migration Wizard MySQL Workbench 6.0 MySQL 5.6 MySQL 5.5 Windows installer & Tools MySQL 5.7 M y S Q L Cluster MySQL MySQL Enterprise Monitor 2.3 & e r M a n a g 3.0 Applier for Hadoop MySQL Enterprise Backup Security MySQL Utilities MySQL Workbench 5.2 & 6.0 Scalability MySQL Cluster 7.2 HA MySQL Enterprise MySQL Cluster 7.1 O r a c l e C e r t i f i c a t i o n s Audit
  • 4. Agenda 1.Table Locks, Row Locks and MVCC 2.Live Demo 3.Deadlocks and Lock Waits Discussion 4.ACID Discussion 5.Summary
  • 5. Table  Locks,  Row  Locks  and  MVCC Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.
  • 6. Customer Accounting System customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 Replacement Acorn $20 $12.50 $17.50
  • 7. Start Backup customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 Replacement Acorn $20 $12.50 $17.50
  • 8. Continues… customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 Replacement Acorn $20 $12.50 $17.50
  • 9. Concurrent Access System - New Customer Added customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc 4 Savvy Squirrel Global invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 10. Backup Continues customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc 4 Savvy Squirrel Global invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 11. Backup Continues… customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc 4 Savvy Squirrel Global invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 12. Backup Complete customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc 4 Savvy Squirrel Global invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 13. What was really backed up customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 14. Next Operation customer customerid 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc 4 X name Acorn Intolerant Squirrels Inc itemid invoice invoiceid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels invoice_item ... X invoiceid Description Amount 1 1 $10 2 2 1 hrs Professional Services Acorn Parts 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 15. Solution • Operation needs to represent a single point in time • When does not matter. • This race condition can exist in all database operations - not just backups.
  • 16. MyISAM Implementation customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc 4 Savvy Squirrel Global invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 17. MyISAM Implementation customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc 4 Savvy Squirrel Global invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 18. MyISAM Implementation customer customerid name 1 Acorn Enterprises 2 First National Squirrel Bank 3 Squirrels 5000 Inc 4 Savvy Squirrel Global invoice_item itemid customerid 1 1 # Acorn Enterprises 2 1 # Acorn Enterprises 3 3 # Squirrels 5000 Inc 4 4 # Savvy Squirrels ... Amount 1 2 1 hrs Professional Services Acorn Parts $10 2 invoiceid Description 1 invoice invoiceid 3 2 $50 4 3 5 hrs Professional Services Delivery Fee 5 3 $20 6 4 Replacement Acorn Premium Acorn $12.50 $17.50 $999
  • 19. MyISAM • • • • Readers may co-exist. Writers are forbidden. Locks a table by default. Programs like mysqldump responsibly lock all tables they need access to before starting.
  • 20. InnoDB Implementation customer Trx ID Rollback Ptr customerid name 123 ## 1 123 ## 2 Acorn Enterprises First National 123 ## 3 401 ## 4 Squirrel Bank Squirrels 5000 Inc Savvy Squirrel invoice_item Rollback Ptr Trx ID Rollback Ptr invoiceid customerid 123 ## 1 123 ## 2 1 # Acorn Enterprises 1 # Acorn 123 ## 3 401 ## 4 Enterprises 3 # Squirrels 5000 Inc 4 # Savvy ... itemid invoiceid Description Amount 123 ## 1 1 2 2 123 ## 3 2 123 ## 4 3 123 ## 5 3 401 ## 6 4 1 hrs Profess Acorn Parts 5 hrs Profess Deliver y Fee Replac ement Premiu m $10 123 ## invoice Trx ID $12.50 $50 $17.50 $20 $999
  • 21. InnoDB • Hidden meta data attached to each row: • Row version • Rollback pointer to older versions if needed. • Every transaction gets allocated a monotonic Transaction ID.
  • 22. InnoDB Implementation Backup process is TRX ID #394. Remembers it is not supposed to see anything newer than itself. New changes are permitted to continue. customer Trx ID Rollback Ptr customerid name 123 ## 1 123 ## 2 Acorn Enterprises First National 123 ## 3 401 ## 4 Squirrel Bank Squirrels 5000 Inc Savvy Squirrel invoice_item Rollback Ptr Trx ID Rollback Ptr invoiceid customerid 123 ## 1 123 ## 2 1 # Acorn Enterprises 1 # Acorn 123 ## 3 401 ## 4 Enterprises 3 # Squirrels 5000 Inc 4 # Savvy ... itemid invoiceid Description Amount 123 ## 1 1 2 2 123 ## 3 2 123 ## 4 3 123 ## 5 3 401 ## 6 4 1 hrs Profess Acorn Parts 5 hrs Profess Deliver y Fee Replac ement Premiu m $10 123 ## invoice Trx ID $12.50 $50 $17.50 $20 $999
  • 23. MVCC Algorithm • Stands for “Multi-version concurrency control” - http:// en.wikipedia.org/wiki/ Multiversion_concurrency_control • Readers don’t block writers! • And actually writers don’t block readers, because they can read older versions.
  • 24. MVCC (cont.) • Multiple versions of the same row may exist. • What you will see depends on the context of your transaction. Even the number of rows in a table depends on your context.
  • 25. Live  Demo Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.
  • 26. Deadlocks  &  Lock  Waits Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.
  • 27. What is a deadlock? • A situation when two sessions are trying to acquire each-other’s respective locks. • Neither can proceed until the other backs down. • It’s no different to when two colleagues get in an irrational argument and neither will back down :)
  • 28. What is a lock wait? • One step short of a deadlock. • You keep trying to do something, but it’s not in the budget this quarter? • Not quite sure of the analogy here, but you are free to retry. • But.. a fair amount of the time it still won’t work
  • 29. Deadlock Detection • InnoDB as a storage engine will detect deadlocks. • Algorithm is Graph detection http://en.wikipedia.org/ wiki/Cycle_detection - happens virtually instantly. • Kills the transaction that modified the least rows. • All changes are rolled back - application developer must now handle/retry.
  • 30. Preventing Deadlocks • Deadlock is a concurrency issue. • Same solutions as when you have colleagues arguing!
  • 31. Solution #1 • Don’t make them work-together. • Reduce concurrency / run known conflicting workers in serial execution. X
  • 32. Solution #2 • Don’t make them work on big projects together. • aka lower the stakes! • Locks are held for the duration of a transaction: • Put people who don’t work together only on small projects as needed (smaller transactions). • Reduce locking required for transactions (more efficient queries, better indexing).
  • 33. Preventing Deadlocks (cont.) • Also, we should accept that some deadlocks are a fact of life. Continuing co-worker analogy: • Essential for business growth that people work together. • Have to be prepared to handle disputes. ! • MyISAM as a storage engine does not actually need deadlock detection (table locking). • Where does this leave concurrency?
  • 34. Preventing Deadlocks (cont.) • You actually want databases to be optimistic. • That is set as few locks as possible, knowing that sometimes oopses will happen. • Alternative is more locks and less concurrency.
  • 35. Handing Deadlocks • Database only owns responsibility of alerting application • It may not reapply transaction, since state has changed. • Application owns responsibility of handling errors. • Yet maybe 5% of applications do this :(
  • 36. Lock Wait Timeout • Will proceed immediately if it can acquire the lock. • Since most transactions are less than 1 second, at 50 seconds seems unlikely retrying will work. • May need to voluntarily rollback and try again or give a user error.
  • 37. “Requires a replay of the transaction” • • • • The problems probably won’t happen often. May be easier to kick back to the user: “try again - no changes have been applied”. Exception may be asynchronous workers? • Usually you’ve made the promise of work without opportunity to back-out.
  • 38. ACID Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.
  • 39. The “ACID” Contract • Atomicity - All or none. • Consistency - Generic term to say all constraints etc must be honoured (primary key, unique key). • Isolation - Changes in one transaction to not interfere with another transaction. • Durability - When you said that the atomic transaction was committed, you will honour it. Even if the server were to crash.
  • 40. Atomicity • Most people know this one: • Transfer $20 from Account A --> Account B. • Multiple failure states... • The money may be subtracted from A, but has not reached B yet. • Wrap your code between START TRANSACTION and COMMIT and you get this solved for free.
  • 41. Consistency • Not too much more to add on this one. • InnoDB has foreign key constraints • MySQL does not feature CHECK constraints where another database may allow more domain specific data validation here as well.
  • 42. Isolation • Actually a very expensive feature for a database to implement (may require lots of locking, stale data kept around etc.) • Many databases make this somewhat tunable. • There are four levels of isolation describing what potential unsafe conditions are possible.
  • 43. Isolation Levels • READ-UNCOMMITTED - No isolation • READ-COMMITTED - Can see other session’s committed rows. • REPEATABLE-READ - Won’t see other session’s committed rows once your session starts (default). • SERIALIZABLE - Reads actually lock. Can’t modify any data another session has changed.
  • 44. Durable • This one’s actually tunable as well. • InnoDB will not lie by default. • It means on COMMIT everything should survive a power loss not be stuck in a buffer in memory.
  • 45. Summary Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.
  • 46. Application Responsibilities • Wrap every logical unit of work in START TRANSACTION and COMMIT. • Expect failure because failure is a database allowing concurrency. • LOCK WAIT TIMEOUT = Retry “may” be possible. • DEADLOCK = Everything must be retried.
  • 47.
  • 49. Mutexes, Latches & Semaphores • Common names given to database “internal locking”. • Today we are not going to differentiate between them. • Not unique to databases, required to provide “thread safety” in multi-threaded programs.
  • 50. Example #1 • InnoDB buffer pool is full • Free space needs to be created in order to be able to load your desired page into memory. • In between freeing and loading, someone else fills desired slot.
  • 51. Example #2 • When a page becomes too full, it may need to split data into other pages. • It is possible that there could be a thread deadlock if two pages left and right of each-other reach this condition at once. • Can’t decide who should proceed first.
  • 52. Internal Locking (cont.) • When mutexes have high contention other tasks are not runable. • Which means that CPUs can appear idle, but nothing is happening.
  • 53. Fixes in newer versions • Responsibility to fix is on MySQL Engineering Team. • High Priority for Us. • Some DBAs will have peripheral knowledge - know situations when it’s time to upgrade.
  • 54. Time to Upgrade? • Kernel mutex split (5.6) • Lock open mutex split (5.6) • Buffer pool mutex contention reduced w/8 buffer pool instances (5.6) • Multiple rollback segments (5.5) • Log-sys mutex split (5.5) • Flush-list mutex split (5.5) More mentioned at: http://www.tocker.ca/2013/11/27/what-is-a-mutex-anyway.html