eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

In this article, we’ll learn about UUIDs and time-based UUIDs.

We’ll see the advantages and disadvantages of time-based UUIDs and when to choose them.

We’ll also explore and compare some libraries that will help us implement these different algorithms for producing UUIDs.

2. UUIDs and Time-Based UUIDs

UUID stands for Universally Unique Identifier. It’s a 128-bit identifier expected to be unique every time we generate it.

We use them to identify something uniquely, even if that thing has no inherent identifier. We can use them in various contexts, such as computer systems, databases, and distributed systems, where we require unique identification of objects.

The likelihood of two UUIDs being the same is so tiny that it is considered statistically impossible, making them a reliable way to identify objects in distributed systems.

A time-based UUID, also known as the version 1 UUID, is generated using the current time and a unique identifier specific to the computer or network that produces the UUID. The timestamp ensures the UUID is unique, even if multiple UUIDs are generated simultaneously.

We’ll find two new versions of the standard (v6 and v7) that are time-related in the libraries implemented below.

Version 1 presents several advantages – the time sorted id is fitter to be a primary key in a table, and containing the creation timestamp can help with analysis and debugging. It also has some disadvantages – the chance of collision is slightly higher when generating multiple IDs from the same host. We’ll see if this is an issue later on.

Also, including the host address may present some security flaws, which is why version 6 of the standard attempted to improve security.

3. Benchmark

To make our comparison more straightforward, let’s write a benchmark program to compare the chance of collision and the generation time of the UUIDs. We start by initializing all the necessary variables:

int threadCount = 128;
int iterationCount = 100_000; 
Map<UUID, Long> uuidMap = new ConcurrentHashMap<>();
AtomicLong collisionCount = new AtomicLong();
long startNanos = System.nanoTime();
CountDownLatch endLatch = new CountDownLatch(threadCount);

We’ll run the benchmark on a number of 128 threads with 100.000 iterations each. Also, we’ll use a ConcurentHashMap to store all UUID generated. Besides that, we’ll use a counter for the collisions. To check the speed performance, we store the current timestamp at the start of execution to compare it with the timestamp at the final. And, at the final, we declare a latch to wait for all threads to finish.

After initializing all the variables necessary for our test, we’ll loop and start every thread:

for (long i = 0; i < threadCount; i++) {
    long threadId = i;
    new Thread(() -> {
        for (long j = 0; j < iterationCount; j++) {
            UUID uuid = UUID.randomUUID();
            Long existingUUID = uuidMap.put(uuid, (threadId * iterationCount) + j);
            if (existingUUID != null) {
                collisionCount.incrementAndGet();
            }
        }
        endLatch.countDown();
    }).start();
}

For every line of execution, we’ll integrate again and start generating UUIDs using java.util.UUID class. We’ll insert all the ids with their corresponding count in the map. The UUID will be the key of the map.

So, if we try to insert an existing UUID in the map, the put() method will return us the already existing key. When we get a duplicate UUID, we’ll increment the collision count. At the end of the iteration, we’ll decrement the countdown.

endLatch.await();
System.out.println(threadCount * iterationCount + " UUIDs generated, " + collisionCount + " collisions in "
        + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos) + "ms");

In the end, we’ll use the await() method of the CountDownLatch class to wait until all threads are finished. We’ll print the result of our benchmark consisting of the number of UUIDs generated, the number of collisions, and the execution time.

Now, let’s run the benchmark against the JDK’s built-in UUID generator:

12800000 UUIDs generated, 0 collisions in 4622ms

We can see that all ids were generated without collision. In the following sections, we’ll compare this with the other generators.

4. UUID Creator

4.1. Dependency

The Java UUID Creator library is valuable and flexible for generating UUIDs. It provides various options for generating UUIDs, and its simple API makes it easy to use in a wide range of applications. We can add the library to our project:

<dependency>
    <groupId>com.github.f4b6a3</groupId>
    <artifactId>uuid-creator</artifactId>
    <version>5.2.0</version>
</dependency>

