Skip to content

fix(cache): separate viewer-specific follow state from cached public profiles#531

Open
Ridanshi wants to merge 1 commit into
Dev-Card:mainfrom
Ridanshi:fix/issue-483-profile-cache-viewer-state
Open

fix(cache): separate viewer-specific follow state from cached public profiles#531
Ridanshi wants to merge 1 commit into
Dev-Card:mainfrom
Ridanshi:fix/issue-483-profile-cache-viewer-state

Conversation

@Ridanshi

@Ridanshi Ridanshi commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Closes #483

Summary

This PR fixes a cache contamination issue where viewer-specific follow state was being stored inside cached public profile responses.

As a result, authenticated users could receive incorrect follow indicators when cached profile data was served.

Changes

  • Refactored public profile caching to store only shared profile data
  • Removed viewer-specific follow state from cache payloads
  • Recomputed follow state for each authenticated request
  • Preserved cache behavior for anonymous visitors
  • Ensured follow relationships are evaluated independently of cache hits

Test Coverage

Added/updated tests covering:

  • Cache hit behavior
  • Cache miss behavior
  • Viewer A and Viewer B receiving different follow states from the same cached profile
  • Anonymous viewer behavior
  • Follow and unfollow state updates with warm cache
  • Verification that viewer-specific state is never persisted in cache

Result

Public profile caching remains efficient while authenticated users always receive accurate follow-state information regardless of cache status.

…e cache

Previously, getPublicProfile stored the full response (including `followed`
state for the request's viewer) directly in Redis. On cache HIT, every
subsequent viewer received the follow state of whoever populated the cache
— causing incorrect followed=false or followed=true for all links regardless
of the actual viewer's follow history.

Fix: cache entries now store only viewer-independent fields (id, platform,
username, url, displayOrder). The `followed` field is excluded from the
cache. On every request — HIT or MISS — follow state is computed fresh from
followLog for the authenticated viewer, then merged into the response before
returning.

Also stores `_userId` in the cache entry so background view-tracking can
fire on cache-HIT requests without an extra DB round-trip, while ensuring
`_userId` is stripped from all HTTP responses.

Add comprehensive tests covering:
- Cache population writes shared-only data (no `followed` field)
- Cache HIT returns X-Cache: HIT and skips DB query
- Authenticated viewer on HIT triggers fresh followLog query
- Anonymous request on HIT skips followLog query
- Viewer A and Viewer B receive different follow states from same cached entry
- Cache entry bytes are identical regardless of which viewer triggered population
- Viewer A's follow state does not bleed into Viewer B's response
- Follow/unfollow with warm cache returns correct state immediately
- Regression: 404, 200, Cache-Control, Redis fallback all intact
@vercel

vercel Bot commented Jun 9, 2026

Copy link
Copy Markdown

@Ridanshi is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

Hi @Ridanshi,

Thanks for opening this pull request.

This PR has been automatically classified based on the files modified.

Applied Labels

  • backend

Primary Review Area

  • backend

Reviewer

@Harxhit has been identified as the primary reviewer for this pull request.

If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer.

Thank you for your contribution!

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

CI — All Checks Passed

Backend — PASS

Check Result
Lint PASS
Test PASS
Typecheck PASS

Mobile — SKIP

Check Result
Lint -
Test -

Web — SKIP

Check Result
Check -
Build -

Last updated: Tue, 09 Jun 2026 19:52:06 GMT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cached public profiles lose viewer-specific follow state and display incorrect follow status

1 participant