Firestore session issues

Hi,

I have a PHP8.2 application deployed in App Engine. Something that regularly occurs, and this is causing problems, is that there's a session error message from Google Firestore. This occurs when there are multiple users in the application. At night or on a a test service that isn't used by others this doesn't seem to occur.

This is the error message.

{
  "textPayload": "    \"message\": \"Aborted due to cross-transaction contention. This occurs when multiple transactions attempt to access the same data, requiring Firestore to abort at least one in order to enforce serializability.\",",
  "insertId": "6617abf4000821249a61370d",
  "resource": {
    "type": "gae_app",
    "labels": {
      "version_id": "...",
      "project_id": "...",
      "module_id": "...",
      "zone": "europe-west3-1"
    }
  },
  "timestamp": "2024-04-11T09:23:00.532772Z",
  "labels": {
    "clone_id": "00a22404dce48a9d883cbff5e20d7eaf739d23e76c30a7d5feafe25139a14d5762fa403dcce69dc3e48d7645bbefe61f0295e8e3039376a4deb7eacf0628391dfd"
  },
  "logName": ".../logs/stderr",
  "receiveTimestamp": "2024-04-11T09:23:00.534529602Z"
}

The error message in full:
Aborted due to cross-transaction contention. This occurs when multiple transactions attempt to access the same data, requiring Firestore to abort at least one in order to enforce serializability.

There must be something I've missed when setting up the session handling. What I've done is to set up the session handling as follows. This was is from Google Cloud's documentation (https://cloud.google.com/php/getting-started/session-handling-with-firestore)

$firestore = new \Google\Cloud\Firestore\FirestoreClient([
    'projectId' => $projectId,
]);
$handler = $firestore->sessionHandler(['gcLimit' => 500]);
// Configure PHP to use the the Firebase session handler.
session_set_save_handler($handler, true);
session_save_path('sessions');
session_start();
unset($handler, $firestore);

There are not more than 500 users. Does the sessions not be be closed on each request? Is a php.ini directive required for closing sessions?

Any help very much appreciated.

 

 

0 1 74
1 REPLY 1

Hello @John8543855,

Welcome to the Google Cloud Community!

Your error message is related to how transactions are handled when multiple users access your application at the same time. Since Firestore needs transactions that modify the same data to run one at a time for consistency, this issue arises.

For more details on this error, check our official documentation on Firestore Aborted errors here

To fix this issue, consider these suggestions:
1. Firestore can automatically adjust to handle sudden increases in traffic, which may initially cause delays.
2. Avoid hot-spots to help Firestore scale effectively. Learn more about designing for scale.
3. Check for data conflicts in transactions and how you use them.
4. Try reducing the frequency of updates to individual documents.

For additional insights, see this StackOverflow discussion on the same topic.