3
6 Comments

Caching strategies for REST Api

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?

posted to Icon for group Developers
Developers
on May 17, 2021
  1. 1

    There are a lot of considerations when attempting to optimize your API.

    A few questions to ask yourself first:

    • Why are you looking to cache the API? Speed, cost savings?
    • If speed, where are the bottle necks in you system. Also, what is fast enough - 3 second response, 100ms response?
    • If cost, similar to speed, what are the expense points? Also, how are your costs growing and can eventual revenue make it not a concern?

    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.

    1. 1

      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?

  2. 1

    I would look into reversed proxy like nginx.

  3. 1

    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.

    1. 1

      Thanks for the reply!
      Do you see any value in instant purge features offered by services like Fastly?

      1. 1

        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.

Trending on Indie Hackers
Your AI Product Is Not A Real Business User Avatar 121 comments Stop Building Features: Why 80% of Your Roadmap is a Waste of Time User Avatar 87 comments I built an enterprise AI chatbot platform solo — 6 microservices, 7 channels, and Claude Code as my co-developer User Avatar 44 comments The Clarity Trap: Why “Pretty” Pages Kill Profits (And What To Do Instead) User Avatar 37 comments How to build a quick and dirty prototype to validate your idea User Avatar 34 comments I got let go, spent 18 months building a productivity app, and now I'm taking it to Kickstarter User Avatar 23 comments