EventSourcing: projection vs cache

January 15th, 2020

A projection is used to create a highly performant read-model.

It is eventually consistent.

 

 

A cache is used when replaying all events to build up state is too slow.

"It checks first in a cache and if the object is not present, it will check in the event store."

(https://hackernoon.com/1-year-of-event-sourcing-and-cqrs-fb9033ccd1c6)

If the cache is present, it will grab the cache, apply the events since the caching to build up the current state.

Rebuilding state needs consistency.

Reading the cache is part of building the state, so it is also consistent.

Saving the new cache can happen non-consistently.

 

Both projection and cache need to be recreated if you change history. (eg. change events in the past)

(in the case you don't require immutable state)

 

 

When you need to be certain you have the latest state,

(aka. you need consistency)

to make certain decisions,

you don't want a projection,

because projections are eventually consistent.