Galera Cluster extends MySQL and MariaDB with synchronous multi-master replication. Auto-increment IDs require careful offset configuration to avoid conflicts across writers. UUID v7 sidesteps this entirely — each application node generates keys independently with no coordination. See UUID in MySQL for the storage guide.

BINARY(16) table schema

CREATE TABLE orders (
  id         BINARY(16)   NOT NULL DEFAULT (UNHEX(REPLACE(UUID(), '-', ''))),
  customer_id BINARY(16)  NOT NULL,
  total      DECIMAL(10,2) NOT NULL,
  status     VARCHAR(20)  NOT NULL DEFAULT 'pending',
  created_at TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  INDEX idx_customer (customer_id)
) ENGINE=InnoDB;

Insert with UUID v7 from Node.js

import mysql2 from 'mysql2/promise';
import { v7 as uuidv7 } from 'uuid';

const pool = mysql2.createPool({ uri: process.env.DATABASE_URL });

// UUID v7 string → BINARY(16) insert
async function createOrder(customerId: string, total: number) {
  const id = uuidv7();
  const binaryId = Buffer.from(id.replace(/-/g, ''), 'hex');
  const binaryCid = Buffer.from(customerId.replace(/-/g, ''), 'hex');

  await pool.execute(
    'INSERT INTO orders (id, customer_id, total) VALUES (?, ?, ?)',
    [binaryId, binaryCid, total]
  );
  return id; // Return string UUID for API response
}

// Read — convert BINARY(16) back to UUID string
async function getOrder(id: string) {
  const binaryId = Buffer.from(id.replace(/-/g, ''), 'hex');
  const [rows] = await pool.execute(
    'SELECT LOWER(HEX(id)) AS id, total, status FROM orders WHERE id = ?',
    [binaryId]
  );
  return (rows as any[])[0] ?? null;
}

Cursor pagination — UUID v7 B-tree scan

// UUID v7 cursor pagination — binary comparison = chronological order
async function listOrders(cursor?: string, limit = 20) {
  const query = cursor
    ? 'SELECT LOWER(HEX(id)) AS id, total, status FROM orders WHERE id > UNHEX(?) ORDER BY id LIMIT ?'
    : 'SELECT LOWER(HEX(id)) AS id, total, status FROM orders ORDER BY id LIMIT ?';

  const params = cursor
    ? [cursor.replace(/-/g, ''), limit]
    : [limit];

  const [rows] = await pool.execute(query, params);
  const items = rows as any[];
  const nextCursor = items.length === limit ? items.at(-1)?.id : null;
  return { items, nextCursor };
}

Galera write conflict comparison

ID strategyMulti-master conflictsIndex localityClock dependency
AUTO_INCREMENTYes (without offset config)ExcellentNone
UUID v4NonePoor (random splits)None
UUID v7NoneGood (time-ordered)Wall clock
ULIDNoneGood (time-ordered)Wall clock

Further reading

External references

Frequently asked questions

Can I use UUID v7 as a primary key in MySQL?

Yes. Store UUID v7 as CHAR(36) or BINARY(16). The BINARY(16) form saves 20 bytes per row and keeps InnoDB page splits lower than UUID v4 because UUID v7 is time-ordered. Use UNHEX(REPLACE(uuid_v7, '-', '')) to insert and LOWER(HEX(id)) to read. See UUID in MySQL for the full guide.

Why does UUID v7 fix Galera Cluster write conflicts?

Galera Cluster uses optimistic concurrency — rows with conflicting primary keys across nodes cause certification failures. Auto-increment IDs collide across nodes without auto_increment_offset tuning. UUID v7 is generated client-side with 74 random bits, making key collisions statistically impossible. See UUID v7 in Distributed Systems.

How does UUID v7 improve MySQL index performance over UUID v4?

InnoDB clusters rows by primary key. UUID v4 (fully random) causes InnoDB B-tree page splits on every insert — the new row rarely lands at the end of the index. UUID v7's 48-bit timestamp prefix means new rows are nearly always appended near the end of the B-tree, reducing page splits and improving insert throughput. See UUID v4 vs v7.