Mastering tldb Distributed Locks: Go & Java Guide to Lock, TryLock, and Unlock

This tutorial explains how tldb implements distributed locks, detailing the lock, trylock, and unlock methods, their parameters, and usage examples in Go and Java, including Maven configuration, spin‑wait techniques, and performance test results with illustrative images.

Architecture Digest
Architecture Digest
Architecture Digest
Mastering tldb Distributed Locks: Go & Java Guide to Lock, TryLock, and Unlock

Distributed locks are a crucial tool in distributed systems, and tldb provides a simple, reliable API that mimics object locks in programs.

tldb distributed lock usage:

lock – a blocking request for a lock.

trylock – attempts to acquire a lock; returns immediately with success or failure.

unlock – releases a previously acquired lock.

Lock (string,int) Method Usage

tldb offers an exclusive lock based on a string identifier. The call lock("abc",3) requires two parameters:

The first parameter is the lock object name; the server assigns a global lock for that name.

The second parameter is the maximum hold time in seconds; after this period the server forcibly releases the lock if it hasn't been unlocked.

UnLock (string) Method Usage

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

TryLock (string,int) Method Usage

TryLock works like lock but is non‑blocking; it returns immediately with a key if successful, or empty data if the lock is already held.

Go Example – Using lock

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 lock failure
}
defer sc.UnLock(key)
// business logic here

Go Example – Using tryLock 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
    } else {
        <-time.After(100 * time.Millisecond)
    }
}
defer sc.UnLock(key)
// business logic here

Java Example – Maven Dependency

<dependency>
    <groupId>io.github.donnie4w</groupId>
    <artifactId>tlmq-j</artifactId>
    <version>0.0.2</version>
</dependency>

Java Example – Using lock

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);
    }
}

Java Example – Using tryLock

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

Test Data

Performance tests show multi‑threaded concurrency when multiple threads request the same lock using lock or tryLock with spin‑wait. The following images illustrate the test results.

Test result image 1
Test result image 1
Test result image 2
Test result image 2
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.

concurrencydistributed locktldb
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.