📜 ⬆️ ⬇️

Watching the collection. Tailable cursors



If you are young and energetic, you are interested in mongoDb, you want to discuss the translation of the word tailable into Russian, and you are interested in where the pickrelayed is taken - welcome under the cat.


')
Some time ago, doing admin panel for one small project, I thought that it would be nice to display a log of some significant events in it. Memory helpfully recalled one of the possibilities MongoDb, about which I heard, but never used in practice - the tail (tailable) cursors.

What kind of beast?

By default, Mongo automatically closes the connection after the client has obtained all the data available to the cursor. For capped collections, additional behavior is possible, the so-called tail cursors.

The tail cursors remain open and continue to receive new data that meets the search criteria. Their action reminds well-familiar tail -f. Mongo uses tail cursors to track opplog.
What you need, I thought

new Thread(new Runnable() { @Override public void run() { DBCollection collection=MongoConnector.getInstance().getCollection("events"); //  addOption DBCursor cur= collection.find().addOption(Bytes.QUERYOPTION_TAILABLE).addOption(Bytes.QUERYOPTION_AWAITDATA); while (cur.hasNext()) { //   ,    ,     JS wsConnection.broadcast((String) cur.next().get("message")); } } }).start(); 

By the way, as practice has shown - the delay between the insertion of a new document and its receipt by the client on the tail cursor is so insignificant that it can be ignored in principle.

A spoon of tar

Official documentation suggests that the use of tail cursors on collections, with a large number of write operations can be very expensive. If the request is based on the fields for which there is an index, in the case of a large number of records, it is better to use the usual cursor, periodically requesting new documents

How it works?

Remember the above I wrote the work tail cursors looks like a tail-f work? In fact, the similarities here are even more than it seems at first glance.
When you set the tail cursor and read data from it, having reached the last document, Mongo leaves the cursor on it and tracks the collection descriptor; if a new element appears that satisfies, the cursor moves to it.
For ordinary (unlimited) collections, this behavior is impossible, since in them the order of storing documents on disk does not always correspond to the order of inserting data.

4 facts about tail cursors that shook the world


1) Tail cursors do not use indexes and return documents in the order in which they are stored on disk. For limited collections, the order of placement always coincides with the order of insertion of documents.

2) Since indexes are not used, at the beginning of the query you will have to read all the records in the collection, which can be very expensive with large amounts of data. After we have already received the available data, screening out the newly added documents that do not satisfy the request is not expensive.

3) The tail cursor may not be valid (there will be no tracking of the collection) if
No document has satisfied the original search query.
The last document was deleted when the cursor pointed at it.

4) Dead cursor has id 0

Source: https://habr.com/ru/post/173653/


All Articles