ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

๐Ÿ“Š ๋™์‹œ์„ฑ ์ œ์–ด ๋ฐฉ์‹ ๋น„๊ต

Series

Java Concurrency Control

Database Concurrency Control

Redis Concurrency Control

Kafka Concurrency Control

Compare Concurrency Control

โš ๏ธ

๋กœ์ปฌ์—์„œ ํ…Œ์ŠคํŠธํ•œ ๊ฒฐ๊ณผ์ด๊ณ ,

์„œ๋ฒ„ ํ™˜๊ฒฝ๊ณผ ์—ฌ๋Ÿฌ ์š”์ธ๋“ค์— ์˜ํ•ด ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๊ณ , ์ •ํ™•ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Case 01

ํ•œ์ •์ˆ˜๋Ÿ‰ : 50,000

  • Total User : 296
  • Processes : 8
  • Threads : 37

(DB Named ๋ฐฉ์‹์€ ์ œ์™ธ)

Result


.

์‹œ๊ฐ„ ๋‚ด์— ๋ชจ๋“  ํŠธ๋ž˜ํ”ฝ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ ๋ฐฉ์‹

  • DB Pessimistic
  • Redis Incr
  • Kafka + Redis


.

์ผ๋ถ€ ์„ฑ๊ณต์„ ํ•˜๊ธด ํ•˜์˜€์ง€๋งŒ, ํŠธ๋ž˜ํ”ฝ์„ ๋ฒ„ํ‹ฐ์ง€ ๋ชปํ•˜๊ณ  ์„ฑ๋Šฅ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๋ฐฉ์‹ (์„ฑ๋Šฅ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ๋„ ๋‹ด์•„๋ณด์•˜๋‹ค.)

  • DB Optimistic โ†’ ์ถฉ๋Œ ์ฒ˜๋ฆฌ ๋น„์šฉ (์ถฉ๋Œ ์‹œ ํ•ด๋‹น ํŠธ๋žœ์žญ์…˜ ์žฌ์‹œ๋„) ์œผ๋กœ ์ธํ•œ ์„ฑ๋Šฅ ์ €ํ•˜
  • DB Named โ†’ ์ปค๋„ฅ์…˜ ํ’€ ๋ถ€์กฑ์œผ๋กœ ์ธํ•œ DeadLock ๋ฐœ์ƒ ๋ฐ ์„ฑ๋Šฅ ์ €ํ•˜
  • Redis Lettuce โ†’ Spin Lock ๋ฐฉ์‹ (๋ฐ˜๋ณต์ ์œผ๋กœ ํ™•์ธํ•˜๋ฉด์„œ ๋ฝ ํš๋“์„ ์‹œ๋„) ์œผ๋กœ ์ธํ•œ CPU ๋ถ€ํ•˜
  • Redis Redisson โ†’ ๋‚ด๋ถ€์ ์œผ๋กœ ๊ตฌํ˜„๋œ ์žฌ์‹œ๋„ ๋กœ์ง (ํŠน์ • ๊ฐ„๊ฒฉ์œผ๋กœ ์žฌ์‹œ๋„) ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ถ€ํ•˜


.

๋กœ์ปฌ PC์—์„œ ๋ฒ„ํ‹ธ ์ˆ˜ ์žˆ๋Š” ํŠธ๋ž˜ํ”ฝ์ด ์•„๋‹ˆ๋ผ์„œ ๊ฒฐ๊ณผ๊ฐ€ ํ˜„์‹ค๊ณผ๋Š” ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ,

์ฒ˜์Œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์•˜์„ ๋•Œ ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ์ž๋ž‘ํ•˜๋Š” Redis ์˜ Lettuce, Redisson ๋ฐฉ์‹์ด ์„ฑ๋Šฅ ์ €ํ•˜ ์ด์Šˆ๊ฐ€ ์žˆ์„ ์ค„์„ ๋ชฐ๋ž๋‹ค.

๋ฝ ํš๋“์„ ์œ„ํ•œ ์žฌ์‹œ๋„ ๋กœ์ง์œผ๋กœ ์ธํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” ์„ฑ๋Šฅ ์ €ํ•˜์ผ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค.

Case 02

ํ•œ์ •์ˆ˜๋Ÿ‰ : 1,000

  • Total User : 99
  • Processes : 3
  • Threads : 33

Result


.

์‹œ๊ฐ„ ๋‚ด์— ๋ชจ๋“  ํŠธ๋ž˜ํ”ฝ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•œ ๋ฐฉ์‹

  • DB Pessimistic
  • DB Optimistic
  • Redis Incr
  • Redis Lettuce
  • Redis Redisson
  • Kafka + Redis


.

์ผ๋ถ€ ์„ฑ๊ณต์„ ํ•˜๊ธด ํ•˜์˜€์ง€๋งŒ, ํŠธ๋ž˜ํ”ฝ์„ ๋ฒ„ํ‹ฐ์ง€ ๋ชปํ•˜๊ณ  ์„ฑ๋Šฅ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๋ฐฉ์‹

  • DB Named โ†’ ์ปค๋„ฅ์…˜ ํ’€ ๋ถ€์กฑ์œผ๋กœ ์ธํ•œ DeadLock ๋ฐœ์ƒ ๋ฐ ์„ฑ๋Šฅ ์ €ํ•˜


.

