首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否可以在没有ACID数据库的情况下拥有事务(就数据一致性而言)?

是否可以在没有ACID数据库的情况下拥有事务(就数据一致性而言)?
EN

Stack Overflow用户
提问于 2018-05-04 22:03:42
回答 2查看 1.9K关注 0票数 3

我正在与NodeJS,Kafka和Elasticsearch栈进行一个项目。我们有一个账户微服务,需要确保账户余额和交易的数据一致性(余额只能对某些客户为负)。

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

如果这个模型很难做到ACID数据库已经在做的事情(而且做得很好),我们将实现一个SQL数据库(PostgreSQL)来适应这种情况。

EN

回答 2

Stack Overflow用户

发布于 2018-05-04 22:21:11

这不是创建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. 准备请求使用url中的读取版本号更新资源
  3. execute query
    • 如果没有错误,我们执行
    • 如果有错误,转到url

在竞争条件的情况下,只有第一个服务将执行请求而不会出现错误。其他竞争者将不得不尝试,直到他们成功。Elasticsearch保证了这类事务的一致性。

示例:

代码语言:javascript
运行
复制
# 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
}
票数 4
EN

Stack Overflow用户

发布于 2018-05-04 23:30:27

是的,你可以做transactional processing in Apache Kafka (vid),你也可以使用kafka-connect-elasticseach连接器(more info)将数据幂等地登陆到Elastic。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50176818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档