Here is my scenario:
- I have two servers with a multi-threaded message queuing consumer on each (two consumers total).
- I have many message types (CreateParent, CreateChild, etc.)
- I am stuck with bad legacy code (creating a child will partially creates a parent. I know it is bad...But I cannot change that.)
- Message ordering cannot be assume (message queuing principle!)
- RabbitMQ is my message queuing broker.
My problem:
- When two threads are running simultaneous (one executing a CreateParent, the other executing a CreateChild), they generate conflicts because the two threads try to create the Parent in the database (remember the legacy code!)
My initial solution:
- Inside the consumer, I created an "entity locking" concept. So when the thread processes a CreateChild message for example, it locks the Child and the Parent (legacy code!!) so the CreateParent message processing can wait. I used basic .net Monitor and list of Ids to implement this concept. It works well.
My initial solution limitation:
- My "entity locking" concept works well on a single consumer in a single process on a single server. But it will not works across multiple servers running multiple consumers.
- I am thinking of using a shared database to "store" my entity locking concept, so each processes (and threads) could access the database to verify which entities are locked.
My question (finally!):
All this is becoming very complex and it increases the bugs risk and code maintenance problems. I really don`t like it! Does anyone already faced this kind of problem? Are they acceptable workarounds for it? Does anyone have an idea for a clean solution for my scenario?
Thanks!