To clarify why this can be so challenging and intrusive, observe that A and B here correspond to arbitrary locks and that there maybe be plenty of operations that touch a pair or more of these locks. While sorting in this simple example by customer ID would work, in general, composing functions and locking/unlocking in various regions would get quite complicated when trying to ensure no deadlocks may occur.
To clarify why this can be so challenging and intrusive, observe that A and B here correspond to arbitrary locks and that there maybe be plenty of operations that touch a pair or more of these locks. While sorting in this simple example by customer ID would work, in general, composing functions and locking/unlocking in various regions would get quite complicated when trying to ensure no deadlocks may occur.