-
Notifications
You must be signed in to change notification settings - Fork 0
Tanstackquery cache data와 invalidateQueries #2
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
Open
4BFC
wants to merge
19
commits into
main
Choose a base branch
from
poby/tanstackquery-cache-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b42ecde
chore: axios 설치
4BFC f05849f
chore: react-router-dom, vite-plugin-pages 설치
4BFC fbe3ca5
feat: React page routing 구현
4BFC 7948197
feat: tanstckquery 페이지 생성
4BFC fc2eed4
feat: AppRoutes에서 micro provider 설정 구현
4BFC 68b8a76
feat: queryClient 안티 패턴 주석 추가 및 config 객체 추가
4BFC fce9869
feat: `reqres` user api에 사용되는 service 힘수 구현
4BFC 8c0acf6
feat: user를 조회하고 생성할 수 있는 훅 구현
4BFC ed3382e
feat: 간단한 interface 구현
4BFC 723d45d
chore: reqres API 등록 및 CORS로 인한 vite-proxy 설정
4BFC f45ba3c
refactor: reqres 응답 구조에 맞게 네이밍을 스네일 케이스로 변경
4BFC 2f3cc0a
chore: env gitignore에 추가
4BFC 6477a93
refactor: reqres에서 mockapi 로 수정
4BFC 66b6d6e
feat: update 함수 구현 및 훅 추가
4BFC 5623253
feat: user 디테일 페이지 구현
4BFC 26e89bf
docs: 작업 중 발생한 트러블 슈팅 문서화 작성
4BFC 330316b
docs: stale cache의 상호작용 문제점 문서화
4BFC 133deed
docs: reference 링크 추가
4BFC 852cc0e
fix: 문서 수정
4BFC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,6 @@ CLAUDE.md | |
| CLUADE.md | ||
| .claude | ||
| .claude/ | ||
|
|
||
| # env | ||
| .env | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # TanStack Query Stale Cache | ||
|
|
||
| ## 배경 | ||
|
|
||
| 상세 정보에서 사용자의 데이터를 변경 또는 수정을 하고 다시 리스트로 돌아가면 리스트에서는 과거의 데이터가 남아 있는 경우가 있다. 이럴 때, cache를 invalidateQueries를 사용해서 무력화 하는데, invalidateQueries가 동작하는 방식과 invalidateQueries 대시 다른 대안과 해결책을 모색하는 것이 목표이자, 배경이다. | ||
|
|
||
| ## 문제점 | ||
|
|
||
| 1. 불필요한 네트워크 요청 - 이미 mutation 응답에 새 데이터가 있는데 또 fetch | ||
| 2. Stale cache 문제 - 상호작용 되어야하는 페이지간 querykey를 여러번 호출 | ||
| 3. UX 저하 - 업데이트 후 깜빡임 | ||
|
|
||
| ## 원인 | ||
|
|
||
| > 해당 문서는 문제점 2번을 중점으로 바라본 문서이다. | ||
|
|
||
| invalidateQueries를 사용하면 cache를 강제로 refetch를 한다고 이해를 했다. 하지만, invalidateQueries는 cache의 상태를 stale 상태로 변경을 하고 마운트 언마운트 상태에 따라 refetch의 수행이 이루어진다. invalidateQueries의 동작방식은 ~~(TQ는 cache를 신선도에 따라 상태를 분리한다.)~~ 현재 key의 cache가 fresh 하던 stale 하던 상관 없이 stale로 상태를 둔다. 그리고 현 페이지가 ~~(invalidateQueries를 호출하고)~~ 마운트가 된 상태라면 refetch가 된다. 정리하자면, 상세 페이지에서는 invalidateQueries로 stale mark를 찍고 마운트가 되면 refetch가 이루어진다. 단, 목록 페이지에서는 cache의 상태값을 변경하는 로직이 없기 때문에, 변경된 값이 반영되지 않고 이전 데이터가 보이는 것이다. | ||
|
|
||
| ## 해결 방안 | ||
|
|
||
| > useUsers.ts의 [useUpdateUser](../../workspace/react/ts/src/hooks/useUsers.ts) 참고 | ||
|
|
||
| - setQueryData | ||
| - setQueryData를 사용해서 직접 특정 querykey를 업데이트, 리스트도 함께 업데이트 하는 방법이 있다. | ||
|
|
||
| ```ts | ||
| export function useUpdateUser() { | ||
| const queryClient = useQueryClient() | ||
|
|
||
| return useMutation({ | ||
| mutationFn: ({ id, ...payload }) => updateUser(id, payload), | ||
| onSuccess: (newUser) => { | ||
| // 서버 응답으로 캐시 직접 업데이트 | ||
| queryClient.setQueryData( | ||
| userKeys.detail(newUser.id), | ||
| newUser | ||
| ) | ||
| // 리스트도 업데이트 | ||
| queryClient.setQueryData(userKeys.lists(), (old) => | ||
| old?.map(user => user.id === newUser.id ? newUser : user) | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| ``` | ||
|
|
||
| - Optimistic update | ||
| - 낙관적 업데이트 방법은 실문에서 자주 사용했다. 변경된 UI를 Network 요청 결과와는 무관하게 즉시 없데이트를 하고, onError와 같은 실패 시 롤백 할 수 있게 구현을 방법이다. | ||
|
|
||
| ```ts | ||
| useMutation({ | ||
| mutationFn: updateUser, | ||
| onMutate: async (newData) => { | ||
| await queryClient.cancelQueries({ queryKey: userKeys.lists() }) | ||
| const previous = queryClient.getQueryData(userKeys.lists()) | ||
|
|
||
| // 즉시 UI 업데이트 | ||
| queryClient.setQueryData(userKeys.lists(), (old) => | ||
| old?.map(u => u.id === newData.id ? {...u, ...newData} : u) | ||
| ) | ||
| return { previous } | ||
| }, | ||
| onError: (err, newData, context) => { | ||
| // 실패 시 롤백 | ||
| queryClient.setQueryData(userKeys.lists(), context.previous) | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ## 결론 | ||
|
|
||
| 처음에는 InvalidateQueries가 좋지 않은 anti-pattern이거나 TQ에서 권장하지 않는 방식이라 생각했다. 개념을 다시 정리하고 살펴보면 단순히 cache의 신선도를 stale로 변경하고 해당 query가 호출, 구독된 컴포넌트(QueryObserver)를 마운트 언마운트에 따라 refetch가 결정되는 방식이란 것을 이해하고 다시 코드 useUsers.ts의 useUpdateUser함수를 보면 lists도 다음과 같이 invalidateQueries를 하면 되는 것 아닌가 싶었다. | ||
|
|
||
| ```ts | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({queryKey: userKeys.details()}) | ||
| queryClient.invalidateQueries({queryKey: userKeys.lists()}) | ||
| } | ||
| ``` | ||
|
|
||
| 즉, querykey가 체이닝되는 관계성만 명확하게 명시, 기획이 되어 있다면, InvalidateQueries가 성능을 저하하거나 좋지 않은 방식은 아니라 생각이 된다. 이외의 다른 관점으로 InvalidateQueries의 평가 문제 정의가 필요하다고 생각한다. | ||
|
|
||
| --- | ||
|
|
||
| ## 트러블슈팅 | ||
|
|
||
| 현재 TQ provider의 query 설정 외부 주입할 수 있게 작성을 했다. 이런 방법은 현재로는 매우 오버스팩일 수 있겠다는 생각을 했다. | ||
|
|
||
| ### createQueryClinet anti-pattern | ||
|
|
||
| - 현재 createQueryClient는 micro 디렉토리 전략 방식에 사용하기 위해서 분리를 했으나 이로 인해서 발생하는 문제는 다음과 같다. | ||
| - 각 페이지간 서로의 캐시를 공유할 수 없게된다. 즉, Provider간 key 값이 달라지기 때문에 그렇다. 그로 인해서 메모리 낭비가 발생한다. 매번 QueryClinet가 여러개 새로 생성되기 때문이다. | ||
|
|
||
| - 그렇다면 createQueryClinet는 어떻게 사용하면 좋을까? | ||
| - 테스트 환경에서 사용하면 좋다. | ||
| - 환경별(dev/prod)와 같이 분리된 공간에서 사용하면 좋다. | ||
| - 임베디드 위젯과 같이 독립된 곳에서 사용하면 좋다. | ||
| - Storybook과 같은 곳에서 사용하면 좋다. | ||
| - 즉, 모노레포와 같은 곳에서 사용, 각기 다른 도메인과 같은 곳에서 사용하면 좋다. 따라서 해상 createQueryClient는 common이나 shared같은 곳에서 관리하면서 사용하면 좋다. | ||
|
|
||
| - 결론은 개별 쿼리 훅(ex.useUsers)에서 관리하면서 사용하는 것이 좋다. | ||
|
|
||
| ### 참고 문서 | ||
|
|
||
| - [micro frontend](https://jobkaehenry.tistory.com/64) | ||
| - [query-invalidation](https://tanstack.com/query/v5/docs/framework/react/guides/query-invalidation) | ||
| - [QueryClient](https://tanstack.com/query/latest/docs/reference/QueryClient) | ||
| - [Mutation 이후 전체 Query를 invalidation 하기](https://yogjin.tistory.com/130) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
query-key-facroty패턴을 통해 일괄적으로 invalidate시킬 수 있긴하지만, 이전 캐싱데이터의 참조에 의한 문제는 결국 optimistic update밖에 없을까 아쉬운 느낌이 드네요ㅠ
ex) 유저 A의 데이터를 업데이트 하고 onsuccess에 detail,list를 invalidate시킨 후 페이지를 이동해서 업데이트한 그 유저의 데이터를 useEffect를 통해 바로 사용하는 경우에는 이전 캐싱데이터를 참조해서 로직을 돌리기 때문에 문제가 발생하는 경우도 있을 것 같아요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
말씀하신 부분도 추가해서 다시 한번 재현을 해보겠습니다! 제가 이해한 이전 케시가 남아 있다는 부분을 stale의 비동기로 인해 발생한 문제로만 이해를 했는데요. 말씀하신 부분의 문제를 명료하게 한번 다시 보고 싶어서요! 감사합니다. 해당 PR에서 이어서 진행해보고 해결 방안을 한번 다시 한번 고려해보겠습니다.
더불어, invalidate를 query-key-factory를 일괄적으로 호출하면 api 호출이 2번 발생할 수 있는 점이 마음에 걸리긴하네요.. 물론 cache를 디테일하게 관리하고 검증한다면 호출 빈도를 안정적으로 맞출수는 있지만, 좀더 네트워크 측면으로 생각해보겠습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문득 들은 생각인데, onSuccess일 경우에 굳이 invalidate시키지말고 queryClient.fetchQuery로 재호출해줘도 괜찮을 것 같다고 생각이 드네요! 이런식으로도 update가 잘 반영되는지도 체크해보면 좋을 것 같아요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵, 해당 부분도 탐구하고 추가해서 작성해 놓겠습니다. 감사합니다.!!
cc. @KimKyungYun @za0012