Search code examples
elixirriakcrdt

What does the Context for Flags relate to in Riak KV


When using flags in Riak KV the flag require a context. In the Elixir client the context is set by Flag.new("my context"), otherwise it will throw :context_required when one try to disable the flag.

Now, the flag can only exist in a Map, and each element in the map must have a key, so multiple flags could be in a Map and still be differentiated by key.

What is the purpose of the context if not to differentiate between them?


Solution

  • The Flag in the Map has a semantic of "add wins" or "true wins". This semantic is achieved using a causality technique called "Observed Remove" that is borrowed from the Observed-Remove CRDT Set. In many ways the Flag is just a set of a single null element that is either in the set (flag is true) or not in the set (flag is false.)

    The Flags bottom value is False. When there is no key for the Flag in the Map, the Flag is false. This is why you can't add a flag key with the False value. Adding the key is the same as setting the Flag to True.

    Adding a Flag causes the key to get a dot, or causal tag. If the flag is added twice concurrently it will have two such causal tags. When the flag is set to False, the context, or causal tags being removed, must be passed to Riak, this ensures that some concurrent setting to True (with tag3) does not get set false when tag1 and tag2 are removed. That is how add wins! The Observed-Remove part is facilitated by the context: the context says what causal tags you have Observed, and therefore which ones you may remove.