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

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

我正在与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保证了此类事务的一致性。
例子:

  1. # creating the resource, take a look on _version field
  2. # POST test/resources/42
  3. {
  4. "_index": "test",
  5. "_type": "resources",
  6. "_id": "42",
  7. "_version": 1,
  8. "result": "created",
  9. "_shards": {
  10. "total": 1,
  11. "successful": 1,
  12. "failed": 0
  13. },
  14. "created": true
  15. }
  16. # reading the resource
  17. # GET test/resources/1
  18. {
  19. "_index": "test",
  20. "_type": "resources",
  21. "_id": "1",
  22. "_version": 1,
  23. "found": true,
  24. "_source": {
  25. "value": 1
  26. }
  27. }
  28. # service number 1 trying to update the resource using version in url
  29. # POST test/resources/1?version=1
  30. # {...}
  31. # response has new version
  32. {
  33. "_index": "test",
  34. "_type": "resources",
  35. "_id": "1",
  36. "_version": 2,
  37. "result": "updated",
  38. "_shards": {
  39. "total": 1,
  40. "successful": 1,
  41. "failed": 0
  42. },
  43. "created": false
  44. }
  45. # service number 1 trying to update the resource -- fails
  46. # POST test/resources/1?version=1
  47. # {...}
  48. {
  49. "error": {
  50. "root_cause": [
  51. {
  52. "type": "version_conflict_engine_exception",
  53. "reason": "[resources][1]: version conflict, current version [2] is different than the one provided [1]",
  54. "index_uuid": "nQcwn90wQ9WauH9ySC7qEw",
  55. "shard": "3",
  56. "index": "test"
  57. }
  58. ],
  59. "type": "version_conflict_engine_exception",
  60. "reason": "[resources][1]: version conflict, current version [2] is different than the one provided [1]",
  61. "index_uuid": "nQcwn90wQ9WauH9ySC7qEw",
  62. "shard": "3",
  63. "index": "test"
  64. },
  65. "status": 409
  66. }
展开查看全部

相关问题