Database Key Design - aragorn/home GitHub Wiki
2021-03-13 JK
λ€μ΄κ°λ©΄μ
Database design μμ Auto increment ID μ UUID κ°μ΄λ°, μ΄λ κ²μ Primary key λ‘ μ¬μ©νλ κ²μ΄ μ’μκ°λΌλ μ§λ¬Έμ΄ μλ€. FAQ λΌκ³ ν λ§ν, μμ£Ό μ νλ©° κ³ λ―Όνκ² λκ³ , μ¬λ¬ μ¬λμ΄ κ²ͺλ λ¬Έμ μΈλ°, λ€λ£¨κΈ° μ΄λ €μ΄ λ¬Έμ μ΄λ€. μννΈμ¨μ΄ λμμΈ λ¬Έμ κ° λ³΄ν΅ κ·Έλ¬νλ°, λͺ μΎνκ² λ¬΄μμ΄ λ μ°μνμ§ κ΅¬λΆνκΈ° μ΄λ ΅κ³ , Pros, Cons μ κ° νλͺ©λ νκ°νκΈ° μ΄λ ΅λ€.
κ·Έλλ, μ΄ν΄λ³΄κ³ κ³ λ―Όν΄ λ³΄λ©΄ μ’μ λ¬Έμ μ΄λ, λͺκΈμ μ μ΄μ μ£Όμ₯ν΄ λ³Έλ€.
Natural Key vs Surrogate Key
μ΄ λ κ°μ§ Key κ° κ°κ° 무μμΈμ§μ λν΄μλΆν° μ΄ν΄νκ³ κ°λ©΄ μ’κ² λ€. ꡬκΈμμ κ²μνκ³ λ νΌλ°μ€ μ°Ύμ보면, λ°λ³΅ν΄μ μ¬λλ€μ΄ μ§λ¬Ένκ³ μκ³ , λ΅μ μ 리νκ³ μλλ°, μ΄ λΆλΆμ λ³΄ν΅ μ¬λλ€μ΄ λ΅νλ λ΄μ©μ΄ μ© μλΏμ§λ μλλ€.
http://www.agiledata.org/essays/keys.html
- Natural key. A key that is formed of attributes that already exist in the real world. For example, U.S. citizens are issued a Social Security Number (SSN) that is unique to them (this isn't guaranteed to be true, but it's pretty darn close in practice). SSN could be used as a natural key, assuming privacy laws allow it, for a Person entity (assuming the scope of your organization is limited to the U.S.).
- Surrogate key. A key with no business meaning.
μμ κ°μ μ€λͺ μ΄ λ§λ€.
μ΄ μ§λ¬Έμ λ€λ₯΄κ² λ΅μ νκ³ μΆλ€. λκ° λ§λ€μ΄μ λνν μλ €μ€ κ²μ΄λΌλ©΄ natural key, λ΄κ° μλ‘ λ§λ€μμΌλ©΄ surrogate key. ν΅μ¬μ ꡬλΆμ λμ λ λλ λ΄λΆμ μΈλΆμ ꡬλΆμ΄λ€.
https://en.wikipedia.org/wiki/Surrogate_key
- Surrogate (1) β Hall, Owlett and Todd (1976)
A surrogate represents an entity in the outside world. The surrogate is internally generated by the system but is nevertheless visible to the user or application.[2]- Surrogate (2) β Wieringa and De Jonge (1991)
A surrogate represents an object in the database itself. The surrogate is internally generated by the system and is invisible to the user or application.
λΉμ¦λμ€ μλ―Έκ° μλ μλ, μ΄λ° κ²μ΄ μ€μνμ§ μμ 보μΈλ€. λΉμ¦λμ€ μλ―Έκ° μλ€λ κ²μ λ¨λ€μ΄ μ΄λ―Έ μ°κ³ μλ€λ κ²μ λ€λ₯Έ ννμΌ λΏμ΄λ€.
μμ€ν μ λ΄λΆμ μΈλΆλ₯Ό ꡬλΆνλ κ²μ κ°λ¨νμ§ μλ€. λ³΄ν΅ μννΈμ¨μ΄ μμ€ν μ κ²°ν¨μ΄ μκΈ°λ νκ³ , κ²½κ³κ° λΆλͺ ννκ±°λ νΌλμ΄ μκΈ°λ νλ€. λͺκ°μ§ κΈ°μ€μ΄ μκΈ΄ νμ§λ§,
- μννΈμ¨μ΄ μλͺ μ£ΌκΈ°λ₯Ό κ°μ΄ νλ λ©μ΄λ¦¬
- νλμ κ°λ°νμ λ²μμ κ·Έ κ°λ°νμ΄ λ΄λΉνλ μμ€ν μ λ²μ
- Git repository μ λ¨μ
λ±μ μκ°ν μ μλλ°, μ λΉν κ²μΌλ‘ Software Development Life Cycle μ κΌ½μ μ μλ€. Development, Test, QA, Deployment, Rollback μ μ£ΌκΈ°λ₯Ό κ°μ΄ λ°λΌκ°λλμ΄λ€. μννΈμ¨μ΄ μμ§λμ΄λ§νμ μ‘°μ§, κ°λ°νμ λ²μλ μ€μνλ€.
κ°λ°μΈμ΄, νλ μμν¬ κ΅¬λΆμ΄ κ²°μ νμ§ μμ κ²μ΄λ€. μλ₯Ό λ€μ΄ Java Spring μ ν리μΌμ΄μ κ³Ό React application μ΄ νλ²μ ν¨ν€μ§λμ΄ λ°°ν¬λλ€λ©΄ λλ ν¨ν€μ§μ λ³λμ΄λλΌλ, λΉλ/QA/λ°°ν¬/λ‘€λ°± μ£ΌκΈ°κ° λκΈ°νλμ΄ μλ€λ©΄, νλμ μμ€ν μΈ κ²μΌλ‘ κ°μ£Όνλ©΄ λλ€.
μ΄λ κ² natural key, surrogate key μ κ°λ μ μ΄ν΄λ³΄λ κ²μ, natural key κ° μ²μ μ΄λ»κ² λ§λ€μ΄μ‘μκΉ λ°μ Έλ³΄λ©΄, κ²°κ΅ surrogate key μμ± λ¬Έμ λ‘ νμλλ€λ κ²μ 보μ¬μ£ΌκΈ° μν΄μμ΄λ€. μ΄λ μμ€ν μ Keyλ₯Ό μλ‘ λ§λ€κΈ° λ§λ ¨μ΄κ³ , κ·Έκ²μ λ€λ₯Έ μμ€ν μ μ ννκ² λλ€. λ겨 λ°μ μͺ½μμλ κ·Έκ²μ natural key λ‘ κ°μ£Όνλ©΄ λκ³ , μ²μ λ§λλ μͺ½μμλ surrogate key λ₯Ό μ΄λ»κ² μ λ§λ€ κ²μΈμ§λ₯Ό κ³ λ―Όνλ κ²κ³Ό λμΌν λ¬Έμ κ° λλ€.
Key λ΄λΆμ code system μ embed νμλ μ¬λΆλ natural vs surrogate key ꡬλΆκ³Ό 무κ΄ν λ€λ₯Έ μ£Όμ μ΄λ€. μλ₯Ό λ€μ΄ νκ΅μ μ£Όλ―Όλ±λ‘λ²νΈλ μλΆλΆ 6κΈμκ° μλ μμΌμ΄κ³ , λ·λΆλΆ 첫κΈμκ° μ±λ³/λ΄κ΅μΈ/μΈκ΅μΈ κ΅¬λΆ λ± μλ―Έλ₯Ό κ°λλ€. κ·Έλ¬λ, μ΄λ° code system μ κ°κ³ μλμ§ μ¬λΆλ, natural vs surrogate key ꡬλΆκ³Ό 무κ΄νλ€. surrogate key λ₯Ό μμ±ν λ, νΈμλ₯Ό μν΄ code system μ μ μ©νλ κ²μ΄ μ₯μ μ΄ μμΌλ©΄, λμ ν μ μλ€. μ΄λ¬ν μμλ₯Ό μ΄νμ μ μν΄ λ³΄κ² λ€.
Auto increment id vs UUID
κ΅¬κΈ κ²μνλ©΄, λ§μ κΈμ΄ λμ¨λ€. 첫νμ΄μ§μ κΈ κ°μ΄λ° λͺκ° λ§ν¬νλ€.
κ·Έλμ UUID κ° λ λ«λ€.
https://mareks-082.medium.com/auto-increment-keys-vs-uuid-a74d81f7476a
μ΄ κΈ μ μμ μ견μ κ° key μ Pros, Cons μμ λ΄ μ견과 κ°κ±°λ λΉμ·νλ€. κ·Έλ°λ°, λ§μ§λ§ κ²°λ‘ μ UUID κ° λ λ«λ€λ κ²μΌλ‘ λ¬λΌμ§λ€.
Auto increment id κ° λ λ«λ€.
https://tomharrisonjr.com/uuid-or-guid-as-primary-keys-be-careful-7b2aa3dcb439
μ΄ κΈ μ μμ μκ²¬μ΄ λμ λΉμ·νλ€.
κ·Έ μΈ
μ΄ λκΈλ Pros, Cons λ₯Ό μ μμ½νκ³ μλ€.
κ·Έλμ λ΄ μ견μ,
κ²°λ‘ λ§ κ°λ¨ν μ΄μΌκΈ°νλ€λ©΄, λλ λμ²΄λ‘ auto increment id μͺ½μ΄λ€.
UUIDμ μ₯μ κ³Ό λ¨μ μ΄ λ¬΄μμΈκ°?
μΌλ¨ UUIDκ° λ¬΄μμΈμ§λΆν° μμμΌ νκ² λ€. μμ£Ό κ°λ¨ν λ§ν΄, 128 bit λ§μμ΄λ€. https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
https://en.wikipedia.org/wiki/Birthday_problem μ νμ΄λ³΄λ©΄, 128 bit UUID κ° λμΌν κ°μ΄ λμ¬ νλ₯ μ μκ³ , λ³΄ν΅ κ²½μ°μλ μΆ©λνμ§ μλλ€κ³ 보면 λλ€. μΌλΆλ¬ random seed λ₯Ό λμΌνκ² λ§μΆμ§ μλλ€λ©΄. λ§μλΌλ κ²μ μ€μν νΉμ§μ΄κ³ , μ₯μ μ΄ λκΈ°λ νμ§λ§, λ¨μ μ΄ λκΈ°λ νλ€.
μΆμΈ‘ν μ μλ€λ κ², κ·Έλμ λ΄λΆμ μ‘΄μ¬νλ id μ무거λ νλλΌλ, μΈλΆμμλ μ°μ΄ λ§ν μκ° μλ€λ κ²μ μ₯μ μΌλ‘ μμ κΌ½λ μ¬λλ€μ΄ μλ€. μ΄λ° νΉμ±μ μμ² ν° λ§μλΌλ κ²μμ λΉλ‘―λ λΉμ°ν νΉμ±μΈλ°, κ·Έκ²μ΄ security κ΄μ μμ μ₯μ μ΄ λλ€λ κ²μ, "λ³λ‘..?"λΌκ³ λ³Έλ€. Security λ₯Ό μ¬κΈ°μ μμ‘΄νλ©΄ μ λλ€. User ID μΆμΈ‘νλ κ²μ μ΄μ°¨νΌ λ€λ₯Έ μμΌλ‘ μμ§ν΄μ μ°μ΄ λ§ν μλ μλ€.
λμ κ΄μ μμ UUIDμ μ₯μ μ, λΆμ° μμ€ν μ μ ν©νλ€λ κ²μ΄λ€. μ€μ μ§μ€λ Online Key Generator κ° μλλΌλ κ²μ΄λ€. UUIDλ Globally unique key λ₯Ό λ§λ€μ΄λ΄κΈ° λλ¬Έμ,
- μ¬λ¬ λ§μ΄ν¬λ‘μλΉμ€ λλ λ°λ‘ λ¨μ΄μ Έμ μλνλ instance κ° κ°κ° key λ₯Ό μμ±ν μ μλ€. Single Point of Failure κ° μ¬λΌμ§λ€. μ±λ₯/λμνμ λ³λͺ©μ μμ κΈ° μ’λ€.
- κ°κ° λ§λ€μ΄μ§κ³ μ μ₯λ λ°μ΄ν°λ₯Ό νλλ‘ λ³ν©νλλΌλ, key μΆ©λμ΄ μλ€. λ―Έκ΅ μλΆμ μμ€ν μ΄ μμ±ν key, λ―Έκ΅ λλΆ μμ€ν μ΄ μμ±ν key, μμΈμμ μμ±ν key, μΌλ³Έμμ μμ±ν key, κ°κ°μ λͺ¨λ λ³ν©ν΄μ BigQuery μ λ°μ΄ν°λ₯Ό λͺ¨μΌλλΌλ, μΆ©λμ΄ μλ€.
λ¨μ μ λ무 κΈΈλ€λ κ²μ΄λ€. μκ·λͺ¨ μμ€ν μμλ μ΄ λ¨μ μ΄ λ€λ₯Έ μ₯μ μ λͺ¨λ κ°λ¦°λ€κ³ λ³Έλ€. λ΄κ° 보기μ κ±°μ μ μΌν λΆλͺ ν μ΄μ μ΄λ€.
- μ¬λμ΄ μΈμ§νκ³ , λΆλ₯΄κ³ , 볡λΆνκ³ , λ€λ£¨κΈ° μ΄λ ΅λ€. λ‘κ·Έμ μ°μΌλ©΄ μ½μ μ μλ€. Hexdigit 6κΈμλ μ½κΈ° μ΄λ €μνλλ°, 32κΈμ λͺ» μ½κ³ λͺ» μΈμ΄λ€. νλ©΄μ μΆλ ₯ν λ λ³΄ν΅ - ν¬ν¨νμ¬ 36κΈμμ΄λ€.
- λ‘λ΄μ ν°μΉμ€ν¬λ¦°μ μΆλ ₯νλ©΄ μ΄μ©λ €κ³ ? νλ©΄ ν λ΄μ μΆλ ₯μ΄ κ°λ₯νκ°? 보μ¬μ€λ€κ³ ν΄μ, μ νν΅ννλ©΄μ μ½μ΄μ€ μ μλ?
μ) μΉ΄μΉ΄μ€ν‘μ μΉ΄μΉ΄μ€ κ³μ ID κ° μλλ°, 64bit integer λ₯Ό μ¬μ©νκ³ , hexadigit λ μλ decimal 10μ§μλ‘ νλ©΄μ μΆλ ₯ν΄ μ€λ€. κ³ κ°μ§μ λ°μΌλ €κ³ νλ©΄, μ΄ μΉ΄μΉ΄μ€ κ³μ ID λ₯Ό μ½μ΄μ€μΌ ν μ μλ€. λλ λ΄λΆ κ°λ°μκ° λλ²κΉ ν λ, μ΄ 8 byte integer λ₯Ό APIμ μ£Όμ νλΌλ―Έν°λ‘ μ°κΈ° λλ¬Έμ, μ΄κ±Έ μ½κ³ μ λ¬ν΄μΌ νλ€. λ§€μ° κ³€λνλ€.
JavaScript μμ λ³ μκ°μμ΄ μ΄ ID λ₯Ό number λ‘ λ€λ£¨λ©΄, precision λ¬Έμ λλ¬Έμ, λ·λΆλΆ μ리μ λμ¬μ―κ°κ° μ‘°μ©ν λ μκ°λ€. λ²κ·Έ μλ μ€ λͺ¨λ₯΄κ³ , νμ°Έ μ§ννλ€ λ€λ¦κ² μ€μ λ°κ²¬νκ³ μ½μ§μ νκ² λλ€.
λλ€λ₯Έ λ¨μ μΌλ‘ μ±λ₯μ λΆλ¦¬νλ€. κΈΈμ΄κ° λ무 κΈΈλ€λ κ², random value λΌλ κ², λ κ°μ§ μ΄μ λλ¬Έμ, μ μ₯μκ° ν¨μ¨μ μΌλ‘ μλνλλ° λΆλ¦¬νλ€. μκ·λͺ¨μμλ μ±λ₯μ λ³ μ΄μκ° μμ§λ§, large system μμ UUID λ₯Ό key index λ‘ μ¬μ©νλ 건, νλν°κ° ν¬λ€. κ·Έλμ ν¨μ¨μ μν΄ λ€μ incremental number μ surrogate key λ₯Ό λ΄λΆμ λ§λ€μ΄ μ¬μ©νκ² λλ€.
λ΄κ° UUIDλ₯Ό μ΄λ€λ©΄, μΈμ μ°κ² λ κΉ?
- multi-region μμ λ 립μ μΌλ‘ μλνλ μλΉμ€λ₯Ό 빨리 λ§λ€μ΄μΌ ν λ.
- key system design μ΄μμ λν΄ λ Όμνκ³ μ€λΉν μ¬λ ₯μ΄ μ λ λ.
Auto increment idμ μ₯μ κ³Ό λ¨μ μ΄ λ¬΄μμΈκ°?
UUIDλ₯Ό μ΄ν΄λ³΄κ³ λλ, auto increment id μ μ₯λ¨μ μ κΌ½κΈ° μ¬μμ‘λ€.
첫λ²μ§Έ μ₯μ . μ§§λ€. μ§§μ key κ°μ΄λ° ꡬνμ΄ μ μΌ μ¬μ΄ λ°©λ²μ΄λ€. λ μ§§μ key λ₯Ό λ§λ€κΈ° μ΄λ ΅λ€. κ½ μ±μ μ¬μ©ν auto increment id λ³΄λ€ λ μ§§μ key λ₯Ό λ§λλ κ²μ λΆκ°λ₯νλ€.
λλ²μ§Έ μ₯μ . Sequential νκΈ° λλ¬Έμ, range λ‘ set μ νννλλ° μ 리νλ€. λ³΄ν΅ λ€λ£¨κ² λλ resourceμ subset μ΄ μ΄λ id λΆν° μ΄λ id μ¬μ΄, μ΄λ° range λ‘ νν κ°λ₯ν΄μ§λ€.
μ) 곡μ₯μμ λΆλμ΄ μκΈ°λ©΄, λλΆλΆ sequential number range λ₯Ό μ§μ ν΄μ recall νλ©΄ λλ€.
μΈλ²μ§Έ μ₯μ . time locality κ° μμ΄μ, λ³΄ν΅ μκ°μ μ λ ¬ λͺ©λ‘μ λ§λ€ λ, μ μ λ‘ cache hit ratio κ° μ¬λΌκ°λ€.
λͺκ°μ§ λ¨μ μ΄ μλ€.
첫λ²μ§Έ. Key generator κ° online μΌλ‘ μλν΄μΌ νλ€. λ€νΈμν¬ μ₯μ κ° λλ©΄ λͺ¨λ λ©μΆλ€λ μλ―Έμ΄λ€. Single Point of Failure κ° λλ€. μ΄κ²μ μΉλͺ μ λ¨μ μ€ νλμ΄λ€. μκ·λͺ¨ μ¨λΌμΈ μμ€ν μμ λ³΄ν΅ μκ΄μ΄ μμ§λ§. κ·Έλ°λ°, multi-region μ΄κ±°λ, offline system κ³Ό λκ°λ₯Ό νλ €κ³ νλ©΄ μΉλͺ μ μ΄κ² λλ€.
λλ²μ§Έ. RDBMS μ incremental id λ₯Ό μ°λ©΄, λ°μ΄ν°λ₯Ό λ£μ΄λ³΄μμΌ key λ₯Ό μ μ μκ² λλ€.
DB table μ λ°μ΄ν°λ₯Ό λ£μ΄λ³΄μμΌ key λ₯Ό μ μ μλ€λ κ²μ, μ ν리μΌμ΄μ μμ auto increment id λ₯Ό μ§μ μμ±νλ κ²μΌλ‘ ν΄κ²°ν μλ μλ€. κ³ μ±λ₯μ Key generator κ° νμν κ²½μ°, mysql λ± DBMSκ° μ 곡νλ auto increment id κ° μλ, application μμ μ§μ μμ±ν key λ₯Ό μ¬μ©νλ©΄μ, bulk/batch processing νλ κΈ°μ μ μ μ©ν μ μλ€. λλ redis κ°μ cache storage μμ id λ₯Ό μμ±νλ λ°©λ²λ μ’λ€.
μΈλ²μ§Έ. λμ©λ λΆμ° μμ€ν μ λ§λλλ° λ³λͺ©μ΄ λμ΄ λ²λ¦°λ€. μμλ λ Έλ μμ€μμ, κ³ μμΌλ‘ μλνκ³ μΆμλ°, Key generator λ₯Ό auto increment number λ‘ μ μ©ν κ²½μ°, λ³λͺ©μ΄ λμ΄ λ²λ¦°λ€. μλ°±λ μ΄μ λ Έλλ‘ λμ΄μ§λ λͺ»νλ€. λ³΄ν΅ μ΄λ° κ²½μ°λ, νμμμ λλμ λ°μ΄ν°κ° μ μ λκ³ μκΈ°μ, μ½κ°μ μ±λ₯ degradation μ΄ λ°μν΄λ, Key generator μμ€ν μ throughput μ΄ κ°μνκ³ , μ΄κ³³μ λΆνκ° μ§μ€λκ³ , μ 체 μμ€ν μ΄ λ¨Ήν΅μ΄ λκ³€ νλ€. Spinlock κΈ°λ°μ κ³ μ±λ₯ key generator λ₯Ό ꡬννλλ λ§λλ, redis key μμ±κΈ°κ° μΆ©λΆν λΉ λ₯΄λ μλλ, μ±λ₯ ν μ€νΈλ μ΄λ»κ² ν κ²μ΄λ©°, μ΄λ μ λμ High availability λ₯Ό 보μ₯ν κ²μ΄λ, κ·Έ νμμ μ₯μ λ₯Ό λ΄λ©΄ λκ° μ΄λ»κ² μ± μμ§ κ²μ΄λ λ± μ΄μκ° λΆκ±°μ§λ€.
UUID μ Increment id κ° κ°κ°μ μ₯λ¨μ μ΄ μλλ°, λμμ΄ μλμ§? ν΄κ²°μ± μ΄ μλμ§?
μ, μ΄μ μ§μ§ λ³Έ μ£Όμ λ‘ λ€μ΄κ°μ. μ΄λ €μ΄ μμ€ν λμμΈ λ¬Έμ μ΄λ€.
첫λ²μ§Έ ν΄κ²°μ± . Natural key λ₯Ό μ°λ κ²μ΄λ€.
Surrogate key λ μ¬λ¬κ°μ§ μ₯μ μ΄ μμ§λ§, λ¨μ λ λ§λ€. μμ€ν λμμΈμ ν λ, μΌλ¨ λ³ μκ°μμ΄ incremental id λ₯Ό λ΄λΆμ λ§λ€μ΄λ΄λ κ²μ κΈ°λ³ΈμΌλ‘ νκ³ λ³Έλ€. κ·ΈλμΌ λ³΄ν΅ μμ€ν μ΄ ν¨μ¨μ μΌλ‘ μλνκ² λμμΈ, ꡬνν μ μλ€. κ·Έλ°λ° μ΄ id λ₯Ό μΈλΆμ λ°μ΄ν° κ΅νν λ μ¬μ©νλ κ²μλ λ§€μ° μ μ€ν΄μΌ νκ³ , λ³΄ν΅ μ λͺ¨λ₯΄κ² μΌλ©΄, μ ν΄μΌ νλ€. UUID μ΄λ , Increment ID μ΄λ , Surrogate key λ₯Ό μΈλΆμ μ ν΅μν€λ κ²μ λ§€μ° μ μ€ν΄μΌ νλ€.
Surrogate key λ‘μ, UUID λ₯Ό μ°μ§ λ§μμΌ νλ μ΄μ μ€ νλμ μ°κ²°λλ€. μΈλΆμ μ ν΅μν€μ§ μμν λ°, Global unique id λΌλ μ₯μ μ΄ λ¬΄μ΄μ νμνλ€λ κ²μΈκ°? λ΄λΆμμλ§ μ°κ² λ€λ©΄, Compact ν Sequential number κ° ν¨μ¬ μ’λ€.
Natural Key λ₯Ό κ·Έλ₯ μ°λ κ²μ΄ λ§€μ° μ’μ λμμ΄λ€.
λ§μΌ μμ§ Natural Key κ° μλ€λ©΄, μ΄κ²μ λ¨μκ² κ·Έ μΌμ λ λκΈ°λ κ²μ μ μ λ‘ νλ€. ννν.
λλ²μ§Έ λ°©λ². Natural key μ hash value λ₯Ό μ¬μ©νλ€.
Natural Key κ° λ무 κΈΈκ±°λ, κ°λ³κΈΈμ΄μ¬μ λ€λ£¨κΈ° νλ κ²½μ°κ° μλ€. λνμ μΌλ‘ url μ κ²½μ°, natural key κ° κΈΈκ³ κ°λ³κΈΈμ΄μ΄λ€. μ΄κ²μ λν΄, SHA1, MD5, murmur λ± hash function μ μ μ©νκ³ , κ·Έ hash value λ₯Ό natural key μΈ κ²μΈμ, λλ candidate key μΈ κ²μΈμ μ¬μ©νλ©΄ λλ€. human readable representation μΌλ‘λ url safe base64 λ₯Ό μ¬μ©νλ€.
κΈΈμ΄λ Birthday problem μ νμ΄λ³΄λ©΄ λ΅μ΄ λμ¨λ€. μ μ₯곡κ°μ μκ»΄μ°κ³ μΆλ€λ©΄, hash value μ μΌλΆλ₯Ό 8 bytes ~ 12 bytes μ λλ‘ λμ΄μ μ¬μ©ν μ μλ€. 8 bytes μ¬μ©νλ κ²½μ°, 2μ 64μΉμ μ κ³±κ·ΌμΈ 2μ 32μΉ κ·Όμ², 4G, 40μ΅ μ λμ λ°μ΄ν°λ₯Ό λ€λ£¨λλ° λ¬Έμ κ° μλ€. 12 bytes μ¬μ©νλ€λ©΄, 2μ 48μΉ = 2μ 32μΉ x 2μ 16μΉ = 40μ΅ x 6λ§ = 240μ‘° μ λμ λ°μ΄ν°λ₯Ό λ€λ£° μ μλ€. μ΄ μ λ μ μμ μΆ©λμ΄ μμ κ²μΌλ‘ μΆμ ν μ μλ€. 8 bytes μ hash value λ base64 λ‘ νννλ©΄ 12κΈμ, 12 bytes μ hash value λ base64 λ‘ νννλ©΄ 16κΈμκ° λλ€.
첫κΈμμ ꡬλΆμλ₯Ό μ μ ν μ μν΄ μ€ μ μλ€. File μ ν ꡬλΆνλ magic number 4~8 bytes μ²λΌ, IDμ μλΆλΆ λͺλΉνΈμ λ°λΌ, user_id, message_id, device_id λ± μ£Όμ resource μ νμ ꡬλΆνλ 체κ³λ₯Ό embed ν μ μλ€. 리μμ€ μ ν ꡬλΆμλ₯Ό λμ νλ κ²μ, hash value μ key μ΄λ auto increment key μ΄λ , μμ± λ°©μκ³Ό 무κ΄νκ² μ μ©νλ€.
μ€μ λ‘ μΉ΄μΉ΄μ€, λ€μ΄λ² λ±μ΄ μ΄λ° λμμΈ μ²΄κ³λ₯Ό κ°κ³ μ(μ)λ€. κ³ λ―Όν΄ λ³΄λ©΄, κ²°κ΅ μ΄ μ²΄κ³λ₯Ό μ ννκ² λλ€. μΈλΆ λν μΌμμ λͺ λ°μ΄νΈ μΈ κ²μ΄λ, hash function 무μμ μΈ κ²μ΄λ, url μμ http/https λΌμ΄λΈ κ²μ μΈ κ²μ΄λ λΆμ΄κ³ μΈ κ²μ΄λ λ± μ΄μκ° μκΈ΄ νμ§λ§.
μΈλ²μ§Έ λ°©λ². Surrogate key λ₯Ό μ§μ μ λ§λ λ€.
Natural Key κ° μμΌλ©΄ κ·Έκ²μ μ¬μ©νλ κ²μ΄ μ’λ€. κ·Έλ°λ°, Natural Key κ° μλ κ²½μ°κ° μλ€.
νμ κ°μ μ μ²μ λ°λ κ³³, μΉ΄μΉ΄μ€ν‘ λ©μ μ μμ λ©μμ§λ₯Ό λκ΅°κ° μμ±ν΄μ μ μ‘νλ €λ μμ λ±μλ, νμID, λ©μμ§ID κ° μ‘΄μ¬νμ§ μλλ€. νμ, λ©μμ§μ λν Natural Key κ° μλ κ²μ΄λ€.
μ΄ κ²½μ°μλ, μμ§λμ΄λ§ μ¬λ ₯μ΄ λλ€λ©΄, λΆμ°λ incremental id μμ±κΈ°λ₯Ό ꡬννλ©΄ μ’κ² λ€. μλ₯Ό λ€μ΄, 8 bytes μ μλ‘ key λ₯Ό μμ±νλλ°, μμ~μλ°±κ° key μμ±κΈ° instance κ° μ¬λΏ λμμ μλνλ κ²½μ°λ₯Ό κ°μ νκ³ , κ°κ°μ key μμ±κΈ°κ° μ€μμ quorum based system μμ key block μ ν λΉ λ°λ λ°©μμ μκ°ν μ μλ€.
μ¬λ¬ μ¬λμ΄ λΉμ·ν λ°©μμ λμμΈκ³Ό ꡬνμ μλνμλ€.
https://betterprogramming.pub/uuid-generation-snowflake-identifiers-unique-2aed8b1771bc
Twitter μμλ λ€μκ³Ό κ°μ΄ key λ₯Ό λ§λ€μλ€κ³ νλ€.
To solve the above project, Twitter developed the Snowflake project as a Thrift Server written in Scala. The generated Ids were composed of:
- Time β 41 bits (millisecond precision)
- Configured machine ID β 10 bits
- Sequence number β 12 bits β rolls over every 4096 per machine
https://sqlblog.iridule.net/index.php/2009/08/30/surrogate-keys-in-distributed-databases/
We have two values to be combined: the database node identifier (DBNodeID), and the row identifier for a particular table on a particular database node (RowID). Letβs assume that both are integer values, which would not be unreasonable in practice. Each integer uses four bytes of storage, so to combine the two values without loss of information, we need eight bytes. We will use bigint as the data type for the combined value, which does require eight bytes of storage. To combine two integer values into one bigint value we will use a technique called bit shifting.
λ€λ²μ§Έ λ°©λ². MySQL μ€μ μ μ ν΄μ, Auto increment id λ₯Ό μ λ§λ λ€.
Auto increment id μ step μ λμ¬μ master-master replication μ€μ μ μ μ©νκ³ , multi-region ꡬμ±μ μ μ©νλ κ²μ΄λ€. μ΄ λ°©λ²μ μ€μ λ‘ μλνλ κ²μ λ³Έ κ²μ μλλ€. μλμ μ ν κ² κ°μλ°, μ ν리μΌμ΄μ , νλ«νΌ ꡬν κ³Όμ μμ μ΄λ€ λ¬Έμ κ° λ°μν κ²μΈμ§λ, μ§μ κ²½νν΄ λ³΄μ§ μμμ μ λͺ¨λ₯΄κ² λ€. μ΄ λ€μμ DBλ₯Ό μλ‘ κ΅¬μ±νκ² λλ©΄ νλ² μλν΄ λ΄μΌκ² λ€.
How does ClearDB work?
ClearDB uses a combination of standard MySQL, mixed in with βmaster-masterβ replication as well as our own specialized layered software stack to provide a highly available MySQL database environment for your applications and services. Each cluster is geographically separated to ensure your databaseβs availability, even if an entire network, datacenter, storage, or system failure occurs. Combined with our high availability routing technology, we ensure that your database is always online and fully available, 24/7.
λ§λ¬΄λ¦¬
μκ°λ κΉμ κ°λ¨ν κΈλ‘ μ 리νλ©° κ²μμ μ’ ν΄λ³΄λ, λ€λ₯Έ μ¬λλ€λ κ³ λ―Ό λ§μ΄ ν΄μ μ’μ λ°©λ²μ΄ μλ €μ§ λ¬Έμ λΌλ κ²λ λ€μ μκ² λμλ€.