sdk支持dynamoDB中的事务

sdk支持dynamoDB中的事务

本文介绍了如何使用javascript aws-sdk支持dynamoDB中的事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用node.js&编写的微服务。我们使用dynamoDB进行数据存储。值以json格式存储在密钥中。在更新服务调用中,我们获取密钥的值,更新json&保存它。

We have a microservice written in node.js & we use dynamoDB for data storage. Value is stored in json format against key. In update service call, we fetch value for a key, update the json & save it.

最近,我们遇到了一个条件,其中2个调用想要更新同一个键的值。所以先调用读取值,然后第二次调用读取值,先调用更新&保存,然后第二次更新&保存了值(通常的竞争条件),所以在这种情况下,第一次调用的更新没有反映在数据库中。

Recently, we came across a condition where 2 calls wanted to update the value of the same key. So first call read the value, then second call read the value, first call updated & saved, then second updated & saved the value (usual case of race condition), so in this case update by first call did not get reflected in DB.

为了解决这个问题,我研究了一下;了解了dynamoDB的事务库。但它似乎还没有在node-js sdk中。

To handle this, I researched a bit & came to know about the transaction library of dynamoDB. But it seems that it is not yet in node-js sdk.

此外,我搜索了版本控制和版本。乐观锁定,但我再次在node-js sdk中找不到对此的支持。

Also, I searched about versioning & optimistic locking but again I did not find a support for this in node-js sdk.

这是否有任何更新?如果它的支持在不久的将来不会出现在node-js sdk中,还有哪些其他选择呢?什么是处理这个问题的最佳方法?

Is there any update with this? If it's support is not going to come in near future in node-js sdk, what are the other options? What could be the best way to handle this issue?

推荐答案


  1. 您可以进行原子更新,例如直接在Dynamo上递增数字而无需阅读,递增,更新。有关详细信息,请参阅

    Are both updates updating the same field? If so you can add a condition to the update that the old value equal what you read. That way if you try to save the 2nd new value, this condition will fail and it won't perform the 2nd update. See this

    这篇关于如何使用javascript aws-sdk支持dynamoDB中的事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:40