I am storing some counters in aerospike - say counter a
,b
and c
along with a parent_id say pid
and obviously a pk say pk
. I need to increment the counters from my service so I wrote three functions incrementA
, incrementB
and incrementC
.
Now assume, I had to call function incrementA
from the service. The counter a
gets incremented but none of the other counters get initialised to 0
. Now, I understand that when I call incrementB
or incrementC
the respective counters will get incremented but I can't find a way to initialise the pid
.
I could think of the following ways to solve the above issue:
- Write the initialisation logic in my service. So, in aerospike I have something like
{'pk':'pk1', 'a':0,'b':0,'c':0,'pid':'pid1'}
. - Whenever I do an increment for any counter, I also do an initialization for other counters or the
pid
if not already done.
The problem with the above ways is:
- To initialize from the service, I will have to check if the initialization is already done (otherwise I will be resetting my counters). This would essentially double the number of aerospike calls. (Would batching calls help here?)
- In case of initializing for others while incrementing a particular counter, I will be updating the
pid
again and again with the same value.
I would appreciate it if someone could suggest a better way!!
PS: I need pid
in the db for every pk
because I need to query all the pk
s having the same pid
.
You can call incrementX (A,B or C) to increment X without initialising the others and on the side of the application that reads the counters if a bin of a specific counter doesn't exists treat it like its counter is 0.
Regarding the pid I would consider adding it (or creating the record with it) in another flow - once you know the pk/parent relationship (even if its when the pk is created) add/update the record (pk and pid only) - not on every incrementation request.
The default value of RecordExistsAction field of the WritePolicy (part of every write method in Aerospike) is UPDATE meaning Create if not exists or Update if exists so it doesn't matter which runs first - the incrementation or the pk - parent_id relationship.
Another solution (a bit of an overkill) might be to add a singleton service that contains a Map<Integer, Boolean> of pk ids and an initialization indications.
When incrementation occurs:
{'pk':'pk1', 'a':0,'b':0,'c':0,'pid':'pid1'}
and add the pk to the singleton map to mark it as initialized.