← Lessons

quiz vs the machine

Platinum1780

System Design

Conflict Free Replicated Counters

Counting across replicas that update independently and merge without coordination using per replica tallies.

5 min read · advanced · beat Platinum to climb

The problem

Many replicas each want to increment a shared counter, such as likes on a post, without coordinating on every change. A naive shared integer requires locking and loses updates when two replicas write at once.

The grow only counter

A grow only counter gives each replica its own slot. A replica only ever increments its own slot, never another. The true value is the sum of all slots.

  • Two replicas can increment concurrently with no conflict.
  • Merging two states keeps the maximum of each slot, since slots only grow.
  • The merge is commutative, so order does not matter.

Supporting decrement

To allow decrements, keep two grow only counters: one for increments and one for decrements. The value is the increment sum minus the decrement sum. This is the positive negative counter.

Why it works

Because every operation is monotonic and merges take the max, replicas converge to the same value no matter what order updates arrive.

Key idea

A conflict free replicated counter gives each replica its own grow only slot, summing slots for the value and merging by taking the max, so replicas converge without any coordination.

Check yourself

Answer to earn rating on the learn ladder.

1. How does a grow only counter avoid conflicts between replicas?

2. How does a positive negative counter support decrements?