Skip to content

Latest commit

ย 

History

History
119 lines (64 loc) ยท 4.05 KB

Transaction Isolation Level.md

File metadata and controls

119 lines (64 loc) ยท 4.05 KB

ํŠธ๋žœ์žญ์…˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€(Transaction Isolation Level)


Isolation level


ํŠธ๋žœ์žญ์…˜์—์„œ ์ผ๊ด€์„ฑ ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ—ˆ์šฉํ•˜๋„๋ก ํ•˜๋Š” ์ˆ˜์ค€


Isolation level์˜ ํ•„์š”์„ฑ


๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ACID ํŠน์ง•๊ณผ ๊ฐ™์ด ํŠธ๋žœ์žญ์…˜์ด ๋…๋ฆฝ์ ์ธ ์ˆ˜ํ–‰์„ ํ•˜๋„๋ก ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ Locking์„ ํ†ตํ•ด, ํŠธ๋žœ์žญ์…˜์ด DB๋ฅผ ๋‹ค๋ฃจ๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์ด ๊ด€์—ฌํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๋ง‰๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ๋ฌด์กฐ๊ฑด Locking์œผ๋กœ ๋™์‹œ์— ์ˆ˜ํ–‰๋˜๋Š” ์ˆ˜๋งŽ์€ ํŠธ๋žœ์žญ์…˜๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์„ฑ๋Šฅ์€ ๋–จ์–ด์ง€๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.

๊ทธ๋ ‡๋‹ค๊ณ  ํ•ด์„œ, ์„ฑ๋Šฅ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด Locking์˜ ๋ฒ”์œ„๋ฅผ ์ค„์ธ๋‹ค๋ฉด, ์ž˜๋ชป๋œ ๊ฐ’์ด ์ฒ˜๋ฆฌ๋  ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

  • ๋”ฐ๋ผ์„œ ์ตœ๋Œ€ํ•œ ํšจ์œจ์ ์ธ Locking ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•จ!

Isolation level ์ข…๋ฅ˜


  1. Read Uncommitted (๋ ˆ๋ฒจ 0)

    SELECT ๋ฌธ์žฅ์ด ์ˆ˜ํ–‰๋˜๋Š” ๋™์•ˆ ํ•ด๋‹น ๋ฐ์ดํ„ฐ์— Shared Lock์ด ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š” ๊ณ„์ธต

    ํŠธ๋žœ์žญ์…˜์— ์ฒ˜๋ฆฌ์ค‘์ด๊ฑฐ๋‚˜, ์•„์ง Commit๋˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์ด ์ฝ๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•จ

    ์‚ฌ์šฉ์ž1์ด A๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ B๋ผ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋™์•ˆ ์‚ฌ์šฉ์ž2๋Š” ์•„์ง ์™„๋ฃŒ๋˜์ง€ ์•Š์€(Uncommitted) ํŠธ๋žœ์žญ์…˜์ด์ง€๋งŒ ๋ฐ์ดํ„ฐB๋ฅผ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค
    

    ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•จ


  2. Read Committed (๋ ˆ๋ฒจ 1)

    SELECT ๋ฌธ์žฅ์ด ์ˆ˜ํ–‰๋˜๋Š” ๋™์•ˆ ํ•ด๋‹น ๋ฐ์ดํ„ฐ์— Shared Lock์ด ๊ฑธ๋ฆฌ๋Š” ๊ณ„์ธต

    ํŠธ๋žœ์žญ์…˜์ด ์ˆ˜ํ–‰๋˜๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์ด ์ ‘๊ทผํ•  ์ˆ˜ ์—†์–ด ๋Œ€๊ธฐํ•˜๊ฒŒ ๋จ

    Commit์ด ์ด๋ฃจ์–ด์ง„ ํŠธ๋žœ์žญ์…˜๋งŒ ์กฐํšŒ ๊ฐ€๋Šฅ

    ๋Œ€๋ถ€๋ถ„์˜ SQL ์„œ๋ฒ„๊ฐ€ Default๋กœ ์‚ฌ์šฉํ•˜๋Š” Isolation Level์ž„

    ์‚ฌ์šฉ์ž1์ด A๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ B๋ผ๋Š” ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋™์•ˆ ์‚ฌ์šฉ์ž2๋Š” ํ•ด๋‹น ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•จ
    

  3. Repeatable Read (๋ ˆ๋ฒจ 2)

    ํŠธ๋žœ์žญ์…˜์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ SELECT ๋ฌธ์žฅ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ์— Shared Lock์ด ๊ฑธ๋ฆฌ๋Š” ๊ณ„์ธต

    ํŠธ๋žœ์žญ์…˜์ด ๋ฒ”์œ„ ๋‚ด์—์„œ ์กฐํšŒํ•œ ๋ฐ์ดํ„ฐ ๋‚ด์šฉ์ด ํ•ญ์ƒ ๋™์ผํ•จ์„ ๋ณด์žฅํ•จ

    ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋Š” ํŠธ๋žœ์žญ์…˜ ์˜์—ญ์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ˆ˜์ • ๋ถˆ๊ฐ€๋Šฅ

    MySQL์—์„œ Default๋กœ ์‚ฌ์šฉํ•˜๋Š” Isolation Level


  4. Serializable (๋ ˆ๋ฒจ 3)

    ํŠธ๋žœ์žญ์…˜์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ SELECT ๋ฌธ์žฅ์ด ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ์— Shared Lock์ด ๊ฑธ๋ฆฌ๋Š” ๊ณ„์ธต

    ์™„๋ฒฝํ•œ ์ฝ๊ธฐ ์ผ๊ด€์„ฑ ๋ชจ๋“œ๋ฅผ ์ œ๊ณตํ•จ

    ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋Š” ํŠธ๋žœ์žญ์…˜ ์˜์—ญ์— ํ•ด๋‹น๋˜๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ˆ˜์ • ๋ฐ ์ž…๋ ฅ ๋ถˆ๊ฐ€๋Šฅ



