How does Cassandra serialise concurrent requests?

Viewed 31

I am reading about Cassandra and how other databases maintain the wal and memtable.

WAL - As name suggests, cassandra writes all mutations sequentially in this file

My question is - if there are thousands of parallel requests coming to the same node of cassandra, how does it maintain sequential writes to WAL.

I check cassandra code base and I could not find any thread lock or anything mutex operation.

Cassandra write code

 private CommitLogPosition addToCommitLog(Mutation mutation)
    {
        // Usually one of these will be true, so first check if that's the case.
        boolean allSkipCommitlog = true;
        boolean noneSkipCommitlog = true;
        for (PartitionUpdate update : mutation.getPartitionUpdates())
        {
            if (update.metadata().params.memtable.factory().writesShouldSkipCommitLog())
                noneSkipCommitlog = false;
            else
                allSkipCommitlog = false;
        }

        if (!noneSkipCommitlog)
        {
            if (allSkipCommitlog)
                return null;
            else
            {
                Set<TableId> ids = new HashSet<>();
                for (PartitionUpdate update : mutation.getPartitionUpdates())
                {
                    if (update.metadata().params.memtable.factory().writesShouldSkipCommitLog())
                        ids.add(update.metadata().id);
                }
                mutation = mutation.without(ids);
            }
        }
        // Note: It may be a good idea to precalculate none/all for the set of all tables in the keyspace,
        // or memoize the mutation.getTableIds()->ids map (needs invalidation on schema version change).

        Tracing.trace("Appending to commitlog");
        return CommitLog.instance.add(mutation);   --- ** Actual commit log write ***
    }

Any help would be appreciated.

1 Answers

All mutations are immediately appended to the end of the commitlog -- in whatever order they arrived on the replica.

The writes come in through a request queue that Cassandra processes first-in-first-out. There isn't any additional ordering that occurs.

Since all mutations are simply appended to the commit log, this makes writes in Cassandra very, very fast. Cheers!

Related