InnoDB Locking Explained with Stick Figures

81
I NNO DB L OCKING E XPLAINED W ITH S TICK F IGURES Bill Karwin

Transcript of InnoDB Locking Explained with Stick Figures

Page 1: InnoDB Locking Explained with Stick Figures

INNODB LOCKINGEXPLAINED

WITH STICK FIGURESBill Karwin

Page 2: InnoDB Locking Explained with Stick Figures

Bill Karwin• Senior Database Architect at School Messenger

(West Corporation)

• Author of SQL Antipatterns: Avoiding the Pitfalls of Database Programming

• 20+ years experience with SQL databases, software development, MySQL consulting and training

Page 3: InnoDB Locking Explained with Stick Figures

why locking?• When multiple clients

access the same data, they have to avoid clobbering each others’ work.

• Databases must restrict access to one client at a time for a given table or row.

https://commons.wikimedia.org/wiki/File:New_York_City_Gridlock.jpg  

Page 4: InnoDB Locking Explained with Stick Figures

why locking?• The DBMS creates locks

against tables and rows, and gives them to clients, first-come, first-serve.

• When a client requests a exclusive lock, but a different client currently holds it, the requestor waits until the holder releases its lock.

• Most locks last until the end of the transaction.

https://commons.wikimedia.org/wiki/Traffic_lights#/media/File:LED_traffic_light.jpg

Page 5: InnoDB Locking Explained with Stick Figures

the analogy

Page 6: InnoDB Locking Explained with Stick Figures

a museum• Many people can visit the museum (a database

table) to view art (rows of data).

Page 7: InnoDB Locking Explained with Stick Figures

reads and writes

Page 8: InnoDB Locking Explained with Stick Figures

reads• Many visitors can view paintings at the same time—

no locking required.

https://commons.wikimedia.org/wiki/File:L%27%C3%A9glise_d%27Auvers-­‐‑sur-­‐‑Oise.jpg

Page 9: InnoDB Locking Explained with Stick Figures

writes• A curator can change the paintings—while casual

visitors are viewing them.

https://commons.wikimedia.org/wiki/File:Vincent_Willem_van_Gogh_128.jpg

Page 10: InnoDB Locking Explained with Stick Figures

writes• A curator can change the paintings—while casual

visitors are viewing them.

Page 11: InnoDB Locking Explained with Stick Figures

repeatable reads• The viewers still see the prior painting in spite of the

change, because they used their tablets to capture the image.*

*  Please  do  not  take  photographs  of  the  art  in  a  real  museum.

Page 12: InnoDB Locking Explained with Stick Figures

read committed• If the viewers are okay allowing their view to

change, they can simply say so.READ  

COMMITTED

Page 13: InnoDB Locking Explained with Stick Figures

read committed• If the viewers are okay allowing their view to

change, they can simply say so.READ  

COMMITTED

I  don’t  mind

Page 14: InnoDB Locking Explained with Stick Figures

exclusive locks

Page 15: InnoDB Locking Explained with Stick Figures

exclusive locks• The curator can change the painting if they are the

exclusive person working on it.

Page 16: InnoDB Locking Explained with Stick Figures

exclusive locks• Casual viewers don’t block the curator.

X-­‐‑lock!

Page 17: InnoDB Locking Explained with Stick Figures

exclusive locks• A second curator who tries to change the same

painting must wait for the first curator to finish.

X-­‐‑lock!…

https://commons.wikimedia.org/wiki/Vincent_van_Gogh#/media/File:Vincent_Willem_van_Gogh_107.jpg

Page 18: InnoDB Locking Explained with Stick Figures

what is an exclusive lock?• A lock that does not share.• It must be the only lock on the resource.• Request for an exclusive lock waits for the release of

any other shared or exclusive lock on that resource.

Page 19: InnoDB Locking Explained with Stick Figures

shared locks

Page 20: InnoDB Locking Explained with Stick Figures

shared locks• The curator cannot make changes while the art

critic is viewing a painting.

S-­‐‑lock! .  .  .

Page 21: InnoDB Locking Explained with Stick Figures

shared locks• Art critics can share—they do not block each other.

S-­‐‑lock! .  .  .

S-­‐‑lock!

Page 22: InnoDB Locking Explained with Stick Figures

