Synchronizing Execution in Distributed Systems using RavenDB

Implementing synchronization for multiple threads in .NET is easy. There are a lot of options for doing that – for example, using the Monitor class. But, what if we need to synchronize execution in a Distributed System? One possible answer is using RavenDB!

How can RavenDB help us to synchronize execution in a distributed system?

RavenDB 4 supports Distributed Compare-Exchange Operations (I strongly recommend you to read this article for a better understanding).

RavenDB uses a consensus protocol to manage much of its distributed state. The consensus is used to ensure consistency in a distributed system and it is open for users as well. You can use this feature to enable some interesting scenarios. (Oren Eini)

Trying to make the idea simple. RavenDB provides you a key/value interface where you can make distributed atomic operations on, knowing that they are entirely consistent.

Show me the code!

Here we go!

public class DistributedLockObject
{
    public DateTime? ExpiresAt { get; set; }
}

public class RavenDbProcessMonitor : IProcessMonitor
{
    private readonly IDocumentStore _documentStore;

    public RavenDbProcessMonitor(IDocumentStore documentStore)
    {
        _documentStore = documentStore;
    }

    public long Enter(string lockKey)
    {
        while (true)
        {
            var now = DateTime.UtcNow;

            var lockObject = new DistributedLockObject
            {
                ExpiresAt = DateTime.UtcNow + TimeSpan.FromMinutes(5)
            };

            var result = _documentStore.Operations.Send(
                new PutCompareExchangeValueOperation<DistributedLockObject>(
                    lockKey, lockObject, 0)
                );

            if (result.Successful)
            {
                // resourceName wasn't present - we managed to reserve
                return result.Index;
            }

            if (result.Value.ExpiresAt < now)
            {
                // Time expired - Update the existing key with the new value
                var takeLockWithTimeoutResult = _documentStore.Operations.Send(
                    new PutCompareExchangeValueOperation<DistributedLockObject>(lockKey, lockObject, result.Index));

                if (takeLockWithTimeoutResult.Successful)
                {
                    return takeLockWithTimeoutResult.Index;
                }
            }
            
            // Wait a little bit and retry
            Thread.Sleep(20);
        }
    }

    public void Exit(string lockKey, long lockIndex)
    {
        _documentStore.Operations.Send(
            new DeleteCompareExchangeValueOperation<DistributedLockObject>(
                lockKey, 
                lockIndex)
            );
    }

    public IDisposable Lock(string lockKey)
        => new ProcessLock(this, lockKey);
}

The previous code is an adaptation of the example available in the official documentation.

The client code specifies a “lock key” trying to acquire a lock. If this key is already in use, the code waits until it is available to use again.

RavenDB ensures that only one client will be able to acquire the lock, even in a cluster.

Here is a little usage example:

public static class Program
{
    public static void Main()
    {
        var store = new DocumentStore
        {
            Database = "TheFlow",
            Urls = new[] {"http://localhost:8080"}
        };
        store.Initialize();

        var monitor = new RavenDbProcessMonitor(store);

        while (true)
        {
            Console.WriteLine("Press ENTER to lock..");
            Console.ReadLine();
            Console.WriteLine("Waiting for the lock...");

            using (monitor.Lock("sample/resource"))
            {
                Console.WriteLine("Locked..");
                Console.WriteLine("Press ENTER to release..");
                Console.ReadLine();
            }

            Console.WriteLine("Released...");
        }
        // ReSharper disable once FunctionNeverReturns
    }
}

To test it, just start multiple instances of this code.

Compartilhe este insight:

Comentários

Participe deixando seu comentário sobre este artigo a seguir:

Subscribe
Notify of
guest
1 Comentário
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Adriano
Adriano
5 anos atrás

Hi Elemar,

Is the CompareExchangeValueOperation a good approach to generating a sequential number that needs to be restarted at the beginning of the day?

Is there any other operation better than cmpXchg for that issue?

[]’s

AUTOR

Elemar Júnior
Fundador e CEO da EximiaCo atua como tech trusted advisor ajudando empresas e profissionais a gerar mais resultados através da tecnologia.

NOVOS HORIZONTES PARA O SEU NEGÓCIO

Nosso time está preparado para superar junto com você grandes desafios tecnológicos.

Entre em contato e vamos juntos utilizar a tecnologia do jeito certo para gerar mais resultados.

Insights EximiaCo

Confira os conteúdos de negócios e tecnologia desenvolvidos pelos nossos consultores:

Arquivo

Pós-pandemia, trabalho remoto e a retenção dos profissionais de TI

CTO Consulting e Especialista em Execução em TI
1
0
Queremos saber a sua opinião, deixe seu comentáriox
Oferta de pré-venda!

Mentoria em
Arquitetura de Software

Práticas, padrões & técnicas para Arquitetura de Software, de maneira efetiva, com base em cenários reais para profissionais envolvidos no projeto e implantação de software.

Muito obrigado!

Deu tudo certo com seu envio!
Logo entraremos em contato

Synchronizing Execution in Distributed Systems using RavenDB

Para se candidatar nesta turma aberta, preencha o formulário a seguir:

Synchronizing Execution in Distributed Systems using RavenDB

Para se candidatar nesta turma aberta, preencha o formulário a seguir:

Condição especial de pré-venda: R$ 14.000,00 - contratando a mentoria até até 31/01/2023 e R$ 15.000,00 - contratando a mentoria a partir de 01/02/2023, em até 12x com taxas.

Tenho interesse nessa capacitação

Para solicitar mais informações sobre essa capacitação para a sua empresa, preencha o formulário a seguir:

Tenho interesse em conversar

Se você está querendo gerar resultados através da tecnologia, preencha este formulário que um de nossos consultores entrará em contato com você:

O seu insight foi excluído com sucesso!

O seu insight foi excluído e não está mais disponível.

O seu insight foi salvo com sucesso!

Ele está na fila de espera, aguardando ser revisado para ter sua publicação programada.

Tenho interesse em conversar

Se você está querendo gerar resultados através da tecnologia, preencha este formulário que um de nossos consultores entrará em contato com você:

Tenho interesse nessa solução

Se você está procurando este tipo de solução para o seu negócio, preencha este formulário que um de nossos consultores entrará em contato com você:

Tenho interesse neste serviço

Se você está procurando este tipo de solução para o seu negócio, preencha este formulário que um de nossos consultores entrará em contato com você:

× Precisa de ajuda?