๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿคท๐Ÿผ‍โ™€๏ธ Etc.../- ๊ธฐํƒ€

[์•„ํ‹ฐํด ๋ฆฌ๋ทฐ] Love Sharma ์˜ 7 Famous Approaches to Generate Distributed ID with Comparison Table

by Wonit 2023. 1. 30.

ํ•ด๋‹น ๊ธ€์€ ๊ฐœ๋ฐœ, ๊ธฐ์ˆ ๊ด€๋ จ ์•„ํ‹ฐํด์ด๋‚˜ ๋ธ”๋กœ๊ทธ ๊ธ€ ํ˜น์€ ์œ ํŠœ๋ธŒ ์˜์ƒ์˜ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๊ฑฐ๋‚˜ ํ›„๊ธฐ๋ฅผ ์ ๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค.

๋ฆฌ๋ทฐํ•  ๊ธ€: 'Love Sharma ์˜ ๋ธ”๋กœ๊ทธ devgenius.io' ์˜ '7 Famous Approaches to Generate Distributed ID with Comparison Table'

 

7 Famous Approaches to Generate Distributed ID with Comparison Table

Nowadays, many small and large systems require unique global identifiers; it is an essential task in distributed computing with growing…

blog.devgenius.io

 

์ฃผ์ œ์™€ ๊ฐ„๋‹จ ์š”์•ฝ

  • 2๊ฐ€์ง€์˜ ์ „ํ†ต์ ์ธ id ์ƒ์„ฑ ๊ธฐ๋ฒ•.
    • ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•
      • ๋ฐฉ๋ฒ• 1. call ๋งˆ๋‹ค shared counter ๋ฅผ ์ด์šฉํ•ด์„œ id ๋ฅผ ์ƒ์„ฑ
      • ๋ฐฉ๋ฒ• 2. timestamp ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ id ์ƒ์„ฑ
    • ๋‹จ์ 
      • ์—ฌ๋Ÿฌ ๋…๋ฆฝ ์„œ๋ฒ„์—์„œ ๋™์ผํ•œ id ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ
    • ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”
      • ๋ถ„์‚ฐ ํ™˜๊ฒฝ์—์„œ๋„ ์œ ์ผํ•œ id ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์–ด์•ผํ•จ
      • High Concurrent Request per Second ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์–ด์•ผ ํ•จ
  • ๋ถ„์‚ฐ id ์ƒ์„ฑ๊ธฐ๊ฐ€ ํ•„์š”ํ•œ ๊ณณ
    • ํ†ต๊ณ„ ์ˆ˜์ง‘ ์žฅ์น˜
    • ์ฑ„ํŒ…, messaging
  • ๋ถ„์‚ฐ ID ์ƒ์„ฑ๊ธฐ์— ๋Œ€ํ•œ ์š”๊ตฌ์‚ฌํ•ญ
    • Functional Requirements
      • 64bit ์–ธ์ €๋ฆฌ์˜ ๊ธธ์ด
      • ํด๋Ÿฌ์Šคํ„ฐ ์ œ์•ฝ ์—†์ด ์ˆœ์ฐจ์ ์ธ unique id ์ƒ์„ฑ
      • ์ดˆ๋‹น 10,000 ๊ฐœ ์ด์ƒ์˜ id ๋ฅผ ์ƒ์„ฑ
    • Non-Functional Requirements
      • High Performance
      • Low Latency including Geo-Latency
      • High Availability
      • Fault-Tolerant
  • 7๊ฐœ์˜ ๋Œ€ํ‘œ์ ์ธ ๋ถ„์‚ฐ id ์ƒ์„ฑ๊ธฐ
    • UUID/GUID
      • ๊ฐ€์žฅ ์ž˜ ์•Œ๋ ค์ง„ ์‰ฌ์šด id ์ƒ์„ฑ๊ธฐ
      • 128๋น„ํŠธ์˜ 16์ง„์ˆ˜ ์ˆซ์ž, 32 ๋ฌธ์ž
        • 8-4-4-4-12
    • MySQL: Centralizing Auto-Increments
      • flickr's ticketing service ๋ผ๊ณ ๋„ ๋ถˆ๋ฆผ
      • id ์ƒ์„ฑ๋งŒ์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      • auto-increment ์™€ replace into ๋ฌธ์„ ์ด์šฉ
    • MySQL: Cluster Mode
      • ์•ž์„  ๋ฐฉ๋ฒ•์—์„œ ๊ณ ๊ฐ€์šฉ์„ฑ์„ ์ถ”๊ฐ€ํ•œ ๋ฐฉ๋ฒ•
      • ๋‘ ๊ฐœ์˜ MySQL ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐœ๋ณ„์ ์œผ๋กœ auto-increment ๋ฅผ ์‚ฌ์šฉํ•จ
    • MongoDB
      • mongoDB ๊ฐ€ ์ž์ฒด์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” id ์ƒ์„ฑ๊ธฐ๋ฒ•
      • 96 bit ์ˆซ์ž
        • 4(timestamp)-5(random)-3(incrementing counter)
    • Twitter Snowflake
      • time based generation
      • 64 bit ์˜ unsigned integers ๋ฅผ ์ด์šฉํ•œ unique id ์ƒ์„ฑ๊ธฐ
        • 41(epoch timestamp in millisec)-10(machine/node/shard id)-12(seq, local counter per machine)
        • epoch timestamp ๋กœ ์ธํ•ด์„œ time sortable, ๊ฑฐ์นœ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•จ
    • Baidu UID Generator
      • snowflake ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ฆ
        • snowflake ์™€ ๋‹ฌ๋ฆฌ workID bit ๋ฅผ client ๊ฐ€ override ํ•  ์ˆ˜ ์žˆ์Œ
        • snowflake ์˜ ๋™์‹œ์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ
        • worker node table ์„ ์ด์šฉํ•ด์„œ ์‹ค์ œ ์‹คํ–‰๋˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ
    • Sonyflake
      • 39(timeunit)-8(seq)-16(machine id)
      • snowflake ๋ฅผ ๋ณด์™„ํ•จ
      • snowflake ์— ๋น„ํ•ด์„œ ์žฅ์ ๊ณผ ๋‹จ์ ์ด ์žˆ์Œ
        • snowflake ๋ณด๋‹ค ๋” ๊ธด lifetime
        • ๋” ๋งŽ์€ distributed machines ๋ฅผ ํ—ˆ์šฉ
        • ๋” ๋Š๋ฆผ

 

