5

We're using Firebase as a backend for our mobile app. Some of our users have sporadically received an error "maxretry" with a transaction writing to a path with single numeric value. We don't have multiple users or connections, nor multiple writes to the same path, as far as I know. What might be causing this?

I have a suspicion that this is caused by using floating point values with many decimal places. This error happened to me locally once and I was able to resolve it by limiting the precision to two decimal places. Can this be it?

-Albert

Edit:

Here's the code that is causing this:

return fireRef.child(fbPath).transaction(function(originalVal) {
  return func(originalVal, by_value);
}, _.noop, false)

where in this case the func looks like this:

function(originalVal, val) {
  return val + (originalVal || 0);
}
aliz_bazar
  • 272
  • 1
  • 2
  • 14
  • Without seeing code that reproduces this problem, it'll be hard to say more than what the error message already says: there are too many retries of a transaction. – Frank van Puffelen May 03 '16 at 13:34
  • The function is run like a hundred times until the error occurs. No other calls are done to the same function / the same path (at least as far as I know). The `originalVal` was something like `57.39999999999998` and val something like `1.01`. – aliz_bazar May 03 '16 at 14:10
  • Given that Firebase transactions are run using a compare-and-swap, the floating point operand is likely the cause of this problem. If that is the case, the solution is to not use floating point, but instead use a discrete value and a multiplier of that, i.e. `5740` for `57.40` – Frank van Puffelen May 03 '16 at 14:52
  • That sounds quite overkill to have a multiplier and the value itself for each value transaction is conducted. Don't you think just storing with a precision of, say, two decimal places would be enough? – aliz_bazar May 03 '16 at 15:15
  • I suggest a fixed multiplier in that case. It's quite common when dealing with money to store everything in "cents". I'm not saying it's the best way (because others may disagree), but I guarantee you that it's common. :-) – Frank van Puffelen May 03 '16 at 17:21

1 Answers1

5

The problem persisted even after limiting precision to 2 decimals (getting maxretry error every once in a while).

It looks like when updating a value using Firebase transactions floating point type should not be used at all.

I moved to using integers and haven't had the problem anymore.

aliz_bazar
  • 272
  • 1
  • 2
  • 14
  • While this works, but there is no firebase documentation showing that decimal points should not be used for transactions. But I have also seen this error when using transactions with decimal points. This happens randomly. But at this point, where I have a number `63.99999999999999`, it's not working any more. I have changed the number to `64` and thid has started working again. @Firebase team should clarify this. – hkchakladar Feb 05 '20 at 13:29