Sharded Counter

High-performance counter component for Convex that uses database sharding to handle concurrent increments without write conflicts.

Installation

npm install @convex-dev/sharded-counter

About Sharded Counter

This 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.

Benefits

Use cases

how to implement high throughput counters in Convex

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.

Convex counter with concurrent updates

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.

scalable like button counter Convex database

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.

Frequently asked questions

How does sharded counter improve performance over regular Convex counters?

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.

Is the sharded counter eventually consistent or strongly consistent?

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.

How many concurrent operations can the sharded counter handle?

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.

Can I use sharded counters for decrementing values?

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.

Links