Distributed Channel Cache: Current behaviour - adamcfraser/cbnotes GitHub Wiki

Current in-memory cache functionality

The in-memory cache consists of a single change cache, which manages a collection of channel caches. When a mutation is received on the server feed (TAP or DCP), it's sent to the change cache for processing. There are some specifics in this handling worth calling out, to ensure they are carried over to an external caching approach.

Change Cache responsibilities

  • Sequence management. Every revision arriving on the TAP feed has a sequence number, but they don't arrive on the feed in order. In order to service incoming 'changes since x' requests, the changes cache needs to know which contiguous sequences it has seen, so it can guarantee that the client doesn't miss any sequences. This includes:
    • Tracking nextSequence - the lowest sequence not yet processed by the cache
    • Tracking pending sequences - sequences higher than nextSequence that have arridved, that need to be cached once all preceding sequences have been cached
    • Tracking skipped sequences - to avoid long blocking waiting for nextSequence, the changeCache has the ability to skip sequences. (#525 functionality)
  • Sequence deduplication. There are cases where the same sequence arrives multiple times on the TAP feed. The change cache ignores duplicate sequences
  • Handling user update sequences

Channel Cache responsibilities

  • Cache of recent sequences for the channel. Keeps a minimum of 50 entries, maximum 500 entries.
  • Doc ID deduplication. When a new sequence is written to the channel cache, any previous sequences sharing the same sequence are removed from the cache. This avoids the cache getting filled with 50 (or 500) revisions of a single volatile document.
  • Tracking 'validFrom', which identifies the oldest sequence that a cache is valid from. Any incoming queries for data older than that sequence need to be retrieved via a view query
  • Executing view queries, and backfilling the channel cache from the results when appropriate.

API Notes

Change Cache API used for feed processing

  • DocChanged

Change Cache API used during _changes processing

  • GetChangesInChannel(channel, options)
  • getOldestSkippedSequence()

Change Cache API used for other processing

  • GetChangeLog(channelname, since) - used by handleDumpChannel

Channel Cache API used during feed processing

  • addToCache

Channel Cache API used during _changes processing

  • InitLateSequenceClient
  • GetLateSequencesSince
  • ReleaseLateSequenceClient
  • GetCachedChanges(options)
  • GetChanges(options)

Channel Cache API used during other processing

  • PruneCache