Counter是幂等的数据,当操作失败时,可能实际操作成功了,也可能不成功。如何处理这种情况?
Bringing together the Apache Cassandra experts from the community and DataStax.
Want to learn? Have a question? Want to share your expertise? You are in the right place!
Not sure where to begin? Getting Started
在分布式计数器的实现里,幂等和重传一直是一个很具有挑战的问题。个人觉得,Cassandra里面实现的Counter是分布式数据库里面能找到最好的一个实现了,因为这个功能已经演进了多达8年的时间,进化了好几个版本,现在,在Cassandra 3.0+的这个Counter实现 可以说是经过了很多生产系统中的实战考验。
但是,你的问题里面提到的这个准确性问题,一直都是理论计算机领域的一个难点。当操作失败时,客户端如果决定重传,就会导致over counting,如果决定不重传,就会导致under counting。
所以我的感觉是:除非有新的研究和论文发明了更好的distributed counter的算法,你应该避免把计数器类型使用在需要100%准确性的数据上,比如银行账户余额 就应该避免使用Counter。DataStax一直在密切跟踪分布式系统学科领域的前沿方向,如果有新的Counter算法被提出来,我们会很有兴趣把它应用到Cassandra中来。
上面提到的银行账户余额这种使用场景,如果你需要准确跟踪余额变化的话,一个更好的办法是使用记账本(Ledger)的数据模型,也就是用一个时间序列的partition把每一笔增减变化都记录在这个账号下面的partition里面,然后周期性的加总,把余额总数放在一个static column里面。这样的话,不管对这个分区有多少并发的写,或者失败重传,都不会丢失准确性。
5 People are following this question.
DataStax Enterprise is powered by the best distribution of Apache Cassandra ™
© 2021 DataStax, Titan, and TitanDB are registered trademarks of DataStax, Inc. and its subsidiaries in the United States and/or other countries.
Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries.
Privacy Policy Terms of Use