feat(followers): implement CRDT-based follower count to eliminate los… - #761
Merged
Chucks1093 merged 1 commit intoAug 29, 2026
Merged
Conversation
Alaka-ibr
force-pushed
the
feat/crdt-follower-count-757
branch
3 times, most recently
from
August 29, 2026 00:19
f110e78 to
e894e44
Compare
…t updates (accesslayerorg#757) Replace monolithic follower count with a per-node G-Counter CRDT structure to prevent lost updates under high concurrency. - FollowerCounterShard and FollowEvent Prisma schema definitions - NODE_ID configuration option in config schema - Follow and unfollow operations using atomic shard increments and idempotent events - getFollowerCount summing across all node shards with 10s Redis caching - Nightly shard compaction merging per-node shards atomically - Unit tests verifying 100 concurrent follows, 50 follows + 30 unfollows, double-follow idempotency, multi-node resolution, and compaction
Alaka-ibr
force-pushed
the
feat/crdt-follower-count-757
branch
from
August 29, 2026 00:27
e894e44 to
4932e2f
Compare
6 tasks
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.
feat(followers): implement CRDT-based follower count to eliminate lost updates (#757)
Summary
Closes #757
Under concurrent follow and unfollow bursts for the same creator, direct integer increment/decrement updates suffer from lost updates and race conditions. This PR replaces the simple counter with a G-Counter CRDT structure per node, computing the true count as the sum of all increments minus decrements across all nodes at read time.
Changes
1. Database Schema (
prisma/schema/follower.prisma)FollowerCounterShard:creatorWallet,nodeId,increments(BigInt),decrements(BigInt),updatedAt. Unique key:(creatorWallet, nodeId).FollowEvent:followerWallet,creatorWallet,direction(FOLLOW|UNFOLLOW),createdAt. Unique key:(followerWallet, creatorWallet).2. Node Identification (
src/config.schema.ts)NODE_IDconfiguration parameter (defaults tonode-local).3. Atomic Follow/Unfollow & Count Resolution (
src/modules/followers/follower.service.ts)UPDATE ... SET increments = increments + 1) for localNODE_ID.getFollowerCountsumsincrementsanddecrementsacross all shards forcreatorWallet, returnscompactShardsForCreatormerges all shards for a creator into a single canonical shard in a single transaction (skips creators updated in the last 5 minutes).4. Endpoints & Nightly Job (
src/modules/followers/follower.controllers.ts,src/jobs/follower-shard-compaction.job.ts)POST /api/v1/followers/:creatorWallet/followPOST /api/v1/followers/:creatorWallet/unfollowGET /api/v1/followers/:creatorWallet/countrunNightlyFollowerShardCompaction.Test Coverage
Unit tests in
src/modules/followers/follower.service.test.ts(5 passed):