Report
We are having a free service where api data changes with out any certain pattern.
Are there any ways to cache the dynamic content at CDN? Or are we limited to random low TTL values alone?
There are a lot of considerations when attempting to optimize your API.
A few questions to ask yourself first:
Answer these types of questions can help you narrow in on a solution, or even if you have a problem at all.
In general I don't like caching the actual API http calls unless the data is mostly static. Given you have dynamic data, I would stay away from a CDN or a reverse proxy (e.g. Varnish is a great one).
If you find the DB to be the slow part, try a faster system like Redis or an in memory DB. Also you can cache objects - like Node-Cache.
If processing time is the issue look to rewrite the processor or add CPUs.
Thanks for the details.
Cost savings - As db is charged based on reads.
Based on some events, data responses will be changing so wanted to cache the api responses.
Can I simply have a system like nginx with cache module to solve this?
I would look into reversed proxy like nginx.
Caching at CDN is going to be a challenge because they're usually not so quick at invalidating the cache entries (although, do check your CDN's docs to see what their API can do for cache invalidation).
Another option is to cache the data yourself in redis, memcached or similar store, and take care of cache invalidation yourself (ie. when you know your data is updated, you clear / update your redis / memcached cache).
Your servers would still be hit for each request, but hopefully they'll have a lot less work serving from cache than from database, etc.
Thanks for the reply!
Do you see any value in instant purge features offered by services like Fastly?
Purging the cache might be an expensive operation, some providers even charge for that! Of course everything depends on how often the data changes at the origin and so on. If it is something that changes often, or rather unpredictably, is probably better to handle caching yourself.
CDN is useful if you also need to be closer to your users and that perhaps might require a deeper analysis.