Back to System Design Lab

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

1 counter row. Each dot stands for 28 waiting likes.
4×

Getting busy

More people arrive, up to 1,500 likes per second.

Per second
01,6623,324Second 0Second 17Second 34Second 51Second 68Answered per secStill waitingFailed
  • Answered per sec
  • Still waiting
  • Failed

Two recordings, same load

One row16 rows
Taps answered per second, median5391,639
Wait until recorded, p954,976 ms5 ms
Likes failed (over 5 seconds)8,6150
Answered failed, still recorded8640
Taps that never got sent40,3770
Repeat taps not counted2,6867,499
Gap between counter and likes00

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.

Views in59,401
Passed the check40,014
Shown40K

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

  • Exact (list of names)1.4 MB
  • HyperLogLog14 KB

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.

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

CheckFieldOne row16 rows
Likes exact (shards = rows, every second and at the end)like_tepatyesyes
Like rows in Postgreslike_baris27,24872,794
Sum of all shardslike_jumlah_shard27,24872,794
New likes minus unlikeslike_baru - batal26,38472,794
Answered failed but committedgagal_tapi_tercatat8640
Repeat taps refusedlike_ganda_ditolak2,6867,499
Seconds without a Postgres readdetik_tanpa_baca_db00
Likes failed (5 s limit)like_galat8,6150
Taps never sent (k6 VUs used up)terbuang_k640,3770
Worst per-second p95 (ms)p95_ms_maks5,041161
Connections waiting on a lock, mostmenunggu_kunci_maks3117
Service CPU, highest (% of 1 vCPU)cpu_layanan_maks9886
HLL error against the exact set (%)galat_hll_persen00
Bot views passed / bot viewsview_bot_lolos2,401 / 11,4022,400 / 11,400
Laptop CPU before / after (%)cpu_host_*17 / 3130 / 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

ShardLikes/s (median, range)p95 msWaiting on a lockService CPU %
1689 (679 - 699)172.719.526
2956 (948 - 964)143.224.533
41,489 (1,360 - 1,618)102.628.048
83,007 (2,921 - 3,093)45.123.067
164,664 (4,369 - 4,958)24.215.388
325,759 (5,338 - 6,181)15.96.094
646,123 (5,981 - 6,265)13.92.097

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.

Pattern sources