julanove
julanove

Reputation: 535

Invoking 1 AWS Lambda with API Gateway sequentially

I know there's a question with the same title but my question is a little different: I got a Lambda API - saveInputAPI() to save the value into a specified field. Users can invoke this API with different parameter, for example:

saveInput({"adressType",1}); //adressType is a DB field.

or

saveInput({"name","test"})  //name is a DB field.

And of course, this hosts on AWS so I'm also using API Gateway as well. But the problem is sometimes, an error like this happened:

enter image description here

As you can see. API No. 19 was invoked first but ended up finishing later

 (10:10:16:828) -> (10:10:18:060) 

While API No.18 was invoked later but finished sooner...

 (10:10:17:611) -> (10:10:17:861)

This leads to a lot of problems in my project. And sometimes, the delay between 2 API was up to 10 seconds. The front project acts independently so users don't know what happens behind. They think they have set addressType to 1 but in reality, the addressType is still 2. Since this project is large and I cannot change this kind of [using only 1 API to update DB value] design. Is there any way for me to fix this problem ?? Really appreciate any idea. Thanks

Upvotes: 1

Views: 246

Answers (2)

Balu Vyamajala
Balu Vyamajala

Reputation: 10393

If updates to Database can't be skipped if last updated timestamp is more recent than the source event timestamp, we need to decouple Api Gateway and Lambda.

  • Api Gateway writes to SQS FIFO Queue.
  • Lambda to consume SQS and process the request.

This will ensure older event is processed first.

Upvotes: 1

Nicolas Voron
Nicolas Voron

Reputation: 3006

Amazon Lambda is asynchronous by design. That means that trying to make it synchronous and predictable is kind of waste.

If your concern is avoiding "old" data (in a sense of scheduling) overwrite "fresh" data, then you might consider timestamping each data and then applying constraints like "if you want to overwrite target data, then your source timestamp have to be in the future compared to timestamp of the targeted data"

Upvotes: 0

Related Questions