Universally Unique Identifier

A UUID; Universally Unique Identifier, is a 128-bit identifier that is guaranteed to be unique across both time and space. It is often represented as a 36-character hexadecimal string, typically separated by hyphens into groups of 8-4-4-12 characters, such as "550e8400-e29b-41d4-a716-446655440000."

  • v1 - Version 1 UUIDs using a timestamp and monotonic counter.
  • v3 - Version 3 UUIDs based on the MD5 hash of some data.
  • v4 - Version 4 UUIDs with random data.
  • v5 - Version 5 UUIDs based on the SHA1 hash of some data.
  • v7 - UUID 버전 7 (UUIDv7)은 가장 중요한 48비트에 유닉스 타임스탬프를 밀리초 단위로 인코딩하고, 나머지 74비트는 무작위로 생성
    • Drawbacks
      • UUIDv7 의 잠재적인 문제점 중 하나는 사용자가 ID 에서 생성 시간을 추출할 수 있다.

Which version to use?

Rust UUID Docs

If you just want to generate unique identifiers then consider version 4 (v4) UUIDs. If you want to use UUIDs as database keys or need to sort them then consider version 7 (v7) UUIDs. Other versions should generally be avoided unless there’s an existing need for them.

Some UUID versions supersede others. Prefer version 6 over version 1 and version 5 over version 3.

UUID7 과 UUID4 의 선택 기준은 성능이냐 보안이냐 갈린다. 만약 생성 시간 노출이 보안적으로 리스크가 있는 서비스라면 UUID4 가 좋으며, 판단하기 힘들때는 일단 UUID4 를 사용하는 것을 추천한다.

UUID with Hibernate

@Entity
class User(
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    @Column(name = "user_id")
    val id: UUID = UUIDGenerator.random(),
)

위 처럼 UUID 를 PK 로 지정하여 사용하는 경우 처음에 user_id 로 조회를 하고 -> 데이터가 없으면 -> insert 를 수행한다. 즉, insert 하기 전에 select 를 먼저하게 된다.

References