This wiki describes a proposal for implementing single reader/writer guarantees in Kafka.

Use Cases

The idea behind this feature is to handle cases where there is supposed to be a single reader or writer for a given partition at any time. This could easily be ensured by just having one process. However for high-availability you often want multiple replicas, only one of which is active at any given time. However failure detection is always imperfect so the possibility exists that a process may seem to be dead, causing us to allow another process to write, but it may not be dead, violating our single-writing (or reading) restriction.

Here are some specific cases we know of that would benefit from this:

Each of these cases have the possibility that a GC pause or other transient failure could cause a false positive in the failure detection.

The Feature

Although it is a bit hard for a group of processes to ensure that there can only be one reader or writer among them, it is actually pretty easy for the server to do this. The proposed mechanism is a new "own" API:

  own_request group [resource generation]

with a corresponding response

  own_response group [resource generation]

Issuing this request will attempt to claim ownership of a given set of topic/partitions.  For our use case resources will always be topic-partition pairs, but they could be any arbitrary string.

The server will keep an optional group, and set of owned resources associated with each connection as well as a hash table of (group, resource) => (owner, generation). When a fence request is made the server will check the current connections to see if there is already anyone who has registered that resource for that group:

Generation 0 will have special meaning. It will always succeed in claiming the resource, and will reset the generation.