Globally Unique, Monotonically Increasing Event IDs

2.0.1 · active · verified Sun Apr 19

eventid is a JavaScript/TypeScript utility for generating unique event identifiers that are globally unique across a network of services and monotonically increasing locally on each machine. Currently at stable version 2.0.1, the library typically receives updates for Node.js compatibility and internal dependency management, with new major versions signifying breaking changes like increased minimum Node.js requirements. It provides a robust alternative to standard JavaScript timestamps, which often lack the necessary millisecond resolution for accurately ordering rapidly occurring events within distributed systems. Its core differentiation lies in producing lexicographically comparable IDs that maintain local monotonicity while guaranteeing global uniqueness, thereby facilitating event ordering across diverse services without relying on imprecise timestamp comparisons.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates instantiation of EventId, generation of unique IDs, and verification of their local monotonic increase. It highlights how different instances ensure global uniqueness while individual instances maintain local order.

import EventId from 'eventid';

// Instantiate a generator. A single instance is sufficient for an application.
const eventIdGenerator = new EventId();

// Generate a globally unique identifier.
const id1: string = eventIdGenerator.new(); 
console.log(`Generated ID 1: ${id1}`);

// Subsequent calls from the same generator instance will produce
// monotonically increasing local IDs.
const id2: string = eventIdGenerator.new();
console.log(`Generated ID 2: ${id2}`);

// Verify monotonic increase locally
if (id1 < id2) {
  console.log('id1 is lexicographically smaller than id2, as expected.');
} else {
  console.error('ID generation is not monotonically increasing locally!');
}

// Another instance (e.g., in a different service or process) will use
// a different base GUID, ensuring global uniqueness.
const anotherGenerator = new EventId();
const id3: string = anotherGenerator.new();
console.log(`Generated ID 3 (from another generator): ${id3}`);

// Example of how they compare - generally, IDs from different generators
// are not necessarily ordered relative to each other.
// But IDs from the *same* generator are strictly ordered.

view raw JSON →