跳至内容
LoadUp Components Global Unique

LoadUp Components Global Unique

基于 PostgreSQL 唯一索引的租户级幂等声明组件;单一 jar 随应用部署,业务侧只注入 GlobalUniqueTemplate。

引入

<dependency>
    <groupId>io.github.loadup-cloud</groupId>
    <artifactId>loadup-components-globalunique</artifactId>
</dependency>

使用

@Transactional
public void createOrder(OrderCreateCommand command) {
    var claim = new GlobalUniqueClaim("ORDER_CREATE", command.orderNo(), command.orderNo(), null);
    if (!globalUniqueTemplate.claim(claim)) {
        return;
    }
    // Business writes must use the same transaction.
}

配置

loadup:
  global-unique:
    enabled: true
  database:
    schema: loadup_payment  # fixed business schema
      enabled: false
  flyway:
    locations: classpath:db/migration

API 在本部署范围内声明幂等键;数据库唯一维度为 biz_type + unique_key,不含 tenant_id。一套部署只服务一个租户并固定使用 loadup.database.schema,因此幂等记录天然只属于该部署。组件显式设置 ID、审计时间和删除标记。数据库隔离现状见 database README。

能力矩阵

能力 支持
当前 schema 内并发幂等 ✓ PostgreSQL 唯一索引
业务元数据、请求快照、记录查询 ✓
事务回滚后重试 ✓ 同一 Spring 事务
ID、审计、逻辑删除 ✓ database 组件统一提供
过期、自动归档、缓存 ✗ 由业务维护

设计与实现

Scope

The component is a single-jar idempotency primitive. It owns claim semantics and the global_unique schema; the database component provides IDs, a clock and tenant configuration.

Runtime flow

business transaction
      │
      └─ GlobalUniqueTemplate.claim
              │
              ├─ allocate ID and populate audit fields
              ├─ insert with ON CONFLICT (biz_type, unique_key) DO NOTHING
              └─ PostgreSQL unique index decides first claim or replay

Only a conflict on the specified business key is treated as a replay. Other constraint failures propagate to the caller. An inactive row that still owns the key is an integrity error.

Uniqueness contract

The current database constraint is:

UNIQUE (biz_type, unique_key)

The API declares idempotency inside one deployment. The persisted row and unique index contain no tenant column: a deployment serves one tenant through the fixed loadup.database.schema, so records belong to that deployment by construction. The fixed schema and database privileges are described by the database component.

bizType and uniqueKey are trimmed and required. Their maximum lengths are 64 and 255 characters. bizId and requestData are diagnostic data and do not participate in uniqueness.

Transaction boundary

The template does not start a transaction. Callers that need claim rollback after business failure must invoke claim inside the same Spring transaction as their business writes. A claim made without an outer transaction commits as a standalone idempotency record.

Persistence

Spring JDBC uses the Spring-managed connection and an explicit soft-delete predicate. The PostgreSQL initial migration contains the standard columns, including a Boolean deletion flag; it does not store or filter a tenant_id column.