Transactions across REST microservices?

前端 未结 11 1911

Let\'s say we have a User, Wallet REST microservices and an API gateway that glues things together. When Bob registers on our website, our API gateway needs to create a user

11条回答
  •  爱一瞬间的悲伤
    2020-11-29 14:58

    All distributed systems have trouble with transactional consistency. The best way to do this is like you said, have a two-phase commit. Have the wallet and the user be created in a pending state. After it is created, make a separate call to activate the user.

    This last call should be safely repeatable (in case your connection drops).

    This will necessitate that the last call know about both tables (so that it can be done in a single JDBC transaction).

    Alternatively, you might want to think about why you are so worried about a user without a wallet. Do you believe this will cause a problem? If so, maybe having those as separate rest calls are a bad idea. If a user shouldn't exist without a wallet, then you should probably add the wallet to the user (in the original POST call to create the user).

提交回复
热议问题