2021-02-17(Wed)

ํ•ญ๋ชฉ

๋‚ด์šฉ

ํ•™์Šต ๋‚ ์งœ

2021-02-17(์ˆ˜)

ํ•™์Šต ์‹œ๊ฐ„

11:00~24:00

ํ•™์Šต ๋ฒ”์œ„ ๋ฐ ์ฃผ์ œ

๋น„๊ด€์  ๋™์‹œ์ œ์–ด

ํ•™์Šต ๋ชฉํ‘œ

Dual ๋ชจ๋“ˆ์—์„œ ์ƒ๊ธด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  ChatRoom์„ ๋งˆ๋ฌด๋ฆฌํ•œ๋‹ค.

๋™๋ฃŒ ํ•™์Šต ๋ฐฉ๋ฒ•

eunhkim, jujeong, sanam, yohlee๋‹˜๊ณผ ํŽ˜์–ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

์ƒ์„ธ ํ•™์Šต ๋‚ด์šฉ

Q) 3๋ช… ์ด์ƒ์˜ ์œ ์ €๊ฐ€ ๋™์‹œ์— '๋ ˆ๋” ๊ฒŒ์ž„' ๋งค์นญ ์‹ ์ฒญ์„ ํ•˜๋ฉด ์˜๋„ํ•œ ๊ฒƒ๊ณผ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ํ”Œ๋ ˆ์ด์–ด ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ฒŒ์ž„์ด ์ƒ์„ฑ๋œ๋‹ค.

๋ฃจ๋น„๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ ๋กœ์ง์—๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์˜€์œผ๋‚˜ ์ด์ƒํ•˜๊ฒŒ ์—ฌ๋Ÿฌ๋ช…์˜ ์œ ์ €๊ฐ€ ๋™์‹œ์— ๊ฒŒ์ž„ ๋งค์นญ ์‹ ์ฒญ์„ ํ•˜๋ฉด ์ด์ƒํ˜„์ƒ์ด ๋‚˜ํƒ€๋‚ฌ๋‹ค.

๊ฐ€๋ น 3๋ช…์˜ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๊ฒŒ์ž„ ์‹ ์ฒญ์„ ํ•˜๋ฉด ์ •์ƒ์ ์ธ ๊ฒŒ์ž„(2๋ช…์˜ ํ”Œ๋ ˆ์ด์–ด๋งŒ ์กด์žฌํ•˜๋ฉฐ ์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ”Œ๋ ˆ์ด์–ด ์œ„์น˜๊ฐ€ ๋‚˜๋‰˜์–ด์ง„ ํ˜•ํƒœ)์ด 1๊ฒŒ์ž„ ๋งค์นญ๋˜๊ณ  ๋‚˜๋จธ์ง€ 1๋ช…์€ ๋‹ค๋ฅธ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๋ ˆ๋” ๋งค์นญ์„ ์‹ ์ฒญํ•˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ ค์•ผํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ง€๊ธˆ์€ 3๋ช…์˜ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๊ฒŒ์ž„ ์‹ ์ฒญ์„ ํ•˜๋ฉด ๋น„์ •์ƒ์ ์ธ ๊ฒŒ์ž„(3๋ช…์˜ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์กด์žฌํ•˜๋ฉฐ ์™ผ์ชฝ์— ํ•œ๋ช… ์˜ค๋ฅธ์ชฝ์— ๋‘๋ช…์˜ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๋ฐฐ์น˜๋œ ํ˜•ํƒœ)์ด 1๊ฐœ ์ƒ์„ฑ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ๋ฐœ์ƒํ•œ๋‹ค.

A)

๋ฃจ๋น„๋กœ ์ž‘์„ฑํ•œ ์ฒ˜๋ฆฌํ๋ฆ„ ์ฝ”๋“œ ์ž์ฒด์˜ ๋…ผ๋ฆฌ์ ์ธ ๋ฌธ์ œ๋Š” ์—†์—ˆ๋‹ค. ์›์ธ์€ ๋ฐ”๋กœ DB ๋™์‹œ์ ‘๊ทผ์„ ๋ฐฉ๋น„ ํ•˜์ง€ ์•Š์•˜๋˜ ๊ฒƒ์ด์—ˆ๋‹ค.

