`contended transaction` error

Hi @nichoth.

Contention refers to a transaction vying for control of a document or index but that keep changing.

In this topic, I go through a step-by-step scenario of contention.

It looks like you are reading a document and trying to update it with a new value (increment it by 1). If there are too many requests made simultaneously then that will most assuredly cause contention.

For something like this counter, or “reactions” (think likes on Facebook, or counting number of retweets), or similar updates that can happen in bursts, you may need to consider something like the “Event Sourcing Pattern” described here: This tutorial describes a strategy for reducing database contention in high write throughput situations. - Fauna Documentation. It’s written with FQL v4 in mind, but the concept still applies to FQL v10.

Speaking of FQL v10, you might find a query like this easier to read. It will still have the same issue with contention, though.

// v10
let logMeta = log_meta.all().first()!
let doc = actions.by_id(${id}) // assuming new v10 index added to `actions` collection.

if (doc.exists()) {
  abort("That ID already exists")
}

logMeta.update({ last_added: logMeta.last_added + 1 })
actions.create(${entry})
1 Like