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

Stockage offline & synchro automatique pour vos...

Stockage offline & synchro automatique pour vos applications web et mobiles

De plus en plus de développeurs web et mobiles utilisent du stockage local pour assurer de bons temps de réponse et que les applications continuent de fonctionner en absence de connection réseau. Mais comment synchronisez vous le stockage local avec le backend ? Comment resolvez-vous les conflits ? Dans ce talk, je vous présente un framework open source (Amplify DataStore) qui vous permet de développer des applications webs et mobiles modernes qui répondent à ces questions avec un minimum de ligne de code.

More Decks by Sébastien Stormacq - AWS Developer Advocate

Other Decks in Technology

Transcript

  1. OFFLINE FIRST Core features should function with or without an

    internet connection. WHAT DOES IT MEAN? #01
  2. OFFLINE FIRST Data is written to and queried locally from

    the end user’s device and periodically uploaded and replicated in the database. WHAT DOES IT MEAN? #02
  3. OFFLINE FIRST Provide end users with a consistent UX when

    internet connectivity is slow or non-existent. WHAT DOES IT MEAN? #03
  4. REAL TIME An application that functions within a time frame

    that the user senses as immediate or very close to it. WHAT DOES IT MEAN #01
  5. REAL TIME Cohesive system where data is in sync across

    all connected devices. WHAT DOES IT MEAN #02
  6. 17 There are only two hard things in Computer Science:

    cache invalidation and naming things.
  7. 17 There are only two hard things in Computer Science:

    cache invalidation and naming things.
  8. Storage Engine Storage Adapter CRUD OBSERVES WRITES READS WRITES OBSERVES

    Sync Engine QUERIES/MUTATIONS SUBSCRIPTIONS ARCHITECTURE DataStore API Developer
  9. Storage Engine Storage Adapter CRUD OBSERVES WRITES READS ARCHITECTURE DataStore

    API Developer Model converted to / from the storage adapter of choice Object and action persistence with specific storage engine (SQL Lite, IndexDB, etc..)
  10. Storage Engine Storage Adapter CRUD OBSERVES WRITES READS OBSERVES WRITES

    / READS Sync Engine QUERIES/MUTATIONS SUBSCRIPTIONS ARCHITECTURE DataStore API Developer
  11. Storage Engine Storage Adapter CRUD OBSERVES WRITES READS OBSERVES WRITES

    / READS Sync Engine QUERIES/MUTATIONS SUBSCRIPTIONS ARCHITECTURE DataStore API Developer Model converted to GraphQL statement Response converted to Model and written to storage engine.
  12. Conflict Free Replicated Data Types (CRDTs) Operational Transforms Google Docs,

    MS Office Online Redis, Facebook, CosmosDB CONFLICT DETECTION & RESOLUTION 2006 1989
  13. person = { name: “Jennifer”, age: 32 } person =

    { name: “Jennifer”, age: 32, title: “Developer”, } person = { name: “Jennifer”, age: 32, title: “Engineer” } original item client a client b CONFLICT RESOLUTION
  14. person = { name: “Jennifer”, age: 32, title: “Developer” }

    person = { name: “Jennifer”, age: 32, title: “Engineer” } client a client b CONFLICT RESOLUTION
  15. person = { name: “Jennifer”, age: 32, title: “Developer” }

    person = { name: “Jennifer”, age: 32, title: “Engineer” } client a client b CONFLICT RESOLUTION person = { name: “Jennifer”, age: 32, title: “Developer” } new item
  16. person = { name: “Jennifer”, age: 32 } person =

    { name: “Jennifer”, age: 32, location: “Nebraska”, } person = { name: “Jennifer”, age: 32, title: “Engineer” } original item client a client b CONFLICT RESOLUTION
  17. person = { name: “Jennifer”, age: 32, location: “Nebraska” }

    person = { name: “Jennifer”, age: 32, title: “Engineer” } client a client b CONFLICT RESOLUTION
  18. person = { name: “Jennifer”, age: 32, location: “Nebraska” }

    person = { name: “Jennifer”, age: 32, title: “Engineer” } client a client b CONFLICT RESOLUTION person = { name: “Jennifer”, age: 32, location: “Nebraska”, title: “Engineer” } new item
  19. person = { name: “Jennifer”, age: 32 hobbies: [“climbing"] }

    person = { name: “Jennifer”, age: 32, hobbies: [“climbing”, “reading”] } person = { name: “Jennifer”, age: 32, hobbies: [“climbing”, “karaoke”] } original item client a client b CONFLICT RESOLUTION Lists
  20. person = { name: “Jennifer”, age: 32, hobbies: [“climbing”, “reading”]

    } person = { name: “Jennifer”, age: 32, hobbies: [“climbing”,“karaoke”] } client a client b CONFLICT RESOLUTION Lists
  21. person = { name: “Jennifer”, age: 32, hobbies: [“climbing”, “reading”]

    } person = { name: “Jennifer”, age: 32, hobbies: [“climbing”,“karaoke”] } client a client b CONFLICT RESOLUTION Lists person = { name: “Jennifer”, age: 32, hobbies: [“climbing”, “karaoke”, “reading”] } new item
  22. AMPLIFY DATASTORE CONFLICT DETECTION & RESOLUTION • Monotonic counters •

    Controlled by the system itself (AppSync) • GraphQL types with versions used for merge or reject decisions • Base table & change table with soft deletes & TTL
  23. MONOTONIC COUNTER person = { id: 100, age: 30, _version:

    1 } person = { id: 100, age: 31, _version: 2 } person = { id: 100, age: 32, _version: 3 } id age name _version 100 32 Amy 3 101 28 Charles 9 102 47 Melissa 8 Base Table
  24. ds_pk ds_sk age name _version _lastChangedAt Person-9386:2020-01-23 13:16:08.127:9386:1 32 Amy

    1 1579785368127 Person-9386:2020-01-23 13:16:21.785:9386:2 33 Amy 2 1579785381785 Person-9386:2020-01-23 13:16:37.859:9386:3 34 Amy 3 1579785397859 Change Table CONFLICT RESOLUTION
  25. ds_pk ds_sk age name _version _lastChangedAt Person-9386:2020-01-23 13:16:08.127:9386:1 32 Amy

    1 1579785368127 Person-9386:2020-01-23 13:16:21.785:9386:2 33 Amy 2 1579785381785 Person-9386:2020-01-23 13:16:37.859:9386:3 34 Amy 3 1579785397859 Change Table typename + uuid + date CONFLICT RESOLUTION
  26. ds_pk ds_sk age name _version _lastChangedAt Person-9386:2020-01-23 13:16:08.127:9386:1 32 Amy

    1 1579785368127 Person-9386:2020-01-23 13:16:21.785:9386:2 33 Amy 2 1579785381785 Person-9386:2020-01-23 13:16:37.859:9386:3 34 Amy 3 1579785397859 Change Table typename + uuid + date timestamp + uuid + version CONFLICT RESOLUTION
  27. User A User B person = { age: 32, _version:

    1 } person = { age: 32, _version: 1 }
  28. User A User B person = { age: 32, _version:

    1 } person = { age: 33, _version: 2 } person = { age: 32, _version: 1 }
  29. User A User B person = { age: 32, _version:

    1 } person = { age: 33, _version: 2 } person = { age: 33, _version: 2 } person = { age: 32, _version: 1 }
  30. User A User B person = { age: 32, _version:

    1 } person = { age: 33, _version: 2 } person = { age: 33, _version: 2 } person = { age: 32, _version: 1 } OFFLINE
  31. User A User B person = { age: 32, _version:

    1 } person = { age: 33, _version: 2 } person = { age: 33, _version: 2 } person = { age: 32, _version: 1 } OFFLINE person = { age: 34, location: “Seattle” _version: 3 }
  32. User A User B person = { age: 32, _version:

    1 } person = { age: 33, _version: 2 } person = { age: 33, _version: 2 } person = { age: 32, _version: 1 } person = { age: 35, _version: 3 } OFFLINE person = { age: 34, location: “Seattle” _version: 3 }
  33. User A User B person = { age: 32, _version:

    1 } person = { age: 33, _version: 2 } person = { age: 33, _version: 2 } person = { age: 32, _version: 1 } person = { age: 35, _version: 3 } OFFLINE ONLINE person = { age: 34, location: “Seattle” _version: 3 }
  34. person = { age: 34, age: 35, location: “Seattle” _version:

    4 } User A User B person = { age: 32, _version: 1 } person = { age: 33, _version: 2 } person = { age: 33, _version: 2 } person = { age: 32, _version: 1 } person = { age: 35, _version: 3 } OFFLINE ONLINE person = { age: 34, location: “Seattle” _version: 3 }
  35. User A User B person = { age: 32, _version:

    1 } person = { age: 33, _version: 2 } person = { age: 33, _version: 2 } person = { age: 32, _version: 1 } person = { age: 35, _version: 3 } OFFLINE ONLINE person = { age: 34, location: “Seattle” _version: 3 } person = { age: 34, location: “Seattle” _version: 4 } person = { age: 35, location: “Seattle” _version: 4 }
  36. AUTO MERGE SYNC ENABLED GRAPHQL RESOLVERS Base table + change

    table + GraphQL types with versions used for merge or reject decisions
  37. id age name _version 100 32 Amy 8 101 28

    Charles 9 SYNCHRONIZATION - BASIC IMPLEMENTATION 102 47 Melissa 3
  38. id age name _version 100 32 Amy 8 101 28

    Charles 9 SYNCHRONIZATION - BASIC IMPLEMENTATION 102 47 Melissa 3
  39. id age name _version 100 32 Amy 8 101 28

    Charles 9 SYNCHRONIZATION - BASIC IMPLEMENTATION 102 47 Melissa 3
  40. id age name _version 100 32 Amy 8 101 28

    Charles 9 DELETE ID 102 SYNCHRONIZATION - BASIC IMPLEMENTATION 102 47 Melissa 3
  41. id age name _version 100 32 Amy 8 101 28

    Charles 9 DELETE ID 102 SYNCHRONIZATION - BASIC IMPLEMENTATION
  42. id age name _version 100 32 Amy 8 101 28

    Charles 9 DELETE ID 102 SYNCHRONIZATION - BASIC IMPLEMENTATION
  43. ds_pk ds_sk age name _version _lastChangedAt Person-9387:2020-01-23 13:16:08.127:9387:7 22 James

    7 1579785368127 Person-9388:2020-01-23 13:16:21.785:9388:3 41 Amy 3 1579785381785 Change Table SYNCHRONIZATION - DELTA SYNC
  44. ds_pk ds_sk age name _version _lastChangedAt Person-9387:2020-01-23 13:16:08.127:9387:7 22 James

    7 1579785368127 Person-9388:2020-01-23 13:16:21.785:9388:3 41 Amy 3 1579785381785 Change Table SYNCHRONIZATION - DELTA SYNC
  45. ds_pk ds_sk age name _version _lastChangedAt Person-9387:2020-01-23 13:16:08.127:9387:7 22 James

    7 1579785368127 Person-9388:2020-01-23 13:16:21.785:9388:3 41 Amy 3 1579785381785 Change Table SYNCHRONIZATION - DELTA SYNC
  46. ds_pk ds_sk age name _version _lastChangedAt Person-9387:2020-01-23 13:16:08.127:9387:7 22 James

    7 1579785368127 Person-9388:2020-01-23 13:16:21.785:9388:3 41 Amy 3 1579785381785 Change Table SYNCHRONIZATION - DELTA SYNC Person-9389:2020-01-23 13:16:37.859:9389:1 34 Jennifer 1 1579785397859
  47. ds_pk ds_sk age name _version _lastChangedAt Person-9387:2020-01-23 13:16:08.127:9387:7 22 James

    7 1579785368127 Person-9388:2020-01-23 13:16:21.785:9388:3 41 Amy 3 1579785381785 Change Table SYNCHRONIZATION - DELTA SYNC Person-9389:2020-01-23 13:16:37.859:9389:1 34 Jennifer 1 1579785397859
  48. ds_pk ds_sk age name _version _lastChangedAt Person-9387:2020-01-23 13:16:08.127:9387:7 22 James

    7 1579785368127 Person-9388:2020-01-23 13:16:21.785:9388:3 41 Amy 3 1579785381785 Change Table SYNCHRONIZATION - DELTA SYNC syncPerson(lastSync: 13:16:22.127) Person-9389:2020-01-23 13:16:37.859:9389:1 34 Jennifer 1 1579785397859
  49. ds_pk ds_sk age name _version _lastChangedAt Person-9387:2020-01-23 13:16:08.127:9387:7 22 James

    7 1579785368127 Person-9388:2020-01-23 13:16:21.785:9388:3 41 Amy 3 1579785381785 Change Table SYNCHRONIZATION - DELTA SYNC syncPerson(lastSync: 13:16:22.127) Person-9389:2020-01-23 13:16:37.859:9389:1 34 Jennifer 1 1579785397859 Person-9389:2020-01-23 13:16:37.859:9389:1 34 Jennifer 1 1579785397859
  50. id age name _version _deleted _ttl 100 32 Amy 8

    101 28 Charles 9 102 47 Melissa 3 TRUE 1582384003 SYNCHRONIZATION ds_pk ds_sk age _deleted _ttl Person-9386:2020-01-23 13:16:08.127:9386:1 32 1582384003 Person-9386:2020-01-23 13:16:21.785:9386:2 33 1582384003 Person-9386:2020-01-23 13:16:37.859:9386:3 47 TRUE 1582384003 Change Table Base Table
  51. TIME TO LIVE ds_pk ds_sk age _deleted _ttl Person-9386:2020-01-23 13:16:08.127:9386:1

    32 1582384003 Person-9386:2020-01-23 13:16:21.785:9386:2 33 1582384003 Person-9386:2020-01-23 13:16:37.859:9386:3 47 TRUE 1582384003
  52. TIME TO LIVE ds_pk ds_sk age _deleted _ttl Person-9386:2020-01-23 13:16:08.127:9386:1

    32 1582384003 Person-9386:2020-01-23 13:16:21.785:9386:2 33 1582384003 Person-9386:2020-01-23 13:16:37.859:9386:3 47 TRUE 1582384003 1582384003 1582384003 1582384003