elasticsearch—在没有acid数据库的情况下,有没有可能有事务(就数据一致性而言)?

vd2z7a6w  于 2021-06-06  发布在  Kafka
关注(0)|答案(2)|浏览(343)

我正在与nodejs、kafka和elasticsearch stack合作一个项目。我们有一个账户微服务,需要确保账户余额和交易数据的一致性(对于某些客户,余额只能是负数)。
考虑到我们不需要事务的结果是实时的,每个操作都可以异步处理,然后结果显示给客户,有一些模型(如事件源,cqrs)可以在没有acid数据库的情况下提供事务的数据一致性吗?
如果该模型只是做acid数据库已经做过的事情(而且做得很好),那么我们将实现一个sql数据库(postgresql)来适应这种情况。

kmynzznz

kmynzznz1#

是的,您可以在apachekafka(vid)中进行事务处理,还可以使用kafka connect elasticseach连接器(更多信息)将数据以幂等方式连接到elastic。

fzsnzjdm

fzsnzjdm2#

这不是创建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
版本控制用例:
假设我们需要更新来自不同工作者的资源。通常的做法是:
读取资源并获取其版本号
准备使用url中的读取版本号更新资源的请求
执行查询
如果没有错误——我们完成了
如果我们有错误转到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
}

相关问题