Skip to content

fix(likes): count unique visitors, not duplicate rows#89

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-correctness-bugs-605e
Draft

fix(likes): count unique visitors, not duplicate rows#89
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-correctness-bugs-605e

Conversation

@cursor
Copy link
Copy Markdown

@cursor cursor Bot commented May 26, 2026

Bug and impact

Parallel setLike mutations could leave multiple likes rows for the same url + ipHash. The public count used row count (likes.length and backfill incrementLikeCount per row), so one visitor could inflate the displayed total (e.g. 2–3×). Dedup logic also called decrementLikeCount per removed duplicate, which could desync the denormalized counter.

Root cause

The denormalized likes work in #85 counted table rows instead of distinct visitors. The earlier race fix branch (4a4e7fc) used Set(ipHash) for reads; the counter migration regressed to row-based counting.

Fix

  • Count unique ipHash per URL for pre-backfill reads (uniqueLikeCount)
  • syncLikeCountForUrl after each setLike so the denormalized counter matches reality
  • Backfill syncs per URL from unique visitors instead of incrementing per row
  • Dedup deletes duplicate rows without decrementing (sync handles the counter)

Validation

  • Added scripts/like-unique-count-test.mjs (parallel POSTs; global count must not move by more than 1 for one visitor)
  • Requires live dev server + Convex to run the script end-to-end

Note: Production may still show stale inflated counts until affected pages get a like toggle or an admin re-runs backfill.run.

Open in Web View Automation 

Parallel setLike races could leave multiple likes rows for one ipHash.
Reads used row count (and backfill incremented per row), inflating counts.
Dedup decrements could also desync the denormalized counter.

Count distinct ipHashes, sync the counter after each mutation, and rebuild
backfill totals per URL from unique visitors.

Co-authored-by: Injoon Oh <injoon5@icloud.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented May 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
web Error Error May 26, 2026 3:07pm

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying web with  Cloudflare Pages  Cloudflare Pages

Latest commit: d6e92c1
Status:🚫  Build failed.

View logs

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant