Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Offline first collaboration with event sourcing

Offline first collaboration with event sourcing

Steven Chan

May 10, 2017
Tweet

More Decks by Steven Chan

Other Decks in Programming

Transcript

  1. Apple CloudKit • Remote database API • Need to implement

    syncing with local cache ourselves • Designing a correct algorithm to sync two object graphs is hard • Limited schema migration support • Undocumented limit for sharing records
  2. Google Firebase • Real-time database • Mobile SDKs available •

    Offline mode: store updates in a queue • Performance become worse as the queue become long
  3. Event sourcing to rescue 1. Store all changes as events

    in a single queue 2. Only need to sync the event queue 3. Derive the app state from the event queue 4. Migration = rewrite the derivation function
  4. Syncing • Server: maintain an event queue = the source

    of truth • Clients: upload local events to server, download unseen events, and then update the app state. • That’s it!
  5. Syncing A B C A B C D E F

    Client Server F D E
  6. Syncing A B C A B C D E F

    Client Server F D E
  7. Syncing A B C A B C D E F

    Client Server F D E
  8. Conflicts handling • Pick the latest value by update time

    • Pick the value with the largest update count • Use vector clocks to detect conflicts
  9. Q&A