distributed-locking
π―Skillfrom bitsoex/bitso-java
Implements RFC-44 compliant distributed locking mechanisms for Java services using PostgreSQL or Redis, simplifying synchronization across distributed systems.
Installation
npx skills add https://github.com/bitsoex/bitso-java --skill distributed-lockingSkill Details
>
Overview
# Distributed Locking
RFC-44 compliant distributed locking patterns for Java services.
When to use this skill
- Implementing distributed locking for scheduled jobs
- Migrating from legacy locking mechanisms
- Choosing between PostgreSQL and Redis locking
- Migrating from Fabric8 leader election
- Migrating from incubated in-repo libraries
Skill Contents
Sections
- [When to use this skill](#when-to-use-this-skill) (L24-L31)
- [Quick Start](#quick-start) (L54-L96)
- [Implementation Options](#implementation-options) (L97-L107)
- [Common Patterns](#common-patterns) (L108-L130)
- [References](#references) (L131-L139)
- [Related Rules](#related-rules) (L140-L143)
- [Related Skills](#related-skills) (L144-L149)
Available Resources
π references/ - Detailed documentation
- [lock patterns](references/lock-patterns.md)
- [migration workflow](references/migration-workflow.md)
- [redis integration](references/redis-integration.md)
- [troubleshooting](references/troubleshooting.md)
---
Quick Start
1. Add Dependencies (PostgreSQL)
```toml
# gradle/libs.versions.toml
[versions]
distributed-locking-api = "2.0.0"
distributed-locking-postgres-jooq = "2.0.0"
[libraries]
distributed-locking-api = { module = "com.bitso.commons:distributed-locking-api", version.ref = "distributed-locking-api" }
distributed-locking-postgres-jooq = { module = "com.bitso.commons:distributed-locking-postgres-jooq", version.ref = "distributed-locking-postgres-jooq" }
```
2. Create Configuration Bean
```java
@Configuration
public class DistributedLockConfiguration {
@Bean
DistributedLockManager
@Qualifier("write-dslcontext") DSLContext dslContext) {
return new JooqPostgresSessionDistributedLockManager(dslContext);
}
}
```
3. Use in Scheduled Jobs
```java
@Scheduled(cron = "${job.cron:-}", zone = "UTC")
public void scheduledJob() {
try (var lock = distributedLockManager.tryLock("job-lock")) {
if (!lock.acquired()) {
log.info("Job already running on another instance");
return;
}
doWork();
}
}
```
Implementation Options
RFC-44 supports two valid locking implementations:
| Implementation | When to Use |
|----------------|-------------|
| PostgreSQL Advisory Locks (Default) | Services with PostgreSQL available |
| Redis Locking (Allowed) | Services without PostgreSQL, or with justified Redis use case |
> Important: Redis-based locking is NOT deprecated. It is explicitly supported per RFC-44.
Common Patterns
Try-with-resources Pattern
```java
try (var lock = distributedLockManager.tryLock("lock-key")) {
if (!lock.acquired()) {
return;
}
executeTask();
}
```
Vavr Pattern
```java
Try.withResources(() -> distributedLockManager.tryLock("lock-key"))
.of(lock -> Option.of(lock)
.filter(DistributedLock::acquired)
.onEmpty(() -> log.info("Lock not acquired"))
.peek(l -> doWork()));
```
References
| Reference | Description |
|-----------|-------------|
| [references/migration-workflow.md](references/migration-workflow.md) | Step-by-step migration guide |
| [references/lock-patterns.md](references/lock-patterns.md) | RFC-44 lock patterns |
| [references/redis-integration.md](references/redis-integration.md) | Redis-based locking setup |
| [references/troubleshooting.md](references/troubleshooting.md) | Common issues and solutions |
Related Rules
.cursor/rules/java-distributed-locking-rfc44.mdc- Full RFC-44 reference
Related Skills
| Skill | Purpose |
|-------|---------|
| [gradle-standards](../gradle-standards/SKILL.md) | Dependency configuration |
| [java-testing](../java-testing/SKILL.md) | Testing lock mechanisms |
More from this repository10
Standardizes REST API development in Java Spring by providing authentication, OpenAPI documentation, and RFC-37 service guidelines.
Configures and manages MCP (Model Context Protocol) server settings across multiple IDEs for seamless integration and development workflow.
Streamlines PostgreSQL database integration in Java projects by configuring jOOQ code generation, Flyway migrations, and version compatibility.
doc-sync skill from bitsoex/bitso-java
Manages and standardizes Git hooks across repositories, ensuring consistent code quality checks and team-wide hook compliance automatically.
Integrates SonarQube with MCP to enable natural language querying of Java code quality issues, analysis, and quality gate checks.
Centralizes and standardizes Gradle build configurations for Java projects, managing dependencies, version catalogs, and multi-module setups efficiently.
Implements RFC-34 structured logging standards for Java services, enabling JSON-formatted logs with required fields and contextual metadata.
Configures and generates JaCoCo code coverage reports for Java/Gradle projects, enabling comprehensive testing analysis and quality metrics.
Upgrades Gradle projects from version 8.x to 9.x, ensuring plugin compatibility and supporting Java 25 migration.