Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:55:42+00:00 2026-06-12T02:55:42+00:00

I have implemented two-phase commits for MongoDB as described in MongoDB’s documentation . Basically,

  • 0

I have implemented two-phase commits for MongoDB as described in MongoDB’s documentation. Basically, this works fine. It was especially easy as all I care about is inserting new documents, I never update them.

Yet, I worry about a particular scenario where I am not sure how to solve this. So any help would be appreciated.

Given: A collection containing documents with the following structure:

id: ...,
subId: ...
payload: {
  ...
}

There may be more than one document with the same id, but all documents with the same id have an increasing subId. So, you have something like:

  • 1 – 1
  • 1 – 2
  • 2 – 1
  • 3 – 1
  • 3 – 2
  • 3 – 3

Each insert only deals with one id, but potentially provides more than one document. So this means that there may be an insert with documents 3 - 4 and 3 - 5, but never an insert with documents 3 - 4 and 4 - 1.

The id and subId fields are calculated by the client inserting the data.

When inserting more than one document, this insert is covered by a 2PC. So either all documents are committed or none.

The problem now arises with the second phase of the 2PC. As I do not want other clients to be able to read uncommitted data, there is a flag within each document which tells me whether the document has already been committed. Once all documents have been written and the transaction has been set to ‘completed’, as a very last step the documents are marked as committed.

So now what happens if I try to insert 3 - 4 and 3 - 5, both get written, the transaction is completed, and 3 - 4 is marked as committed, and just before 3 - 5 gets marked as committed, another client reads all documents with id 3, sees subId 4as highest one, creates a new document with 3 - 5 and inserts it?

Then the commit fails and I have mixed version numbers from two transactions.

How to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:55:44+00:00Added an answer on June 12, 2026 at 2:55 am

    Oh, forget about it … typical case of “I see it when I write it”. The scenario I described is no problem as 3 - 5 was already written by the first transaction, it is just not committed. So as there is a unique index constraint on id - subId another transaction can not interfere.

    Problem solved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query implemented in two ways: SELECT race1, mode1 FROM organization WHERE
I have implemented two classes, isbn10 and isbn13. I would like to create a
I have implemented inheritance for two parent classes called Table and Field . Each
i have two int array of images , i have implemented it in grid
I have implemented a list view. Each item in my list view has two
I have spent two day trying to figure it out. I have implemented two
I have implemented a custom gallery with two UIViewController subclasses: AlbumController and PhotosController .
I have an interface implemented by two different classes. Then, how can I say
I have a two phase animation including a div full of images and to
I have implemented UILocalNotifications in two different apps now. One uses Location Services, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.