SQL - Concurrency and Concurrency approaches for data access

Concurrency can be defined as the ability of multiple processes to access or change shared data at the same time.

The greater the number of concurrent user processes that can be active without interfering with each other, the greater the concurrency of the database system.

Concurrency is reduced when a process that is changing data prevents other processes from reading that data or when a process that is reading data prevents other processes from changing that data.

Concurrency is also affected when multiple processes are attempting to change the same data simultaneously.

Two approaches to managing concurrent data access:

- Pessimistic Concurrency

Default behavior: acquire locks to block access to data that another process is using.

Assumes that enough data modification operations are in the system that any given read operation is likely affected by a data modification made by another user (assumes conflicts will occur).

Avoids conflicts by acquiring a lock on data being read so no other processes can modify that data.

Also acquires locks on data being modified so no other processes can access the data for either reading or modifying.

Readers block writer, writers block readers and writers.

- Optimistic Concurrency

Assumes that there are sufficiently few conflicting data modification operations in the system that any single transaction is unlikely to modify data that another transaction is modifying.

Default behavior: use row version to allow data readers to see the state of the data before the modification occurs.

Older versions of the data are saved so a process reading data can see the data as it was when the process started reading and not affected by any changes being made to that data.

Processes modifying the data is unaffected by processes reading the data because the reader is accessing a saved version of the data rows.

Readers do not block writers and writers do not block readers, but, writers can and will block writers.