Microservice Solution: Distributed Locking

Distributed locking is a common pattern in distributed systems to ensure that only one instance of a service can execute a specific operation at a time. This document explains how the distributed locking mechanism works in the microservice solution template. You can learn more in the Distributed Locking document.

Distributed Locking in Microservice Solutions

The microservice solution template uses Redis to implement the distributed locking mechanism. You can configure the Redis connection in the appsettings.json file of the related projects. The following is an example configuration:

  "Redis": {
    "Configuration": "localhost:6379"
  }

Additionally, you can configure the distributed lock key prefix in the ConfigureServices method. This approach helps prevent key conflicts if you use the same Redis server for multiple applications. Here is an example configuration:

Configure<AbpDistributedLockOptions>(options =>
{
    options.KeyPrefix = "MyApp1";
});

The distributed locking mechanism is often needed in background workers. You can inject the IAbpDistributedLock interface into your background worker class and use it to acquire the lock. Here is an example:

public class MyService : ITransientDependency
{
    private readonly IAbpDistributedLock _distributedLock;

    public MyService(IAbpDistributedLock distributedLock)
    {
        _distributedLock = distributedLock;
    }
    
    public async Task MyMethodAsync()
    {
        await using (var handle = await _distributedLock.TryAcquireAsync("MyLockName"))
        {
            if (handle != null)
            {
                // your code that access the shared resource
            }
        }   
    }
}
Was this page helpful?
Please make a selection.
Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document

testtestOtherLiveEvents

19 Jun, 03:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More