TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Cassandra is not row level consistent

274 pointsby leastangleover 8 years ago

20 comments

jbellisover 8 years ago
Cassandra developer here.<p>Lots of comments here about how Cassandra is AP so of course you get inconsistent (non-serializable) results.<p>This is true, to a point. I&#x27;m firmly convinced that AP is a better way to build distributed systems for fault tolerance, performance, and simplicity. But it&#x27;s incredibly useful to be able to &quot;opt in&quot; to CP for pieces of the application as needed. That&#x27;s what Cassandra&#x27;s lightweight transactions (LWT) are for, and that&#x27;s what the authors of this piece used.<p>However! Fundamentally, mixing serializable (LWT) and non-serializable (plain UPDATE) ops will produce unpredictable results and that&#x27;s what bit them here.<p>Basically the same as if you marked half the accesses to a concurrently-updated Java variable with &quot;synchronized&quot; and left it off of the other half as an &quot;optimization.&quot;<p>Don&#x27;t take shortcuts and you won&#x27;t get burned.
评论 #13036563 未加载
评论 #13036712 未加载
评论 #13034882 未加载
评论 #13038751 未加载
评论 #13035670 未加载
评论 #13039047 未加载
评论 #13035616 未加载
评论 #13036299 未加载
helperover 8 years ago
As a long time Cassandra user its easy to forget that some of Cassandra&#x27;s semantics will be surprising to new users. That being said, if you are considering adopting an AP database it really is important for you to know the details about how write conflicts get resolved. This is perhaps the biggest difference between Cassandra other databases like Riak and ought to be part of your decision making process instead of a surprise you run into later.<p>That being said, using Cassandra for distributed locks is a terrible idea. I can&#x27;t think of any way in which Cassandra would be better than using {Zookeeper,etcd,consul}. Trying to force a database to do something it really isn&#x27;t designed for will almost always lead to disappointment (and often resentment) of said database.
评论 #13033835 未加载
评论 #13033631 未加载
评论 #13033712 未加载
评论 #13034841 未加载
评论 #13033749 未加载
Animatsover 8 years ago
This:<p><pre><code> INSERT INTO locks (id, lock, revision) VALUES (&#x27;Tom&#x27;, true, 1) IF NOT EXISTS USING TTL 20; </code></pre> looks like a race condition. The same problem comes up in SQL databases - you can&#x27;t lock a row that doesn&#x27;t exist yet. If you write, in SQL:<p><pre><code> BEGIN TRANSACTION SELECT FROM locks WHERE id = &quot;Tom&quot; AND lock = true AND revision = 1; -- if no records returned INSERT INTO LOCKS locks (id, lock, revision) VALUES (&#x27;Tom&#x27;, true, 1) COMMIT </code></pre> you have a race condition. If two threads make that identical request near-simultaneously, both get a no-find from the select, and both do the INSERT. SELECT doesn&#x27;t lock rows that don&#x27;t exist.<p>The usual solution in SQL is to use UNIQUE indices which will cause an INSERT to fail if the record about to be inserted already exists.<p>I ran into this reassembling SMS message fragments, where I wanted to detect that all the parts had come in. The right answer was to do an INSERT for each new fragment, then COMMIT, then do a SELECT to see if all the fragments of a message were in. Doing the SELECT first produced a race condition.
评论 #13034297 未加载
评论 #13034726 未加载
评论 #13034303 未加载
aarturover 8 years ago
And there&#x27;s another surprise waiting to be discovered. The execution of a LWT is not guaranteed to return applied&#x2F;not-applied response [1]. It can raise a WriteTimeout exception that means &quot;I don&#x27;t know if applied&quot;. It looks like in that case it can be worked around by inserting a UUID and in case of a WriteTimeout reading the UUID using SERIAL consistency and checking if it&#x27;s the inserted UUID. But generally this limitation of LWTs makes implementing some algorithms impossible, e.g. you can&#x27;t implement a 100% reliable counter.<p>[1] <a href="https:&#x2F;&#x2F;issues.apache.org&#x2F;jira&#x2F;browse&#x2F;CASSANDRA-9328" rel="nofollow">https:&#x2F;&#x2F;issues.apache.org&#x2F;jira&#x2F;browse&#x2F;CASSANDRA-9328</a>
seanparsonsover 8 years ago
I railed against CQL right from the start and it&#x27;s precisely because of this kind of thing. Imitating SQL has the side effect of setting certain expectations and drags a certain mental model along with it.
im_down_w_otpover 8 years ago
If you&#x27;re not writing purely immutable data or can&#x27;t 100% guarantee a serialized reader&#x2F;writer, then you&#x27;re just looking for trouble with Cassandra.
beefsackover 8 years ago
The post was quite interesting, but I find image macros and GIFs really distracting in technical writings.
zzzcpanover 8 years ago
Shouldn&#x27;t Cassandra be using Lamport timestamps or even vector clocks there? Relying on timer and its resolution sounds strange for a database, especially a distributed one.
评论 #13033771 未加载
评论 #13033620 未加载
leastangleover 8 years ago
Author here.<p>Great discussion around the CAP theorem but it misses the point. AP vs CP &#x2F; Cassandra being AP is not relevant to this particular problem:<p>1) This is not a distributed systems corner case. You will run into this if you are running Cassandra on a single node. A node should be able to guarantee consistency internally during normal operation. If it is not able to do that, there is something wrong with the system.<p>2) This is a case where queries are being send from the same process&#x2F;thread and go to exactly the same nodes. Attach a simple, monotonically increasing query counter to each call and you can easily serialize it on the other side.
sigyover 8 years ago
I see this as a basic misunderstanding of how LWT works. If you want to ensure serializable operations, then you need to use LWT with preconditions that ensure serializable operations.<p>Even better, stop trying to emulate the old and tired distributed lock methods that have been proven over and over again to be insufficient.
refresh-credsover 8 years ago
If it&#x27;s not obtained synchronously what does the lock achieve?
supergirlover 8 years ago
Couldn&#x27;t explain the problem in a less cringy way? Sounds like a bug to me. So file a bug report?
评论 #13033548 未加载
agentgtover 8 years ago
I guess I&#x27;m old or just not hip (most likely both) but I had to google WAT (I know WTF but WAT ... never seen it).<p>Even now I&#x27;m still not sure but I presume WAT = what!
评论 #13034529 未加载
评论 #13036033 未加载
评论 #13034480 未加载
second_picardover 8 years ago
Hazelcast has a distributed lock (see <a href="http:&#x2F;&#x2F;docs.hazelcast.org&#x2F;docs&#x2F;3.7&#x2F;manual&#x2F;html-single&#x2F;index.html#lock" rel="nofollow">http:&#x2F;&#x2F;docs.hazelcast.org&#x2F;docs&#x2F;3.7&#x2F;manual&#x2F;html-single&#x2F;index....</a>) and I&#x27;ve used for more than a year to synchronize jobs across a cluster.
steeveover 8 years ago
Using Cassandra as a lock is a terrible, terrible idea.
knownover 8 years ago
With the Oracle&#x2F;PostgreSQL, readers never wait for writers and writers never wait for readers <a href="http:&#x2F;&#x2F;philip.greenspun.com&#x2F;sql&#x2F;your-own-rdbms.html" rel="nofollow">http:&#x2F;&#x2F;philip.greenspun.com&#x2F;sql&#x2F;your-own-rdbms.html</a> using underlying locking mechanism <a href="http:&#x2F;&#x2F;www.beej.us&#x2F;guide&#x2F;bgipc&#x2F;output&#x2F;html&#x2F;singlepage&#x2F;bgipc.html#flocking" rel="nofollow">http:&#x2F;&#x2F;www.beej.us&#x2F;guide&#x2F;bgipc&#x2F;output&#x2F;html&#x2F;singlepage&#x2F;bgipc....</a>
knownover 8 years ago
Its&#x27; better to implement <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Priority_inversion" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Priority_inversion</a> in all <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;NoSQL#Types_and_examples_of_NoSQL_databases" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;NoSQL#Types_and_examples_of_No...</a>
cbsmithover 8 years ago
It feels like it is 2013 all over again: <a href="https:&#x2F;&#x2F;aphyr.com&#x2F;posts&#x2F;294-jepsen-cassandra" rel="nofollow">https:&#x2F;&#x2F;aphyr.com&#x2F;posts&#x2F;294-jepsen-cassandra</a>
mydpyover 8 years ago
As others have noted, this blog uses an approach to data modeling that is considered an anti pattern for an AP data store like Cassandra.
neeleshsover 8 years ago
Anyone has experience around this on HBase, a CP database?
评论 #13034684 未加载