Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: Identify a Nap

      September 23, 2025

      Ambient Animations In Web Design: Principles And Implementation (Part 1)

      September 23, 2025

      Benchmarking AI-assisted developers (and their tools) for superior AI governance

      September 23, 2025

      Digital.ai launches White-box Cryptography Agent to enable stronger application security

      September 23, 2025

      Development Release: MX Linux 25 Beta 1

      September 22, 2025

      DistroWatch Weekly, Issue 1140

      September 21, 2025

      Distribution Release: DietPi 9.17

      September 21, 2025

      Development Release: Zorin OS 18 Beta

      September 19, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Stop using .reverse().find(): meet findLast()

      September 23, 2025
      Recent

      Stop using .reverse().find(): meet findLast()

      September 23, 2025

      @ts-ignore is almost always the worst option

      September 22, 2025

      MutativeJS v1.3.0 is out with massive performance gains

      September 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      How I Configure Polybar to Customize My Linux Desktop

      September 23, 2025
      Recent

      How I Configure Polybar to Customize My Linux Desktop

      September 23, 2025

      Development Release: MX Linux 25 Beta 1

      September 22, 2025

      DistroWatch Weekly, Issue 1140

      September 21, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Tech & Work»CodeSOD: Identify a Nap

    CodeSOD: Identify a Nap

    September 23, 2025

    Guy picked up a bug ticket. There was a Hiesenbug; sometimes, saving a new entry in the application resulted in a duplicate primary key error, which should never happen.

    The error was in the message-bus implementation someone else at the company had inner-platformed together, and it didn’t take long to understand why it failed.

    <span class="hljs-comment">/**
     * This generator is used to generate message ids.
     * This implementation merely returns the current timestamp as long.
     *
     * We are, thus, limited to insert 1000 new messages per second.
     * That throughput seems reasonable in regard with the overall
     * processing of a ticket.
     *
     * Might have to re-consider that if needed.
     *
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">IdGenerator</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">IdentifierGenerator</span>
    {
    
            <span class="hljs-type">long</span> previousId;
           
            <span class="hljs-meta">@Override</span>
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">synchronized</span> Long <span class="hljs-title function_">generate</span> <span class="hljs-params">(SessionImplementor session, Object parent)</span> <span class="hljs-keyword">throws</span> HibernateException {
                    <span class="hljs-type">long</span> <span class="hljs-variable">newId</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>().getTime();
                    <span class="hljs-keyword">if</span> (newId == previousId) {
                            <span class="hljs-keyword">try</span> { Thread.sleep(<span class="hljs-number">1</span>); } <span class="hljs-keyword">catch</span> (InterruptedException ignore) {}
                            newId = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>().getTime();
                    }
                    <span class="hljs-keyword">return</span> newId;
            }
    }
    

    This generates IDs based off of the current timestamp. If too many requests come in and we start seeing repeating IDs, we sleep for a second and then try again.

    This… this is just an autoincrementing counter with extra steps. Which most, but I suppose not all databases supply natively. It does save you the trouble of storing the current counter value outside of a running program, I guess, but at the cost of having your application take a break when it’s under heavier than average load.

    One thing you might note is absent here: generate doesn’t update previousId. Which does, at least, mean we won’t ever sleep for a second. But it also means we’re not doing anything to avoid collisions here. But that, as it turns out, isn’t really that much of a problem. Why?

    Because this application doesn’t just run on a single server. It’s distributed across a handful of nodes, both for load balancing and resiliency. Which means even if the code properly updated previousId, this still wouldn’t prevent collisions across multiple nodes, unless they suddenly start syncing previousId amongst each other.

    I guess the fix might be to combine a timestamp with something unique to each machine, like… I don’t know… hmmm… maybe the MAC address on one of their network interfaces? Oh! Or maybe you could use a sufficiently large random number, like really large. 128-bits or something. Or, if you’re getting really fancy, combine the timestamp with some randomness. I dunno, something like that really sounds like it could get you to some kind of universally unique value.

    Then again, since the throughput is well under 1,000 messages per second, you could probably also just let your database handle it, and maybe not generate the IDs in code.

    [Advertisement]
    Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.

    Source: Read More 

    news
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleAmbient Animations In Web Design: Principles And Implementation (Part 1)
    Next Article Stop using .reverse().find(): meet findLast()

    Related Posts

    Tech & Work

    Ambient Animations In Web Design: Principles And Implementation (Part 1)

    September 23, 2025
    Tech & Work

    Benchmarking AI-assisted developers (and their tools) for superior AI governance

    September 23, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    How to use chatgpt4o to redesign your website

    Web Development

    CVE-2025-6620 – TOTOLINK CA300-PoE Os Command Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-37094 – HPE StoreOnce Directory Traversal File Deletion Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4529 – Seeyon Zhiyuan OA Web Application System Path Traversal Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Artificial Intelligence

    Exploring data and its influence on political behavior

    July 7, 2025

    Data and politics are becoming increasingly intertwined. Today’s political campaigns and voter mobilization efforts are…

    Building Autonomous AI Agents: Unlocking Scalable Growth for Modern Businesses🤖

    July 16, 2025

    CVE-2025-6650 – PDF-XChange Editor U3D File Parsing Out-Of-Bounds Read Information Disclosure

    June 25, 2025

    CVE-2025-52812 – ApusWP Domnoo PHP Local File Inclusion Vulnerability

    June 27, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.