{"id":50343026,"url":"https://github.com/vitallan/spring-boot-starter-leader-election","last_synced_at":"2026-05-29T18:30:22.456Z","repository":{"id":358364347,"uuid":"1240882680","full_name":"vitallan/spring-boot-starter-leader-election","owner":"vitallan","description":"Spring Boot Starter to enable a leader election process on a multi-node Spring app using database locking","archived":false,"fork":false,"pushed_at":"2026-05-17T01:27:29.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-17T03:24:47.153Z","etag":null,"topics":["java","spring","spring-boot","spring-boot-starter"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vitallan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-16T17:30:19.000Z","updated_at":"2026-05-17T01:27:33.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/vitallan/spring-boot-starter-leader-election","commit_stats":null,"previous_names":["vitallan/spring-boot-starter-leader-election"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/vitallan/spring-boot-starter-leader-election","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitallan%2Fspring-boot-starter-leader-election","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitallan%2Fspring-boot-starter-leader-election/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitallan%2Fspring-boot-starter-leader-election/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitallan%2Fspring-boot-starter-leader-election/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vitallan","download_url":"https://codeload.github.com/vitallan/spring-boot-starter-leader-election/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vitallan%2Fspring-boot-starter-leader-election/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33666290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","spring","spring-boot","spring-boot-starter"],"created_at":"2026-05-29T18:30:21.178Z","updated_at":"2026-05-29T18:30:22.441Z","avatar_url":"https://github.com/vitallan.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spring Boot Starter Leader Election\n\nDatabase-backed leader election starter for Spring Boot applications.\n\nThe starter exposes a simple `LeaderLatch` API that application code can inject and query via `isLeader()`. Leadership is maintained asynchronously in the background; `isLeader()` is a fast in-memory read.\n\n## Compatibility\n\n- Spring Boot: 4.0.x \n- Java: 21\n\n## Modules\n\n- `leader-election-core`: public API and election loop (no Spring Boot dependency).\n- `leader-election-autoconfigure`: Spring Boot auto-configuration and JPA implementation.\n- `leader-election-spring-boot-starter`: dependency aggregator used by application projects.\n\n## Installation\n\nAdd the starter:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.allanvital\u003c/groupId\u003e\n    \u003cartifactId\u003eleader-election-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e0.0.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Usage\n\nInject `LeaderLatch` and guard leader-only logic:\n\n```java\nimport com.allanvital.leaderelection.LeaderLatch;\nimport org.springframework.stereotype.Service;\n\n@Service\nclass LeaderOnlyJob {\n\n    private final LeaderLatch leaderLatch;\n\n    LeaderOnlyJob(LeaderLatch leaderLatch) {\n        this.leaderLatch = leaderLatch;\n    }\n\n    void run() {\n        if (!leaderLatch.isLeader()) {\n            return;\n        }\n        // leader-only logic\n    }\n}\n```\n\nExample using Spring scheduling:\n\n```java\nimport com.allanvital.leaderelection.LeaderLatch;\nimport org.springframework.scheduling.annotation.Scheduled;\nimport org.springframework.stereotype.Component;\n\n@Component\nclass LeaderScheduledTask {\n\n    private final LeaderLatch leaderLatch;\n\n    LeaderScheduledTask(LeaderLatch leaderLatch) {\n        this.leaderLatch = leaderLatch;\n    }\n\n    @Scheduled(fixedDelayString = \"PT10S\")\n    void tick() {\n        if (leaderLatch.isLeader()) {\n            // do the work\n        }\n    }\n}\n```\n\n## Configuration\n\nPrefix: `leader.election`\n\nAll properties (Spring `.properties` format):\n\n- `leader.election.enabled` (default: `true`)\n- `leader.election.lock-name` (default: `default`)\n- `leader.election.owner-id` (default: generated from host/process/random suffix)\n- `leader.election.lease-duration` (default: `15s`)\n- `leader.election.renew-interval` (default: `5s`)\n- `leader.election.acquire-interval` (default: `2s`)\n\n## Schema Contract\n\nExpected table contract:\n\n- table: `leader_lock`\n- columns:\n  - `lock_name` varchar primary key\n  - `owner_id` varchar not null\n  - `lease_until` timestamp not null\n  - `version` bigint not null\n  - `updated_at` timestamp not null\n\n`lock-name` maps to `leader_lock.lock_name` (one row per lock).\n\nMySQL example schema (baseline):\n\n```sql\ncreate table leader_lock (\n  lock_name varchar(128) not null,\n  owner_id varchar(256) not null,\n  lease_until timestamp(6) not null,\n  version bigint not null,\n  updated_at timestamp(6) not null,\n  primary key (lock_name)\n);\n```\n\n## Schema Management\n\nThis starter does not run migrations.\n\n- If `spring.jpa.hibernate.ddl-auto=create|update|create-drop`, Hibernate can create/manage the table\n- If `spring.jpa.hibernate.ddl-auto=validate|none`, the table must already exist (for example via Flyway/Liquibase)\n\nIf the table is missing or the DB user cannot read/write it, the application keeps running but stays follower. The JPA lease store logs an `ERROR` once with guidance.\n\n## How It Works\n\n- Lease-based leader election over a single DB row per lock name.\n- `lock-name` is the logical election scope:\n  - every distinct `leader.election.lock-name` maps to a different `leader_lock.lock_name` row\n  - instances contending for the same `lock-name` elect exactly one leader\n  - different lock names are independent elections (useful if you want multiple unrelated leader-only processes)\n  - for practical purposes, the default usage is in a single row, so all \"nodes of the same cluster\" should have the same `lock-name`\n- Time source for lease decisions is database time (`CURRENT_TIMESTAMP` via JPQL)\n- Contention control uses pessimistic row locking (a single row is locked briefly during acquire/renew)\n- Follower behavior: periodically attempts to acquire the lock row at `leader.election.acquire-interval`\n- Leader behavior: periodically renews the lease at `leader.election.renew-interval`\n- `LeaderLatch.isLeader()` is always an in-memory atomic read.\n\n## Operational Notes\n\nTuning guidelines:\n\n- `renew-interval` must be less than `lease-duration`.\n- Larger `lease-duration` makes leadership more stable (fewer handoffs) but increases worst-case time to fail over when a leader dies\n- Smaller `lease-duration` makes failover faster, but increases the chance of churn (leadership changing hands) during short DB pauses or GC pauses\n- Smaller `renew-interval` renews more aggressively (more DB traffic) and can tolerate shorter `lease-duration`\n- Larger `acquire-interval` reduces DB traffic for followers but makes takeover slower after lease expiry\n\nLogging:\n\n- Enable debug logs:\n  - `logging.level.com.allanvital.leaderelection=DEBUG`\n- More detail:\n  - `logging.level.com.allanvital.leaderelection=TRACE`\n\n## Testing\n\n- Unit tests run with `mvn test`\n- Integration tests use Testcontainers (PostgreSQL and MySQL) and require Docker\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitallan%2Fspring-boot-starter-leader-election","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvitallan%2Fspring-boot-starter-leader-election","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvitallan%2Fspring-boot-starter-leader-election/lists"}