Skip to content

Cache Writer optimization

Adam Fraser edited this page Mar 31, 2015 · 7 revisions

We want to optimize the cache writer to support:

  • parallel cache writes for tap feed entries

Non-sparse Cache Design

####Data Model Cache Block

Key format: _cache_block:[channelName]:[blockIndex]

Raw storage, with the format

  • Sequence offset- 8 bytes. Starting sequence for the block.
  • Sequence array - 1 byte per sequence, indexed as (sequence - sequenceOffset). Currently only using two bits of each byte for information:
    • bit 0 - flag for whether the sequence is in this channel
    • bit 1 - flag for whether the sequence indicates a removal from the channel

Cache clock

Key = _cache_clock:[channelname]

Integer value - incremented whenever a sequence is added to the channel

Cache entry

Key: _cache_entry:[sequence] JSON document containing LogEntry information for a sequence returned by the _changes feed: DocID, RevID, Sequence, Flags

####Write For each tap feed entry:

  • 1 write for the _cache:entry document
    • stores the information sent by the _changes feed (DocID, RevID, Sequence, Flags)
  • per channel:
    • 1 read for the cache block
    • 1 CAS write for the cache block
    • 1 incr for the cache clock

Total ops per feed entry: 1 + (# channels * 3)

####Read For each poll iteration:

  • per channel:
    • 1 read of the cache clock
    • if clock shows changes
      • 1 read of cache block (low potential for additional cache block read):
        • per sequence:
          • read of _cache:entry document

Total ops per iteration: (# channels) * (2 + (#changes))

Benefits:

  • automatic sequence ordering in cache blocks
  • identify target block for sequence by key - no additional call to look up which block a sequence is in

Cons:

  • Read/CAS write needed for update
    • for a single streamed writer, it's slow
    • for multi-streamed or multiple writers, high contention on busy channels
  • unused storage for sparse channels, could be as low as 1 file per sequence
Clone this wiki locally