๋ฆฌ๋ทฐ์™€ ๋‚˜์˜ ํ•ด์„

 

ํšŒ์‚ฌ ์ผ์„ ์ œ์™ธํ•˜๊ณ  ์•„๋งˆ ๊ทผ ์‹œ์ผ๋™์•ˆ ๊ฐ€์žฅ ๊ด€์‹ฌ์žˆ๊ฒŒ ์ž‘์—…์„ ํ•  ๋ถ„์•ผ๊ฐ€ ๋ฐ”๋กœ distributed system id generation ์ผ ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋Ÿฐ ์˜๋ฏธ์—์„œ ์ด ๊ธ€์„ ์ฝ์„ ๋•Œ ๊ต‰์žฅํžˆ ์žฌ๋ฐŒ๊ฒŒ ์ฝ์—ˆ๋‹ค

 

์šฐ์„ , snowflake ์— ๋Œ€ํ•ด์„œ๋„ ๋‹น์—ฐํžˆ ํฅ๋ฏธ๋กœ์› ์ง€๋งŒ MySQL ์˜ Centralizing ์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•๊ณผ cluster mode ์ด ์ •๋ง ํฅ๋ฏธ๋กœ์› ๋‹ค.

 

MySQL ์˜ replace into ๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŒ์•ฝ sameID ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด REPLACE INTO ๋ฅผ ํ†ตํ•ด์„œ id ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋Š” ์ปจ์…‰์ด๋‹ค.

 

์ด์™ธ์—๋„ snowflake ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๊ฒช์—ˆ๋˜ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ถˆํŽธํ•œ ์ ๋“ค์ด ์žˆ์—ˆ๋Š”๋ฐ

 

  • 64 bit ๋ผ๋Š” ๋„ˆ๋ฌด ๊ธด ๋ฌธ์ž์—ด
  • ์‰ฝ๊ฒŒ ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š๋Š” machineId ์— ๋Œ€ํ•œ ์ •๋ณด
  • spof

 

์ด๋Ÿฌํ•œ ๊ฒƒ๋“ค์„ Baidu ์™€ Sonyflake ๋ฅผ ์ด์šฉํ•ด์„œ ์–ด๋Š์ •๋„ ๋ณด์™„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์ด ๊ฝค๋‚˜ ์ค‘์š”ํ•œ ์ •๋ณด์˜€๋‹ค

๋Œ“๊ธ€