Cassandra write lock

2019-08-18 00:44发布

问题:

I'm doing a project where we use Cassandra and I've stumped at an optimisation point. We want to change the database structure considering that read actions are a pretty heavy load on Cassandra.

I searched the documentation and forums for an answer but I couldn't find a clear answer to the following question.

Currently we write in small batches to Cassandra and reading will happen continuously. We want to make the batches bigger so we maybe change like half the table every 10-15 minutes or so. And as far as I could find Cassandra only locks the row when applying a write action. But is this true? Or does it lock a whole table when writing in it. And (maybe a little bit of a stupid question) can you read while there is a write-lock? (because there is a chance you can still read when there is only a write-lock right?).

The documentation does not show this kind of flow and the question seems to have never been asked before. Thanks in advanced!

回答1:

Cassandra does not Lock row

In Cassandra batch is used to achieve Atomicity. Atomic means that if any of the batch succeeds, all of it will.

To achieve atomicity, By default Cassandra first writes the serialized batch to the batchlog system table that consumes the serialized batch as blob data. When the rows in the batch have been successfully written and persisted (or hinted) the batchlog data is removed

Although atomic batch guarantees atomicity. there is no batch isolation. Clients are able to read the first updated rows from the batch, while other rows are still being updated on the server. However, transactional row updates within a partition key are isolated: clients cannot read a partial update.

Source : http://docs.datastax.com/en/cql/3.1/cql/cql_reference/batch_r.html

Another Thing It is recommended that batch size must be small. There are two configuration property in cassandra.yaml, which not recommended to change batch_size_warn_threshold_in_kb and batch_size_fail_threshold_in_kb

Default value :

batch_size_warn_threshold_in_kb: 5
batch_size_fail_threshold_in_kb: 50

So when your batch size is 5k, a warning will be logged and when the batch size is 50k or grater your batch will failed.



回答2:

There's no locks in Cassandra, where did you read about row locking?

Cassandra working as kind of "append only" writing, and "stale" data removed during compactions.

With all the changes you are doing, you can create many "stale" cells, and your reads will be heavy, so with such high number of changes to the rows i'll suggest using LCS, which suits the most for many of such workflows.