This is old and out-dated. Please see https://doc.replicache.dev/strategies/overview instead.

🌁 Overview

The Global Hash Strategy is relatively easy to implement and very easy to maintain.

In contrast to every other pattern presented here, no additional fields need to be added to server-side entities and no special care needs to be taken to maintain those fields. In contrast to The Last-Modified Strategy and The Global Version Strategy, no special care needs to be taken around deletes and selection changes.

The downside is that it is also the most expensive strategy in terms of database read load. Each pull request causes a full read of all of the data backing the client view.

👩🏻‍🏫 How it Works

Setup

  1. Allocate some storage somewhere to cache historical client views in. This storage need not be durable nor transactional with any other storage. For example, it could be redis or memcached.

On Pull

  1. Read the entire client view from primary storage (or a cache as necessary/available)
  2. Compute a hash over the serialization
  3. Read the previous hash, if any, from the pull request cookie
  4. If they match, there are no changes — return an empty patch with same response cookie
  5. Otherwise, if there is a request cookie, lookup the corresponding preview client view in the cache
  6. If not found, return a reset patch — a clear op followed by put ops for each entry in the Client View
  7. If found, compute a diff between the old and new client views and return that as the patch along with the new cookie

😓 Challenges

The challenge is the load on the primary database. Every pull reads all data.

🌈 Variations

A way to reduce load is to break the Client View down into more granular pieces and use some other strategy for each piece, then the hash within the piece. For example, if you are application is syncing spreadsheets, where each spreadsheet is modeled as a set of key/value pairs in the Client View, you could use The Global Version Strategy or The Row Version Strategy to track the version of each spreadsheet, and then the The Global Hash Strategy for all data within the spreadsheet.