shared locks• Once the art critics leave, the curator can proceed

to change the art.

X-­‐‑lock!

Page 23: InnoDB Locking Explained with Stick Figures

shared locks• A new art critic will not begin his viewing while the

curator is still working on changing the painting.

X-­‐‑lock!…

Page 24: InnoDB Locking Explained with Stick Figures

what is a shared lock?• A lock that allows other shared locks on the same

table or rows.• Shared locks blocks exclusive locks.• Exclusive locks block shared locks.

Page 25: InnoDB Locking Explained with Stick Figures

table intention locks

Page 26: InnoDB Locking Explained with Stick Figures

table locks• A construction worker needs to remodel the

museum, but not while visitors are inside.

Page 27: InnoDB Locking Explained with Stick Figures

table locks• Each person is given a special badge as they enter

the museum, showing their intention to view or change the paintings.

IS!IX!

Page 28: InnoDB Locking Explained with Stick Figures

table locks• A construction worker requests exclusive access to

the museum, but can not get it.

.  .  .IS!IX!

Page 29: InnoDB Locking Explained with Stick Figures

table locks• When all the visitors have left, the worker can finally

get his exclusive access to do his work.LOCK  TABLE!

Page 30: InnoDB Locking Explained with Stick Figures

table locks• While the construction is going on, visitors cannot

get their badges, and thereforecannot enter the museum.

https://commons.wikimedia.org/wiki/Category:Under_construction_icons#/media/File:Enobras.PNG

LOCK  TABLE!

Page 31: InnoDB Locking Explained with Stick Figures

what is an intention lock?• Before SELECT…LOCK IN SHARE MODE or other

shared lock, request an intention shared lock (“IS”) on the table.

• Before SELECT…FOR UPDATE, INSERT, UPDATE, DELETE, request an intention exclusive lock (“IX”).

• IS and IX locks allow access by multiple clients. They won’t necessarily conflict until they try to get real locks on the same rows.

• But a table lock (ALTER TABLE, DROP TABLE, LOCK TABLES) blocks both IS and IX, and vice-versa.

Page 32: InnoDB Locking Explained with Stick Figures

gap locks

Page 33: InnoDB Locking Explained with Stick Figures

gap locks• The art critic needs to view the whole collection

without changes and no new inserts.

gap

.  .  .S-­‐‑lock!

Page 34: InnoDB Locking Explained with Stick Figures

what is a gap lock?• A lock on a painting locks the “space” before the

painting.• The gap lock prevents inserts of new paintings

before the locked painting (within the space).• This happens automatically, to prevent “phantom

reads”—i.e. the view of data changes during a transaction.

Page 35: InnoDB Locking Explained with Stick Figures

what is a gap lock?• Exception: if the art critic is okay with seeing the

latest additions among the paintings, he can choose to use the READ COMMITTED transaction isolation level.

• Another exception: no gap lock is needed for a UNIQUE or PRIMARY KEY index.

Page 36: InnoDB Locking Explained with Stick Figures

gap locks• Optionally be more permissive about inserts.

gap

S-­‐‑lock!

READ  COMMITTED

Page 37: InnoDB Locking Explained with Stick Figures

gap locks• Optionally be more permissive about inserts.

X-­‐‑lock!S-­‐‑lock!

READ  COMMITTED

I  don’t  mind

Page 38: InnoDB Locking Explained with Stick Figures

traveling exhibit• Some art exhibits move to another museum.

Page 39: InnoDB Locking Explained with Stick Figures

installation plans• The curators must keep records of how to install the

art, so the show is the same at each museum.

Label  all  paintings  on  this  wall  with  “Van  Gogh”

Page 40: InnoDB Locking Explained with Stick Figures

installation plans• The curators must keep records of how to install the

art, so the show is the same at each museum.

Van  Gogh Van  Gogh

Page 41: InnoDB Locking Explained with Stick Figures

installation plans• Another painting is added.

Van  Gogh Van  Gogh

Install  this  in  the  gap  and  

label  it  “Lautrec”

https://commons.wikimedia.org/wiki/Moulin_Rouge#/media/File:Lautrec_at_the_moulin_rouge_two_women_waltzing_1892.jpg

Page 42: InnoDB Locking Explained with Stick Figures

