BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns

Multi-User Systems and Concurrency

In a single-user application information can be read, created, updated or deleted without the risk of interfering with other people's work. When more than one user has the ability to access shared information, concurrency control becomes essential.

Pessimistic Locking

Pessimistic locking gets it name by being pessimistic about the number of collisions that may occur in a multi-user application. This strategy either prevents or limits access to information that is being edited. The process for editing an item in such an environment is as follows:

  • The user reads the original information from the official data store. A lock is created in the data store that can be seen by other users. Any other user that attempts to access the data being edited is prevented from doing so or is limited to read-only access.
  • The user makes changes to their local copy of the information.
  • The user saves their changes. Once the new data is committed to the official data store, the previously created lock is removed. Other users may now access the updated information.

The lock that is created can be simple or sophisticated. In the simplest solutions, a unique identifier for the data is held in a table of locks that other users can query. This can be enhanced by storing information such as the creation time of the lock and details of the user that holds it.

Pessimistic locking is most suited to systems where the risk of collisions is high because data is modified often and by many different users and services. It only works for connected systems, as access to the central store must be available to correctly maintain the locks.

Advantages of Pessimistic Locking

The key advantages of pessimistic locking systems are:

  • The risk of inadvertently overwriting other users' changes is removed.
  • It is possible to prevent users from reading information from the official data store that is currently being edited and that may therefore be out of date.
  • Users changes will not normally be rejected on saving.
  • With interrogation of the current locks, it is possible to identify who is currently editing information and which records are being worked upon.

Disadvantages of Pessimistic Locking

The key disadvantages of pessimistic locking systems are:

  • As the number of users grows, the risk of collisions increases. This increases the risk that users will be prevented from accessing information that they require and reduces the scalability of the software.
  • The additional overhead of maintaining the locks can lower the performance of the solution.
  • Misuse and failures for overly simplistic pessimistic locking solutions can lead to items being permanently locked. In some cases this may require manual intervention to remove the lock and restore access to the information.
  • The requirement to maintain locks centrally requires that users remain permanently connected to the central data store.
11 March 2010