You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by John Sanda <jo...@gmail.com> on 2015/07/08 03:35:01 UTC

Consistent reads and first write wins

Suppose I have the following schema,

CREATE TABLE foo (
    id text,
    time timeuuid,
    prop1 text,
    PRIMARY KEY (id, time)
)
WITHCLUSTERING ORDER BY (time ASC);

And I have two clients who execute quorum writes, e.g.,

// client 1
INSERT INTO FOO (id, time, prop1) VALUES ('test', <time_uuid_1>, 'bar');

// client 2
INSERT INTO FOO (id, time, prop1) VALUES ('test', <time_uuid_2>, 'bam');

If time_uuid_1 comes before time_uuid_2 and if both clients follow up the
writes with quorum reads, then will both clients see the value 'bar' for
prop1? Are there situations in which clients might see different values?


-- 

- John

Re: Consistent reads and first write wins

Posted by Jens Rantil <je...@tink.se>.
Hi John,

The general answer: Each cell in a CQL table has a corresponding timestamp
which is taken from the clock on the Cassandra node that orchestrates the
write. When you are reading from a Cassandra cluster the node that
coordinates the read will compare the timestamps of the values it fetches.
Last write(=highest timestamp) wins and will be returned to the client.

As you may now understand, the above is why it is crucial you NTP sync your
Cassandra nodes.

If time_uuid_1 comes before time_uuid_2 and if both clients follow up the
> writes with quorum reads, then will both clients see the value 'bar' for
> prop1?


As you might have understood by now, the values of your timeuuid aren't
really relevant here - the timestamp transparently taken from the clock of
the coordinating node is. This is because you could supply your own
timeuuid from the client, which might have a differing clock. However, it
will basically correspond to the timestamp if you use the helper function
`now()` in CQL.

Anyway, if you make a quorum write (that succeeds) and then make a
successful quorum read, you can be 100% that you will get the latest value.

Are there situations in which clients might see different values?


I can see three scenarios where that could happen:

   1. If you write with a weaker consistency such as ONE and read quorum.
   2. If you write with quorum and read with a weaker consistency such as
   ONE.
   3. If you make a quorum write that fails. That write might still have
   been applied to some node. Cassandra does not guarantee atomic writes (that
   is, either applied or not at all). In other words a failed write will not
   roll back partially applied writes in any way.

Cheers,
Jens

On Wed, Jul 8, 2015 at 3:35 AM, John Sanda <jo...@gmail.com> wrote:

> Suppose I have the following schema,
>
> CREATE TABLE foo (
>     id text,
>     time timeuuid,
>     prop1 text,
>     PRIMARY KEY (id, time)
> )
> WITHCLUSTERING ORDER BY (time ASC);
>
> And I have two clients who execute quorum writes, e.g.,
>
> // client 1
> INSERT INTO FOO (id, time, prop1) VALUES ('test', <time_uuid_1>, 'bar');
>
> // client 2
> INSERT INTO FOO (id, time, prop1) VALUES ('test', <time_uuid_2>, 'bam');
>
> If time_uuid_1 comes before time_uuid_2 and if both clients follow up the
> writes with quorum reads, then will both clients see the value 'bar' for
> prop1? Are there situations in which clients might see different values?
>
>
> --
>
> - John
>



-- 
Jens Rantil
Backend engineer
Tink AB

Email: jens.rantil@tink.se
Phone: +46 708 84 18 32
Web: www.tink.se

Facebook <https://www.facebook.com/#!/tink.se> Linkedin
<http://www.linkedin.com/company/2735919?trk=vsrp_companies_res_photo&trkInfo=VSRPsearchId%3A1057023381369207406670%2CVSRPtargetId%3A2735919%2CVSRPcmpt%3Aprimary>
 Twitter <https://twitter.com/tink>