gpt4 book ai didi

elasticsearch - 没有 ACID 数据库是否可以进行事务(就数据一致性而言)?

转载 作者:行者123 更新时间:2023-12-02 22:12:13 25 4
gpt4 key购买 nike

我正在开发一个使用 NodeJS、Kafka 和 Elasticsearch 堆栈的项目。我们有一个账户微服务,需要保证账户余额和交易的数据一致性(余额只能对某些客户为负数)。

考虑到我们不需要交易结果是实时的,每个操作都可以异步处理,然后将结果显示给客户,有一些模型(例如事件源,CQRS)提供数据没有 ACID 数据库事务的一致性?

如果该模型对于执行 ACID 数据库已经执行的操作(并且做得很好)来说非常令人头疼,我们将实现一个 SQL 数据库 (PostgreSQL) 来适应这种情况。

最佳答案

这不是 Elasticsearch 的创建目的。尽管您可以在一定程度上使用事务和锁定,但它将保持“最终一致”的数据库

您可以检查以下链接以了解elasticsearch 中可用的内容:

https://www.elastic.co/blog/versioning

https://www.elastic.co/guide/en/elasticsearch/guide/current/concurrency-solutions.html

https://www.elastic.co/blog/found-elasticsearch-as-nosql

https://discuss.elastic.co/t/transactional-acid-features-in-es/5357/2

versioning的用例:

假设我们需要更新不同工作人员的资源。常见的方法是:

  1. 读取资源并获取其版本号
  2. 使用网址中读取的版本号准备更新资源的请求
  3. 执行查询
    • 如果没有错误——我们就完成了
    • 如果出现错误,请转到 1

在出现竞争情况的情况下,只有第一个服务才会执行请求而不会出现错误。其他竞争对手必须不断尝试,直到成功。Elasticsearch 保证此类事务的一致性。

示例:

# creating the resource, take a look on _version field
# POST test/resources/42
{
"_index": "test",
"_type": "resources",
"_id": "42",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"created": true
}

# reading the resource
# GET test/resources/1
{
"_index": "test",
"_type": "resources",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"value": 1
}
}

# service number 1 trying to update the resource using version in url
# POST test/resources/1?version=1
# {...}
# response has new version
{
"_index": "test",
"_type": "resources",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"created": false
}

# service number 1 trying to update the resource -- fails
# POST test/resources/1?version=1
# {...}
{
"error": {
"root_cause": [
{
"type": "version_conflict_engine_exception",
"reason": "[resources][1]: version conflict, current version [2] is different than the one provided [1]",
"index_uuid": "nQcwn90wQ9WauH9ySC7qEw",
"shard": "3",
"index": "test"
}
],
"type": "version_conflict_engine_exception",
"reason": "[resources][1]: version conflict, current version [2] is different than the one provided [1]",
"index_uuid": "nQcwn90wQ9WauH9ySC7qEw",
"shard": "3",
"index": "test"
},
"status": 409
}

关于elasticsearch - 没有 ACID 数据库是否可以进行事务(就数据一致性而言)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50176818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com