AlonL
AlonL

Reputation: 6660

Cassandra's lightweight transactions & Paxos consensus algorithm

I have a very particular question regarding Paxos algorithm, which is implemented in Cassandra's lightweight transactions:

What happens if two nodes issue the same proposal at the same time? Do them both get '[applied]: true' ?

For example, consider this table:

ids:
+-------------------+---------------+
| id_name (varchar) | next_id (int) |
+-------------------+---------------+
| person_id         |             1 |
+-------------------+---------------+

And this query:

UPDATE ids
SET next_id = 2
WHERE id_name = 'person_id'
IF next_id = 1

If I execute this query, I get as a response:

[{[applied]: True}]

If I execute it again, which then it won't be accepted since next_id != 1, I get:

[{[applied]: False, next_id: 2}]

My question is - what happens if I execute this query from two nodes in parallel. Is there a chance that they both get accepted?

(My use case is described in this stackoverflow question)

Upvotes: 3

Views: 1124

Answers (2)

G Quintana
G Quintana

Reputation: 4667

The effect of Paxos, is that queries get "linearized": 2 queries executed at the same time on the same row on 2 different nodes will result in one of them being executed after the other. And the second will not be applied. Obviously, both queries must use CAS for this to work. More info here and here.

Upvotes: 4

Stefan Podkowinski
Stefan Podkowinski

Reputation: 5249

It's not possible that both queries would be executed concurrently. For each query a proposal is created that will be used to reach consensus based on paxos. This will happen based on the timestamp associated with the proposal where an identical timestamp will still make on of the two proposals fail.

Upvotes: 1

Related Questions