bruth / rita

Toolkit for event-centric and reactive patterns leveraging https://nats.io.
Apache License 2.0
86 stars 6 forks source link

atomic counters #6

Open gedw99 opened 2 years ago

gedw99 commented 2 years ago

Rita looks really nice.

As per https://github.com/nats-io/nats-server/issues/2656 It looks like Rita might be a good way to calculate atomic counters and other such things. In the issue the hack is to send +1 and -1 and then recalc and store the KV value.

bruth commented 2 years ago

I guess the proposal is effectively asking for the server to serialize incr/decr commands from concurrent actors. I have a hard time thinking about write-only counters. Meaning if you need to read the value and decide how much to incr/decr, then you would want the kv.Update command as Derek suggested (a compare-and-swap). A more robust option would be a CRDT that could properly merge concurrent operations.

That said, if eventual consistency of the counter is sufficient, then one option would be to have a model of state that have a live subscription to receives incr/decr operations and the value is derived. It doesn't solve the atomic problem, but agree that a live model of state could be useful. I had written up an API design for this a while ago, I will dig it up and post.

gedw99 commented 2 years ago

CRDT is definitely another way !

This nats project models the data in a CRDT style: https://github.com/simpleiot/simpleiot/search?q=crdt

It does not have as far as i can see a way to reconcile the state using CRDT yet from what i can see.

But its interesting to see. they also use an embedded database btw called genji, which is backed by the cockroachdb pebble, which i am sure you know. https://github.com/genjidb/genji/blob/main/go.mod#L8

Its interesting to compare rita and siot. Sito uses a generalized type structure called points. https://github.com/simpleiot/simpleiot/blob/master/docs/adr/1-consider-changing-point-data-type.md I like how it goes through the considerations of how and why ...

gedw99 commented 2 years ago

@bruth " I had written up an API design for this a while ago, I will dig it up and post." If you find it would be cool to read 👍

bruth commented 1 year ago

@gedw99 There has been some internal discussion about a stream type/operation that enables an atomic counter. It is currently a stretch goal for the next minor release (2.10.0), but may land in the coming months.

gedw99 commented 1 year ago

hey @bruth thanks for letting me know about this !

If there are any issues on the nats repo please do add here :)

Also i am working on time smearing for nats. It basically keeps the time on all nodes the same and accounts for leap years. It's sort of related to the whole eventual consistency aspects we are discussing.