官术网_书友最值得收藏!

Read isolation and consistency

MongoDB read operations would be characterized as read uncommitted in a traditional RDBMS definition. What this means is that by default reads may get values that may not finally persist to the disk in the event, for example, of data loss or a replica set rollback operation.

In particular, when updating multiple documents with the default write behavior, lack of isolation may result in the following results:

  • Reads may miss documents that were updated during the update operations
  • Non-serializable operations
  • Read operations are not point-in-time

These can be resolved by using the $isolated operator with a heavy performance penalty.

Queries with cursors that don't use .snapshot() may also in some cases get inconsistent results. This can happen if the query's result cursor fetches a document, this document receives an update while the query is still fetching results and because of insufficient padding, ends up in a different physical location on disk, ahead of the query's result cursor position. .snapshot() is a solution for this edge case, with the following limitations:

  • It doesn't work with sharding
  • It doesn't work with sort() or hint() to force an index to be used
  • It still will not provide point-in-time read behavior

If our collection has mostly static data, we can use a unique index in the query field to simulate snapshot() and still be able to apply sort() to it.

All in all, we need to apply safeguards at the application level to make sure that we won't end up with unexpected results.

Starting from version 3.4, MongoDB offers a linearizable read concern. With linearizable read concern from a primary member of a replica set and a majority write concern, we can ensure that multiple threads can read and write a single document as if a single thread was performing these operations one after the other. This is considered a linearizable schedule in RDBMS and MongoDB calls it the real time order.

主站蜘蛛池模板: 孟津县| 敦化市| 中超| 黄浦区| 齐齐哈尔市| 土默特右旗| 藁城市| 绵竹市| 宣城市| 沙田区| 延边| 阳春市| 康保县| 惠来县| 静乐县| 当雄县| 耒阳市| 平罗县| 明水县| 金堂县| 周至县| 石柱| 深州市| 新绛县| 乌兰察布市| 赣州市| 长沙县| 普格县| 永靖县| 余庆县| 云龙县| 阿拉善盟| 玛纳斯县| 鹿泉市| 聂拉木县| 宣威市| 昭觉县| 县级市| 天柱县| 宜黄县| 黄大仙区|