【问题标题】:Atomic operations across two ignite caches跨两个点燃缓存的原子操作
【发布时间】:2020-12-13 21:25:20
【问题描述】:

我有两个 ignite 缓存。它们都使用键/值对(并且不涉及数据库)来存储一些数据。

cache1 存储实际数据,而cache2 存储一些元数据。要求是确保两个缓存始终保持同步,即使出现任何问题。

有没有一种方法可以确保两个更新都能保证发生?如果其中一个操作失败,那么另一个操作也应该回滚。

这里是示例代码

cache1.put(someKey, someValue);
// some other code
cache2.put(anotherKey, anotherValue);

如何确保两个缓存均已更新或均未更新?

【问题讨论】:

    标签: ignite distributed-caching


    【解决方案1】:

    是的,您需要use transactions。例如:

    Ignite ignite = Ignition.ignite();
    
    IgniteTransactions transactions = ignite.transactions();
    
    try (Transaction tx = transactions.txStart()) {
      Integer hello = cache.get("Hello");
    
      if (hello == 1)
        cache.put("Hello", 11);
    
      cache.put("World", 22);
    
      tx.commit(); 
    }
    

    您的两个缓存都需要将 atomicityMode 参数设置为 TRANSACTIONAL 才能正常工作(默认为 ATOMIC)。

    【讨论】:

    • 谢谢@Stephen Darlington,我可以在同一个事务块中使用两个不同的缓存吗?我已更新问题以提供代码示例。此外,我看到事务是在ignite 而不是特定的缓存上获得的。这会在事务执行时影响其他线程/缓存吗?
    • @Yasin 只要它们都是事务性的,它就可以工作。在记录将被锁定的意义上,它会影响其他缓存/线程。如果两个进程不太可能更改同一记录,则可以将其更改为使用乐观锁定。
    • @Yasin 查看这两个详细介绍 Ignite 事务的记录:1) Apache Ignite ACID 事务和一致性架构师指南:youtu.be/TCsl-W0tsEE 2) 将 Apache Ignite 迁移到生产环境:分布式的最佳实践交易:youtu.be/yvMWK-4grYo
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多