繁体   English   中英

微服务对共享数据库的并发更新..丢失更新

[英]Concurrent updates to shared database by microservices .. Lost Updates

流程图

我们的微服务正在调用第三方服务,使用 REST API 访问(读取/更新)共享数据库中的相同记录

用例 1:同一个微服务多次调用访问同一个记录

  • 客户购买了 10 件产品 A
  • 另一位客户购买了 5 件相同的产品 A
  • UI API 在几毫秒内对端点 /decrementProduct 进行了 2 次调用
  • 两个调用最终可能会读取产品 A 的相同库存计数,并且两者都将根据它们读取的库存计数减少产品 A 的采购单位。

例子:

  • 通话前库存盘点:10
  • 调用 1 从 10 减少 5 个单位,并更新回 5 作为当前库存计数。
  • 调用 2 从 10 减少 2 个单位,并更新回 8 作为当前库存计数。
  • 通话后库存盘点:8
  • 调用后正确的库存计数应为:3

用例 2:多个微服务多次调用以访问同一记录
在这种情况下,用例 1 中解释的问题会加剧,因为需要多次调用来更新数据库中的相同记录。

Edit: 13-April-2021 The shared database is exposed to our microservices using REST API and we don't have any control over the physical database and the exposed REST API, to implement any transactions or locking mechanism at the database level.

I don't know which database you are using but traditional relational databases like: oracle, postgresql, mysql, db2, etc... already address these kind of issues using locks in the records that are being updated to ensure that there are not concurrent问题。

我的意思是,如果您打开一个读取值然后更新它的事务,则不会有任何问题,因为如果您尝试使用与当前设置的版本号不同的版本号更新数据库中的一行(一个过时的版本), 数据库不会让更新它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM