Redis
cosid-spring-redis stores segment counters and machine id leases in Redis, through the application's Spring Data Redis connection (StringRedisTemplate):
SpringRedisIdSegmentDistributor: segment counters, one key per generator,cosid:{namespace.name}.adder.SpringRedisMachineLeaseStore: machine id leases in acosid:{namespace}:leasehash, next to the keys 3.4 used. Lease times come from the RedisTIMEcommand.
Keys carry a hash tag ({...}), so Redis Cluster works: all keys of one counter or one namespace land in one slot.
Installation
kotlin
dependencies {
implementation(platform("me.ahoo.cosid:cosid-bom:$cosidVersion"))
implementation("me.ahoo.cosid:cosid-spring-boot-starter")
implementation("me.ahoo.cosid:cosid-spring-redis")
implementation("org.springframework.boot:spring-boot-starter-data-redis")
}xml
<dependencies>
<dependency>
<groupId>me.ahoo.cosid</groupId>
<artifactId>cosid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>me.ahoo.cosid</groupId>
<artifactId>cosid-spring-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>Configuration
yaml
spring:
data:
redis:
host: localhost # the connection CosId uses
cosid:
namespace: ${spring.application.name}
machine:
enabled: true # for Snowflake IDs and CosIdGenerator
distributor:
type: redis
segment:
enabled: true # for segment IDs
distributor:
type: redis| Property | Default | Description |
|---|---|---|
cosid.segment.distributor.redis.timeout | 1s | Each segment command; 0 disables it |
cosid.machine.distributor.redis.timeout | 1s | Lease renewal and release; must be positive |
cosid.machine.distributor.redis.distribute-timeout | 10s | Each command of the claim at startup, whose first command also pays for connecting; must be positive |
Persistence
A counter that goes back after Redis restarts without its data hands out IDs a second time. Run Redis with AOF persistence (appendonly yes), or use another store for segments.
See CosId-Example-Redis for a runnable application.
