-
Notifications
You must be signed in to change notification settings - Fork 903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add caching to survey sync #2011
Conversation
@jonas-hoebenreich is attempting to deploy a commit to the formbricks Team on Vercel. A member of the Team first needs to authorize it. |
Thank you for following the naming conventions for pull request titles! 🙏 |
@jonas-hoebenreich thank you for the PR 😊💪 I'm not sure if caching the whole endpoint for a fixed amount of time is the best approach, since we also have cases where we resync faster (e.g. with identified users, when a survey is answered and we fetch the new list of surveys from the backend and calculate the surveys the user qualifies for based on the surveys he has seen and answered). This might fail with the proposed caching approach (at least as I understand it) since we get the (at most) 10 minutes old values that include the already answered survey. But I will look at this approach in more detail and check for edge cases and how we can solve them. |
@mattinannt, thanks for the quick response. If I understand correctly, the configuration in the frontend is only updated every 15 minutes (https://github.com/formbricks/formbricks/blob/main/packages/js/src/lib/config.ts#L28). This means that you have to assume that every (unidentified) user has a configuration that is 15 minutes old. This change would extend this span by 10 minutes (worst case scenario: after 15 minutes the user fetches 10min old config --> config is 25min old by the time the frontend cache expires). If that seems too long, how about a shorter Cache-Control time? The fundamental problem with unstable_cache is that it does not allow multiple containers (as the cache is specific to each container and is only revalidated in this container. If several containers are running at the same time, the cache in some containers is only revalidated after 30 minutes). |
@jonas-hoebenreich Thanks for the answer; yes, that makes sense :-) Next.js is also working on ways to better support complex caching strategies: https://nextjs.org/docs/app/building-your-application/deploying#configuring-caching |
Yes, this PR only reduces the load on the single container and improves the response time for users in remote regions (when using a CDN). The biggest problem with multiple containers that we have noticed is that in the backend the data is no longer displayed consistently in the backend. |
@jonas-hoebenreich Quick update: We will merge our advanced targeting today and afterwards I will test this PR again and merge it. Since the Advanced Targeting PR also changes a lot in terms of caching and sync endpoints, I want to make sure they work well together. |
What does this PR do?
This PR adds public caching of 10min to the app.formbricks.com/api/v1/client//in-app/sync endpoint. Since Formbricks caches the survey endpoint for 15min in the frontend I think it makes sense to also add it to the backend to avoid unnecessary server load.
The cache times are up for discussion, how do you see the balance between rapid updates and server load?
Fixes # (issue)
How should this be tested?
Checklist
Required
pnpm build
console.logs
git pull origin main
Appreciated