Mastering tldb Distributed Locks: Go, Java, and Python Examples

This article explains how tldb implements language‑agnostic distributed locks, describes the Lock, TryLock, and Unlock APIs, and provides practical Go and Java code samples along with test results to help developers integrate reliable distributed locking into their systems.

Architect
Architect
Architect
Mastering tldb Distributed Locks: Go, Java, and Python Examples

What is a Distributed Lock?

Distributed locks are essential tools in distributed systems. tldb provides a simple, reliable distributed lock that works like an object lock in a program, using third‑party services such as Redis, MQ, databases, or Zookeeper.

tldb Distributed Lock API

Lock(string, int)

– blocking request for a lock. TryLock(string, int) – non‑blocking attempt; returns a key if successful. Unlock(string) – releases the lock.

The lock is controlled by the tldb server, making it language‑agnostic. Clients in Java, Go, Python, and JavaScript are already available, and other languages can be added easily.

Lock Method Usage

The Lock method takes two parameters: the lock name (e.g., "abc") and the maximum hold time in seconds. If the lock is not released within the timeout, the server forces its release.

Unlock Method Usage

After acquiring a lock, the server returns a key. The client must call Unlock(key) to release the lock; otherwise the server will release it after the timeout.

TryLock Method Usage

TryLock

behaves like Lock but returns immediately. If the lock is available, it returns a key and a success flag; otherwise it returns empty data.

Go Example

import "github.com/donnie4w/tlmq-go/cli"

sc := cli.NewMqClient("ws://127.0.0.1:5001", "mymq=123")
sc.Connect()
key, err := sc.Lock("testlock", 3)
if err != nil {
    // handle error
} else {
    defer sc.UnLock(key)
    // business logic
}

TryLock example with spin‑wait:

sc := cli.NewMqClient("ws://127.0.0.1:5001", "mymq=123")
sc.Connect()
var key string
for {
    if v, ok := sc.TryLock("testlock", 3); ok {
        key = v
        break
    }
    <-time.After(100 * time.Millisecond)
}
defer sc.UnLock(key)
// business logic

Java Example

MqClient mc = new SimpleClient("ws://127.0.0.1:5001", "mymq=123");
mc.connect();
String key = null;
try {
    key = mc.lock("testlock", 3);
    // business logic
} finally {
    if (key != null) {
        mc.unLock(key);
    }
}

Test Results

Multi‑threaded concurrency tests show lock acquisition behavior for both Lock and TryLock (spin).

Lock test result
Lock test result
TryLock spin test result
TryLock spin test result
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Godistributed-locktldbmq client
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.