installation plans• Another painting is installed in the gap.

Van  Gogh Van  GoghLautrec

Page 43: InnoDB Locking Explained with Stick Figures

installation plans• The latter curator commits his installation plans first.

Van  Gogh Van  GoghLautrec

COMMIT

Page 44: InnoDB Locking Explained with Stick Figures

installation plans• Then the first curator commits his installation plans.

Van  Gogh Van  GoghLautrec

COMMIT

Page 45: InnoDB Locking Explained with Stick Figures

traveling exhibit• The exhibit is installed in the second museum.

Page 46: InnoDB Locking Explained with Stick Figures

order matters• The installation instructions are carried out in the

wrong order!

Install  this  in  the  gap  and  

label  it  “Lautrec”

Page 47: InnoDB Locking Explained with Stick Figures

order matters• The installation instructions are carried out in the

wrong order!

LautrecLabel  all  

paintings  on  this  wall  with  “Van  Gogh”

Page 48: InnoDB Locking Explained with Stick Figures

order matters• The installation instructions are carried out in the

wrong order!

Van  GoghVan  Gogh Van  Gogh

Page 49: InnoDB Locking Explained with Stick Figures

what happened?• Installation instructions are recorded in the order

they were committed, not the order they were originally done.

• Therefore the installation at the next museum may repeat the steps in an incorrect order.

Page 50: InnoDB Locking Explained with Stick Figures

how to solve this?• Labeling “Van Gogh” should have first locked all

the spaces on that wall.

Van  Gogh Van  Gogh

gap

Page 51: InnoDB Locking Explained with Stick Figures

how to solve this?• The addition would be forced to wait for the locks

to be released.

Van  Gogh Van  Gogh

gap

Page 52: InnoDB Locking Explained with Stick Figures

insert intention locks

Page 53: InnoDB Locking Explained with Stick Figures

insert intention locks• One curator wants to update paintings where

year > 1886

X-­‐‑lock!

gap

Page 54: InnoDB Locking Explained with Stick Figures

insert intention locks• The second curator wants to insert an 1887 painting,

but it would fall within the existing gap lock.

X-­‐‑lock! …

gap

Page 55: InnoDB Locking Explained with Stick Figures

insert intention locks---TRANSACTION 32070411, ACTIVE 6 sec insertingmysql tables in use 1, locked 1LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 1MySQL thread id 26, OS thread handle 0x7f2ba845f700, query id 1423 192.168.50.1 root updateinsert into Museum (year) values (1887)------- TRX HAS BEEN WAITING 6 SEC FOR THIS LOCK TO BE GRANTED:RECORD LOCKS space id 3337 page no 4 n bits 72 index `year` of table `test`.`Museum` trx id 32070411 lock_mode X insert intention waiting

Page 56: InnoDB Locking Explained with Stick Figures

insert intention locks• But if the first curator doesn’t care about new

paintings entering his view…

X-­‐‑lock!

gap

READ  COMMITTED

Page 57: InnoDB Locking Explained with Stick Figures

insert intention locks• Then the second curator gets an insert intention

lock, and is then free to insert.

insert-­‐‑lock!

gap

X-­‐‑lock!READ  

COMMITTED

I  don’t  mind

Page 58: InnoDB Locking Explained with Stick Figures

insert intention locks• Then the second curator gets an insert intention

lock, and is then free to insert.

X-­‐‑lock!X-­‐‑lock!READ  

COMMITTED

I  don’t  mind

Page 59: InnoDB Locking Explained with Stick Figures

what is an insert intention lock?• A special kind of gap lock, requested before a

client tries to insert a row.• Insert locks are shared, not exclusive—multiple

clients can acquire insert locks on the same gap.• But insert locks conflict with other exclusive locks.

Page 60: InnoDB Locking Explained with Stick Figures

why is insert intention lock shared?• Multiple clients prepare to insert into the same gap.• They may be inserting different rows within the same

gap, so they don’t conflict with each other.• But the insert intention lock blocks other clients from

requesting exclusive locks on the same gap.

Page 61: InnoDB Locking Explained with Stick Figures

auto-inc locks

Page 62: InnoDB Locking Explained with Stick Figures

auto-inc locks• Two curators are installing paintings. They both need

