Bembew Programming
Home Topics Guides
Bembew Programming
Home Topics Guides

HTML Basics Learning Guide

HTML Basics Learning Guide
    • A Comprehensive Guide to HTML Tags
    • Configuring the head in VuePress
    • Controlling Access with the `meta` Attribute in Advanced Vue Router Scenarios
    • Distributed IDs
    • Text Tags: Title, Paragraph, Link
    • Image Tags: img and Image Attributes
    • List Tags: Unordered Lists and Ordered Lists
    • Table Tags: Tables, Rows, and Cells
    • Form Labels: Input Fields, Text Areas, and Buttons
    • The Role of the Tag</a></li> <li><a href="/en/article/content-inside-the-body-tag.html?guide-code=html" class="sidebar-article-link">Content Inside the <body> Tag</a></li> <li><a href="/en/article/using-the-header-tag.html?guide-code=html" class="sidebar-article-link">Using the <header> Tag</a></li> <li><a href="/en/article/using-the-nav-tag.html?guide-code=html" class="sidebar-article-link">Using the <nav> Tag</a></li> <li><a href="/en/article/using-the-main-tag.html?guide-code=html" class="sidebar-article-link">Using the <main> Tag</a></li> <li><a href="/en/article/using-the-article-tag.html?guide-code=html" class="sidebar-article-link">Using the <article> Tag</a></li> <li><a href="/en/article/using-the-aside-tag.html?guide-code=html" class="sidebar-article-link">Using the <aside> Tag</a></li> <li><a href="/en/article/using-the-footer-tag.html?guide-code=html" class="sidebar-article-link">Using the <footer> Tag</a></li> <li><a href="/en/article/using-the-class-attribute-in-html.html?guide-code=html" class="sidebar-article-link">Using the class Attribute in HTML</a></li> <li><a href="/en/article/using-the-style-attribute-in-css.html?guide-code=html" class="sidebar-article-link">Using the style Attribute in CSS</a></li> <li><a href="/en/article/using-the-src-attribute.html?guide-code=html" class="sidebar-article-link">Using the src Attribute</a></li> <li><a href="/en/article/using-the-href-attribute.html?guide-code=html" class="sidebar-article-link">Using the href Attribute</a></li> <li><a href="/en/article/secure-usage-of-the-html5-canvas-element.html?guide-code=html" class="sidebar-article-link">Secure Usage of the HTML5 Canvas Element</a></li> <li><a href="/en/article/css-selectors.html?guide-code=html" class="sidebar-article-link">CSS Selectors</a></li> <li><a href="/en/article/mobile-adaptation.html?guide-code=html" class="sidebar-article-link">Mobile Adaptation</a></li> <li><a href="/en/article/image-optimization.html?guide-code=html" class="sidebar-article-link">Image Optimization</a></li> <li><a href="/en/article/installing-and-configuring-node-js-for-nuxt-js-vue-react-next-js-rust-mongodb-sql-vuex.html?guide-code=html" class="sidebar-article-link">Installing and Configuring Node.js for Nuxt.js, Vue, React, Next.js, Rust, MongoDB, SQL, Vuex</a></li> <li><a href="/en/article/html5-overview.html?guide-code=html" class="sidebar-article-link">HTML5 Overview</a></li> <li><a href="/en/article/semantic-html5-tags.html?guide-code=html" class="sidebar-article-link">Semantic HTML5 Tags</a></li> <li><a href="/en/article/form-elements-input-textarea-select.html?guide-code=html" class="sidebar-article-link">Form Elements (Input, Textarea, Select)</a></li> <li><a href="/en/article/multimedia-elements-audio-and-video.html?guide-code=html" class="sidebar-article-link">Multimedia Elements (Audio and Video)</a></li> <li><a href="/en/article/html5-form-structure.html?guide-code=html" class="sidebar-article-link">HTML5 Form Structure</a></li> <li><a href="/en/article/form-validation-required-minlength-maxlength.html?guide-code=html" class="sidebar-article-link">Form Validation (required, minlength, maxlength)</a></li> <li><a href="/en/article/custom-form-validation.html?guide-code=html" class="sidebar-article-link">Custom Form Validation</a></li> <li><a href="/en/article/client-side-and-server-side-validation.html?guide-code=html" class="sidebar-article-link">Client-Side and Server-Side Validation</a></li> <li><a href="/en/article/using-audio-and-video-tags.html?guide-code=html" class="sidebar-article-link">Using Audio and Video Tags</a></li> <li><a href="/en/article/controlling-the-video-player.html?guide-code=html" class="sidebar-article-link">Controlling the Video Player</a></li> <li><a href="/en/article/html5-animation-with-canvas-api.html?guide-code=html" class="sidebar-article-link">HTML5 Animation with Canvas API</a></li> <li><a href="/en/article/overview-of-responsive-design.html?guide-code=html" class="sidebar-article-link">Overview of Responsive Design</a></li> <li><a href="/en/article/the-viewport-tag-in-html.html?guide-code=html" class="sidebar-article-link">The Viewport Tag in HTML</a></li> <li><a href="/en/article/media-queries.html?guide-code=html" class="sidebar-article-link">Media Queries</a></li> <li><a href="/en/article/responsive-design.html?guide-code=html" class="sidebar-article-link">Responsive Design</a></li> <li><a href="/en/article/ui-design-principles.html?guide-code=html" class="sidebar-article-link">UI Design Principles</a></li> <li><a href="/en/article/combining-html-and-css.html?guide-code=html" class="sidebar-article-link">Combining HTML and CSS</a></li> <li><a href="/en/article/essential-web-design-techniques.html?guide-code=html" class="sidebar-article-link">Essential Web Design Techniques</a></li> <li><a href="/en/article/canvas-api.html?guide-code=html" class="sidebar-article-link">Canvas API</a></li> <li><a href="/en/article/svg-graphics.html?guide-code=html" class="sidebar-article-link">SVG Graphics</a></li> <li><a href="/en/article/accessibility-testing.html?guide-code=html" class="sidebar-article-link">Accessibility Testing</a></li> </ul> </li> </ul> </div> </div> </aside> <main class="main-content sidebar-content toc-content"> <div class="article-content"> <nav class="breadcrumb-nav" aria-label="breadcrumb"> <ol class="breadcrumb"> <li class="breadcrumb-item"><a href="/en/guide/index.html">Guides</a></li> <li class="breadcrumb-item"><a href="/en/guide/html.html">HTML Basics Learning Guide</a></li> <li class="breadcrumb-item active">Distributed IDs</li> </ol> </nav> <h1>Distributed IDs</h1> <div class="article-body"><p>When building large-scale microservice architectures, the strategy for generating Global Unique Identifiers (GUIDs) serves as the cornerstone of system design. While traditional UUID schemes guarantee global uniqueness, their 128-bit length brings limitations such as high storage overhead, increased bandwidth consumption, and difficulty in performing range queries. In contrast, Distributed IDs aim to address these pain points. They typically consist of a shorter random component and an ordered time-series component, ensuring uniqueness while supporting time-based sorting, which significantly enhances database indexing efficiency and business query performance. However, implementing a high-performance, low-latency, and globally unique Distributed ID is not trivial; it requires a deep understanding of clock synchronization, the principles behind the Snowflake algorithm, and system fault tolerance mechanisms.</p> <h2 id="core-design-principles-the-combination-of-time-and">Core Design Principles: The Combination of Time and Randomness</h2> <p>The core logic for generating Distributed IDs generally follows the formula "Timestamp + Machine ID + Sequence Number". This design first ensures the temporal ordering of IDs, facilitating sorting or pagination based on creation time within business logic; secondly, by introducing a Machine ID (node identifier) and an auto-incrementing sequence number, it effectively resolves the issue of duplicate IDs generated during concurrent writes across multiple nodes.</p> <p>Specifically, a complete Distributed ID structure typically includes the following elements:</p> <ul> <li><strong>Timestamp</strong>: Records the precise millisecond at which the ID was generated, ensuring that IDs generated at different moments have a chronological order.</li> <li><strong>Machine Identifier (MachineID)</strong>: Uniquely identifies the server node generating the ID, preventing duplicate IDs from being generated by different nodes at the same moment.</li> <li><strong>Sequence Number</strong>: Within the same millisecond, a single node may generate multiple IDs; the auto-incrementing counter guarantees uniqueness at the microsecond or nanosecond level.</li> </ul> <p>To address clock desynchronization issues caused by network jitter, excellent Distributed ID schemes must incorporate a "Clock Offset" mechanism. When the system detects that the current time is earlier than the time when the last ID was generated, it should not write directly but rather wait for a period until time advances forward, thereby avoiding the generation of duplicate IDs.</p> <h2 id="classic-implementation-scheme-snowflake-algorithm-">Classic Implementation Scheme: Snowflake Algorithm Analysis</h2> <p>The Distributed ID generation scheme currently adopted most widely in the industry is Alibaba's open-source "Snowflake" algorithm. This algorithm cleverly utilizes a 64-bit long integer, dividing it into five parts, with the specific structure as follows:</p> <ol> <li><strong>Sign Bit</strong>: Occupies 1 bit, used to identify the sign (positive or negative).</li> <li><strong>Timestamp</strong>: Occupies 41 bits, representing a millisecond-level timestamp. This allows the algorithm to support a time span of approximately 6800 years ($2^{41}$ milliseconds), which is sufficient to meet the lifecycle requirements of the vast majority of systems.</li> <li><strong>Machine Identifier</strong>: Occupies 5 bits. In practical applications, this part is often extended to 12 bits or more to support a larger number of server nodes.</li> <li><strong>Data Center Identifier</strong>: Occupies 5 bits, used to distinguish different physical data centers.</li> <li><strong>Sequence Number</strong>: Occupies 12 bits, representing the auto-incrementing count for that node within the same millisecond, with a maximum value of 4096.</li> </ol> <p>Below is a code snippet demonstrating the core logic of the Snowflake algorithm implemented in Java, showing how to calculate and generate an ID:</p> <pre><code class="language-java">public class SnowflakeIdWorker { private final long epochStart; // Start time point private final long dataCenterId; private final long workerId; private long sequence = 0L; private final long timestamp; public SnowflakeIdWorker(long dataCenterId, long workerId, long epochStart) { this.dataCenterId = dataCenterId; this.workerId = workerId; this.epochStart = epochStart; this.timestamp = getTimestamp(); } private long getTimestamp() { return System.currentTimeMillis(); } public long nextId() { long timestamp = getTimestamp(); // Clock offset check: If the current time is earlier than the time when the last ID was generated, wait if (timestamp < this.timestamp) { throw new RuntimeException("Clock moved backwards"); } // Reset sequence number and update timestamp this.sequence = (this.sequence + 1) & 0x3FFFFFFF; // Modulo 2^21, max sequence number is 4096 if (this.sequence == 0) { timestamp = getTimestamp(); // Check clock offset again if (timestamp < this.timestamp) { throw new RuntimeException("Clock moved backwards"); } } long id = ((timestamp - epochStart) << 22) | (dataCenterId << 10) | (workerId << 12) | sequence; this.timestamp = timestamp; return id; } } </code></pre> <h2 id="optimization-and-precautions-in-production-environ">Optimization and Precautions in Production Environments</h2> <p>In actual production environments, relying solely on the Snowflake algorithm may face issues such as clock drift, network latency, and sequence number exhaustion under high concurrency. Therefore, designers need to consider additional optimization strategies.</p> <p>First, <strong>Clock Synchronization Mechanisms</strong> are critical. Although the Snowflake algorithm includes simple offset logic, in extreme cases (such as server restarts or long periods of inactivity), it is still necessary to ensure that all nodes have accurate time sources. It is generally recommended to synchronize system time with an NTP server, or adopt a clock synchronization service similar to Google Chubby in distributed systems to coordinate the time across nodes.</p> <p>Secondly, <strong>Handling Sequence Number Overflow</strong>. When the request volume within the same millisecond exceeds 4096, the sequence number will wrap around to 0. While this theoretically does not lead to ID duplication (since the timestamp changes), it may temporarily reduce performance in extreme high-concurrency scenarios. Optimization options include introducing a wider bit width for the sequence number, or adjusting the "time granularity" from millisecond-level to microsecond or nanosecond levels; however, this increases the requirements for hardware clock precision.</p> <p>Finally, <strong>Fault Tolerance and Degradation Strategies</strong>. Distributed systems cannot guarantee 100% availability. During ID generation, if network partitioning prevents access to global state, the system should be capable of local caching or asynchronous retry. For example, the ID generation service can be designed as a highly available cluster; when the primary node fails, it automatically switches to a backup node to continue providing services, while logging abnormal events for subsequent analysis.</p> <p>In summary, Distributed IDs are an indispensable component in microservice architectures. They are not only details of technical implementation but also a comprehensive reflection of system scalability, consistency, and performance. Reasonably selecting and optimizing Distributed ID schemes lays a solid foundation for building highly available and high-performance internet applications.</p> </div> </div> </main> <aside class="toc-sidebar" id="tocSidebar"> <div class="toc-sidebar-inner"> <div class="toc-title">On This Page</div> <nav class="toc-nav"> <ul> <li class="toc-level-2"><a href="#core-design-principles-the-combination-of-time-and">Core Design Principles: The Combination of Time and Randomness</a> </li> <li class="toc-level-2"><a href="#classic-implementation-scheme-snowflake-algorithm-">Classic Implementation Scheme: Snowflake Algorithm Analysis</a> </li> <li class="toc-level-2"><a href="#optimization-and-precautions-in-production-environ">Optimization and Precautions in Production Environments</a> </li> </ul> </nav> </div> </aside> </div> <footer class="site-footer"> <div class="footer-sites"> <span class="footer-sites-label">Recommended Sites</span> <a href="https://math.bembew.com/en/index.html" target="_blank" rel="noopener">Mathematics</a> <span class="footer-sep">·</span> <a href="https://physics.bembew.com/en/index.html" target="_blank" rel="noopener">Physics</a> <span class="footer-sep">·</span> <a href="https://economics.bembew.com/en/index.html" target="_blank" rel="noopener">Economics</a> <span class="footer-sep">·</span> <a href="https://chemistry.bembew.com/en/index.html" target="_blank" rel="noopener">Chemistry</a> <span class="footer-sep">·</span> <a href="https://biology.bembew.com/en/index.html" target="_blank" rel="noopener">Biology</a> </div> <div class="footer-meta"> <a href="/en/info/about-website.html">About This Site</a> <span class="footer-sep">·</span> <a href="/en/info/privacy.html">Privacy Policy</a> <span class="footer-sep">·</span> <span>© 2025-2026 Bembew - All rights reserved</span> </div> </footer> <div id="cookieConsent" class="cookie-consent" role="region" aria-label="Cookie notice" style="display:none"> <div class="cookie-consent-inner"> <p class="cookie-consent-text">This website uses strictly necessary cookies to keep it secure and functioning (e.g., access control and bot protection), and collects limited anonymous technical data via our CDN. We do not use advertising or tracking cookies. See our <a href="/en/info/privacy.html">Privacy Policy</a>.</p> <button id="cookieConsentAccept" class="cookie-consent-btn" type="button">OK</button> </div> </div> <script src="https://cdn.bootcdn.net/ajax/libs/KaTeX/0.16.9/katex.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/KaTeX/0.16.9/contrib/auto-render.min.js"></script> <script> document.addEventListener('DOMContentLoaded', function() { if (typeof renderMathInElement === 'function') { renderMathInElement(document.body, { delimiters: [ {left: '$$', right: '$$', display: true}, {left: '$', right: '$', display: false}, {left: '\\(', right: '\\)', display: false}, {left: '\[', right: '\]', display: true} ], throwOnError: false, strict: false, trust: true }); } }); </script> <script> document.addEventListener('DOMContentLoaded', function() { var headings = document.querySelectorAll('.article-body h1[id], .article-body h2[id], .article-body h3[id], .article-body h4[id], .article-body h5[id], .article-body h6[id]'); var tocLinks = document.querySelectorAll('.toc-nav a'); if (headings.length === 0 || tocLinks.length === 0) return; // Highlight current heading on scroll using IntersectionObserver if ('IntersectionObserver' in window) { var observer = new IntersectionObserver(function(entries) { entries.forEach(function(entry) { if (entry.isIntersecting) { var id = entry.target.getAttribute('id'); tocLinks.forEach(function(link) { var isActive = link.getAttribute('href') === '#' + id; link.classList.toggle('active', isActive); }); } }); }, { rootMargin: '-80px 0px -70% 0px', threshold: 0 }); headings.forEach(function(h) { observer.observe(h); }); } }); </script> <script> document.addEventListener('DOMContentLoaded', function() { // Close language dropdown and mobile menu when clicking outside document.addEventListener('click', function(e) { var langMenu = document.getElementById('langMenu'); var langToggle = document.querySelector('.lang-dropdown-toggle'); if (langMenu && langMenu.classList.contains('show') && !langMenu.contains(e.target) && !langToggle.contains(e.target)) { langMenu.classList.remove('show'); } var mobileMenu = document.getElementById('mobileMenu'); var menuTrigger = document.getElementById('menuTrigger'); if (mobileMenu && mobileMenu.classList.contains('active') && !mobileMenu.contains(e.target) && !menuTrigger.contains(e.target)) { mobileMenu.classList.remove('active'); } }); // Close mobile menu when clicking a link inside it var mobileMenu = document.getElementById('mobileMenu'); if (mobileMenu) { mobileMenu.querySelectorAll('a').forEach(function(a) { a.addEventListener('click', function() { mobileMenu.classList.remove('active'); }); }); } // Close TOC overlay when clicking a link inside it (mobile) var tocSidebar = document.getElementById('tocSidebar'); if (tocSidebar) { tocSidebar.querySelectorAll('a').forEach(function(a) { a.addEventListener('click', function() { closeToc(); }); }); } // Close sidebar overlay when clicking a link inside it (mobile) var articleSidebar = document.getElementById('articleSidebar'); if (articleSidebar) { articleSidebar.querySelectorAll('a').forEach(function(a) { a.addEventListener('click', function() { closeSidebar(); }); }); } // Topic 详情页 Tab 切换(介绍 / 指南) var topicTabs = document.querySelector('.topic-tabs'); if (topicTabs) { var topicTabButtons = topicTabs.querySelectorAll('.topic-tab'); var topicTabPanels = topicTabs.querySelectorAll('.topic-tab-panel'); topicTabButtons.forEach(function(tab) { tab.addEventListener('click', function() { var key = tab.getAttribute('data-tab'); topicTabButtons.forEach(function(btn) { var on = btn === tab; btn.classList.toggle('active', on); btn.setAttribute('aria-selected', on ? 'true' : 'false'); }); topicTabPanels.forEach(function(panel) { panel.classList.toggle('active', panel.getAttribute('data-panel') === key); }); }); }); } }); // Mobile sidebar toggle (article page) function toggleSidebar() { var sidebar = document.getElementById('articleSidebar'); var overlay = document.getElementById('sidebarOverlay'); if (sidebar) sidebar.classList.toggle('open'); if (overlay) overlay.classList.toggle('show'); } function closeSidebar() { var sidebar = document.getElementById('articleSidebar'); var overlay = document.getElementById('sidebarOverlay'); if (sidebar) sidebar.classList.remove('open'); if (overlay) overlay.classList.remove('show'); } // Mobile TOC toggle function toggleToc() { var toc = document.getElementById('tocSidebar'); var overlay = document.getElementById('tocOverlay'); if (toc) toc.classList.toggle('open'); if (overlay) overlay.classList.toggle('show'); } function closeToc() { var toc = document.getElementById('tocSidebar'); var overlay = document.getElementById('tocOverlay'); if (toc) toc.classList.remove('open'); if (overlay) overlay.classList.remove('show'); } </script> <script> (function() { try { if (localStorage.getItem('cookie_consent')) return; var el = document.getElementById('cookieConsent'); var btn = document.getElementById('cookieConsentAccept'); if (!el || !btn) return; el.style.display = 'block'; btn.addEventListener('click', function() { try { localStorage.setItem('cookie_consent', 'v1'); } catch (e) {} el.style.display = 'none'; }); } catch (e) {} })(); </script> <script> !function(p){"use strict";!function(t){var s=window,e=document,i=p,c="".concat("https:"===e.location.protocol?"https://":"http://","sdk.51.la/js-sdk-pro.min.js"),n=e.createElement("script"),r=e.getElementsByTagName("script")[0];n.type="text/javascript",n.setAttribute("charset","UTF-8"),n.async=!0,n.src=c,n.id="LA_COLLECT",i.d=n;var o=function(){s.LA.ids.push(i)};s.LA?s.LA.ids&&o():(s.LA=p,s.LA.ids=[],o()),r.parentNode.insertBefore(n,r)}()}({id:"KNg95ReuAl9m0Oxc",ck:"KNg95ReuAl9m0Oxc"}); </script> </body> </html>