How to guard/lock entites in reactive pipelines between each subscription

246 views Asked by At

How do I lock an entity in a Reactive context between each subscription?

I have an endpoint to cancel an order. In the flow, I call some other services to cancel promotion and so on, and at last, I will mark Order in cancelled status and persist it.

... // order is retrieved by some repository
return Flux.concat(
    cartService.defrost(order.getCartId()), // 1
    promotionsService.cancel(order.getRedemption()), // 2
    Mono.fromRunnable(() -> orderService.cancelOrder(order)), // 3
).then();

I want to make sure that when concurrent calls come in(another subscription in the Reactive pipeline)and another thread is launched to check and modify the status of the same order, it would not call external services again. I think dirty writes is OK when I already completed step 3, but when transaction 2 comes in when transaction 1 only reaches step 1 and 2, it's still too early.

Do I have some operators in Reactor 3, or I can only rely on pessimistic locking in Postgres? And BTW, if I really want to lock on row level, setting the isolation level will only be a hint for Postgresql 11 or will lock it indeed?

0

There are 0 answers