I can’t figure out what it is you’re doing. Is add_counter_for_key operating on a key that’s actually key + "counter", or something similar?
Are the transaction conflicts that big of a problem? You should be retrying the operation in the event of a conflict; occasional conflicts are to be expected.
If you’re writing stuff in C(++) you’ll want to write yourself some sort of handler which can retry transactions. Something like: