High-performance counter component for Convex that uses database sharding to handle concurrent increments without write conflicts.
npm install @convex-dev/sharded-counterThis component adds counters to Convex. It acts as a key-value store from string to number, with sharding to increase throughput when updating values.
Since it's built on Convex, everything is automatically consistent, reactive, and cached. Since it's built with Components, the operations are isolated and increment/decrement are atomic even if run in parallel.
For example, if you want to display one million checkboxes on your Convex site, you want to count the checkboxes in real-time while allowing a lot of the boxes to change in parallel.
More generally, whenever you have a counter that is changing frequently, you can use this component to keep track of it efficiently.
The Sharded Counter component distributes counter operations across multiple database shards to prevent write conflicts. It automatically manages shard allocation and provides atomic increment/decrement operations that scale with concurrent usage.
Use the Sharded Counter to handle scenarios where many users simultaneously update the same counter value. The component splits counter state across multiple documents, eliminating the single-document write bottleneck that causes conflicts in traditional counter implementations.
The Sharded Counter enables like buttons and voting systems that can handle hundreds of concurrent interactions. It provides consistent read operations that aggregate shard values while allowing unlimited concurrent increments without database write conflicts.
The Sharded Counter component splits counter state across multiple database documents instead of storing everything in a single document. This eliminates write conflicts when multiple users increment the counter simultaneously, allowing much higher throughput than traditional single-document counters.
The Sharded Counter provides strong consistency for individual increment operations and eventually consistent reads. Each increment operation is atomic within its shard, and read operations aggregate all shards to return the current total value.
The Sharded Counter automatically scales based on contention levels and can handle thousands of concurrent increment operations. The component dynamically manages shard allocation to distribute load and prevent any single shard from becoming a bottleneck.
Yes, the Sharded Counter component supports both increment and decrement operations. Both operations are distributed across shards in the same way, allowing you to build features like upvote/downvote systems or inventory tracking with high concurrency.