Writer race
Up to 200 producers send commands to one writer for three seconds, and everyone watching sees the batches grow, tick by tick.
The demo
live
Race 90
Finished in 3.0 s
- Commands
- 256,934
- Transactions
- 1,938
- Average batch
- 132.6
- Commit p99
- 5.1 ms
85,616 commands a second
over the whole race: commands divided by its time
commands that share one commit
Transactions by size
commands per transaction, in powers of two
commands waiting for the writer, of 200 producers
Commit p99
5.1 ms
of the last 1,024 transactions, BEGIN to COMMIT
In line
Nobody waits. A race you start now runs at once.
Last races
| Race | Producers, sync | Commands a second | Per transaction | p99 | Commands | Transactions |
|---|---|---|---|---|---|---|
| 90(yours) | 200, NORMAL | 85,616 | 132.6 | 5.1 ms | 256,934 | 1,938 |
| 89(yours) | 200, FULL | 21,335 | 129.6 | 14 ms | 64,157 | 495 |
| 88(yours) | 200, NORMAL | 84,914 | 140.0 | 7.7 ms | 254,828 | 1,820 |
| 87(yours) | 200, NORMAL | 83,795 | 152.1 | 19 ms | 251,470 | 1,653 |
| 86(yours) | 200, NORMAL | 84,268 | 127.1 | 5.8 ms | 252,890 | 1,989 |
| 85(yours) | 200, NORMAL | 90,317 | 130.9 | 5.6 ms | 270,952 | 2,070 |
| 84(yours) | 200, NORMAL | 86,143 | 136.8 | 5.9 ms | 258,430 | 1,889 |
| 83(yours) | 200, NORMAL | 49,178 | 158.6 | 20 ms | 147,536 | 930 |
How it works
The command each producer sends
Every command of the race is a transfer, the command of lith's writer benchmark: it reads a balance, debits one account, credits another and appends to a ledger. With the writer's SAVEPOINT and RELEASE around it, that is six statements.
func (c transfer) Apply(tx *writer.Tx) error {
var balance int64
if err := tx.QueryRow(`SELECT balance FROM accounts WHERE id = ?`, c.from).Scan(&balance); err != nil {
return err
}
if balance < c.amount {
return errInsufficient
}
debit := `UPDATE accounts SET balance = balance - ?, version = version + 1 WHERE id = ?`
if _, err := tx.Exec(debit, c.amount, c.from); err != nil {
return err
}
credit := `UPDATE accounts SET balance = balance + ?, version = version + 1 WHERE id = ?`
if _, err := tx.Exec(credit, c.amount, c.to); err != nil {
return err
}
entry := `INSERT INTO ledger (from_id, to_id, amount, at) VALUES (?, ?, ?, ?)`
if _, err := tx.Exec(entry, c.from, c.to, c.amount, time.Now().Unix()); err != nil {
return err
}
tx.Touch(c.topic)
return nil
}
The producers
A producer waits until its command has committed, as a request handler waits in w.Do, and then sends the next one. While the writer runs a batch, the commands of the other producers queue up, and the next batch takes all of them. One producer gets a transaction for every command. With two hundred, a transaction takes a hundred commands or more, and they share its commit, and with synchronous=FULL its fsync.
Two hundred goroutines waiting in w.Do kept most of a core busy at the site's priority. So one goroutine, the driver, sends for every producer, on a thread with the writer's lower priority, and each command hands its producer back to the driver with tx.AfterCommit, which runs on the writer's thread:
for waiting > 0 {
select {
case p := <-committed:
waiting--
if time.Now().Before(deadline) && ctx.Err() == nil {
if err := send(p); err != nil {
fail(err)
continue
}
waiting++
}
case err := <-failures:
waiting-- // that producer stops
fail(err)
}
}
The numbers come from writer.Stats
Every 50 ms the race reads its writer's Stats and counts from the moment the producers started. Stats.Sizes counts the transactions by the commands they took, in powers of two: the histogram on the track draws it. P99Commit is the 99th percentile of the last 1,024 transactions, from BEGIN IMMEDIATE to the end of COMMIT.
read := func() Sample {
st := w.Stats()
return Sample{
At: time.Since(start),
Commands: st.Commands - base.Commands,
Batches: st.Batches - base.Batches,
Failures: st.Failures - base.Failures,
Queue: st.Queue,
MaxBatch: st.MaxBatch,
P99: st.P99Commit,
Sizes: sizesOf(st.Sizes).minus(sizesOf(base.Sizes)),
}
}
From the race to every tab
The race has a writer of its own on a file of its own: its commands never queue in front of the site's. Its samples reach your page the way every change on this site does, as commands on the site's writer. The runner sends each sample as a Record with Send, which returns at once, and ends the race with a Finish.
func (r *runner) run(race Race) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
last, err := r.e.Run(ctx, race.ID, race.Settings, func(s Sample) {
switch err := r.w.Send(Record{Race: race.ID, Sample: s}); {
case errors.Is(err, writer.ErrClosed):
cancel() // the site stops: so does the race
case err != nil:
r.log.Warn("writer race: a sample", "race", race.ID, "err", err)
}
})
Record stores the sample and touches live:writer-race. The track's view listens to that topic: it renders once in the next tick, from the samples in SQLite, and every tab that shows the track gets the same frame. Its frames morph at once, without a view transition; the bars and the progress ease between them in CSS.
func trackView() live.View {
return live.View{
Key: Slug + ":track",
Topics: []string{Topic, LiveTopic},
Render: func(ctx context.Context, tx *sql.Tx) (any, error) {
t, err := loadTrack(ctx, demo.Querier(ctx, tx))
if err != nil {
return nil, err
}
return demo.Patch(ctx, Track(t))
},
}
}
One race at a time
The line is a table on the site's database, so it survives a deploy. One goroutine, the runner, takes races off it. A Start that puts a race in line wakes it with tx.AfterCommit; it then runs races until nobody waits. claim is a command too: it marks the first race in line as running once it has waited 800 ms, so that every tab sees it join the line before it leaves it.
func (r *runner) races() error {
for {
n, err := r.w.DoValue(context.Background(), claim(time.Now(), r.dwell))
switch {
case err != nil:
return err
case n.Wait > 0:
time.Sleep(n.Wait) // the first in line joined a moment ago
case n.Race.ID == 0:
return nil // nobody waits
default:
r.run(n.Race)
// The host rests longer the more races wait: half a pause
// more for each.
time.Sleep(r.pause + time.Duration(n.Behind)*r.pause/2)
}
}
}
A long line also makes each race smaller: with three or more races behind it, a race runs with 50 producers at most, and the track says so.
What a race costs this server
The race's writer is one goroutine, so a race keeps one core busy at most, and less with FULL, where it waits for the disk. On the development machine of lith's benchmarks, an Intel i5-13500, with 100 tabs open on this page, the whole site used 0.8 to 1 core while a race of 200 producers with NORMAL ran, and its heap rose by about 20 MiB at the peak. The race's file grows to about 7 MiB and is deleted when the race ends; the site deletes any that a restart left behind when it starts.
The writer's goroutine and the producers' driver each stay on a thread of their own, at a lower CPU and disk priority, so the site and the other services on this server win whenever they want the CPU or the disk. The threads end with the race.
func lowerPriority() error {
tid := syscall.Gettid()
if err := syscall.Setpriority(syscall.PRIO_PROCESS, tid, 10); err != nil {
return err
}
const whoProcess, classBestEffort, classShift, lowest = 1, 2, 13, 7
if _, _, e := syscall.Syscall(syscall.SYS_IOPRIO_SET, whoProcess, uintptr(tid), classBestEffort<<classShift|lowest); e != 0 {
return e
}
return nil
}