Skip to main content

Searching...

Tools
Articles
View All Results

Developer Lab · Scala

Generate UUID in Scala

UUID generation in Scala using the built-in Java UUID class - zero extra dependencies.

Implementation Code

Production Ready
scala
import java.util.UUID

object UUIDExample extends App {
  // UUID v4 - random, CSPRNG-backed
  val id: UUID = UUID.randomUUID()
  println(id.toString)

  // Generate multiple
  val ids: Seq[String] = Seq.fill(5)(UUID.randomUUID().toString)
  ids.foreach(println)
}

Explanation

Scala runs on the JVM and has full access to java.util.UUID with no additional dependencies. UUID.randomUUID() generates a version 4 UUID using SecureRandom internally - cryptographically secure by default.

Output Example

a3f2c1d4-8e7b-4a9f-b6c5-2d1e0f3a4b5c

Best Practices, Performance, and Security

Best practices

Use UUID.randomUUID() directly - no wrapper needed. For database primary keys, prefer a native UUID or BINARY(16) column over VARCHAR(36).

Performance

Excellent. JVM's SecureRandom is highly optimised and thread-safe. Suitable for high-throughput services.

Security

Cryptographically secure. Backed by java.security.SecureRandom which uses OS-level entropy (e.g. /dev/urandom).

Installation

UUID v4 (java.util.UUID)

bash
// No build.sbt entry needed
import java.util.UUID

No dependencies required - java.util.UUID is part of the JDK.

Frequently Asked Questions

How do I generate a UUID in Scala?

Scala uses the JVM's built-in java.util.UUID, so there are no extra dependencies. Call java.util.UUID.randomUUID().toString for a random v4 UUID backed by SecureRandom. For v7, use a JVM library such as java-uuid-generator.

Is UUID.randomUUID() cryptographically secure?

Yes. UUID.randomUUID() uses the platform CSPRNG (operating system secure random source), suitable for session tokens, API keys, and idempotency keys. Do not use non-cryptographic random sources for security-sensitive identifiers.

What is the difference between UUID v4 and v7 in Scala?

UUID v4 (UUID.randomUUID() or equivalent) is fully random and not sortable. UUID v7 embeds a millisecond timestamp for chronological sorting (RFC 9562). Use v4 for general-purpose IDs; use v7 for database primary keys at scale.

Do I need to install a package for UUID generation in Scala?

No additional package is required for basic v4 generation in Scala. Check the Installation section for version-specific notes.

How do I validate a UUID string in Scala?

Use the platform's UUID parse/validation function, or test against the RFC 4122 regex: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i. Always validate external input at API boundaries.

Should I use UUIDs as database primary keys in Scala applications?

UUIDs work well as primary keys for distributed systems. Prefer native UUID/BINARY(16) column types over VARCHAR(36). For very large tables, consider UUID v7 for better B-tree insert locality.

Can I generate UUIDs in Scala without a network connection?

Yes. UUID generation uses local OS entropy sources and does not require network access. Each call is independent and thread-safe on modern platforms.

What output formats are available in Scala?

The standard hyphenated lowercase string (36 chars) is the default. Most APIs also support 32-char hex (no hyphens) and 16-byte binary formats. Use string format for APIs and binary for database storage.

What RFC standards apply to Scala UUID generation?

Version 4 UUIDs follow RFC 4122. UUID v7 follows RFC 9562 (May 2024). Ensure your chosen method produces compliant version and variant bits.

When should I avoid UUID v1?

Avoid UUID v1 in security-sensitive contexts - it embeds MAC address and timestamp information. Prefer v4 (UUID.randomUUID() or equivalent) unless you need legacy Cassandra timeuuid compatibility.

Key definitions

UUID
128-bit universally unique identifier, usually shown as 36 hex characters with hyphens.
CSPRNG
Cryptographically secure pseudo-random number generator - the entropy source behind secure UUID generation.
RFC 4122
IETF standard defining UUID versions 1 through 5. Version 4 is random.
RFC 9562
IETF standard adding UUID versions 6, 7, and 8. Version 7 is time-ordered.