ํ•œ์ •์ˆ˜๋Ÿ‰์ด 50,000 ๊ฑด์ผ ๊ฒฝ์šฐ์— ๋น„ํ•ด ๋Œ€๋ถ€๋ถ„์˜ ๋ฐฉ์‹์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋ชจ๋“  ํŠธ๋ž˜ํ”ฝ์„ ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์œ„ ๊ฒฐ๊ณผ๋“ค์„ ํ†ตํ•ด ์ง€๊ทนํžˆ ๊ฐœ์ธ์ ์ธ ์ƒ๊ฐ์œผ๋กœ ๊ฐ ๋ฐฉ์‹์— ๋Œ€ํ•œ ๊ฒฐ๋ก ์„ ๋‚ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

Compare

Java

  • Java Async
    • ๋™์‹œ์„ฑ ์ด์Šˆ ์‚ฌ๋ก€์˜ ํ‘œ๋ณธ
  • Java Sync
    • ์„œ๋ฒ„๊ฐ€ 2๋Œ€ ์ด์ƒ์ผ ๊ฒฝ์šฐ ๋™์‹œ์„ฑ ์ด์Šˆ ๋ฐœ์ƒ

์‹ค๋ฌด์—์„œ ๋™์‹œ์„ฑ ์ฒ˜๋ฆฌ์— ์ ˆ๋Œ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๋ฐฉ์‹.

Database

  • Databse Pessimistic Lock
    • ์ถฉ๋Œ์ด ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์ด๊ฑฐ๋‚˜
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ถ€ํ•˜๊ฐ€ ์˜ฌ ์ •๋„์˜ ๋™์‹œ ํŠธ๋ž˜ํ”ฝ์„ ๊ฒฌ๋ŽŒ์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์•„๋‹ˆ๋ผ๋ฉด ์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐ
  • Databse Optimistic Lock
    • ์ ๋‹นํ•œ ํŠธ๋ž˜ํ”ฝ์—์„œ๋Š” ์‚ฌ์šฉํ• ๋งŒ ํ•˜์ง€๋งŒ,
    • Ramp-Up ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ์–ผ๋งˆํผ ๋ฒ„ํ‹ธ ์ˆ˜ ์žˆ์„์ง€ ๊ฒ€์ฆ์ด ํ•„์š”
  • Databse Named Lock
    • ์ปค๋„ฅ์…˜ ํ’€ ๋ถ€์กฑ ํ˜„์ƒ์œผ๋กœ ์‹ค๋ฌด์—์„œ ์ ์šฉ์€ ์–ด๋ ค์›Œ ๋ณด์ž„

๊ฐœ์ธ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฝ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ์‹์€

๊ฐ™์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ์„œ๋น„์Šค์— ์„ฑ๋Šฅ ์ด์Šˆ๋ฅผ ์ „ํŒŒํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ

๋Œ€๋Ÿ‰์˜ ํŠธ๋ž˜ํ”ฝ์„ ๊ฒฌ๋ŽŒ์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด Redis, Kafka ๋ฅผ ํ™œ์šฉํ•˜๊ณ 

์†Œ๋Ÿ‰์˜ ํŠธ๋ž˜ํ”ฝ๋งŒ ๊ฒฌ๋ŽŒ๋„ ๋˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด Pessimistic Lock ์„ ์ ์šฉํ•˜๋Š”๊ฒŒ ์ ์ ˆํ•˜๋‹ค๊ณ  ์ƒ๊ฐ

Redis

  • Redis Incr
    • ๊ฐ€์žฅ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋ณด์œ 
    • ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜์ด๊ณ  ์„œ๋ฒ„๊ฐ€ ์žฌ์‹œ์ž‘๋˜๊ฑฐ๋‚˜ ์žฅ์•  ๋ฐœ์ƒ ์‹œ ๋ฐ์ดํ„ฐ ์œ ์‹ค ๊ฐ€๋Šฅ์„ฑ์ด ์กด์žฌํ•˜๋Š” ๋‹จ์ 
  • Redis Lettuce / Redisson

Redis Incr ๋ฐฉ์‹์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์‹ค๋˜๋Š” ์ƒํ™ฉ์— ๋Œ€ํ•œ ๋Œ€๋น„๋งŒ ์ž˜ ๋˜์–ด์žˆ๋‹ค๋ฉด ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ์‹์ด๋ผ๊ณ  ์ƒ๊ฐ

Kafka

  • Kafka + Redis
    • ์ž„๊ณ„๊ฐ’ ์ œ์–ด์™€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ์˜ ์—ญํ• ์„ ๋ถ„๋ฆฌ
    • ๋ถ€ํ•˜๊ฐ€ ๋ถ„์‚ฐ๋˜๋ฏ€๋กœ ๋Œ€๋Ÿ‰์˜ ํŠธ๋ž˜ํ”ฝ์— ์ ํ•ฉํ•œ ๋ฐฉ์‹์ด๋ผ๊ณ  ์ƒ๊ฐ

Redis Inc ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์€ ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์ด ๋‹ค์†Œ ์†Œ์š”๋˜๋‹ค ๋ณด๋‹ˆ

์ž„๊ณ„๊ฐ’ ์ œ์–ด๋Š” Redis์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ , ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์€ Kafka Consumer๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ

๋ถ€ํ•˜๋ฅผ ๋ถ„์‚ฐ์‹œ์ผœ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ์ƒ๊ฐ

Reference.

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
๋งํฌ
Total
Today
Yesterday