---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://ydb.tech/docs/en/troubleshooting/performance/queries/transaction-lock-invalidation.md?version=v25.3
  - https://ydb.tech/docs/ru/troubleshooting/performance/queries/transaction-lock-invalidation.md?version=v25.3
  - href: en/troubleshooting/performance/queries/transaction-lock-invalidation.md
    type: text/markdown
    title: Markdown version
  - href: ../../../llms.txt
    type: text/markdown
    title: llms.txt
sourcePath: en/core/troubleshooting/performance/queries/transaction-lock-invalidation.md
---
> **Documentation Index:** Fetch the complete configuration index at https://ydb.tech/docs/en/llms.txt

# Transaction lock invalidation

YDB uses [optimistic locking](https://en.wikipedia.org/wiki/Optimistic_concurrency_control) to find conflicts with other transactions being executed. If the locks check during the commit phase reveals conflicting modifications, the committing transaction rolls back and must be restarted. In this case, YDB returns a **transaction locks invalidated** error. Restarting a significant share of transactions can degrade your application's performance.

{% note info %}

The YDB SDK provides a built-in mechanism for handling temporary failures. For more information, see [Handling errors](https://ydb.tech/docs/en/reference/ydb-sdk/error_handling.md?version=v25.3).

{% endnote %}


## Diagnostics

<!-- The include is added to allow partial overrides in overlays  -->
<!-- source: en/troubleshooting/performance/queries/_includes/transaction-lock-invalidation.md -->
1. Open the **[DB overview](https://ydb.tech/docs/en/reference/observability/metrics/grafana-dashboards.md?version=v25.3#dboverview)** Grafana dashboard.

1. See if the **Transaction Locks Invalidation** chart shows any spikes.

    ![](_assets/transactions-locks-invalidation.png)

    This chart shows the number of queries that returned the transaction locks invalidation error per second.
<!-- endsource: en/troubleshooting/performance/queries/_includes/transaction-lock-invalidation.md -->

## Recommendations

Consider the following recommendations:

- The longer a transaction lasts, the higher the likelihood of encountering a **transaction locks invalidated** error.

    If possible, avoid [interactive transactions](https://ydb.tech/docs/en/concepts/glossary.md?version=v25.3#interactive-transaction). A better approach is to use a single YQL query with `begin;` and `commit;` to select data, update data, and commit the transaction.

    If you do need interactive transactions, perform `commit` in the last query in the transaction.

- Analyze the range of primary keys where conflicting modifications occur, and try to change the application logic to reduce the number of conflicts.

    For example, if a single row with a total balance value is frequently updated, split this row into a hundred rows and calculate the total balance as a sum of these rows. This will drastically reduce the number of **transaction locks invalidated** errors.
