Consider the following situation: There is an update request on Entity A, to create sub-entity A.B. there might be many B's on A, each B has unique email address. The entity A is a shared entity, and the same request can happen in multiple servers in parallel (scalable micro-service).
In order to create A.B we have to verify that B does not already exist as sub entity on A (according to B's email address).
The service which handles this update request should lock A(by it's unique id) in order to make the update safe.
My questions are more conceptual than technical:
Does locking the resource A in this case is part of the business logic of this update task?
Would you consider putting the resource lock in a separate middleware than the one which handles the verify and update procedure? (the other option is to treat the lock as part of the business logic and put it directly in the middleware responsible for the business logic.)