MongoDb sharding and consistency

时间:2018-07-25 04:42:28

标签: mongodb sharding

I want to create a send money system , When user A send $X to user B I want :

  • Decrease balance of A (A_BALANCE -= X)
  • Increase balance of B (B_BALANCE += X)
  • Add a transaction log that show A sent $X to B

I want to model the data as follow

  • account collection has fields
    • _id
    • name
    • balance
  • transaction collection has fields
    • _id
    • from_account_id
    • to_account_id
    • status
    • value
    • time

If I choose shard key of transaction one of from_account_id or to_account_id I can query incoming and outgoing transaction of a specific account with Targeted Operations

But if I want to get list of transaction for specific account sorting by _id I have Broadcast Operations

How can I model better data , If I design transaction collection as follow

  • new transaction collection has fields
    • _id
    • account_id
    • from_account_id
    • to_account_id
    • status
    • value
    • time

and choose account_id as my shard key , and when we have a transaction I insert two same documents in transaction collection and only change account_id in two document , in first document account_id is payer and in other is payee

This data model has a consistency issue , How can I sync these two records?

Is there better data modeling?

0 个答案:

没有答案