Can I atomically increment a column value in BigTable?
Asked Answered
D

2

5

Does BigTable support atomic increment operations similar to INCR in Redis?

There is this function in the Golang library for BT - https://godoc.org/cloud.google.com/go/bigtable#ReadModifyWrite.Increment. However, I can see a situation where two instances of an app try to increment at the same time, but it's only incremented once due to a race condition.

Drolet answered 9/8, 2018 at 17:32 Comment(0)
A
10

Bigtable operations are atomic at a row level - you won't encounter a race condition with this API call.

Analcite answered 9/8, 2018 at 20:29 Comment(3)
Thank you. With the Increment function in the library, isn't the increment happening client-side rather than server-side? So, if two clients incremented at the same time, could they end up writing the same number to BigTable in their own atomic operations?Drolet
The function happens on the server-side.Ericson
note that ReadModifyWrite replicates the function of a sequence in an RDBMS.Almuce
D
0

As of the time of this response, AddToCell would be the Bigtable equivalent of Redis' INCR and PFADD (for HLL) as these increment without doing a read first. https://cloud.google.com/blog/products/databases/distributed-counting-with-bigtable

For INCR like behavior, when defining the column family set it to intsum (below it is set to never garbage collect but you can set that to whatever you want)

cbt createfamily mytable families=myfamily:never:intsum

then add new values using addtocell

cbt addtocell mytable myrowkey mycolumnfamily:mycolumnqualifier=myvalue@mytimestamp

above syntax is the same as SetCell just swap AddToCell if you're familiar with SetCell.

You can do this using the client libraries as well e.g.

https://cloud.google.com/bigtable/docs/reference/data/rpc/google.bigtable.v2#google.bigtable.v2.Mutation.AddToCell

The feature supports intsum (for INCR), inthll (for approximate count distinct), intmin and intmax for min/max.

Droplight answered 21/8 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.