Make likes system robust: denormalized counter + optimistic UI#85
Merged
Conversation
Reads no longer collect every like row to count. A denormalized likeCounts table (mirroring commentUrlCounts) keeps the count O(1), and get reads only the counter doc plus the visitor's single membership row, so a viewer's realtime subscription re-runs only when the count or their own like changes. The toggle mutation is replaced with an idempotent setLike(liked): rapid clicks and double-fires converge on the final desired state instead of racing, and idempotent re-sends consume no rate-limit token. liked omitted still toggles for backward compatibility. The button now updates optimistically and serializes writes (one in flight, always converging on the latest intent), reconciling against the realtime query, so like/unlike feels instant and never drifts out of sync. Adds a likeCounts backfill job + migration flag with a read-time fallback so counts stay correct before backfill. https://claude.ai/code/session_01BcpfW29BRVq2KEJYypLVs6
Replace the single-shot flush with a serialized sync loop that keeps the request in flight until the server is driven to the latest intent. Because inFlight stays true across every send, the reconcile effect (gated on !inFlight) can only clear the optimistic override after the server actually reflects the intent — so a fast like/unlike during a request can no longer settle the button opposite to the server. On any failure the optimistic state reverts to the server value and an error is shown. Navigating mid-request abandons the old page's intent while still letting the in-flight write persist, and a new intent that arrives while the lock is held is re-driven once it releases. https://claude.ai/code/session_01BcpfW29BRVq2KEJYypLVs6
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This was referenced May 26, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Reads no longer collect every like row to count. A denormalized
likeCounts table (mirroring commentUrlCounts) keeps the count O(1),
and get reads only the counter doc plus the visitor's single
membership row, so a viewer's realtime subscription re-runs only
when the count or their own like changes.
The toggle mutation is replaced with an idempotent setLike(liked):
rapid clicks and double-fires converge on the final desired state
instead of racing, and idempotent re-sends consume no rate-limit
token. liked omitted still toggles for backward compatibility.
The button now updates optimistically and serializes writes (one
in flight, always converging on the latest intent), reconciling
against the realtime query, so like/unlike feels instant and never
drifts out of sync. Adds a likeCounts backfill job + migration flag
with a read-time fallback so counts stay correct before backfill.
https://claude.ai/code/session_01BcpfW29BRVq2KEJYypLVs6