관리 메뉴

Dev Blog

Unique Identifiers 본문

Development Notes/Terminology and Concepts

Unique Identifiers

Yongjae Kim 2024. 12. 8. 10:14

Unique Identifiers: Overview, Pros, Cons, and Best Practices

Unique identifiers are crucial for ensuring uniqueness, reducing collision probability, and enabling easy tracking in various systems. Below is a summary of common identifier types:


1. UID (Unique Identifier)

  • Description: General term for any unique identifier, often implemented as an incremental integer, hash, or other ID type.
  • Common Uses: Database primary keys, legacy systems.
  • Pros:
    • Simple to implement and easy to read (e.g., sequential numbers).
    • Easy to query and index in relational databases.
  • Cons:
    • Collisions possible if improperly managed.
    • Sequential IDs can expose system usage details.
  • Best Practices:
    • Avoid in distributed systems without a centralized authority.
    • Use for internal systems or predictable cases without security concerns.

2. UUID (Universally Unique Identifier)

  • Description: A 128-bit identifier standardized by RFC 4122, often versioned (e.g., UUID v4 for random generation).
  • Common Uses: Database keys, distributed systems, software product keys.
  • Pros:
    • Universal across programming languages and systems.
    • Large range minimizes collision probability.
  • Cons:
    • Long (36 characters), impacting database indexing performance.
    • Non-sequential, unsuitable for ordered lists.
  • Best Practices:
    • Use UUID v4 for general purposes, v1 for time-based order.
    • Avoid for high-performance primary keys in relational databases.

3. GUID (Globally Unique Identifier)

  • Description: A Microsoft-specific implementation of UUIDs.
  • Common Uses: Windows registry, COM programming, Microsoft systems.
  • Pros:
    • Interoperable with UUIDs.
    • Standard for .NET applications.
  • Cons:
    • Platform-dependent and not distinct from UUIDs outside Microsoft systems.
  • Best Practices:
    • Use in Microsoft or .NET environments where GUIDs are standard.

4. CUID (Collision-Resistant Unique Identifier)

  • Description: Compact, collision-resistant ID for distributed systems.
  • Common Uses: URLs, distributed databases, session IDs.
  • Pros:
    • Shorter and URL-friendly.
    • Reduced collision probability due to structured algorithm.
  • Cons:
    • Less universally supported.
    • Not fully collision-proof in high-scale systems.
  • Best Practices:
    • Ideal for frontend-heavy apps, distributed systems, and URL-safe IDs.

5. Nano ID

  • Description: Compact, customizable, URL-friendly ID based on randomness.
  • Common Uses: URL slugs, client-side identifiers, short-lived unique IDs.
  • Pros:
    • Extremely small (21 characters by default) and fast to generate.
    • Highly customizable in length and character sets.
  • Cons:
    • No timestamping for chronological tracking.
    • Customization may require additional work.
  • Best Practices:
    • Use for short, unique frontend or URL-safe IDs.
    • Customize for specific uniqueness or constraints.

Additional Unique Identifier Types

1. Snowflake ID (Twitter Snowflake)

  • Description: A distributed, 64-bit, time-based identifier combining timestamp, machine ID, and a sequence number.
  • Pros:
    • Compact and scalable.
    • Ordered for chronological sorting.
  • Cons:
    • Requires machine IDs and server clock accuracy.
  • Best Practices:
    • Ideal for distributed systems needing time-order (e.g., social media posts).

2. KSUID (K-Sortable Unique Identifier)

  • Description: Time-based identifier designed to be k-sortable and UUID-like.
  • Pros:
    • Encodes a timestamp for ordered IDs.
    • No need for centralized databases in distributed systems.
  • Cons:
    • Longer (27 characters), increasing storage or bandwidth use.
  • Best Practices:
    • Use for distributed systems requiring time-order and uniqueness (e.g., event logs).

3. ObjectId (MongoDB)

  • Description: A 12-byte identifier combining timestamp, machine, process, and a counter.
  • Pros:
    • Shorter than UUIDs and naturally time-ordered.
  • Cons:
    • MongoDB-specific format may need adaptation elsewhere.
  • Best Practices:
    • Ideal for MongoDB or document-based databases.

Summary Comparison

Recommendations for Choosing the Right Identifier

  • Distributed Systems: Use UUIDs, CUIDs, or Snowflake IDs.
  • URLs/User-Friendly IDs: Nano ID or CUID for compact and URL-safe options.
  • Database Keys: Avoid UUIDs for primary keys due to indexing costs; consider CUIDs or Snowflake IDs for compactness and order.
Comments