์„ ํƒ ์‹œ ๊ณ ๋ ค์‚ฌํ•ญ

Isolation Level์— ๋Œ€ํ•œ ์กฐ์ •์€, ๋™์‹œ์„ฑ๊ณผ ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์— ์—ฐ๊ด€๋˜์–ด ์žˆ์Œ

๋™์‹œ์„ฑ์„ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์„ ์œ ์ง€ํ•˜๋ฉด ๋™์‹œ์„ฑ์ด ๋–จ์–ด์ง€๊ฒŒ ๋จ

๋ ˆ๋ฒจ์„ ๋†’๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜๋ก ๋ฐœ์ƒํ•˜๋Š” ๋น„์šฉ์ด ์ฆ๊ฐ€ํ•จ


๋‚ฎ์€ ๋‹จ๊ณ„ Isolation Level์„ ํ™œ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ํ˜„์ƒ๋“ค
  • Dirty Read

    ์ปค๋ฐ‹๋˜์ง€ ์•Š์€ ์ˆ˜์ •์ค‘์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์—์„œ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ํ˜„์ƒ

    ์–ด๋–ค ํŠธ๋žœ์žญ์…˜์—์„œ ์•„์ง ์‹คํ–‰์ด ๋๋‚˜์ง€ ์•Š์€ ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์— ์˜ํ•œ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋ณด๊ฒŒ๋˜๋Š” ๊ฒฝ์šฐ

    • ๋ฐœ์ƒ Level: Read Uncommitted
  • Non-Repeatable Read

    ํ•œ ํŠธ๋žœ์žญ์…˜์—์„œ ๊ฐ™์€ ์ฟผ๋ฆฌ๋ฅผ ๋‘ ๋ฒˆ ์ˆ˜ํ–‰ํ•  ๋•Œ ๊ทธ ์‚ฌ์ด์— ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜ ๊ฐ’์„ ์ˆ˜์ • ๋˜๋Š” ์‚ญ์ œํ•˜๋ฉด์„œ ๋‘ ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ƒ์ดํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚˜๋Š” ์ผ๊ด€์„ฑ์ด ๊นจ์ง„ ํ˜„์ƒ

    • ๋ฐœ์ƒ Level: Read Committed, Read Uncommitted
  • Phantom Read

    ํ•œ ํŠธ๋žœ์žญ์…˜ ์•ˆ์—์„œ ์ผ์ • ๋ฒ”์œ„์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋‘ ๋ฒˆ ์ด์ƒ ์ฝ์—ˆ์„ ๋•Œ, ์ฒซ๋ฒˆ์งธ ์ฟผ๋ฆฌ์—์„œ ์—†๋˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋‘๋ฒˆ์งธ ์ฟผ๋ฆฌ์—์„œ ๋‚˜ํƒ€๋‚˜๋Š” ํ˜„์ƒ

    ํŠธ๋žœ์žญ์…˜ ๋„์ค‘ ์ƒˆ๋กœ์šด ๋ ˆ์ฝ”๋“œ ์‚ฝ์ž…์„ ํ—ˆ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜ํƒ€๋‚˜๋Š” ํ˜„์ƒ์ž„

    • ๋ฐœ์ƒ Level: Repeatable Read, Read Committed, Read Uncommitted