Change Cache and Channel Caches - adamcfraser/cbnotes GitHub Wiki

Change Cache

The change cache manages interaction between the server feed and the channel caches. Whenever the feed receives a document, it gets routed to the Change Cache. The Change Cache does sequence buffering, and routes the document through to the appropriate Channel Caches.

On Sync Gateway startup, it sets the initialSequence for the Change Cache to the current sequence number for the database (retrieved from the _sync:seq document using the DB's sequence allocator). Any incoming documents with sequences earlier than initialSequence will be ignored. This should prevent issues with missing sequence numbers on SG restart - it only cares about documents that are written after the sync gateway starts.

For each document on the feed, it calls processEntry:

  • The change cache maintains a map, receivedSeqs, of all sequences that have been received (a map of empty struct, indexed by seq id). processEntry checks if the sequence has already been received, and if not, adds to this map.
  • buffers the sequences:
    • If the sequence is the expected next sequence, calls _addToCache, then checks whether the next sequence(s) have been buffered in pendingLogs - if so, pops from queue and does _addToCache for each
    • If it's not the next expected sequences, pushes to pendingLogs priority queue
    • If there are more than 10000 (MaxChannelLogPendingCount) pending, it gives up and writes the oldest.
    • If it receives an earlier sequence (< nextSequence) that's later than the initial sequence, logs a warning to the Cache log channel and adds to cache

Has a periodic cleanup function that looks for documents that have been in the pendingLogs cache too long (MaxChannelLogPendingWaitTime), in case the feed has gone quiet.

_addToCache(change):

  • for each channel, calls channelCache.addToCache
  • unless it's an old seq, updates change cache's 'nextSequence' to change.sequence + 1

Channel Caches

A channel cache stores a recent history of revisions seen on the feed in memory. Has a min and max cache size (50/500). Retains the minimum (when available). When cache size is higher than min but below max, revisions are kept for 60 seconds.

A channel cache maintains a cacheValidFrom value.

The GetChanges method takes a 'since sequence' as input (options.Since.Seq). If this value is larger than cacheValidFrom, the cache returns the data from the cache. If not, it executes a view query to satisfy the query. If there's room in the cache, it backfills the result of the query into the cache and updates cacheValidFrom.

When the change cache gets a new revision from the feed for a given channel, it calls addToCache on the channel cache. In addition to adding that entry to the cache, this triggers a cache pruning (based on min size, timeout)