4.2. Usage

This library provides us with three ways to generate time-based UUIDs:

  • UuidCreator.getTimeBased() – the time-based version with gregorian epoch specified in RFC-4122
  • UuidCreator.getTimeOrdered() – the time-ordered version with gregorian epoch proposed as a new UUID format
  • UuidCreator.getTimeOrderedEpoch() – the time-ordered version with Unix epoch proposed as a new UUID format

We can use them directly in our code after adding the dependency:

System.out.println("UUID Version 1: " + UuidCreator.getTimeBased());
System.out.println("UUID Version 6: " + UuidCreator.getTimeOrdered());
System.out.println("UUID Version 7: " + UuidCreator.getTimeOrderedEpoch());

And we can see in the output that all three have the same classic UUID format:

UUID Version 1: 0da151ed-c82d-11ed-a2f6-6748247d7506
UUID Version 6: 1edc82d0-da0e-654b-9a98-79d770c05a84
UUID Version 7: 01870603-f211-7b9a-a7ea-4a98f5320ff8

This article will focus on the getTimeBased() method that uses the traditional version 1 UUID. It has three parts: timestamp, clock sequence, and node identifier.

Time-based UUID structure

 00000000-0000-v000-m000-000000000000
|1-----------------|2---|3-----------|

1: timestamp
2: clock-sequence
3: node identifier

4.3. Benchmark

In this section, we’ll run the benchmark from the previous part, but we’ll generate de UUIDs with the UuidCreator.getTimeBased() methodAfter that, we get the result:

12800000 UUIDs generated, 0 collisions in 2595ms

We can see that this algorithm also managed to generate all the UUIDs without duplicates. Besides this, it even managed to get a better time than the JDK one. This is just a basic benchmark, though there are more detailed benchmarks available.

5. Java UUID Generator (JUG)

5.1. Dependency

Java UUID Generator (JUG) is a set of Java classes for working with UUIDs. It includes generating UUIDs using standard methods, efficiently outputting, sorting, etc. It generates UUIDs according to the UUID specification (RFC-4122).

To use the library, we should add the Maven dependency:

<dependency>
    <groupId>com.fasterxml.uuid</groupId>
    <artifactId>java-uuid-generator</artifactId>
    <version>4.1.0</version>
</dependency>

5.2. Usage

This library also provides three methods to create time-based UUIDs (the classic version 1 and the new versions 6 and 7). We can generate them by selecting a kind of generator and then calling its generate() method:

System.out.println("UUID Version 1: " + Generators.timeBasedGenerator().generate());
System.out.println("UUID Version 6: " + Generators.timeBasedReorderedGenerator().generate());
System.out.println("UUID Version 7: " + Generators.timeBasedEpochGenerator().generate());

Then we can check the UUIDs in the console:

UUID Version 1: e6e3422c-c82d-11ed-8761-3ff799965458
UUID Version 6: 1edc82de-6e34-622d-8761-dffbc0ff00e8
UUID Version 7: 01870609-81e5-793b-9e4f-011ee370187b

5.3. Benchmark

Like the previous section, we’ll focus on the first variant of UUID offered by this library. We can also test the chance of a collision by replacing the generation of the UUID from the previous example with the following:

UUID uuid = Generators.timeBasedGenerator().generate();

And after running the code, we can see the results:

12800000 UUIDs generated, 0 collisions in 15795ms

From this, we can see that we also got no duplicates of UUID, like in the previous example. But also, we see a difference in the execution time. Even if the difference seems large, both libraries generated many IDs quickly.

The documentation of this library tells us that generation speed is unlikely to be a bottleneck, and choosing based on the stability of packages and API is better.

6. Conclusion

In this tutorial, we saw the time-based UUIDs’ structure, advantages, and disadvantages. We implemented them in our code using two of the most popular libraries for UUIDs and compared them afterward.

We saw that choosing a type of UUID or library might depend on our needs.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – LS – NPI (cat=Java)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)
3 Comments
Oldest
Newest
Inline Feedbacks
View all comments