Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8266709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:15:30+00:00 2026-06-08T05:15:30+00:00

Having one puzzling requirement. Basically I need to create unique id with these criteria

  • 0

Having one puzzling requirement.

Basically I need to create unique id with these criteria

  • 9 digits number, unique for the day (means it’s ok if the number appears again the next day )
  • generated in realtime ; java only ( means no sequence number generation from database -actually no database access at all )
  • the number is generated to populate a requestID, and around 1.000.000 id will be generated per day.
  • UUID or UID should not be used ( more than 9 digits )

Here is my consideration :

  • using sequence number sounds good, but in case JVM restart, the
    requestId might be re-generated.
  • using time HHmmssSSS ( Hour Minute Second Milliseconds ) have 2 issues :

a. System Hour might be changed by server admin.
b. Can cause issue
if 2 requests being asked on same milliseconds.

Any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:15:31+00:00Added an answer on June 8, 2026 at 5:15 am

    Nine digits to handle 1,000,000 IDs gives us three digits to play with (we need the other six for the 0-999999 for the ID).

    I assume you have a multi-server setup. Assign each server a three-digit server ID, and then you can allocate unique ID values within each server without worrying about overlap between them. It can just be an ever-increasing value in memory, except to survive JVM restarts, we need to echo the most recently allocated value to disk (well, to anywhere you want to store it — local disk, memcache, whatever).

    To ensure you don’t hit the overhead of file/whatever I/O on each request, you allocate the IDs in blocks, echoing the endpoint of the block back to the storage.

    So it ends up being:

    • Give each server an ID
    • Have storage on the server which stores the last allocated value for the day (a file, for instance)
    • Have the ID allocator work in blocks (10 IDs at a time, 100, whatever)
    • To allocate a block:
      • Read the file, write back a number increased by your blocksize
    • Use IDs from the block
    • The ID would be , e.g. 12000000027 for the 28th ID allocated by server #12
    • When the day changes (e.g., midnight), throw away your current block and allocate a new one for the new day

    In pseudocode:

    class IDAllocator {
        Storage storage;
        int     nextId;
        int     idCount;
        int     blockSize;
        long    lastIdTime;
    
        /**
         * Creates an IDAllocator with the given server ID, backing storage,
         * and block size.
         *
         * @param   serverId        the ID of the server (e.g., 12)
         * @param   s               the backing storage to use
         * @param   size            the block size to use
         * @throws  SomeException   if something goes wrong
         */
        IDAllocator(int serverId, Storage s, int size)
        throws SomeException {
    
            // Remember our info
            this.serverId = serverId * 1000000; // Add a million to make life easy
            this.storage = s;
            this.nextId = 0;
            this.idCount = 0;
            this.blockSize = bs;
            this.lastIdTime = this.getDayMilliseconds();
    
            // Get the first block. If you like and depending on
            // what container this code is running in, you could
            // spin this out to a separate thread.
            this.getBlock();
        }
    
        public synchronized int getNextId()
        throws SomeException {
            int id;
    
            // If we're out of IDs, or if the day has changed, get a new block
            if (idCount == 0 || this.lastIdTime < this.getDayMilliseconds()) {
                this.getBlock();
            }
    
            // Alloc from the block    
            id = this.nextId;
            --this.idCount;
            ++this.nextId;
    
            // If you wanted (and depending on what container this
            // code is running in), you could proactively retrieve
            // the next block here if you were getting low...
    
            // Return the ID
            return id + this.serverId;
        }
    
        protected long getDayMilliseconds() {
            return System.currentTimeMillis() % 86400000;
        }
    
        protected void getBlock()
        throws SomeException {
            int id;
    
            synchronized (this) {
                synchronized (this.storage.syncRoot()) {
                    id = this.storage.readIntFromStorage();
                    this.storage.writeIntToStroage(id + blocksize);
                }
    
                this.nextId = id;
                this.idCount = blocksize;
            }
        }
    }
    

    …but again, that’s pseudocode, and you might want to throw some proactive stuff in there so you never block on I/O waiting for an ID when you need one.

    The above is written assuming you already have some kind of application-wide singleton, and the IDAllocator instance would just be a data member in that single instance. If not, you could readily make the above a singleton instead, by giving it the classic getInstance method and having it read its configuration from the environment rather than receiving it as arguments to the constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having one requirement for locking the entire dataset. Is it possible to
I'm having some weird behavior in an application which is puzzling me. I create
I am having one hell of a time coming up with a decent way
I am having one heck of a hard time trying to figure this out.
I am having one problem with the PHP json_encode function. It encodes numbers as
I am having one Gtk+ and C application in which i want to set
I'm having one hell of a time trying to get my databinding to work
I'm having one doubt about the VIM ENCRYPTION key . I having a text
I am having one GtkVbox and I am adding it to GtkViewPort . View
I am having one flow field manager added on screen & in that manager

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.