Handling Firebase Realtime Database merge conflicts with Cloud Functions

495 views
Skip to first unread message

Andy Geers

unread,
Jan 15, 2018, 1:51:26 PM1/15/18
to Firebase Google Group
I'm wanting to have a bit more control over how merge conflicts are handled with a project that uses the Realtime Database, and was wondering if it's possible to use Cloud Functions to handle them?

I'm specifically addressing a situation where one device was offline when a change was made, then a second (online) device makes a change, and then the first device comes back online. I am not using persistence since I already store all user data offline in a SQLite database, and want to be able to reconcile the data from the two devices. Specifically, the plan is that each record will contain a timestamp of when the a text field was last changed, and I want to be able to block uploads that have an out-of-date timestamp - can I do that using Cloud Functions?

Andreas B

unread,
Jan 15, 2018, 3:31:42 PM1/15/18
to Firebase Google Group
You can use security rules to allow a write only if the timestamp that is supposed to be written is later than the timestamp already stored in the database.

Something like
".write": "data.child('timestamp').val() < newData.child('timestamp').val()",
".validate": "newData.child('timestamp').exists() && newData.child('timestamp').isNumber()"

There might be a problem with having an exact, shared time between devices in the first place - but if that's not an issue (for example because writes typically happen hours apart instead of milliseconds), this might be sufficient.

Kato Richardson

unread,
Jan 16, 2018, 10:19:08 AM1/16/18
to Firebase Google Group
You probably don't need Functions for this unless there are complexities for your use case that you haven't shared here. Simply deny write when the client comes back online via rules, and then have the client handle the merge appropriately and try again.

To directly answer your question, Functions can certainly do this. You'd probably want to send writes to an event queue (i.e. a separate path) and have Functions perform the write to the final data after deciding on the merge strategy. 

Note that if you're working with something complex like collaborative docs, you don't want merges, you want a composite pattern similar to what we did with Firepad.

> There might be a problem with having an exact, shared time between devices 
You can avoid the time sync issues by just using an incremental update counter instead of a timestamp.

☼, Kato


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/85d35229-57a8-4c94-a8eb-fa0613b74c23%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages