grahamrb
grahamrb

Reputation: 2269

How to efficiently count requests to API and Lambda function based on other variables?

I'm in the process of building a service on AWS that will run on several Lambda functions behind an API Gateway. The API Gateway will use a Lambda Authoriser to validate whether the API request can come through. I need a way to track the number of requests made based on some parameters of the request (two identifiers).

To do this I need to increment a count cheaply, efficiently and reliably against some key. What's an appropriate solution?

Options considered already

  1. I could use DynamoDB or SQL but don't think it's particularly efficient or reliable to read the value of an entry an then increment it - race conditions primarily.
  2. I've also considered using the built in API Key function but this is limited to 500 keys and can only in increased to 10k maximum which may not be enough.
  3. Redis or Memcached seem like an options although I don't have experience with these so am not certain - I'd also prefer something that had a lower starting price point.

More specific details

I plan on making an API available and charging for X hundred requests per month for each client that is calling it. A user might have multiple clients - so Bob might have 10 clients with 400 requests per month for each of them. When one of the clients goes over 400 requests I would then block access until they added more credits.

Upvotes: 0

Views: 837

Answers (1)

grahamrb
grahamrb

Reputation: 2269

Clearly I was failing to search for the right things earlier...

DynamoDB supports Atomic Counters which will increment a field without impacting any other attributes being updated.

Redis also looks like a good option to have higher speed and is probably cheaper for the use case as transaction volumes increase assuming the cluster stays quite small. The down side being the durability risks relating to Redis should the cluster go down.

Upvotes: 1

Related Questions