In Cassandra, if I run a query that increments a counter, then selects from that counter is that atomic?
Asked Answered
S

1

6

If I run a query that increments a counter, then selects from that counter is that atomic?

I am wanting to generate smaller (integer) numbers using Cassandra.

The usage will likely be generating fewer than 1000 Ids/day, but definitely needs to be non-conflicting, and would prefer minimal chance of waste (unused blocks of numbers).


I've considered the following option(s)

  1. I know I can do conditional read+update until I get a successful result.
  2. Similar to #1, but updating with a block of 10 or 100, then using that block to issue new ids in a separate service.
  3. Using a separate service altogether (sql backed?) for this purpose.

I'm also using UUIDs for records on creation, I'm only wanting to use the sequential numbers for records that are publicly published, and will be part of a URL.

Stewart answered 5/3, 2015 at 23:1 Comment(0)
R
7

The update operation is atomic, however you don't get the updated value back as an output from the operation. Update/Select combination is definitely not atomic. Any other process can increment/decrement counter value in between of your update and select calls, therefore making your results unpredictable. Here is a good description of how counters work: http://www.datastax.com/dev/blog/whats-new-in-cassandra-2-1-a-better-implementation-of-counters. Try to use some synchronization layer in front of Cassandra to accomplish atomicity of this operation. Astyanax is one of the examples.

Rosenkrantz answered 7/3, 2015 at 0:1 Comment(1)
Thanks... looks like I'll be doing conditional updates to get a group of id's with a retry mechanism in place.Stewart

© 2022 - 2024 — McMap. All rights reserved.