|
| 1 | +# Long Transactions Detection |
| 2 | + |
| 3 | +When working with databases, 🐙 userver provides the ability to create a transaction in code and execute database |
| 4 | +queries within it. From creation to commit/rollback, a transaction holds one of the database connections entirely. |
| 5 | +If a heavy operation is called during the transaction’s lifetime (e.g., an HTTP request), then in case of an incident |
| 6 | +(e.g., the service being called via HTTP goes down), the execution time of that operation will increase. |
| 7 | +Consequently, the transaction’s lifetime will also increase while the connection is hold, which may cause the entire |
| 8 | +database to fail. |
| 9 | + |
| 10 | +Consequently there is a built-in mechanism for long transactions detection. See also @ref utils::trx_tracker. |
| 11 | + |
| 12 | + |
| 13 | +## Drawbacks of Long Transactions |
| 14 | + |
| 15 | +* **Exhaustion of the connection pool**. Each transaction occupies a database connection for its entire duration. |
| 16 | + Prolonged transactions tie up more connections, risking exhaustion of the connection pool. |
| 17 | +* **Exhaustion of database resources**. To ensure transaction isolation between data writing and completion, the |
| 18 | + database must maintain two versions of each row (old and new). With many concurrent transactions, the number of row |
| 19 | + duplicates grows significantly, leading to substantial increases in table storage usage. |
| 20 | +* **Execution linearization**. Only one operation can modify a specific database row at a time; others must wait. |
| 21 | + If a writing transaction becomes blocked, all subsequent write operations are also halt prepared to proceed. |
| 22 | + |
| 23 | + |
| 24 | +## Where the Long Transactions Detection check works |
| 25 | + |
| 26 | +To detect long operations within transactions, we insert a check for active transactions into every heavy operation |
| 27 | +in userver. When triggered, this check writes a log and a metric. |
| 28 | + |
| 29 | +The check is inserted into: |
| 30 | + |
| 31 | ++ HTTP requests: |
| 32 | + + Synchronous code‑generated requests; |
| 33 | + + Waiting for asynchronous code‑generated requests; |
| 34 | + + Working with STQ (CallLater, Reschedule, ...); |
| 35 | ++ gRPC requests. |
| 36 | + |
| 37 | +Transactions from the following databases are monitored: |
| 38 | + |
| 39 | ++ MySQL; |
| 40 | ++ PostgreSQL; |
| 41 | ++ SQLite; |
| 42 | ++ YDB. |
| 43 | + |
| 44 | +The check works in testsuite tests but is disabled in gtest and gbenchmark. |
| 45 | + |
| 46 | + |
| 47 | +## How the Long Transactions Detection check failure is displayed? |
| 48 | + |
| 49 | +**Logs**: |
| 50 | + |
| 51 | +When the check is triggered, the service will spam `WARN` logs with the following text: |
| 52 | +`Long call while having active transactions`. The `meta.location` field displays the name of the method that calls the |
| 53 | +endpoint where the check was triggered. |
| 54 | + |
| 55 | +**Metrics**: |
| 56 | + |
| 57 | +Metric: `engine.heavy-operations-in-transactions` is incremented in case of heavy operation in transaction. |
| 58 | + |
| 59 | + |
| 60 | +## How to fix the Long Transactions Detection check failure |
| 61 | + |
| 62 | +Move the heavy request out from the transaction. |
| 63 | + |
| 64 | +If the bahavior is understood and it is fine to have a long transaction in that particular case, the check could be |
| 65 | +disabled for a scope via @ref utils::trx_tracker::CheckDisabler : |
| 66 | + |
| 67 | +@snippet utils/trx_tracker_test.cpp Sample CheckDisabler usage |
| 68 | + |
| 69 | +To disable the check for the whole service use the `enable_trx_tracker` static configuration option of |
| 70 | +@ref components::ManagerControllerComponent. Note that this is **not** recommended. |
| 71 | + |
| 72 | + |
| 73 | +---------- |
| 74 | + |
| 75 | +@htmlonly <div class="bottom-nav"> @endhtmlonly |
| 76 | +⇦ @ref scripts/docs/en/userver/dump_coroutines.md | @ref scripts/docs/en/userver/caches.md ⇨ |
| 77 | +@htmlonly </div> @endhtmlonly |
| 78 | + |
0 commit comments