When I run the following function async a few times, so the same function is getting called many times in parallel, I’m running into Contended Transaction
export const GetTest = function (size: number) {
return Map(
Select(
["data"],
Paginate(
Join(
Range(
Match(Index(Thing_Indexes.THINGS_BY_TIME)),
[],
Now()
),
Lambda(["time", "ref"], Match(Index(Thing_Indexes.THINGS_BY_SERVED), Var("ref")))),
{ size: size })
),
Lambda([ThingModel.COUNT_SERVED, "ref"],
Let({
updateCount: Update(Var("ref"), {
data: {
[ThingModel.COUNT_SERVED]: Add(Select(["data", ThingModel.COUNT_SERVED], Get(Var("ref"))), 1)
},
})
}, Var("updateCount"))
)
)
};
in parallel, I’m seeing
"code": "contended transaction",\n' +
' "description": "Transaction was aborted due to detection of concurrent modification."\n' +
Now, let me explain the reason for this query.
I don’t actually care, at this point in time, about tracking COUNT_SERVED. This was an attempt to ensure that my Things
are served evenly. Eg if I have 10 things, I want all 10 of them to get equal exposure if only 2 of them are being served at at time.
My preferred solution would be to use a randomizer and randomly select a Thing
to be served. This would also solve my use case, because over a large number of queries the Things
would be served evenly.
And I’m currently investigating faunadb-fql-lib/RandomString.ts at master · shiftx/faunadb-fql-lib · GitHub which I found from Random number generation
It does seem like this issue falls into Best practices :: Fauna Documentation and Scaling write throughput :: Fauna Documentation
But if I can get a quick win with the random approach, I’ll be sticking to that. Wondering if there are any thoughts? Thank you