Codementor Events

Generating Distributed UUID’s using Zookeeper

Published Jul 30, 2021

I have been exploring how to generate Sequence Ids in an environment where the applications are distributed and decoupled.

In my case, I had to generate UUIDs in sequential order. As we all know that many distributed systems use Zookeeper to manage the processes for various use-cases.

Quoting from the documentation:

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them, which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.

Therefore, I am using Zookeeper as a centralized sequence generator for scheduling a pool of jobs or tasks.

Zookeeper provides two types of modes to create a sequential ZNode.

EPHEMERAL_SEQUENTIAL mode: The node gets created when the client connects with the server and once the client disconnects the node will be deleted. The name is appended with a monotonically increasing number.

PERSISTENT_SEQUENTIAL mode: The node gets created when the client connects with the server and once the client disconnects the node will not be deleted automatically. The name is appended with a monotonically increasing number.

I am using the EPHEMERAL_SEQUENTIAL mode for creating sequential UUIDs in our application. Here is a sample code which generates sequential ID’s using zookeeper.

package com.practice.sequences;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;


//References for writing this code snippet for creating a distributed sequence generator are as below :
// https://stackoverflow.com/questions/10338076/zookeeper-persistent-sequential-incrementing-by-two/36927266#36927266
// https://www.mail-archive.com/zookeeper-user@hadoop.apache.org/msg01967.html
public class DistributedSequenceGenerator {

    private static ZooKeeper zookeeper;

    //Latch is used to make the main thread wait until the zookeeper    client connects with the ensemble.
    private CountDownLatch latch = new CountDownLatch(0);

    public static void main(String[] args){
        DistributedSequenceGenerator sequenceGenerator = new DistributedSequenceGenerator();
        try{
            zookeeper = sequenceGenerator.connect("localhost",2181);
            String out = zookeeper.create("/sequence","test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            System.out.println(out);
            sequenceGenerator.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public ZooKeeper connect(String host,int port) throws IOException, InterruptedException {
        zookeeper = new ZooKeeper(host, port, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                    if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
                        latch.countDown();
                    }
            }
        });

        latch.await();
        return zookeeper;
    }

    public void close() throws InterruptedException{
        zookeeper.close();
    }
}
Discover and read more posts from Phani Kumar Yadavilli
get started
post commentsBe the first to share your opinion
Show more replies