Transaction Isolation (ํŠธ๋žœ์žญ์…˜์˜ ๊ฒฉ๋ฆฌ์„ฑ)

2021. 1. 10. 03:04ใ†๐Ÿ™‹๐Ÿป‍โ™€๏ธ Study/โœ”๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ( Database )

๋ฐ˜์‘ํ˜•

 

์ €๋ฒˆ ๊ธ€์—์„œ ํŠธ๋žœ์žญ์…˜์˜ ACID ํŠน์„ฑ์— ๋Œ€ํ•ด ์ž ๊น ์–ธ๊ธ‰ํ–ˆ๋˜ ์ ์ด ์žˆ๋Š”๋ฐ์š”.

์˜ค๋Š˜์€ ํŠธ๋žœ์žญ์…˜์˜ ACID ์ค‘ I(Isolation)์ธ ๊ฒฉ๋ฆฌ์„ฑ์˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์— ๋Œ€ํ•œ ๊ธ€์„ ํฌ์ŠคํŒ…ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

Docker์™€ MySQL์„ ์ด์šฉํ•œ ํŠธ๋žœ์žญ์…˜ rollback / commit ์‹ค์Šต์€ ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š” : )

 

 

[Docker/MySQL] ํŠธ๋žœ์žญ์…˜(Transaction)

ํŠธ๋žœ ์žญ์…˜์ด๋ž€(Transaction) ?! ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ƒํƒœ๋ฅผ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ํ•˜๋‚˜์˜ ๋…ผ๋ฆฌ์  ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ž‘์—… / ํ•œ๊บผ๋ฒˆ์— ์ˆ˜ํ–‰๋˜์–ด์•ผํ•  ์ผ๋ จ์˜ ์ž‘์—… ์—ฐ์‚ฐ์„ ์˜๋ฏธํ•œ๋‹ค. ์—ฌ๋Ÿฌ ์ค„์˜ SQL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ• 

yunaaaas.tistory.com

 

 

Isolation Level (๊ฒฉ๋ฆฌ ์ˆ˜์ค€) ์ด๋ž€?!

๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ / ํŠธ๋žœ์žญ์…˜์ด ๋™์‹œ์— ๋™์ผํ•œ ํ…Œ์ด๋ธ” / ๋ ˆ์ฝ”๋“œ์— ์ ‘๊ทผํ•  ๋•Œ ์ ์šฉํ•˜๋Š” ๊ทœ์น™

Lock ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ๋‹ค๋ฅด๋‹ค!

 

 

๐Ÿ“Œ Read Uncommitted

commit ์—ฌ๋ถ€์™€ ๊ด€๊ณ„์—†์ด ํ˜„์žฌ ๋ ˆ์ฝ”๋“œ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ

๊ฐ€์žฅ ๋‚ฎ์€ ๋ฐ์ดํ„ฐ์˜ ์•ˆ์ •์„ฑ But ๊ฐ€์žฅ ๋†’์€ ์„ฑ๋Šฅ

Dirty Read ํ˜„์ƒ ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•˜๋‹ค !

rollback ์ˆ˜ํ–‰ ์‹œ ์ฝ์–ด๊ฐ”๋˜ ๊ฐ’์€ ์“ฐ๋ ˆ๊ธฐ ๊ฐ’์ด ๋˜์–ด์ง€๋Š” ํ˜„์ƒ

 

 

๐Ÿ“ŒRead Committed

์˜ค๋ผํด์˜ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ Commit ๋œ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ

์ผ๋ฐ˜์ ์œผ๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜์–ด์ง€๋Š” ๊ฒฉ๋ฆฌ ์ˆ˜์ค€

Non - Repeatable Read ํ˜„์ƒ ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•˜๋‹ค !

๋™์ผํ•œ ํŠธ๋žœ์žญ์…˜ ์•ˆ์—์„œ ๋™์ผํ•œ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€๋Š” ํ˜„์ƒ

 

๐Ÿ“ŒReapeatable Read 

MySQL์˜ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ๋™์ผํ•œ ํŠธ๋žœ์žญ์…˜์—์„œ๋Š” ํ•œ ์ฟผ๋ฆฌ์˜ ๊ฐ’์ด ํ•ญ์ƒ ์ผ์ •ํ•œ ๊ฒƒ

๋ฐฑ์—…์ด๋‚˜ ๋ณต์ œ์™€ ๊ฐ™์ด ์ƒ๋Œ€์ ์œผ๋กœ ๊ธด ์‹œ๊ฐ„๋™์•ˆ ๋™์ž‘ํ•˜๋Š” ํŠธ๋žœ์žญ์…˜์—์„œ ์•ˆ์ •์ ์ธ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ๋ณด์žฅ๋˜๊ฑฐ๋‚˜ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉ

Phantom Read ํ˜„์ƒ ๋ฐœ์ƒ ๊ฐ€๋Šฅํ•˜๋‹ค !

์ค‘๊ฐ„์— ์ƒˆ๋กœ์šด ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์—†๋˜ ๊ฐ’์ด ๋‚˜ํƒ€๋‚˜๋Š” ํ˜„์ƒ

Q . ์™œ Phantom Read ํ˜„์ƒ์ด ๊ฐ€๋Šฅํ• ๊นŒ!? 

A . ๋ฐฑ์—… / ๋ณต์ œ ์‹œ์— ๋ฐ์ดํ„ฐ์˜ ๋ณ€๊ฒฝ์„ ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ถ”๊ฐ€ / ์‚ญ์ œ๋Š” ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•œ๋‹ค.

 

๐Ÿ“ŒSerializable Read

๋‘ ๊ฐœ์˜ ํŠธ๋žœ์žญ์…˜์ด ๋™์‹œ์— ์ˆ˜ํ–‰๋˜์ง€ ์•Š๋Š” ๊ฒƒ

์ด์ „ ํŠธ๋žœ์žญ์…˜์ด ์ปค๋ฐ‹์ด ์™„๋ฃŒ ๋˜๋ฉด ๋‹ค์Œ ํŠธ๋žœ์žญ์…˜์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

๊ฐ€์žฅ ๋†’์€ ๋ฐ์ดํ„ฐ ์•ˆ์ •์„ฑ But ๊ฐ€์žฅ ๋‚ฎ์€ ์„ฑ๋Šฅ -> ์‹ค์ œ๋กœ๋Š” ๋งŽ์ด ์•ˆ ์“ฐ์ธ๋‹ค.

 

 

ํŠธ๋žœ ์žญ์…˜์˜ ๊ฒฉ๋ฆฌ์„ฑ ์ˆ˜์ค€ ์ด ์ •๋ฆฌ

Isolation Level Dirty Read Non - Repeatable Read Phantom Read
Read Uncommitted ๊ฐ€๋Šฅ ๊ฐ€๋Šฅ ๊ฐ€๋Šฅ
Read Committed ๋ถˆ๊ฐ€๋Šฅ ๊ฐ€๋Šฅ ๊ฐ€๋Šฅ
Repeatable Read ๋ถˆ๊ฐ€๋Šฅ ๋ถˆ๊ฐ€๋Šฅ ๊ฐ€๋Šฅ
Serializable Read ๋ถˆ๊ฐ€๋Šฅ ๋ถˆ๊ฐ€๋Šฅ ๋ถˆ๊ฐ€๋Šฅ

 

MySQL Isolation ๊ด€๋ จ ์‹ค์Šต

์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ˜„์žฌ Isolation ๊ฐ’ ํ™•์ธ๊ณผ ๋ณ€๊ฒฝ์— ๊ด€ํ•œ ์‹ค์Šต์ด๋‹ˆ ์ฝ”๋“œ๋งŒ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ค์ œ ์‹ค์Šต์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค : )

show variables like '%isolation%'; // ํ˜„์žฌ isolation ๊ฐ’ ํ™•์ธํ•˜๊ธฐ
set tx_isolation = 'READ-COMMITTED' ; // read - committed ๋กœ isolation ๋ณ€๊ฒฝํ•˜๊ธฐ
show variables like '%isolation%'; // ๋ณ€๊ฒฝ ๋œ isolation ๊ฐ’ ํ™•์ธ ํ•˜๊ธฐ

MySQL์˜ ๊ธฐ๋ณธ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์€ Repeatable Read ์ด๋‹ค!

 

why?! ๋ฐฑ์—… / ๋ณต์ œ์˜ ์•ˆ์ •์„ฑ ๋•Œ๋ฌธ !

๐Ÿ‘‰๐Ÿป MySQL์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์ธ ๋ณต์ œ๊ฐ€ ์•ˆ์ •์ ์œผ๋กœ ๋™์ž‘ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์„ฑ๋Šฅ์„ ํฌ์ƒํ•˜์—ฌ default ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์„ repeatable read๋กœ ์„ค์ •๋˜์–ด์ง„๋‹ค. ์ด๋ก ์ ์œผ๋กœ๋Š” Read Committed (Oracle์˜ ๊ธฐ๋ณธ ๊ฒฉ๋ฆฌ์ˆ˜์ค€)์ด ๋น ๋ฅด์ง€๋งŒ ๋‘ ๊ฐœ์˜ ์„ฑ๋Šฅ ์ฐจ์ด๋Š” ๋ฏธ์„ธํ•˜๋‹ค.

 

ํŠธ๋žœ์žญ์…˜์˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€ ๋น„๊ต

  • ํ˜„์žฌ์˜ ๋ฐ์ดํ„ฐ ๊ฐ’ VS Undo Log ์ƒ์˜ ๋งˆ์ง€๋ง‰ ์ปค๋ฐ‹๋œ ๊ฐ’ (Read Uncommitted VS Read Committed)
  • Undo Log ์ƒ์˜ ๋งˆ์ง€๋ง‰ ์ปค๋ฐ‹๋œ ๊ฐ’ VS Undo Log ์ƒ์˜ ์ปค๋ฐ‹๋œ ๊ฐ’๋“ค ์ค‘์—์„œ ์ฒ˜์Œ์œผ๋กœ ์ฝ์–ด๊ฐ„ ์‹œ๊ฐ„ ๊ณ ๋ ค (Read Committed VS Repeatable Read)

 

 

๋ฐ˜์‘ํ˜•