Scala World 2026 in the Lake District, UK focuses on functional Scala, Cats Effect, and typed FP patterns. UUID v7 sessions demonstrate how time-ordered identifiers fit Scala’s effect system — generation wrapped in IO for referential transparency, with no global mutable state. See UUID in Java for the JVM library context.

build.sbt dependency

libraryDependencies ++= Seq(
  "com.fasterxml.uuid"  % "java-uuid-generator" % "4.3.0",
  "org.tpolecat"       %% "doobie-core"          % "1.0.0-RC4",
  "org.tpolecat"       %% "doobie-postgres"       % "1.0.0-RC4",
  "org.typelevel"      %% "cats-effect"           % "3.5.4"
)

UUID v7 generation in Cats Effect IO

import cats.effect.IO
import com.fasterxml.uuid.{Generators, NoArgGenerator}
import java.util.UUID

object UUIDv7 {
  private val generator: NoArgGenerator =
    Generators.timeBasedEpochGenerator()

  // Wrapped in IO — referentially transparent
  val generate: IO[UUID] = IO(generator.generate())

  // Extract creation timestamp from UUID v7
  def createdAt(id: UUID): Long = id.getMostSignificantBits >>> 16
}

Scala 3 opaque type UUID wrapper

import java.util.UUID

object Domain {
  opaque type OrderId    = UUID
  opaque type CustomerId = UUID

  object OrderId {
    def apply(id: UUID): OrderId      = id
    def generate: IO[OrderId]         = UUIDv7.generate.map(OrderId(_))
    extension (id: OrderId) def value = id
  }

  object CustomerId {
    def apply(id: UUID): CustomerId      = id
    extension (id: CustomerId) def value = id
  }
}

Doobie insert with UUID v7

import doobie._
import doobie.implicits._
import cats.effect.IO
import Domain._

case class Order(id: OrderId, customerId: CustomerId, total: BigDecimal)

def createOrder(customerId: CustomerId, total: BigDecimal): IO[Order] =
  for {
    id    <- OrderId.generate
    order  = Order(id, customerId, total)
    _     <- sql"""
                INSERT INTO orders (id, customer_id, total)
                VALUES (${id.value}, ${customerId.value}, $total)
              """.update.run.transact(xa)
  } yield order

Cursor pagination with Doobie

// UUID v7 lexicographic sort = chronological — no ORDER BY created_at
def listOrders(cursor: Option[UUID], limit: Int): IO[List[Order]] = {
  val query = cursor match {
    case None =>
      sql"""SELECT id, customer_id, total FROM orders
            ORDER BY id LIMIT $limit"""
    case Some(c) =>
      sql"""SELECT id, customer_id, total FROM orders
            WHERE id > $c ORDER BY id LIMIT $limit"""
  }
  query.query[Order].to[List].transact(xa)
}

Further reading

External references

Frequently asked questions

How do I generate UUID v7 in Scala?

Use the java-uuid-generator (JUG) library: com.fasterxml.uuid.Generators.timeBasedEpochGenerator().generate() returns a UUID v7 as java.util.UUID. The JUG library is available on Maven Central. Wrap in IO(Generators.timeBasedEpochGenerator().generate()) for Cats Effect purity. See UUID in Java.

How does UUID v7 work with Doobie and PostgreSQL?

Doobie maps java.util.UUID to PostgreSQL uuid natively. Generate the UUID v7 with JUG in your IO program, then pass it to a Doobie sql interpolator: sql"INSERT INTO orders (id, total) VALUES ($id, $total)". No custom Meta instance needed — Doobie's default UUID support handles UUID v7. See UUID in PostgreSQL.

What is a Scala opaque type UUID wrapper?

A Scala 3 opaque type like opaque type OrderId = UUID prevents mixing IDs across domain entities at compile time. OrderId and CustomerId are different types even though both are UUIDs at runtime — the compiler rejects passing a CustomerId where an OrderId is expected. This pattern works with UUID v7 without any runtime overhead.