The Slotted Counter Pattern

The Slotted Counter Pattern

4/9/2025

notes

a lot of simple things break when the needs of scale and real time start putting on pressure.

link

https://planetscale.com/blog/the-slotted-counter-pattern

summary

It is a common database pattern to increment an INT column when an event happens, such as a download or page view. You can go far with this pattern until bursts of these types of events happen in parallel and you experience contention on a single row. When multiple transactions are trying to update the counter, you are essentially forcing these transactions to run serially, which is bad for concurrency and can cause deadlocks. You can also see dramatic increases in query time when bursts like this occur.

tags

MySQL ꞏ database ꞏ concurrency ꞏ performance ꞏ optimization ꞏ counter