๋‹จ์ˆœ transaction ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด์„œ๋Š” DB ๋™์‹œ ์ ‘๊ทผ์„ ๋ง‰์„ ์ˆ˜ ์—†์—ˆ๋‹ค. ์ด ๋•Œ ๋ ˆ์ผ์ฆˆ์—์„œ์˜ ๋‚™๊ด€์  ๋™์‹œ์„ฑ ์ œ์–ด๋Š” ์—๋Ÿฌ๊ฐ€ ์žˆ์Œ์„ ํ™•์ธํ–ˆ๋˜ ๋ฐ”๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ๋ฐ”๋กœ ๋น„๊ด€์  ๋™์‹œ์ œ์–ด๋ฅผ ๊ณ ๋ คํ–ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ with_lock ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•ด์„œ ํ–‰ ๋‹จ์œ„๋กœ ๋ ˆ์ฝ”๋“œ ์ ‘๊ทผ์„ ๋ง‰๋Š” ์‹์œผ๋กœ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

๋น„๊ด€์  ๋™์‹œ์„ฑ ์ œ์–ด์˜ ํŠน์ง•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. ์‚ฌ์šฉ์ž๋“ค์ด ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋™์‹œ์— ์ˆ˜์ •ํ•  ๊ฒƒ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

  2. ํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๋Š” ์‹œ์ ์— Lock์„ ๊ฑธ๊ณ  ์กฐํšŒ ๋˜๋Š” ๊ฐฑ์‹ ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์ด๋ฅผ ์œ ์ง€ํ•œ๋‹ค. ๋งˆ์น˜ ๋ฎคํ…์Šค!

  3. Locking์€ ์ฒซ ๋ฒˆ์งธ ์‚ฌ์šฉ์ž๊ฐ€ ํŠธ๋žœ์žญ์…˜์„ ์™„๋ฃŒํ•˜๊ธฐ ์ „๊นŒ์ง€ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋“ค์ด ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ด€์  ๋™์‹œ์„ฑ ์ œ์–ด๋ฅผ ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋ฉด ๋™์‹œ์„ฑ์„ ์ €ํ•ด ๋ฐ›๊ฒŒ ๋œ๋‹ค.

  4. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด lock ๋˜์–ด์žˆ๋Š” DB์— ์ ‘๊ทผํ•  ๋•Œ ๋ฌดํ•œ์ • ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ผ์ • ์‹œ๊ฐ„ ๋™์•ˆ๋งŒ ๊ธฐ๋‹ค๋ฆฌ๊ณ  Exception์„ ๋งŒ๋‚˜๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ณ  ์ด ๊ฒฝ์šฐ ์˜คํžˆ๋ ค ๋™์‹œ์„ฑ์„ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

์ด ๋•Œ with_lock์— ์˜ํ•ด Exception์ด ๋˜์ ธ์ง€๊ณ , ๋‹ค์‹œ ํŠธ๋žœ์žญ์…˜์„ ์‹œ์ž‘ํ•  ๋•Œ๊นŒ์ง€์˜ ์‹œ๊ฐ„์ฐจ๋ฅผ sleep rand/100 ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•ด์„œ ๊ฐ ์š”์ฒญ๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ์กฐ์ ˆํ•˜๋ฉด ๋™์‹œ์„ฑ์„ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค!

https://medium.com/@vipulbest1/handling-concurrency-through-database-in-rails-part-1-8619ffe75f2d

https://selleo.com/blog/pessimistic-locking-in-rails

https://stackoverflow.com/questions/6867233/multiple-models-creation-and-updation-under-one-transaction/6867341

https://stackoverflow.com/questions/20819403/how-do-i-ensure-a-model-always-uses-a-transaction-and-locks-in-rails

https://www.honeybadger.io/blog/how-to-try-again-when-exceptions-happen-in-ruby/

https://kbs4674.tistory.com/164?category=876767

ํ•™์Šต ๋‚ด์šฉ์— ๋Œ€ํ•œ ๊ฐœ์ธ์ ์ธ ์ดํ‰

ํ›„ํ›„ ๋™์‹œ์„ฑ ์ œ์–ด์— ๋Œ€ํ•ด์„œ ๋”๋”์šฑ ์•Œ๊ฒŒ ๋˜์–ด ์•Œ์ฐผ๋‹ค. ํ™•์‹คํžˆ ์—ฌ๋Ÿฌ๋ช…์ด ๋จธ๋ฆฌ๋ฅผ ๋งž๋Œ€๋‹ˆ ๋น ๋ฅด๊ฒŒ ๋ฌธ์ œ๊ฐ€ ํ’€๋ฆฌ๋Š”๊ตฌ๋‚˜ ์‹ถ๋‹ค.

๋‹ค์Œ ํ•™์Šต ๊ณ„ํš

  • chat message ํ”„๋กœํ•„์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹ ๊ฐœ์„ 

Last updated