Does invalidate(); perform an actual refetch?

I am half way into the course video and I see void ctx.post.getAll.invalidate(); to show the new post we are making. Isn't this just refetching? Wouldn't it be better to have a local state/catch being updated when successful vs trying to refetch everything all over again? just seems very costly especially if an app grows. Please let me know if I am wrong in my thinking or how I should be thinking when we consider refetching data once successful. Thank you
Solution:
on personal projects where I know perfectly the architecture and every API call being made, I always update the cache, and set the stale duration to Infinity. On enterprise projects where 50 devs work on the same app, sometimes it's just a mess and you'd have to update 7 different cache entries and know exactly how the data is structured in each, so we tend to just invalidate
Jump to solution
10 Replies
Futures Trader
Looks like there is a way. I'll continue with the course then revisit this as looking at the docs there is a
setData queryClient.setQueryData
getData queryClient.getQueryData
setData queryClient.setQueryData
getData queryClient.getQueryData
option where we just update local catch vs trying to perform a fetch But still if someone things updating local cache is a bad idea. Please let me know about your experience
bostonsheraff
bostonsheraff2y ago
updating the cache can be better indeed. I think your intuition is correct but invalidate is more like "reality hit, and it's actually much simpler and not that much slower"
Solution
bostonsheraff
bostonsheraff2y ago
on personal projects where I know perfectly the architecture and every API call being made, I always update the cache, and set the stale duration to Infinity. On enterprise projects where 50 devs work on the same app, sometimes it's just a mess and you'd have to update 7 different cache entries and know exactly how the data is structured in each, so we tend to just invalidate
bostonsheraff
bostonsheraff2y ago
invalidate will refetch only if the query is mounted anyway, so you might invalidate 7 things and in actuality only 1 or 2 get refetched instantly (the rest will get refetched later if they ever become mounted again)
Futures Trader
Thank you for this input and also providing personal experience. I do have a question. Depending on the project, can it become costly if you are just refetching? For example a social media site considering you are refetching 10+ data with profile image, text, title etc Although I do consider performance, I was also thinking the cost of trying to get a lot of data again after updating or posting a single post for example.
bostonsheraff
bostonsheraff2y ago
depends on your infra. Some endpoints might actually just hit the cache layer and are virtually "free" to refetch, and on the other end of the spectrum some endpoints trigger a waterfall of unoptimised database queries that clog your server so then the decision of whether to invalidate by default, or update the cache by default (with exceptions allowed in both cases of course) becomes a matter of context, how much code do you want to ship on initial load (each cache update represents a few lines of JS), how slow your server is, how many users you have, how fast you need to ship features... there isn't really a perfect answer. But I do agree that "philosophically", updating the cache makes more sense that invalidating it
Futures Trader
Thank you for this input. A lot to consider but also some aren't "too" critical until an application actually scales and sees a performance hit. Always fun (as of late and when I got into NextJS) to consider different situations to optimize SEO, Performance and even cost considering data is coming in and out A rate limiter that I am learning now is amazing! considering someone refreshing 100+ times for fun would be a killer xD I'll close this one and reopen it if I get stuck with local update after the course. Thank you again @bostonsheraff for replying 🙏 appreciate it.
bostonsheraff
bostonsheraff2y ago
It's always good to check you aren't really doing something that is bad practice when you're learning on your own. But in that case it seems like you're doing ok, and then on the flip-side, be careful of premature optimisation. and no worries, was bored at work today
Futures Trader
Yea that is the hard part haha "premature optimisation" What is enough and what is not enough when trying to start something Can always try to optimize but it also takes a lot of time. Don't get to hopefully get to a point where you get a call that it all crashed As it couldn't handle an uptick (but I guess that is also good news as it means people like it xD)
bostonsheraff
bostonsheraff2y ago
Yeah don't plan too much for a future that might never exist
Want results from more Discord servers?
Add your server