Skip to content

Add a diff flag to changefeeds #4707

Open

Description

@coffeemug

One of our users mentioned that they can't easily use changefeeds because their use case has the following properties:

  • Each document is fairly large and can't be easily broken up into smaller documents
  • The changes that happen to each document are typically very small

They asked for a diff: true | false flag to changes(). If set to true, instead of sending new_val and old_val, we'd send a diff between the two that could be applied to the last known value. That would make feeds work dramatically better for their use case.

There are existing algorithms for json diffing and patching libraries I found online (e.g. https://github.com/benjamine/jsondiffpatch). I also vaguely remember that there is a standard for this, though I can't find it right now.

Activity

  1. added this to the subsequent milestone on Aug 18, 2015
  2. danielmewes commented on Aug 18, 2015

    @danielmewes
    Member

    Related to #3575 . I'm going to close the other issue in favor of this one, since this one is a bit more concise.

    Also copying @AtnNn's comment from the other issue to here:

    If users write apps or use libraries that support jsonpatch, it might be halpful to add .diff and .patch commands that could be used for things like .replace(lambda row: row.patch(patch)).

  3. opsb commented on Sep 27, 2015

    @opsb

    👍 for using json-patch. I'm going to be writing an orbit.js adapter for rethinkdb. orbit.js uses json-patch operations to synchronise between datasources so this would line up nicely.

  4. coffeemug commented on Sep 28, 2015

    @coffeemug
    ContributorAuthor

    @opsb -- very cool! Once the plugin API is in (#4785), we can also turn it into a RethinkDB plugin which will make access via orbit API much easier. Really excited about it!

  5. opsb commented on Sep 28, 2015

    @opsb

    @coffeemug the plugin API looks awesome! I'm not sure I quite follow what you're suggesting with regards to orbitjs integration though, do you mean there'd be an orbitjs plugin for rethinkdb that provided a specialised API for orbit.js to connect to?

  6. coffeemug commented on Sep 28, 2015

    @coffeemug
    ContributorAuthor

    do you mean there'd be an orbitjs plugin for rethinkdb that provided a specialised API for orbit.js to connect to?

    Yes.

  7. opsb commented on Sep 28, 2015

    @opsb

    👍

  8. ember-max commented on Oct 4, 2015

    @ember-max

    I'm assuming adding the 'diff' flag wouldn't just be a cosmetic wrapper around something like jsondiffpatch? Using an external diff library is simple enough, it seems the issue is the performance involved in actually diff'ing large objects, not to mention the bandwidth consumed. Imagine you have thousands of users connected in a real time environment, and each user document has thousands of rows, and all these users are doing little things to affect themselves and/or other users all the time (read: just a few diffs at a time occurring very frequently)... changefeeds become an amazing tool for propagating those changes to the clients IF you're efficiently able to tell each client about the deltas... it's not clear that that is very feasible if you have to use application server CPU (or even DB server CPU) to compute the deltas. What I would want personally would just be a stream of the individual write commands and their arguments, it seems like that is what is naturally available without doing any more work.

    tl;dr
    +1 :)

  9. ember-max commented on Oct 4, 2015

    @ember-max

    After running some simple tests, the perf of (https://github.com/benjamine/jsondiffpatch) seems decent and is quite a bit better than at least one other alternative I came across (https://github.com/flitbit/diff). That said, it still represents actual blocking, CPU-chewing code (as opposed to IO), in the 5-15 millisecond range for large objects, so to the extent it could be moved off of application servers and into the rethinkdb processes (and ideally even optimized further using C++), it seems like that could be a big win without drastically altering the current changefeed implementation.

  10. coffeemug commented on Oct 5, 2015

    @coffeemug
    ContributorAuthor

    @ember-max --there are two different aspects to this.

    The first is the protocol to use to send the diff to the client. Since there is no standard way to specify JSON diffs, we can do it in a few ways -- use the spec defined by jsondiffpatch, define our own, find another existing one, etc.

    The second aspect is how to implement it. We wouldn't have to do a full diff on two json documents because the database knows what the user is trying to do; we'd merely have to take that information and transform it into the user-facing diff protocol. The most naive way to do it is to diff two documents, and the first version of this feature may end up doing that to get it out faster, but sooner or later we'll be able to send efficient diffs back to the client without having to actually diff the documents.

  11. ember-max commented on Oct 5, 2015

    @ember-max

    As to the spec, I think I would +1 json-patch over jsondiffpatch but can't say I'm too opinionated on it.

    For the performance, I'll make a final quick plea and then shutup: it seems the whole point of adding the diff at the RethinkDB level is for performance - it's about two lines of code to require('jsondiffpatch').diff and then diff(old_val, new_val) - extremely easy, but a waste of bandwidth and CPU. So if RethinkDB adds the same functionality without a bandwidth + CPU gain, it doesn't really end up helping, as the target use case is presumably people who have large objects and/or frequent changes in realtime constrained applications.

    Anyway, just my 2c, thanks!

  12. opsb commented on Oct 5, 2015

    @opsb

    I was thinking json-patch myself. Something to consider though is the issue Allow correlating writes with feed notifications. If something like jsondiffpatch were used then you could receive a single diff for each write to a document. With json-patch you'd potentially need several json-patch operations to express the change (although you could also have a wrapper that contained all of the json-patch operations).

  13. coffeemug commented on Oct 5, 2015

    @coffeemug
    ContributorAuthor

    For the performance, I'll make a final quick plea and then shutup: it seems the whole point of adding the diff at the RethinkDB level is for performance

    👍 message received

  14. danielmewes commented on Oct 6, 2015

    @danielmewes
    Member

    Even if we compute a diff on the server for each change and don't take information from the write itself into account (the latter might be difficult), we could implement this in a way that saves a lot of CPU and bandwidth.
    For example if there are a lot of changefeeds listening on the same table, we could perform the diffing only once on each RethinkDB server (or even just once on the primary). Then we would send the same diff to all subscribed clients.

    This saves bandwidth, and is a lot more efficient than computing the diff individually for each client.

  15. ember-max commented on Oct 6, 2015

    @ember-max

    👍 excited to try it out when it's ready!

  16. timothyclemansinsea commented on Dec 17, 2015

    @timothyclemansinsea

    I would really like this.

  17. skosch commented on Jan 11, 2016

    @skosch

    I'd like to +1 this (and #5188) – whether RFC6902 is used or the more concise Delta format. It would make it very easy to apply changesets coming down to the browser via socket to a partial data store (à la Meteor DDP/Minimongo), and let something else (e.g. Mobservable) handle the reactivity based on that data store ... for those of us who can't or don't yet want to commit to the full Relay/GraphQL "experience".

  18. karlrasche commented on Feb 8, 2016

    @karlrasche

    I'll +1 this as well - mostly for performance/efficiency reasons. I'll stay out of the diff format side of it..

    Going down the path of having fat documents for atomic update purposes, I quickly ran into the issue of needing document diffs. While diffing objects in the app can be a reasonable workaround for moderate sized documents, it's never going to be as fast as if we had knowledge of what parts of the document changed.

    And while you could go about building hierarchies of update counters into the document to help accelerate the diff code, there's still the overhead of parsing and transferring the whole document.

  19. thelinuxlich commented on Feb 25, 2016

    @thelinuxlich

    +1 Excited to see this feature!

  20. geyang commented on Mar 21, 2016

    @geyang

    +1 to json-diff-patch.

    I recently implemented a diff patch API for a collaborative editor I was building. In the end, requiring large documents in the megabyte range to be transmitted between mongoDB and the application server becomes the bottleneck. If this can be done by the database instance it would be a major win since only very small diffs need to be transmitted.

    However this algorithm also requires reading from both the document record itself, as well as a shadow collection that we use to keep track of versions for various clients. Would these be accessible in the plugin API?

    ----------------------------------- details ----------------------------------------

    From the application engineering's perspective, the key here with jsondiffpatch is that the algorithm need to be very strict w.r.t. the exactness of the base version that it patches to. Aka the patch can only be applied with the base version is exactly the same as the base version on the client.

    In a collaborative environment, if such exactness is not enforced, shadow copies on individual clients diverge quickly.

    To prevent these from happening, the plugin has to keep track of the object hash of the smallest diff unit, be it a field of the entire document (assures consistency between fields). It also keeps a collection of recent shadow copies with a version number and a pre-computed hash for each.

    In my implementation, this required five round trips between the server and mongoDB. I can imagine if this is implemented as a plugin with tight hooks, colocation of such operation and in-memory access to data without network IO are going to be huge wins. It does, however require reading from both the document record itself, as well as a shadow collection that we use to keep track of versions.

  21. Prinzhorn commented on Nov 19, 2016

    @Prinzhorn

    I'd love to see full diff/patch support in RethinkDB at some point, not just for changes feed. I'm talking about a table.patch operation in addition to table.update and table.replace. And also r.patch and r.diff for document manipulation.

    My use cases:

    • Space efficient document history (I'm currently storing a copy of a full document when it is changed)
      • For this use case it would also be sick to apply a list of patches to a document in series. This way we only need a full snapshot every now and then and can get to any point in time by applying N patches
    • Multi-user collaboration: I'm currently using optimistic locking and it's not really "collaboration" because the second write will fail. Having full patch/diff support means every edit a user does in our app can be a patch. Unless they patch the same part of the document, we can have full collaboration support without writing tons of application logic.

    Edit: table.update should then also return a patch and inverse patch as part of the response. They describe the operations needed to do or undo the update.

  22. babakness commented on Feb 6, 2017

    @babakness

    Looking forward to this feature!

  23. vans163 commented on Nov 18, 2017

    @vans163

    +1 on this. The usecase is a stateless frontend (stateless react components for example). Where you want to send a state differential to save on bandwith instead of sending huge objects each time.

    It should have some consistency guarantee like if the connection the changefeed is attached to dies, when you reconnect and subscribe you get the initial state, then further changes come as differentials.

    The diff calculation is actually a lot harder then it seems especially taking into consideration nested maps and the hardest, lists. I created something to do this, if you read erlang, https://github.com/vans163/crdt/blob/master/src/crdt_etc.erl.

    There is lots of ways to handle deletes, I ended up setting a static unique delete key. The frontend knew when it saw this delete_key to delete the entry from the map. But this is not a way I like anymore. Also I did not support diffs on lists, and if the map member was a list that changed, I sent the full replacement.

  24. renej-github commented on Jul 7, 2018

    @renej-github

    For "compressing" the new_val and old_val we use a js on changefeed that removes all duplicate "flat" keys where the values are equal. It provides enough information to derive added, changed and removed keys and there new and old value. Array values are removed for duplicate keys if equal, but complete array values are used, so no array diffing. Saves a lot of data in case of large documents with many small changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions