Part 5 of 5 · Building Kommercly

The First Go Service Real People Hit: Kommercly's Analytics Endpoint

I am learning Go. The usual way to learn a language is to rewrite something in it. I am not going to do that to Kommercly.

Kommercly works. A rewrite would cost months, add risk, and give customers nothing they can see. So the Next.js app stays. I am moving one thing out of it: the tracking endpoint.

The service went live on 12 November 2026. This post has the design, the real numbers after about two and a half days, and what I learned writing it.

Why this endpoint

/api/track receives every page view and every add-to-cart from every shop. It is the busiest route in the app, and it is simple. It takes a small event, checks a few rules and stores it.

That is the ideal first Go service to put in front of real people. It is hot, so the result is measurable. It is small, so I can finish it. And it has one dependency, the database, so there is not much to get tangled in.

The Next.js app on the same server uses about 423 MB of RAM right now (439 MB counting everything systemd tracks, with a peak of 505 MB). The Go service uses about 11 MB, with a peak of 15 MB, and ships as a single binary of 11 MB. The two are not doing exactly the same job, but the gap is large.

The rules stay the same

I did not want to change behaviour, only the engine. So the Go version keeps every rule from before:

  • A bot filter.
  • A rate limit of 120 requests a minute per IP.
  • The IP comes from the CF-Connecting-IP header.
  • It checks which stores count.

The design

It uses net/http for the server and pgx for Postgres. The interesting part is how events are written.

The handler does not write to the database. It validates the event and puts it on a buffered channel, then returns right away. One writer goroutine reads from that channel and flushes to Postgres every second, or as soon as 500 events are waiting, whichever comes first. It uses a bulk COPY, so a batch is one round trip instead of 500 inserts.

Three rules make this safe:

  • It never blocks the request. The handler must return fast.
  • If the buffer is full, it drops the event and counts it. Losing one page view is better than slowing a shop. The counter tells me if it ever happens.
  • On shutdown it flushes. On SIGTERM, the service stops taking requests, drains the channel and writes what is left, so a deploy does not lose events.

Here is the batching loop, simplified:

func writer(ctx context.Context, events <-chan Event, db *pgxpool.Pool) {
	batch := make([]Event, 0, 500)
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()

	flush := func() {
		if len(batch) == 0 {
			return
		}
		if err := copyEvents(context.Background(), db, batch); err != nil {
			log.Printf("flush failed: %v", err)
		}
		batch = batch[:0]
	}

	for {
		select {
		case e := <-events:
			batch = append(batch, e)
			if len(batch) >= 500 {
				flush()
			}
		case <-ticker.C:
			flush()
		case <-ctx.Done():
			// drain what is already buffered, then write it
			for {
				select {
				case e := <-events:
					batch = append(batch, e)
				default:
					flush()
					return
				}
			}
		}
	}
}

And the handler side, which is the part that must never wait:

select {
case events <- ev:
default:
	dropped.Add(1) // buffer full: count it, do not block
}

That select with a default is the whole non-blocking trick.

Rolling out safely

I did not switch it on and hope. The plan was:

  1. Run the Go service next to the Node app.
  2. Send the same requests to both and compare the responses side by side. If they differ, fix the Go version.
  3. Route /api/track to Go with a single Caddy rule.

Rolling back is removing that one rule and reloading Caddy, about 20 seconds. That is the main reason I picked this path first: the exit is cheap. I have not needed it.

The real numbers

These are from the production server, read on 14 November 2026 at 21:00, about 64 hours after go-live.

  • Uptime. Up since 12 November, 04:48, with 0 restarts.
  • Memory. The Go service holds 11 MB resident, peak 15 MB. The Next.js process holds 423 MB resident, and it has peaked at 505 MB. That is roughly 37 times more.
  • Events. The tracker counted 60 events enqueued, 60 written, 0 dropped, 0 failed, 0 waiting.
  • Per day. About 30 on 12 November, 24 on 13 November, and 4 so far on 14 November.
  • Batches. I do not log batch sizes, only counters. With 60 events over 64 hours, almost every flush was a batch of one or two. The 500-event path has never been tested by real traffic.
  • Latency. I fired 100 requests at each service from the server itself. They carried an empty payload, so both rejected them with a 400 and nothing was stored. The Go service answered in 1.4 ms at p50 and 6 ms at p95. Next.js took 10.5 ms and 21 ms. This only measures the reject path, not a real insert, and it skips the network. The Go insert happens after the response anyway, so the visitor never waits for the database.
  • One warning in the logs. Four times, forwarding an event to PostHog timed out. The shop events were still saved.

The honest reading is that traffic is small. Kommercly does not have many visitors yet, and the tracker has barely been tested. The volume was higher a few weeks ago: 917 rows on 5 November and 703 on 6 November, before the move. So I cannot tell you what this does at 10,000 events a minute. I can tell you it costs almost nothing while idle and has not lost anything.

What I found interesting as a newcomer

  • Goroutines are cheap. Coming from JavaScript, where concurrency is an event loop and a promise chain, running real concurrent code with go writer(...) is a surprise.
  • Channels instead of locks. I expected to need a mutex around a shared slice. Instead the handler sends and one goroutine owns the slice. Nothing is shared, so nothing needs locking.
  • Errors are values. There is no try and catch. A function returns an error and you deal with it right there. It is wordy, and it also means I can see every place something can fail.
  • One static binary. I copy a single file to the server. No node_modules, no runtime to install.
  • Context for shutdown. Passing a context down and cancelling it on SIGTERM is a clean way to say “stop, but finish what you are doing.”

What I do not know yet

I have not seen this under real load. I do not know if 120 per minute per IP is right at scale, or what my drop counter will say on a busy day. I also know a rate limiter and a batching loop are the easy, textbook parts of Go, and the harder lessons will come when it runs for weeks.

If traffic grows, I will write a follow-up with the numbers from a busy day.


Kommercly is a phone-first store builder for Bangladeshi f-commerce sellers. Free plan, 0% commission, orders to your WhatsApp. kommercly.com

Written by . Software engineer from Bangladesh. I ship products end to end, from the browser to the board: code generation, firmware and PCB tooling at IDEEZA, and my own products ProcureIndex, Akhra and Kommercly. Also farms, cycles and fights MMA.

Published

Get new logs by email. No spam, unsubscribe anytime.