Handling multiple counters in against a Key

Hi All,

I am new to this forum. We need help for one of our use case. We are stuck with one of our use case and trying to solve it without UDF or LUA, .i.e through single client call. Application on which we are currently working support high TPS. So We don’t want to do multiple round trips to Aerospike from our java application.

Scenario ,: We have 8 counter bins against a key let say “customerId” Bins:

  1. quota : Maximum cap on usage
  2. Sun: Sunday total used quota
  3. Mon: Monday total used quota
  4. Tue: Tuesday total used quota … so on for full week

Now we are incrementing counter in the bin which points to todays day of week. But white doing this increment, we have to stop if {Value(quota) - [Value(Sun) + Value(Mon) + ...+ Value(Sat)] } < 0. It means, all quota has been exhaust. We want to stop incrementing in bin if this formula returns 0.

Also there are scenarios in failure of task, we have to decrement current day usage counter. And a background daily job for T-2 day, recalculate used quota and decrement used value from Quota bin and reset that day bin value to 0.

You will want to use a Filter Expression. Which will abort the transaction if not true.

See the below pseudo code:


If you need additional help with this problem, please provide the client library and server versions that you are currently using.

BTW, you could save a bit of storage by using a list of counters instead of 8 bins - but you would then need to use expressions to increment the counters.

We are using java client version 4.1.2. FilterExpression is not part of this client. I believe server version is also 4.9

But i found the mentioned feature in latest client version

            List<Exp> exps= new ArrayList<Exp>();
            for(DayOfWeek day : DayOfWeek.values()) {
            	exps.add(Exp.intBin(day.getDisplayName(TextStyle.SHORT, Locale.ENGLISH)));
           policy.filterExp=Exp.build(Exp.gt(Exp.sub(exps.toArray(new Exp[0])), Exp.val(0l)));

PredExp is the older (deprecated) way to specify conditional operations. You can find it the 4.1.2 Java client documentation. It seems PredExp should allow you to do what you are trying. However, you may want to consider upgrading to the releases that support FilterExpression, as the PredExp code will not work in a future release when the support for it is withdrawn.

Thanks for help we upgraded the Aerospike to use FilterExpression. Also we need bin convergence too to support write on same key in cross cluster (XDR)

Great - glad you have the solution.

This topic was automatically closed 84 days after the last reply. New replies are no longer allowed.