A simple group scoped counter system (PostgreSQL/MongoDB/MySql)

June 18, 2024 (10mo ago)

Id of every new task in JIRA board starts with 1 and increments by 1 for each new task. There isn’t any database that supports group scoped auto increment. Let's explore some solutions in this blog.

Using auto increment (MySql/PostgreSQL)

Using sequence in PostgreSQL

Using counter table (PostgreSQL/MongoDB)

We can use counter table to solve above problems.

    Column     |            Type             | Collation | Nullable | Default
---------------+-----------------------------+-----------+----------+---------
 name          | character varying(255)      |           | not null |
 value         | bigint                      |           | not null |

Query to update the counter and return updated value in one round trip

UPDATE counter
SET
  value = value+1
WHERE
  name = :counterName
RETURNING value

Note: In MongoDB, you can use a dedicated document for each counter similar to dedicated row in PostgreSQL/MySql

Scaling Challanges

You must be a very big company(like google, microsoft) before you hit the limit.

Scaling above counter system very simple, let's start from zero and scale it to handle large number of requests.

Scaling: Counter table in the same database

If you are not an enterprice or huge tech gaint(google/amazon), you can start with keeping the counter table along with other tables in database.

Scaling: Moving counter table to a dedicated database

If you hit the limit, and counter table is frequently used. You can move counter table to a dedicated database. It'll allows you to scale up/down your counter service as per your need. For most of the companies, vertical scaling should be enough.

Scaling: Using sharding

If you hit the limit of vertical scale, you can use sharding. Keep different counters on different databases. This way, you'll be able to achieve ultimate scale.

Scaling: beyond sharding

Congratulations 👏

Summary

A lightweight, scalable, and durable group-scoped counter system can be built using a simple counter table or document in PostgreSQL/MongoDB, avoiding the limitations of auto-increment and heavy database sequences.