DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Coordinating an Apache Ignite Cluster With GridGain Control Center
  • Stateful Microservices With Apache Ignite
  • Apache Ignite — More Than Just a Data Grid
  • Monitoring Apache Ignite Cluster With Grafana (Part 1)

Trending

  • The Impact of Biometric Authentication on User Privacy and the Role of Blockchain in Preserving Secure Data
  • Spring Boot 3.2: Replace Your RestTemplate With RestClient
  • Types of Data Breaches in Today’s World
  • Building Safe AI: A Comprehensive Guide to Bias Mitigation, Inclusive Datasets, and Ethical Considerations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Exploring Four Unique Key Generation Strategies in GridGain and Apache Ignite

Exploring Four Unique Key Generation Strategies in GridGain and Apache Ignite

Creating "autoincrement" fields in a distributed system such as Apache Ignite is difficult. In this piece, we explore four different options and their tradeoffs.

By 
Stephen Darlington user avatar
Stephen Darlington
·
Feb. 29, 24 · Tutorial
Like (1)
Save
Tweet
Share
2.3K Views

Join the DZone community and get the full member experience.

Join For Free

Some databases have a feature where a column can be “autoincremented.” In this piece, we’ll explain why GridGain and Apache Ignite do not and what you can do to get equivalent functionality.

The naming varies, but the concept is straightforward: the system automatically generates a key for you if there is no unique business key for a table. Typically, this would be a numeric column, and the data would increase the value by one every time you insert a new record. MySQL and MariaDB have specific SQL syntax for “AUTO_INCREMENT.” SQL Server has a special data type called IDENTITY. Oracle used to make you use a sequence and a trigger, but it has made it a bit easier in recent years.

GridGain does not have an equivalent syntax. If we see this requirement frequently, why not? The short story is that having a single incrementing value is challenging to implement efficiently in a distributed system. Having to maintain state means that you need to have a single, global counter (or replicate it to all nodes). And anything “global” means that it’s not scalable, either because you have a single source of truth or because you need to surround any “next value” operation with expensive locks.

The good news is that several different ways to build an automated key exist. Which is the best for your use case depends on your requirements.

Sequences

GridGain does have sequences that you can use as your key.

Java
 
var counter1 = ignite.atomicSequence("SEQ", 0, true);
var next1 = counter1.getAndIncrement();

var counter2 = ignite.atomicLong("SEQ2", 0, true);
var next2 = counter2.getAndIncrement();


They are called an atomic sequence and an atomic long. As you can see, they look very similar, but there is a significant difference.

The IgniteAtomicLong operates pretty much as I described above: each time you request the next value, it reaches out to a singleton on another node, increments the value, and returns the value back to the originating node. On the plus side, it means that when you call it twice, the second value is guaranteed to be larger than the first. On the negative side, if you have multiple writers, they will end up waiting for each other. Oddly, writing the data to the cluster can be performed entirely in parallel, but generating the next sequence number requires a singleton!

If the keys need to be unique but not necessarily continually increasing, you can use the IgniteAtomicSequence. When you get a reference to the sequence, you also get allocated a block of numbers (by default, 1000). In this way, your client only needs to talk to the rest of the cluster when it has exhausted that range. Of course, this means that if you have two clients writing to the cluster, the first will start from zero, and the second will start from a thousand. The numbers will be unique but not consecutive.

UUID

If your key only needs to be unique, there’s a better way of creating a key: the UUID. A UUID can be made in an entirely distributed manner without talking to any other node in the cluster. GridGain even “understands” UUIDs, meaning that they’re stored efficiently and can be seamlessly converted to a platform’s native format. For example, I can write into my cluster using a Java UUID and then read the same value as a GUID from a .NET client.

Java
 
try (var ds = ignite.<UUID,Person>dataStreamer("PERSON")) {
    for (var i = 0; i < 1000; i++) {
        ds.addData(UUID.randomUUID(),
                   Person.builder()
                           .name("Person " + i)
                           .height(160 + rnd.nextInt(40))
                           .build());
    }
}


Using a UUID requires no special GridGain magic, just the standard java.util.UUID class.

TSID

But what if you need to maintain some kind of ordering and want multiple clients to write to a cluster, and you want it to be distributed?

There is an answer to this. There’s a specification for a Universally Unique Lexicographically Sortable Identifier, and multiple implementations exist. I picked tsid-creator for the sample code below. 

Java
 
try (var ds = ignite.<String,Person>dataStreamer("PERSON")) {
    var tsidGen = new TsidFactory(1);
    for (var i = 0; i < 1000; i++) {
      ds.addData(tsidGen.create().toString(),
                 Person.builder()
                         .name("Person " + i)
                         .height(160 + rnd.nextInt(40))
                         .build());
    }
}


Code-wise, it’s basically identical to the UUID example. The difference is that a TSID can be saved as a long or a string by specifying a node id (the “1” in the constructor), there can’t be clashes between clients, and it’s possible to work backward to get a timestamp.

Sortable, unique, and distributed. What’s not to like?

Conclusion

In this piece, we’ve seen four ways to create unique keys you can use in your application. Each has different strengths and weaknesses, and all are valid approaches that we’ve seen with various clients. Let us know if you have found any other promising approaches.

Apache Ignite Database

Published at DZone with permission of Stephen Darlington. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Coordinating an Apache Ignite Cluster With GridGain Control Center
  • Stateful Microservices With Apache Ignite
  • Apache Ignite — More Than Just a Data Grid
  • Monitoring Apache Ignite Cluster With Grafana (Part 1)

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: