Repository navigation
Description
The question of restarting feeds came up in #2953, #1118, and #2613. Since we've learned a lot since these issues were opened, I decided to start with a clean slate and a specific proposal.
When the user calls changes() on a stream, the feed protocol would inject an opaque timestamp into results as follows:
# Returns a feed along with an opaque timestamp
> r.table('foo').changes()
[{ 'old_val': ..., 'new_val': ..., 'timestamp': OPAQUE}]The user could then pass the opaque timestamp back to changes() via the return_initial optarg to get all the changes since the timestamp. We'd be piggy-backing off existing replication logic, so we'd have to tell the user the range of "obsolete" keys they should delete, and then fill them in on the new values in that range:
> r.table('foo').changes(return_initial=OPAQUE)
[
# Obsolete range of keys to delete
{ 'obsolete_range': [LEFT_KEY1, RIGHT_KEY1] },
# Backfill the user on values they missed in the range
{ 'new_val': ..., 'timestamp': OPAQUE}, { 'new_val': ..., 'timestamp': OPAQUE},
# Another obsolete range
{ 'obsolete_range': [LEFT_KEY2, RIGHT_KEY2] },
# Values to backfill in the range
{ 'new_val': ..., 'timestamp': OPAQUE}, { 'new_val': ..., 'timestamp': OPAQUE},
# Now we can start the feed
{ 'old_val': ..., 'new_val': ..., 'timestamp': OPAQUE},
...
]A special value for return_initial is True, which backfills the user from scratch:
> r.table('foo').changes(return_initial=True)
[
# Backfill the user on all the values
{ 'new_val': ..., 'timestamp': OPAQUE}, { 'new_val': ..., 'timestamp': OPAQUE},
# Now we can start the feed
{ 'old_val': ..., 'new_val': ..., 'timestamp': OPAQUE},
...
]This would work on streams in general, like t.map().changes() and t.filter().changes().
Note, if the changefeed is set up on a datum (e.g. a single document), the only legal values for return_initial are True or False.
There is a question of what the default for return_initial should be. I can see a couple of options:
- Set it to
False. This is annoying for datum feeds (e.g. a feed on a document). - Set it to
True. This may be annoying for stream feeds (e.g. a feed on a table). - Set it to
Falseon streams, and toTrueon datums. This might be confusing to users.
Also note, under this proposal the squash optarg would continue operating as it does now, but would have no effect on the initial values.
/cc @danielmewes @timmaxw @mlucy. How hard would this be to implement on top of the current replication logic? Are there flaws in the API? Would it work on more general streams (like t.filter().changes())?
Activity
coffeemug commentedon Dec 23, 2014
Also note, the meaning of the opaque timestamp value is always monotonically increasing.
danielmewes commentedon Dec 23, 2014
My guess is that this is probably not too difficult in a basic form.
There are a few open questions:
Point 3 will become especially relevant when we implement automatic failover.
A minor detail: In our current backfilling logic, the "drop potentially obsolete key range" requests are restricted to a given hash shard. We have to either expose the hashing function to the user in some way, or make sure that we re-send the given key range over all hash shards.
danielmewes commentedon Dec 23, 2014
Another thing to note is that our backfill implementation relies on taking a snapshot of the whole table while it's running. We have to think about how that interacts with changefeeds that might be open for a long time because the client is slow in reading all the (initial) data of the cursor. Specifically I'm concerned about memory consumption on tables with high write loads.
It might be necessary to enhance the buffer cache to allow paging out snapshotted blocks to disk. This will be useful for backfills as well, and will later become useful for #3464.
Can I change my "My guess is that this is probably not too difficult in a basic form." to a "This is a significant amount of work, but seems feasible."?
coffeemug commentedon Dec 23, 2014
WRT to snapshot memory issues, I'd also ignore that for now. We already run into that in case of backfills, so I wouldn't worry about it too much until later. If we really wanted to be careful here, we could just terminate the feed if the snapshot gets too big (i.e. the user is too far behind), and they could restart the feed.
I think it'll be really important to do this quickly after 2.0 since it's a huge limitations to the current system, so I'd try to cut scope in every way possible. I suspect we can get away with doing very little with respect to many of the operational issues like these.
danielmewes commentedon Dec 23, 2014
I personally would always set
return_initialtoFalseby default, simply because having itTruecomes with significant costs in some of the cases. If a user needs the initial value they will know that they do so, no matter whether it's a single document or a range.@coffeemug you're probably thinking of specific use cases when you say that
return_initialwould be more conveniently default toTruefor single-document changefeeds andFalseotherwise. Could you elaborate a bit on those?coffeemug commentedon Dec 23, 2014
You'd want it to be true any time you're loading a realtime web page. Things like
t.orderBy().limit().changes()andt.get().changes(), since if you don't get the initial value right away, you can't populate the page.danielmewes commentedon Dec 23, 2014
@coffeemug
I agree that we should avoid exposing sharding details to the user. My question in 1 was with respect to how we would actually implement the timestamp. I think we need the individual ranges there because there's no single synchronized (replication) timestamp across different shards.
I think solving the snapshot memory issue is worth looking into. It might not actually be that difficult to do.
Terminating the changefeed if the user is too far behind is annoying. On large tables transferring the initial results might well take so long that no matter what the user does, the changefeed will be aborted before it even gets a chance to catch up with more recent writes. At that point the feature becomes unusable.
If we want this feature to make changefeeds more reliable for replication purposes, that is probably not the way to go.
danielmewes commentedon Dec 23, 2014
Actually thinking more about it we can probably avoid implementing pageable snapshots in the cache.
Instead we could use a similar technique to what is suggested in #1944. We would stream the initial results in batches of small primary key ranges. After every range, we would send the user a new opaque timestamp that reflects the fact that we have backfilled up to a given replication timestamp for that small range.
If we implement this, we can either start streaming changes for a given key range to the user as soon as we have "backfilled" all initial results for that small range, or keep accumulating them in a disk backed queue until the initial results for the whole table have been sent.
I think both would be fine. The former is nicer because we can keep the disk backed queues really small (or maybe avoid them in the first place), the latter has the property that all initial results are sent over the changefeed before any update is received which might make it minimally easier for the user to parse.
danielmewes commentedon Dec 23, 2014
Ah I see. I think this is not to bad then:
It sounds confusing because the user has to know which queries return a stream and which return a datum. In practice, at least with the types of changefeeds that we currently support, I think it's sufficiently clear though.
deontologician commentedon Dec 23, 2014
You guys had mentioned something like
r.db('system').table('changefeeds')that is populated with changefeeds that are currently active. Maybe with some kind of changefeed pseudotype for the documents that the drivers can convert into a cursor:So the drivers could maybe do something like:
mlucy commentedon Jan 15, 2015
Here's an alternate proposal that would take a lot less work to implement:
We'd basically give people a way to create a named changefeed that exists above the connection level, so another client can steal that changefeed and keep reading changes from it in case the first client goes down. This wouldn't let people requests all the changes starting at an arbitrary point in time, but it's way way easier to implement. (We could also keep the last batch sent around in memory, and have an optarg to indicate whether or not (2) receives the last sent batch a second time, which I think is usually what you'd want in the case where (1) dies.)
deontologician commentedon Jan 15, 2015
What happens to changefeeds that people forget to delete?
mlucy commentedon Jan 15, 2015
They use up memory indefinitely, but not an arbitrary amount of memory (at most 100k changes right now). We could also add a reasonable timeout after which they're evicted (although it's debatable whether reasonable would mean "an hour" or "a week").
172 remaining items
aral commentedon Jan 4, 2018
Having just arrived here from the node-rethinkdb-job-queue thread, I just wanted to inquire about the state of this.
As I understand it, changefeeds are unreliable. Is this correct?
(Or is this only an issue in clustered deployments? Given the nature of our project – personal web sites – there should never really be a reason to cluster a deployment as they will be deployments for one. I’m pretty sure that even if Stephen Fry decided to get one he wouldn’t need a cluster.) :)
I was considering making RethinkDB the “single source of truth” for an ActivityPub server implementation I’ve just started working on but, after reading the posts here (e.g., @gebrits’s), I’m reconsidering that.
Can anyone using changefeeds in production please tell me if I’m in for a world of hurt if I decide to depend on them? Would love to hear from folks with practical experience in this area.
(Also, if changefeeds – a core, distinguishing, and loudly advertised feature – is unreliable, this should be mentioned in the documentation, right at the top. This is not something you want to find out when you’re several months into dev.)
srh commentedon Jan 4, 2018
Hi, I don't know what you mean by node-rethinkdb-job-queue thread. Do you have a link?
Changefeeds are unreliable in the sense that they could fail, explicitly, and then if you make a new changefeed, you'll miss out on writes. They'll explicitly fail if one of the servers the feed is receiving changes from goes down or gets network partitioned. Or if the server your ran the query on goes down. Or if the client goes down.
You can hack your way around the problem of resuming a changefeed by putting a timestamp, either logical or wall-clock, on each row, then making an index by that timestamp (for efficiency), and then, when you want to restart a changefeed, doing so by starting a changefeed, and after that starting a range query on the secondary index by timestamp, piecing together the information that you've lost. There are of course issues to work out with timestamp monotonicity, which you can deal with but add an extra level of discomfort.
(I don't have practical experience in this area as a user -- I'm a developer of the DB -- so I don't have personal experience with the difficulty here.)
It would be possible to remove the issues with timestamp monotonicity if a server feature were developed that let your queries generate a (shard id, logical timestamp) pair that is guaranteed to increase (for each shard).
aral commentedon Jan 4, 2018
Thanks for the quick response, Sam. I meant the thread referenced above from the node-rethinkdb-job-queue project (grantcarthew/node-rethinkdb-job-queue#77).
So if I have a component, say, listening for a changefeeds to distribute messages to followers, if the feed fails, I will get an immediate notification and I can restart the feed (and if I’m using timestamps, I won’t lose my place?) That sounds acceptable for my use case, if so. Reading some of the earlier issues, I was under the impression that the fails were not immediately reported but that there was some lag.
Thanks again.
zappjones commentedon Jan 4, 2018
Hi aral -
We've been using rethinkdb for over a year in production and initially leaned heavily on changefeeds. With a low amount of database traffic things seemed to be working quite fine. Once traffic was increased and we added more changefeeds into the mix all queries (reads / writes / cfs) were slowed down into an unusable state.
Within the last month we just removed our last changefeed from our server stack. We are still using rethink as our primary nosql db but all realtime communication is now done through redis pubsubs.
Changefeeds hold a lot of great promise but I would not currently recommend using them in a production environment.
In our setup we were using three rethink nodes each running on a i3.4xl ec2 instance. At about 15-20k concurrent changefeeds is when issues would crop up.
srh commentedon Jan 4, 2018
There should be some lag because it does take some time to notice that a server's not responding and decide that it's timed out.
It might also be that there's some honest-to-god bug where a changefeed just never gets responses instead of a notification happening.
The documentation does a poor job of describing what error conditions are possible and what sort of errors can be returned. So I've got some uncertainty there.
@zappjones is right -- if you have a large number of changefeeds listening on key ranges (and not individual documents), you'll pay a huge CPU-time price to see if each write should be sent to each of the change feeds. (There could be a performance problem when you have a ton of change feeds on individual documents, too, but it wouldn't be for the same reason.)
aral commentedon Jan 5, 2018
Thanks, Sam. That’s really useful information. I’m going to spike out RethinkDB because I’d love to implement its beautiful workflow if at all possible. It would be awesome to see changefeed issues better documented. Would you like me to open a separate issue for that?
aral commentedon Jan 8, 2018
@zappjones Thank you so much, that’s invaluable information.
ceelian commentedon Feb 22, 2018
@srh Can you elaborate a bit more on this statement:
As I understand you mean that the website example:
r.table('game').orderBy('score').limit(3).changes()will have "bad" performance already on ~15.000 active changefeeds?
That would also be bad for us, we are currently evaluating rethinkdb but will have approximately 50.000 - 100.000 parallel active changefeeds with range queries (pagination). A lot of the live queries will be the same, so I am not sure if that will help with the performance (might be the case if rethinkdb does internal deduplication of changefeed subscriptions)
srh commentedon Feb 22, 2018
I wasn't thinking of the limit example, just the range example (with small ranges, so that the overhead is "bad").
The limit example would also be hit by bad performance. Basically every write will interact with every range change feed, which costs 15000x work with 15000 change feeds.
It is possible to improve the db implementation, so that both this and limit change feeds were reasonably fast, but I don't know anybody that has the free time for that right now.
srh commentedon Feb 22, 2018
I'll have to check and see how identical subscriptions are treated. Various order_by/skip/limit change feeds ought to be handled together, and maybe those, and identical ranges, are handled more efficiently. I don't know that they are, so I can look sometime when I get the chance.
wmertens commentedon Mar 25, 2018
srh commentedon Mar 26, 2018
In fact, writes already do mark areas with a version number -- this is used for incremental replication. The current change feed API does provide old_val and new_val though. I'm not sure of the guarantees but I think there is one that values don't get "skipped" by a change feed.
wmertens commentedon Mar 26, 2018
thedodd commentedon Sep 19, 2018
Would be an awesome feature.
ralyodio commentedon Sep 19, 2018
Without change feeds, this database is dead to me since it lacks most of what you need in a database out of the box.