node
backend
redis
database
well-grounded

Redis Lock - Mutex

Author: Ireneusz Pliƛ

Problem context

Our application is using external system for user checks. Communication with the system is complicated because of multiple requests required to achieve one business operation. From our perspective, we want to have atomic changes, but it is hard to achieve it in distributed environment with complex communication.

Complex communication

    %%{init: {'theme': 'dark', 'themeVariables': { 'darkMode': true }}}%%
    sequenceDiagram
        participant A as Our System
        participant E as External Service
    alt
        A-)+E: Register user
        E--)-A: OK
        A-)+E: Enable monitoring
        E--)-A: OK
        A-)+E: Disable all other monitored
        E--)-A: OK
    end

To achieve one business operation few requests have to be made.

Even more complex

    %%{init: {'theme': 'dark', 'themeVariables': { 'darkMode': true }}}%%
    sequenceDiagram
        participant A as Our System
        participant E as External Service
    alt
        A-)+E: Create user
        E--)-A: OK
    else
        A-)+E: Update user
        E--)-A: OK
    else
        A-)+E: Get updated data
        E--)-A: User data
    end

We have few business cases so the communication is complicating even more.

Concurrency

%%{init: {'theme': 'dark', 'themeVariables': { 'darkMode': true }}}%%
sequenceDiagram
    participant 1 as Transaction
    participant 2 as Another Transaction
    participant E as External Service
    1-)+E: Register user X
    2-)+E: Register user X
    E--)-2: Registered X
    E--)-1: Registered X

The system could be put into an inconsistent state in case of concurrent access resulting in more than one monitored search or no searches.