Hey @mattrothenberg_4zns, welcome to the forum, and thanks for being a loyal customer!
I think the nuance here is what your client means by āas close to instantly as possibleā. Is a 1-minute delay acceptable? 5 seconds? Is it ok for a few visitors to see stale content for several seconds while the cache updates in the background, or does every single visitor need real-time information?
If this is something that has to be truly real-time with updates visible in < 1 second (like live chat, or video games, or high frequency stocks, or some other use case like that), caching it will be very difficult. Itās possible that DatoCMS (or other headless services, for that matter) wouldnāt even be the right fit for that kind of data, vs having it live in some edge KV store or a fast redis server or similar. It really depends on the use case here⦠what kind of data are they storing, how often is it written and read, how quickly do invalidations have to happen, how long can a client wait for a network response, etc.?
If itās a more common use case like blog posts, many clients SAY they want fast updates, but if they are not very technical, sometimes they donāt really understand the range of what āfastā could mean in the web context.
As you probably know, using stale-while-revalidate
in combination with something like incremental static regeneration is a typical way to solve āfast enoughā for things like blog posts. Itās built in to frameworks like Next and can be manually added to Astro with a lot of work. You can also build similar things on top of Cloudflare Workers yourself, but by that point youāre basically reinventing Vercel.
Iām not familiar enough with Remix to be able to explain how it handles this, but it does seem like it supports SWR for rebuilding and redeploying pages (but itās not clear to me how it ties into a hosting provider⦠seems to me it might just work automatically on Vercel, or manually using remix-serve
on VMs? Iām not sure). Here are some links discussing it, though:
In every case, the common thing between those implementations is that they all have some sort of external caching going on, where pages are pre-baked into HTML files and served from a CDN. Those requests donāt hit our API at all and so you wonāt be charged for them (except for the invalidations).
If page-level caching isnāt fast enough for you, you might have to roll your own caching proxy for our API, again using something like a serverless worker, or even just a simple Cloudflare Page Rule that caches our paths on a time-based invalidation.
If that still isnāt fast enough, you could consider mirroring all your DatoCMS content (itās just a bunch of JSONs) into a memory cache like redis, but at that point Iād question whether this is the right service to use for data that realtime =/
Worst case, yes, you can have every visitor directly hit our API from their browser, but that will very quickly exhaust your API limits for any popular site. It also runs the risk of a malicious user extracting your API key and DoSing your account limit by just making the requests over and over again.
Despite this, some of our customers do exactly that (have their visitors directly query our APIs)⦠we donāt recommend it, though, because it does get very expensive quickly. Usually they come back and ask how to make it not so expensive, and we just recommend the same caching strategies as above (basically, static generation + regeneration at the file level, or at the very least a caching proxy for our API).
If I were in your situation, Iād probably try to build a few simple prototypes (in various frameworks), comparing the different caching strategies for their performance and costs. You can then make a report for your client, estimating the costs (made-up example): Approach A would cost $20/mo, but maybe 0.1% of your visitors would see slightly stale data for one minute, and your host/CDN would protect you from DoS attacks. Approach B means everybody sees up-the-second data all the time, but itāll cost you $500/mo in estimated monthly overages, potentially spiking a lot higher if you get DoSed. Explain the tradeoffs to them (in performance vs cost) and let them pick which one theyād want you to build, and charge them accordingly?
Probably most clients would be ok sacrificing a LITTLE bit of freshness for a lot of dollars savings, but it really depends on their use case!
Hope that helps a bit?