hicetnunc-api icon indicating copy to clipboard operation
hicetnunc-api copied to clipboard

Improve API performance with a cache

Open RadW2020 opened this issue 4 years ago • 2 comments

API responses speed could be dramatically increased if a memory cache like REDIS is implemented. What do you think about this? I could give a hand if you need it https://redis.io/topics/client-side-caching

RadW2020 avatar Apr 05 '21 21:04 RadW2020

Without adding new hardware (for the time being) would it be easier to add caching on calls to resources that don't change often but are requested often?

For example we could cache the feed query for even 10 seconds, in memory (JS). We don't really want people sitting there updating the feed more than every 10 seconds anyway right?

We could even add this to random feed which I'm sure would save a LOT of bandwidth. Why update it on each call? 10-30 seconds would be fine to give people time to actually look through the items returned.

All that said it looks like there is some code in a pull request to do some caching, although I don't quite understand it yet? Or at least some throttling. https://github.com/hicetnunc2000/hicetnunc-api/pull/15

There also seems to be code in the main branch that has been commented out that looks like it was meant to do some caching but unsure as it seems incomplete.

tarwin avatar Apr 06 '21 06:04 tarwin

I think this could improve the page load time by 15s, or almost 10x. The opportunity is that fetching here is currently uncached: https://github.com/hicetnunc2000/hicetnunc-api/blob/master/lib/router/readFeed.js#L7

For example, this link: https://51rknuvw76.execute-api.us-east-1.amazonaws.com/dev/featured?counter=0&max_time=1623454080000

So there are two ways forward if we want to cache. (1) Add a CloudFront caching layer in front of the Lambda function, caching on the GET request. (2) Instrument application-level caching using Elasticache or Redis with the route itself.

Here's a good tutorial on caching options with Lambda functions on AWS: https://theburningmonk.com/2019/10/all-you-need-to-know-about-caching-for-serverless-applications/

Engineering considerations:

  • What timeout is good for the front page? I think we can reuse the OBJKTs cache.
  • What other opportunities to cache are there?

17s load, 15s of it from that JSON query: image

wyc avatar Jun 11 '21 23:06 wyc