Skip to main content

Lightweight DB-backed coordination primitive with leases and fencing tokens

Project description

Sentinel

A lightweight coordination primitive for backend systems. Useful for coordinating workers, preventing duplicate jobs, and ensuring safe execution with fencing tokens.


Installation

pip install sentinel-coordination

Features

  • DB-backed leases
  • Fencing tokens (prevents stale worker writes)
  • Simple API (acquire, heartbeat, release, lease)

Example

import sentinel

with sentinel.lease(conn, "order_123", ttl_ms=5000, get_conn=get_conn):
    process_order()

API

acquire

result = sentinel.acquire(conn, key, ttl_ms=5000)

Returns:

  • acquired (bool)
  • owner_id (str)
  • expires_at (timestamp)
  • fencing_token (int)

heartbeat

result = sentinel.heartbeat(conn, key, owner_id, ttl_ms=5000)

Returns:

  • extended (bool)

release

result = sentinel.release(conn, key, owner_id)

Returns:

  • released (bool)

lease (recommended)

with sentinel.lease(conn, key, ttl_ms=5000, get_conn=get_conn):
    do_work()

Handles:

  • acquire
  • automatic heartbeat
  • release on exit

Fencing Tokens (Important)

Each acquire returns a monotonically increasing fencing token.

To use fencing tokens safely:

  1. Store fencing_token in your application data
  2. Include a condition in all writes

Example:

UPDATE orders
SET status = 'paid', fencing_token = %s
WHERE id = %s AND fencing_token <= %s;

This ensures:

  • stale workers cannot overwrite newer ones
  • race conditions are safe

Status

⚠️ Beta (v0.1.0)


Limitations

  • No retries yet
  • No async support
  • TTL tuning required

Long-running / stuck jobs

The lease() helper maintains ownership via continuous heartbeats.

If a job:

  • hangs
  • enters an infinite loop
  • or gets stuck without crashing

it will continue renewing the lease indefinitely.

This prevents other workers from taking over.

Recommendation:

  • Set appropriate timeouts at the application level
  • Avoid unbounded blocking operations
  • Consider external watchdogs for critical workflows

Philosophy

Sentinel does not eliminate race conditions. It makes them safe.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sentinel_coordination-0.1.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sentinel_coordination-0.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file sentinel_coordination-0.1.2.tar.gz.

File metadata

  • Download URL: sentinel_coordination-0.1.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for sentinel_coordination-0.1.2.tar.gz
Algorithm Hash digest
SHA256 48c17c195c85e585c8c6d212dda22d3372e649c7f059e1e88674af1d60c0474e
MD5 f4d25228c8c05b3c73ac8214453a94a6
BLAKE2b-256 0b0d1c57a17118ee2b8d25fbb23d92417feb032068d5de430a153ab68f5e6a20

See more details on using hashes here.

File details

Details for the file sentinel_coordination-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sentinel_coordination-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ffb16fdd291bf0a2608179b55a9520bf94ac82ce5754c39a9506626bf0102042
MD5 0aed4f7c96f479aaf68ede5fa9162676
BLAKE2b-256 d81c884fcf230520067ccafc6c5dfa6186b8b909c4d54cb231698dadaf0d5c73

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page