Aidbox / Issues

Issue tracker for Aidbox FHIR backend by Health Samurai
7 stars 0 forks source link

Conflicts on frequent conditional update #563

Closed pavlushkin closed 9 months ago

pavlushkin commented 10 months ago

Question

Aidbox Version: stable v:2302-lts.829cd38d AWS RDS Engine version 14.8

Scenario:

Result:

{
  "resourceType": "OperationOutcome",
  "id": "resource-has-been-changed",
  "text": {
    "status": "generated",
    "div": "Requested resource has been changed since the start of transaction"
  },
  "issue": [
    {
      "severity": "fatal",
      "code": "conflict",
      "diagnostics": "Requested resource has been changed since the start of transaction"
    }
  ]
}

I am struggle to identify an exact bottleneck here. Do you have ideas how to fix this?

milaryz commented 9 months ago

This problem was fixed in 2304. If you don't use multi-instance Aidbox, then set the

BOX_CACHE_REPLICATION_DISABLE=true

environment variable (or the equivalent option in the Aidbox configuration project).

Details: in Aidbox prior to 2304 there was a _cache_bigint table, which was mostly used for the Changes API. This table stored latest version id per resource type. To maintain this table, Aidbox updated it during every PUT/POST/DELETE query. For example, if you had created two Patient resources, Aidbox would have updated the single row in the _cache_bigint table two times.

For conditional operations and transaction Aidbox uses SERIALIZABLE transaction isolation level. This is essential to enforce the absence of duplicates. In this level Postgres can reject transactions to prevent serialization anomalies. https://www.postgresql.org/docs/current/transaction-iso.html

Concurrent updates of the same row of the _cache_bigint table were often the reason for Postgres to reject transactions.

In older versions of Aidbox BOX_CACHE_REPLICATION_DISABLE=true disabled use of the _cache_bigint table (it was only needed for the multi-instance scenario). From 2304 Aidbox does not use the _cache_bigint table.


Regarding queries, Aidbox shows queries only for Search API when you use AIdbox format or _explain parameter. You can enable log_statement=all Postgres option to see full SQL queries in the Postgres logs. But this option could significantly reduce Postgres performance.

milaryz commented 9 months ago

The issue has been resolved.