Likes must be exact, views can be close
A photo of a CGM rice cooker went viral. Thousands of people tap like and watch in the same second. This is a load test on a laptop, played back second by second.
How likes are counted
Getting busy
More people arrive, up to 1,500 likes per second.
- Answered per sec
- Still waiting
- Failed
Two recordings, same load
| One row | 16 rows | |
|---|---|---|
| Taps answered per second, median | 539 | 1,639 |
| Wait until recorded, p95 | 4,976 ms | 5 ms |
| Likes failed (over 5 seconds) | 8,615 | 0 |
| Answered failed, still recorded | 864 | 0 |
| Taps that never got sent | 40,377 | 0 |
| Repeat taps not counted | 2,686 | 7,499 |
| Gap between counter and likes | 0 | 0 |
Your like never counts twice
One in ten people in this test tapped like twice at once. Those 10,185 repeat taps were turned away, so each person counts once. The like count and the list of people who liked are written together, so they match in every second. With one row, 864 likes were answered as failed although they were already recorded. If the person taps again, it still counts once.
How many rows are enough
With one row, every like waits for the previous one to reach the disk. Each extra row opens another lane, until the limit is no longer the rows but the server's processor.
Likes recorded per second, 64 people tapping nonstop. Two rounds per point; the thin line is the range.
The cost is on the read side: the total is the sum of all rows, so 64 rows means adding 64 numbers every time the page loads.
View counts can be close
Rewatches, bots and people scrolling past all come in. The number you see is filtered and rounded. It only goes up after a check, about every 5 seconds.
Unique viewers
An exact count needs a list of every viewer: 30,415 names, 1.4 MB. HyperLogLog keeps 16,384 small numbers, 14 KB, and estimates 30,415. Across both recordings it is off by at most 1.32%.
Bots that got through
The check drops views shorter than 3 seconds and every view after the third from the same person. Bots that replay nonstop are dropped too. Bots that use a new name for each view and watch for 4 seconds get through: 2,400 of 11,400 bot views. Catching them needs other signals, in the bot experiment.
Try the HyperLogLog
Add viewers and watch the 16,384 boxes below. Each viewer raises at most one box. Viewers who come back change nothing.
Show the technical side
One like, one SQL statement
WITH baru AS (
INSERT INTO suka (pos, orang) VALUES ($1, $2)
ON CONFLICT DO NOTHING RETURNING 1)
UPDATE suka_hitung SET n = n + 1
WHERE pos = $1 AND shard = $3 -- $3 acak, 0..N-1
AND EXISTS (SELECT 1 FROM baru)- suka has the primary key (pos, orang). A second tap from the same person waits on the unique index until the first commits, then hits ON CONFLICT and raises nothing.
- One statement is one transaction: the like row and the shard go up together or not at all. So the shard sum always equals the like rows in any snapshot, and the sampler checks it every second in one SELECT.
- UPDATE holds the shard row lock until commit, and commit waits for the WAL to reach disk (default synchronous_commit). With one shard every like queues on that lock; with N shards the 32 pool connections spread over N rows.
- Reading the total is SELECT sum(n) over N rows. Your own like status is read straight from the suka row, so it is always exact even while others read the total.
- Unlike uses the same shape with DELETE and n - 1 on a random shard; one shard can go negative, only the sum means anything.
- The 5 second limit cuts the request on the service side, but a COMMIT that already reached Postgres still goes through. So a like answered as failed may already be recorded. Because the INSERT is idempotent, the client can safely retry: it is still one row.
One view, one Redis pipeline
- POST /api/lihat sends INCR sk:mentah, PFADD sk:hll, SADD sk:pasti (the lab-only reference) and XADD to the sk:periksa stream in one round trip.
- The checker reads the stream every 5 seconds in batches of 5,000. Its rule only sees identity and watch time: under 3 seconds is dropped, more than 3 counted views per identity is dropped. The bot flag from k6 sits in another field and is read only after the decision, to measure the rule.
- The public number is the verified count rounded down (12.3K), like YouTube's public counts. Rounding down means the public number never exceeds the real one.
- Only one checker may run. With several instances it needs a consumer group and per-identity counts raised in Lua.
Evidence from the two recordings
| Check | Field | One row | 16 rows |
|---|---|---|---|
| Likes exact (shards = rows, every second and at the end) | like_tepat | yes | yes |
| Like rows in Postgres | like_baris | 27,248 | 72,794 |
| Sum of all shards | like_jumlah_shard | 27,248 | 72,794 |
| New likes minus unlikes | like_baru - batal | 26,384 | 72,794 |
| Answered failed but committed | gagal_tapi_tercatat | 864 | 0 |
| Repeat taps refused | like_ganda_ditolak | 2,686 | 7,499 |
| Seconds without a Postgres read | detik_tanpa_baca_db | 0 | 0 |
| Likes failed (5 s limit) | like_galat | 8,615 | 0 |
| Taps never sent (k6 VUs used up) | terbuang_k6 | 40,377 | 0 |
| Worst per-second p95 (ms) | p95_ms_maks | 5,041 | 161 |
| Connections waiting on a lock, most | menunggu_kunci_maks | 31 | 17 |
| Service CPU, highest (% of 1 vCPU) | cpu_layanan_maks | 98 | 86 |
| HLL error against the exact set (%) | galat_hll_persen | 0 | 0 |
| Bot views passed / bot views | view_bot_lolos | 2,401 / 11,402 | 2,400 / 11,400 |
| Laptop CPU before / after (%) | cpu_host_* | 17 / 31 | 30 / 31 |
Load: 1,500 likes per second at the peak (10 s ramp up, hold, 10 s ramp down, 60 s), 800 views per second from 40,000 identities, plus bots. Recorded 2026-11-23, hasil/like/shard-1 and shard-16.
Shard curve
| Shard | Likes/s (median, range) | p95 ms | Waiting on a lock | Service CPU % |
|---|---|---|---|---|
| 1 | 689 (679 - 699) | 172.7 | 19.5 | 26 |
| 2 | 956 (948 - 964) | 143.2 | 24.5 | 33 |
| 4 | 1,489 (1,360 - 1,618) | 102.6 | 28.0 | 48 |
| 8 | 3,007 (2,921 - 3,093) | 45.1 | 23.0 | 67 |
| 16 | 4,664 (4,369 - 4,958) | 24.2 | 15.3 | 88 |
| 32 | 5,759 (5,338 - 6,181) | 15.9 | 6.0 | 94 |
| 64 | 6,123 (5,981 - 6,265) | 13.9 | 2.0 | 97 |
Closed load: 64 k6 users tapping like nonstop, 15 s per point, two interleaved rounds. Median of the steady seconds. Laptop CPU 42% before, 18% after. suka dibatasi 1 vCPU / 1 GB (GOMAXPROCS 1, 32 koneksi Postgres); Postgres 16 dan Redis 7.4 tanpa batas; laptop 12 CPU (AMD Ryzen 5 5500), RAM 32 GB; k6 di laptop yang sama.
HyperLogLog
- Redis uses 16,384 six-bit registers, 12 KB of content, with a 0.81% standard error. MEMORY USAGE reads 14 KB because of the header and allocator rounding. The exact set for 30,415 viewers: 1.4 MB.
- The Go test measures the error against the exact set: 100 viewers exact, 5,000 viewers 0.02%, 60,000 viewers 1.18%, all under the test's 2% limit (about 2.5 times the standard error).
- The demo on this page uses its own HyperLogLog (cyrb53 hash, Flajolet's formula and linear counting), not Redis, so its numbers will not match Redis exactly. Its error is tested the same way: under 3 times the standard error up to 200,000 viewers.