Quick Background: As I am going back and redesigning some critical parts of an application, I keep wondering about locking and its impact on performance. The app has a large Tree style data structure which caches data/DTO from the database. Updates to the large tree can come about in two main ways: 1. user triggered commands, 2. auto updates from jobs that ran in the background.
When either operation type occurs (user/auto), I am locking down (explicitly locking) the data structure. I was running into consistency issues, so locking down everything seemed to make the most sense to protect the integrity of the data in the cache.
Question: Since many auto updates can occur at once I was thinking of implementing some kind of queue (JMS maybe) to handle instructions to the data structure, where any user driven updates get pushed to the top and handled first. When it comes to handling a bulk/unknown size set of auto "tasks", I am trying to figure out if I should let them run and lock individually or try and bulk them together by time and interact with locking once. The real crux of the problem is that any one of the tasks to update could affect the entire tree.
In terms of overall performance (general, nothing specific), is it more efficient to have many transactions locking potentially doing large updates, or try and combine to one massive bulk update and only lock once but for a lot longer? I know a lot of this probably hinges on the data, the type of updates, frequency, etc. I didn't know if there was a general rule of thumb of "smaller more frequent locks" or "one large potentially longer" lock.