api3dao / airnode

Airnode monorepo
https://docs.api3.org/
MIT License
165 stars 72 forks source link

API responses can't be cached #26

Closed andreogle closed 2 years ago

andreogle commented 4 years ago

Description

API calls may or may not be idempotent so the API provider might not want to make duplicate API calls between serverless function invocations while the transaction is pending. The API response needs to be optionally cached.

The cache should store the following information against the requestId:

  1. The raw value extracted from _path or the error
  2. The timestamp
  3. The status boolean - either successful or failure.

Cache keys need to have an expiration mechanism too.

Before making an API call, the cache should be checked for the requestId. If a value is found, skip making that call and attempt another transaction with that value.

API calls should default to cached if a cache is configured. Otherwise, a new call should be made each serverless invocation.

Questions

  1. How exactly to implement the cache. What AWS service to use etc.
  2. When should the cache key be cleared? If a requestid exists in the cache, but no unfulfilled request is found?
bbenligiray commented 4 years ago

2 is very possible with something like a block reorg. We can safely let them expire on their own.

bbenligiray commented 4 years ago

One option we have for this is to call the API through a gateway and let it handle the caching (AWS can go up to an hour) https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-caching.html The issue here is that how reliable the cache is (because an unexpected cache miss would mean a duplicate API call).

The obvious solution is to use a NoSQL database, cache by requestId and expire each item in an hour.

Note that both solutions are cloud provider-specific.

MrAngus666 commented 3 years ago

An implementation that supports the AWS, Azure and GCP can use the redis services that are provided as a service on the three providers: https://cloud.google.com/memorystore https://azure.microsoft.com/en-us/services/cache/#overview https://aws.amazon.com/elasticache/redis/

We will have code specific for each provider but at the end it is the same core product, redis, using it as as key, value pair.

bbenligiray commented 3 years ago

This will be easier to implement if we migrate to bare Terraform, which is currently being worked on

bbenligiray commented 3 years ago

We'll probably not support this for the containerized version (at least initially)

bbenligiray commented 2 years ago

0.7.0 allows caching in the global scope with no hard guarantees. Closing this issue in line with the changes in how we use Github issues internally.