# Storage-Offline 1:1 Chat

* [Flowchart](#flowchart)
  * [Initial design](#initial-design)
  * [Improve with sequence num](#improve-with-sequence-num)
* [Storage requirements](#storage-requirements)
  * [Initial schema](#initial-schema)
  * [Improved schema: Decouple msg content from sender and receiver](#improved-schema-decouple-msg-content-from-sender-and-receiver)

## Flowchart

### Initial design

![](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-0420f2149d7af531e9029657be2c189e2907f19c%2Fmessenger_offline_sync_original.png?alt=media)

### Improve with sequence num

![](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-c088a4e8f59b2b62fa66e6533b8105bfc9f0702b%2Fmessenger_offline_sync.png?alt=media)

## Storage requirements

* Requirement1: Query all 1-on-1 conversations a user participates in after a given timestamp.
* Requirement2: For each conversation, load all messages within that conversation created later than a given timestamp.

### Initial schema

![](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-7a4f06c3ea3e19cf51d5d33a75a07b4856e988c0%2Fim_groupchat_recentContact_one_to_one.png?alt=media)

### Improved schema: Decouple msg content from sender and receiver

* Intuition:
  * Even if sender A deletes the message on his machine, the receiver B should still be able to see it
  * Create a message\_content table and message\_index table

![](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-1ee6db3ac7f6edd61668a8bfb05e330009cd47d1%2Fim_groupchat_recentContact_1to1_decouple.png?alt=media)
