KeyValue Map policy not retrieving values as expected

Not applicable

I am incrementing a variable named counter by 1 every time a new request hits the Proxy flow.I am trying to achieve this using KVM policy.

Its working fine for first 4 requests.

Once 4 requests are done,I am resetting the value of counter to 1.I am able to retrieve the value as 1 in the final javascript policy of previous run.

But when the next request comes,it is showing as 3 for the counter variable.Most of the times,after 4 runs,its not showing expected value.

I have attached the trace session and the Proxy here.Please suggest what is the issue.

0 6 180
6 REPLIES 6

You can use distributed Quota policy to achieve a counter. This will not be accurate but will be better than KVM.

Imagine 2 requests hitting 2 MPs in Apigee and bumping the counter. One of them is usually lost as these changes are only compacted in the underlying Cassandra DB and not applied sequentially.

BaaS has a counter feature, please take a look

http://apigee.com/docs/app-services/content/events...

@Madhan Sadasivam ,Thanks for your response.

I checked BaaS counter feature already.But as it is been said in the page,"It may take up to 30 seconds after an event has been posted for the counter to be incremented.",this seems to be a setback in the scenario where we check the counter against some max threshold value and reset the counter.If the update in counter takes 30 seconds,it may not show correct results.Please let me know if you think otherwise.

@RadhamaniRamadoss , Just to add to @Madhan Sadasivam answer,

Use a Quota Policy with fairly large value per Month, enabled distributed option so that a central counter should be maintained and continuously synchronized across all Apigee Edge message processors.

Read the Quota used and then divide by your threshold in JS Policy when ever reminder is 0 pull records from BaaS based on request index.

For Example,

Threshold - 10

Quota Counter - 0 - 9 % 10 !=0 // do nothing - Store request in BaaS

Quota Counter - 10 % threshold = 10 % 10 = 0 , Pull results from 0 - 10 , If successful from target delete these records in response flow (May be time Consuming)

Quota Counter - 20 % threshold = 10 % 20 = 0 , Pull results from 0 - 20 , If successful from target delete these records in response flow

Just FYI, Its always better to do message collection in client app / server itself instead of doing it in the API proxy level until unless there is no other option.

@Anil Sagar,Thanks for your response.

As quota is not meant for implementing business logic of Collecting messages,I didnt check before on using quota.However,I will check its possibilities as you have suggested.

I have also got other suggestions to use Counters,Node.I will update if it works fine.

adas
Participant V

@RadhamaniRamadoss You are right. Even though you might be able to achieve this with Distributed Quota counters, that's not what its really meant for. If you are implementing business logic you would rather not use something which is meant for traffic management.

If your apiproxy is using node, it might be good to explore options with nodejs where you can write to something like redis. Here's a very simple example: https://gist.github.com/hubgit/399898

@arghya das,Thanks for providing the link.I will check it.