[docs] database concurrency (#13341)

make a new concepts page for discussing database concurrency
This commit is contained in:
Oliver authored and GitHub committed 2026-11-21 15:07:24 +11:00
1 parent 371ac83bbb
commit 7933454b89
7 files changed
+57 -42

No files matched your search

+4
View File
@@ -203,3 +203,7 @@ Or, when accessing the API from an account which has read-only permissions:
### Permission Denied
If an API action outside of the user's role(s) is attempted, the server will respond with a 403 permission error message.
### Conflict
If an API request attempts to modify an object which is currently locked by another process (e.g. a background task), the server may respond with a `409 Conflict` error. No changes are made, and the request can safely be retried once the other process has finished. Refer to the [database concurrency](../concepts/concurrency.md) documentation for more information.
+45
View File
@@ -0,0 +1,45 @@
---
title: Database Concurrency
---
## Database Concurrency
InvenTree is a multi-user system, and it also runs [background tasks](../start/processes.md#background-worker) which modify the database. This means that the same database record can be changed by two processes at (almost) the same time - for example, a user edits a build order while a background task is completing the outputs of that same build order.
If this is not managed, one process can silently overwrite the changes made by the other (a "lost update"), as each process saves the data it read *before* the other process finished.
### How Contention is Managed
InvenTree relies on *row-level locking* in the database to prevent lost updates:
- **Editing an object:** When an existing object is updated via the API, the database row is locked *before* the object is read. The object is then validated and saved within a single database transaction, and the lock is released when the request completes.
- **Performing an action:** Actions against an existing object (e.g. issuing, completing or cancelling an order, completing build outputs, receiving items, assigning a barcode) are handled in the same way: the target object is locked for the duration of the request, and if the action fails then none of its changes are saved.
- **Background tasks:** Tasks which adjust quantities (e.g. completing build outputs, consuming allocated stock, receiving items) lock the rows they work on, and re-read the current values before changing them.
A request waits for a short time (around two seconds) for a lock to be released, which is long enough for most operations to complete without the user noticing. If the object is still locked after this time (e.g. a background task is processing a large number of items), the request is rejected with a `409 Conflict` response, and no changes are made. The request can safely be submitted again once the other process has finished.
In the user interface, this is displayed as an error message at the top of the form:
{{ image("start/contention_error.png", "Error message displayed when an item is locked by another process") }}
!!! tip "Try Again"
This message does not indicate a problem with the data which was entered. Wait a few moments for the other process to complete, and then submit the form again.
### Where Failures Can Still Occur
Row-level locking prevents the most common lost updates, but it cannot cover every case:
- **Stale forms:** A lock is only held while a request is being processed, not while a form is open. If an object is changed by someone else after a form has been opened, submitting the form overwrites those changes with the (older) values displayed in the form.
- **Operations across many objects:** Operations which act on many objects at once (e.g. bulk stock adjustments) are not locked as a single unit. Two such operations which overlap may have to wait for each other, and one of them may fail.
- **Deadlocks:** If two processes each wait for a lock held by the other, the database aborts one of them. The aborted request (or background task) fails with an error, and its changes are not saved.
- **[Plugins](../plugins/index.md) and custom code:** Code which loads an object, modifies it and saves it without locking the row (e.g. a third-party plugin, or the admin interface) is not protected, and can overwrite a concurrent change. Plugin developers should refer to the [development guidelines](../plugins/develop.md#development-guidelines).
- **Serializable isolation:** If the [`INVENTREE_DB_ISOLATION_SERIALIZABLE`](../start/config.md#postgresql-settings) option is enabled, the database itself rejects any transaction which conflicts with another. Data is never silently overwritten in this mode, but InvenTree does not automatically retry a rejected transaction: a request or background task which conflicts with another fails with a server error, and must be retried manually.
### SQLite Limitations
SQLite does not support row-level locking - instead, the entire database is locked while a process is writing to it. As a result:
- The protections described above are **not available**. A concurrent edit can still overwrite changes made by another process.
- Only one process can write to the database at a time. A process which cannot obtain the database lock within the configured [`INVENTREE_DB_TIMEOUT`](../start/config.md#sqlite-settings) fails with a "database is locked" error.
This is one of the reasons that SQLite is not recommended for production use, particularly where multiple users (or a background worker) are active at the same time. Refer to the [SQLite limitations](../start/processes.md#sqlite-limitations) for more information. Use PostgreSQL or MySQL for any installation where data integrity is important.
+1
View File
@@ -45,6 +45,7 @@ from plugin.mixins import APICallMixin, SettingsMixin, ScheduleMixin, BarcodeMix
- If you need to use a private infrastructure, use the 'Releases' functions in GitHub or Gitlab. Point to the 'latest' release endpoint when installing to make sure the update function works
- Tag your GitHub repo with `inventree` and `inventreeplugins` to make discovery easier.
- Use GitHub actions to test your plugin regularly (you can [schedule actions](https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#schedule)) against the 'latest' [docker-build](https://hub.docker.com/r/inventree/inventree) of InvenTree
- Lock database rows (e.g. using `select_for_update()` within a transaction) when modifying existing objects, so that your plugin does not overwrite concurrent changes made by users or background tasks. Refer to the [database concurrency](../concepts/concurrency.md) documentation for more information
- If you use the AppMixin pin your plugin against the stable branch of InvenTree, your migrations might get messed up otherwise
+3 -41
View File
@@ -283,7 +283,7 @@ If running with a PostgreSQL database backend, the following additional options
| `INVENTREE_DB_TCP_KEEPALIVES_INTERVAL` | database.tcp_keepalives_interval | 5 | TCP keepalive interval |
| `INVENTREE_DB_TCP_KEEPALIVES_COUNT` | database.tcp_keepalives_count | 5 | TCP keepalive count |
| `INVENTREE_DB_TCP_USER_TIMEOUT` | database.tcp_user_timeout | 2000 | TCP user timeout (ms) |
| `INVENTREE_DB_ISOLATION_SERIALIZABLE` | database.serializable | False | Database isolation level configured to "serializable" |
| `INVENTREE_DB_ISOLATION_SERIALIZABLE` | database.serializable | False | Database isolation level configured to "serializable" (see [concurrency](../concepts/concurrency.md#where-failures-can-still-occur)) |
| `INVENTREE_DB_JIT_ENABLED` | database.jit_enabled | False | Enable JIT compilation for PostgreSQL queries |
!!! info "JIT Compilation"
@@ -297,7 +297,7 @@ If running with a MySQL database backend, the following additional options are a
{{ configtable() }}
{{ configsetting("INVENTREE_DB_TIMEOUT", default="10") }} Database connection timeout (s) |
| `INVENTREE_DB_ISOLATION_SERIALIZABLE` | database.serializable | False | Database isolation level configured to "serializable" |
| `INVENTREE_DB_ISOLATION_SERIALIZABLE` | database.serializable | False | Database isolation level configured to "serializable" (see [concurrency](../concepts/concurrency.md#where-failures-can-still-occur)) |
### SQLite Settings
@@ -314,45 +314,7 @@ If running with a SQLite database backend, the following additional options are
### Concurrent Updates
InvenTree is a multi-user system, and it also runs [background tasks](./processes.md#background-worker) which modify the database. This means that the same database record can be changed by two processes at (almost) the same time - for example, a user edits a build order while a background task is completing the outputs of that same build order.
If this is not managed, one process can silently overwrite the changes made by the other (a "lost update"), as each process saves the data it read *before* the other process finished.
#### How Contention is Managed
InvenTree relies on *row-level locking* in the database to prevent lost updates:
- **Editing an object:** When an existing object is updated via the API, the database row is locked *before* the object is read. The object is then validated and saved within a single database transaction, and the lock is released when the request completes.
- **Performing an action:** Actions against an existing object (e.g. issuing, completing or cancelling an order, completing build outputs, receiving items, assigning a barcode) are handled in the same way: the target object is locked for the duration of the request, and if the action fails then none of its changes are saved.
- **Background tasks:** Tasks which adjust quantities (e.g. completing build outputs, consuming allocated stock, receiving items) lock the rows they work on, and re-read the current values before changing them.
A request waits for a short time (around two seconds) for a lock to be released, which is long enough for most operations to complete without the user noticing. If the object is still locked after this time (e.g. a background task is processing a large number of items), the request is rejected with a `409 Conflict` response, and no changes are made. The request can safely be submitted again once the other process has finished.
In the user interface, this is displayed as an error message at the top of the form:
{{ image("start/contention_error.png", "Error message displayed when an item is locked by another process") }}
!!! tip "Try Again"
This message does not indicate a problem with the data which was entered. Wait a few moments for the other process to complete, and then submit the form again.
#### Where Failures Can Still Occur
Row-level locking prevents the most common lost updates, but it cannot cover every case:
- **Stale forms:** A lock is only held while a request is being processed, not while a form is open. If an object is changed by someone else after a form has been opened, submitting the form overwrites those changes with the (older) values displayed in the form.
- **Operations across many objects:** Operations which act on many objects at once (e.g. bulk stock adjustments) are not locked as a single unit. Two such operations which overlap may have to wait for each other, and one of them may fail.
- **Deadlocks:** If two processes each wait for a lock held by the other, the database aborts one of them. The aborted request (or background task) fails with an error, and its changes are not saved.
- **Plugins and custom code:** Code which loads an object, modifies it and saves it without locking the row (e.g. a third-party plugin, or the admin interface) is not protected, and can overwrite a concurrent change.
- **Serializable isolation:** If the `INVENTREE_DB_ISOLATION_SERIALIZABLE` option is enabled, the database itself rejects any transaction which conflicts with another. Data is never silently overwritten in this mode, but InvenTree does not automatically retry a rejected transaction: a request or background task which conflicts with another fails with a server error, and must be retried manually.
#### SQLite Limitations
SQLite does not support row-level locking - instead, the entire database is locked while a process is writing to it. As a result:
- The protections described above are **not available**. A concurrent edit can still overwrite changes made by another process.
- Only one process can write to the database at a time. A process which cannot obtain the database lock within the configured `INVENTREE_DB_TIMEOUT` fails with a "database is locked" error.
This is one of the reasons that SQLite is not recommended for production use, particularly where multiple users (or a background worker) are active at the same time. Use PostgreSQL or MySQL for any installation where data integrity is important.
InvenTree uses row-level database locking to prevent concurrent changes (from multiple users, or from [background tasks](./processes.md#background-worker)) from overwriting each other. Refer to the [database concurrency](../concepts/concurrency.md) documentation for details on how this works, the limitations which apply, and how the choice of database backend (and the `INVENTREE_DB_ISOLATION_SERIALIZABLE` option) affects this behavior.
## Caching
+1 -1
View File
@@ -125,7 +125,7 @@ Extra python packages can be installed by setting the environment variable `SETU
The used database backend can be configured with environment variables (before the first setup) or in the config file after the installation. Check the [configuration section](./config.md#database-options) for more information.
!!! warning "SQLite Performance"
SQLite is not recommended for production use, as it is not designed for high concurrency.
SQLite is not recommended for production use, as it is not designed for high concurrency. Refer to the [database concurrency](../concepts/concurrency.md#sqlite-limitations) documentation for more information.
## Moving Data
+2
View File
@@ -28,6 +28,8 @@ If running InvenTree via [docker compose](./docker_install.md), the database pro
While SQLite is supported, it is strongly *not* recommended for a production installation, especially where there may be multiple users accessing the system concurrently. SQLite is designed for low-concurrency applications, and can experience performance issues when multiple users are accessing the database at the same time.
SQLite also does not support the row-level locking which InvenTree uses to protect against [concurrent updates](../concepts/concurrency.md#sqlite-limitations).
In addition to concurrency issues, there are other structural limitations which exist in SQLite that can prevent operations on large querysets.
If you are using SQLite, you should be aware of these limitations. It is important to ensure that the database file is stored on a fast storage medium (such as an SSD), and that the database options are configured correctly to minimize locking issues. Refer to the [database configuration guide](./config.md#database-options) for more information on configuring SQLite options.
+1
View File
@@ -118,6 +118,7 @@ nav:
- Notes: concepts/notes.md
- Tags: concepts/tags.md
- Audit Log: concepts/audit.md
- Database Concurrency: concepts/concurrency.md
- Barcodes:
- Barcode Support: barcodes/index.md
- Internal Barcodes: barcodes/internal.md