Backend Development 10 min read

tldb Distributed Lock: Usage Guide for Go and Java

This article introduces tldb's distributed lock mechanism, explains lock, trylock, and unlock methods, and provides step‑by‑step Go and Java client examples—including code snippets—for acquiring and releasing locks in a multi‑language environment.

Top Architect
Top Architect
Top Architect
tldb Distributed Lock: Usage Guide for Go and Java

Distributed locks are essential tools in distributed systems. tldb offers a simple, reliable distributed lock service that mimics object‑level locking in a program, simplifying design by using a string as the lock identifier.

Lock, TryLock, and Unlock Methods

tldb provides three main operations:

lock : a blocking call that waits until the lock is granted.

tryLock : a non‑blocking call that returns immediately with a lock key if successful, or empty if the lock is already held.

unlock : releases a previously acquired lock using the returned key.

Go Client Example

First, import the Go client library:

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

Lock (blocking) example:

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 (non‑blocking) example:

sc := cli.NewMqClient("ws://127.0.0.1:5001", "mymq=123")
sc.Connect()
if key, ok := sc.TryLock("testlock2", 3); ok {
    defer sc.UnLock(key)
    // business logic
}

Spin‑wait using TryLock:

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

Java Client Example

Add the Maven dependency:

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

Lock (blocking) 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);
    }
}

TryLock (non‑blocking) example:

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

These examples demonstrate how to acquire and release tldb distributed locks from both Go and Java clients, enabling cross‑language lock coordination in a message‑queue based architecture.

JavaBackend DevelopmentGoMessage QueueDistributed Locktldb
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login 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.