Identifier generation in applications represents a fundamental business requirement. Whether creating purchase orders, users, or any other business entity, each object requires a unique and traceable identifier.
Identifier generation in applications represents a fundamental business requirement. Whether creating purchase orders, users, or any other business entity, each object requires a unique and traceable identifier. This requirement becomes critical when the application begins to scale horizontally (instance multiplication, microservices, etc.).
In conventional applications, auto-incremented identifiers or UUIDs are commonly employed. However, these methods present limitations:
A more structured and secure approach thus becomes necessary to generate robust, non-predictable identifiers compatible with distributed architecture.
A Global ID (GID) constitutes a unique representation of an entity within an application. It enables reliable and traceable identification of any business object (user, order, document, etc.), while accounting for tenant context (for example, an organization or client in a multi-tenant application).
Note: a tenant represents an organization or client in a multi-tenant application (i.e., an application utilized by multiple organizations).
In this article, we will work with a 24-byte GID. This format is non-standard: most technologies utilize 16 bytes, as defined by standards ISO/IEC 11578:1996, ITU-T Rec. X.667, and RFC 4122 (UUID specification).
We choose a 24-byte (192-bit) GID to address specific requirements:
Regarding performance impact: while 24 bytes exceed standard UUID size (16 bytes), the integrated business information enables avoiding numerous SQL joins, substantially offsetting this overhead. Additionally, indexing remains efficient thanks to the predictable structure.
By analogy:
Therefore:
General formula:
We will implement a 24-byte GID with Go, in a multi-tenant system. Each identifier block receives precise allocation to constitute the 24 bytes
| Bytes | Content | Size |
|---|---|---|
| 0-7 | Tenant ID | 8 bytes (64 bits) |
| 8-9 | Entity Type | 2 bytes (16 bits) |
| 10-17 | Timestamp | 8 bytes (64 bits) |
| 18-23 | Random | 6 bytes (48 bits) |
Thus, the GID contains both business information (tenant, type), a timestamp for traceability, and random data guaranteeing global uniqueness.
package gid
import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"fmt"
"strings"
"time"
)
const (
GIDSize = 24 // 192 bits total
)
type (
GID [GIDSize]byte // GID = 24-byte array
TenantID [8]byte // Tenant on 8 bytes
)
package gid
func NewGID(tenantID TenantID, entityType uint16) {
var id GID // initialize a 24-byte array for our GID
}
package gid
func NewGID(tenantID TenantID, entityType uint16) (GID, error) {
var id GID
// 1. Tenant ID (bytes 0 to 7)
// Copy the 8 bytes from tenantID into the first 8 bytes of id
copy(id[0:8], tenantID[:])
// 2. Entity Type (bytes 8 to 9)
// - entityType is a uint16 (16 bits = 2 bytes)
// - Encode it as Big Endian bytes and write it to id[8] and id[9]
binary.BigEndian.PutUint16(id[8:10], entityType)
// 3. Timestamp (bytes 10 to 17)
// - Retrieve the current timestamp in milliseconds
// - Convert it to uint64 (64 bits = 8 bytes)
// - Write these 8 bytes to id[10] through id[17]
now := time.Now().UnixMilli()
binary.BigEndian.PutUint64(id[10:18], uint64(now))
// 4. Random Data (bytes 18 to 23)
// - These 6 bytes serve to guarantee global GID uniqueness
// - Fill them with random bytes
_, err := rand.Read(id[18:24])
if err != nil {
return Nil, fmt.Errorf("failed to generate random bytes: %v", err)
}
return id, nil
}
The code presented here is Go-specific, but the GID concept can be transposed to any programming language.
The primary objective is to visualize how to represent a Global Identifier (GID) informatively, by grouping concrete business data (tenant, entity type, timestamp) while guaranteeing:
Thus, the GID enables creating identifiers that are simultaneously robust, traceable, and business-useful, while remaining optimized for distributed or multi-tenant architecture.