to post a unique number for self-guided tours.

? ?

Page 63: InnoDB Locking Explained with Stick Figures

auto-inc locks• There must be one number

generator per table.• One curator at a time can

request the next value.

http://www.istockphoto.com/photo/ticket-­‐‑dispenser-­‐‑isolated-­‐‑9396862

Page 64: InnoDB Locking Explained with Stick Figures

auto-inc locks• The first curator gets a number.

auto-­‐‑inc! 1 …

Page 65: InnoDB Locking Explained with Stick Figures

auto-inc locks• As soon as the first curator gets his number, the

second curator can proceed.

1 2 auto-­‐‑inc!

Page 66: InnoDB Locking Explained with Stick Figures

auto-inc locks• They may both keep locks on the paintings, but

they’re done allocating numbers.

X-­‐‑lock! 1 2 X-­‐‑lock!

Page 67: InnoDB Locking Explained with Stick Figures

what is an auto-inc lock?• A table lock, used when a client requests the next

unique id for a given table.• Ensures that each id is given to one client.• Brief—it is released as soon as the id is generated,

instead of lasting to the end of the transaction like other locks.

• Because the lock is so brief, neither client can “undo”— i.e. they cannot return their id to the stack for someone else to use.

Page 68: InnoDB Locking Explained with Stick Figures

deadlocks

Page 69: InnoDB Locking Explained with Stick Figures

deadlocks• Two curators are updating the art, but they start

from different ends of the collection.

X-­‐‑lock!X-­‐‑lock!

Page 70: InnoDB Locking Explained with Stick Figures

deadlocks• A curator requests a lock on the second painting,

which is already locked. He waits.

X-­‐‑lock!X-­‐‑lock! …

Page 71: InnoDB Locking Explained with Stick Figures

deadlocks• The other curator requests a lock on the first

painting, which is already locked. He also waits.

X-­‐‑lock!X-­‐‑lock! ……

Page 72: InnoDB Locking Explained with Stick Figures

deadlocks• Neither will give up the lock they have, so they are

doomed to wait until one or both of them dies.

X-­‐‑lock!X-­‐‑lock! … …

Page 73: InnoDB Locking Explained with Stick Figures

what is a deadlock?• When two or more concurrent clients wait for each

other to release their locks, but since they are both waiting, they will never give up the lock they have.

• In other words, a “catch-22” of lock-waits.

• Many people use the term “deadlock” incorrectly—when they are describing a simple one-way lock wait.

Page 74: InnoDB Locking Explained with Stick Figures

resolving deadlocks• MySQL detects cycles in lock waits, and kills one of

the transactions immediately.

X-­‐‑lock!X-­‐‑lock! ……

Page 75: InnoDB Locking Explained with Stick Figures

resolving deadlocks• MySQL detects cycles in lock waits, and

automatically kills one of the transactions.

X-­‐‑lock! X-­‐‑lock!

Page 76: InnoDB Locking Explained with Stick Figures

avoiding deadlocks• All clients request locks in the same order.

X-­‐‑lock!

…X-­‐‑lock!

Page 77: InnoDB Locking Explained with Stick Figures

avoiding deadlocks• Each client locks everything they need in one

atomic request.

X-­‐‑lock!

Page 78: InnoDB Locking Explained with Stick Figures

conclusion

Page 79: InnoDB Locking Explained with Stick Figures

read committed all the things?

• Yes … and no.• READ COMMITTED avoids gap locks, therefore

reduces lock waits and allows greater throughput.• But using READ COMMITTED allows more cases

where you could get deadlocks.

Page 80: InnoDB Locking Explained with Stick Figures

thank you• I hope your trip to the museum was educational!

Page 81: InnoDB Locking Explained with Stick Figures

license and copyrightCopyright 2015-2016 Bill Karwin

http://www.slideshare.net/billkarwinReleased under a Creative Commons 3.0 License:

http://creativecommons.org/licenses/by-nc-nd/3.0/You are free to share—to copy, distribute, and

transmit this work, under the following conditions:

Attribution.You  must  attribute  this  work  to  Bill  Karwin.

Noncommercial.You  may  not  use  this  work  for  commercial  

purposes.

No  Derivative  Works.You may  not  alter,  

transform,  or  build  upon  this  work.