architect-handbook

Software Architect Handbook

View on GitHub

Coarse Grained Lock

Locks a set of related objects with a single lock.

Overview

Objects can often be edited as a group. If so, it makes sense to lock all of these items if you want to lock any one of them.

If your locking strategy requires that an object is loaded in order to be locked, such as with Optimistic Offline Lock, locking a large group affects performance. And with Pessimistic Offline Lock a large lock set is a management headache and increases lock table contention.

A Coarse Grained Lock is a single lock that covers many objects. It not only simplifies the locking action itself but also frees you from having to load all the members of a group in order to lock them.

How It Works

Create a single point of contention for locking a group of objects. Then you provide the shortest path possible to finding that single lock point in order to minimize the group members that must be identified and possibly loaded into memory in the process of obtaining that lock.

Shared Lock

Root Lock

An aggregate is defined as a cluster of associated objects that we treat as a unit for data changes. Each aggregate has a root that provides the only access point to members of the set and a boundary that defines what’s included in the set.

When to Use It