Database Reference
In-Depth Information
UPDATE "user_status_updates"
SET "starred_by_users" = "starred_by_users" + {'dave'}
WHERE "username" = 'alice'
AND "id" = 76e7a4d0-e796-11e3-90ce-5f98e903bf02;
Again, on reading the row, we can confirm that the list contains all the usernames that we
expected:
Crucially, this approach is entirely resilient to the concurrent update scenario we explored
previously. The problem with concurrent updates in the naïve approach was that one pro-
cess read a value for starred_by_users that subsequently became stale when a dif-
ferent process wrote an updated value to the column. This problem simply can't occur
with our strategy of directly adding a new value to the set in CQL, for the simple reason
that we never actually read a value that can become stale. The underlying structure of
Cassandra sets guarantees that concurrent additions to the set will not result in data loss;
the new value is added discretely to the set, not as part of a full overwrite of the set's val-
ues.
Removing values from a set
Let's say that dave has second thoughts and no longer wishes to have starred alice 's
status update. We can just as easily remove his name from the list:
UPDATE "user_status_updates"
SET "starred_by_users" = "starred_by_users" - {'dave'}
WHERE "username" = 'alice'
AND "id" = 76e7a4d0-e796-11e3-90ce-5f98e903bf02;
This is identical to our previous query, except that we use a - operator to indicate removal
rather than a + operator to indicate addition.
Search WWH ::




Custom Search