projects / polyhook

Replay Systems 2026

polyhook

One OpenAPI 3.1 webhook API, built twice: once in Go, once in Node, behind a single SvelteKit and Svelte 5 frontend.

polyhook architecture: one SvelteKit frontend over a Go and a Node backend
  • 10 rps Per-IP ingest limit, burst 30 Rate limiter config

Overview

Problem

Comparing Go and Node usually means comparing two different programs.

Approach

The same contract implemented twice, with SSE fan-out, cursor pagination, ETags, replay with backoff and a 24 hour sweeper in both. Only the base URL changes between backends.

Result

Same behaviour in two idioms, with the code for each concern side by side: a goroutine worker pool against a promise drain loop, sync.RWMutex against a callback set.

One frontend, two backends, one OpenAPI 3.1 contract: the comparison itself is the deliverable.

On the design

Decisions · why this stack

  1. Contract first

    One OpenAPI 3.1 spec, committed identically to both repos, so the same UI and EventSource code work against either runtime unmodified.

  2. Non-blocking SSE fan-out

    Each bin keeps its own subscriber set and publishes without blocking, so a slow client never stalls ingest.

  3. Cursor pagination with ETags

    A composite (received_at, id) cursor stays stable under concurrent writes, and If-None-Match makes polling cheap.

  4. Replay with backoff and dead-letter

    Failed deliveries retry after 1, 2, 4, 8 and 16 seconds, then land in a dead-letter queue.

  5. Only measured numbers

    Only line counts are on the page. Image size, latency and memory stay off it until a k6 run and a clean Docker build measure them.

Stack

  • Go
  • chi
  • modernc.org/sqlite
  • Node.js 22
  • Fastify
  • node:sqlite
  • TypeScript
  • OpenAPI 3.1
  • SvelteKit 2
  • Svelte 5
  • Docker
  • Railway
  • Vercel

Demo

Contract walkthrough

chi · log/slog · modernc.org/sqlite

  1. POST /v1/bins bin created with an ingest URL and a 24 h expiry
  2. GET /v1/bins/:id/stream SSE feed open, resumable with Last-Event-ID per-bin subscriber map behind sync.RWMutex
  3. POST /v1/bins/:id/in request captured, raw body preserved
  4. event: request pushed to every subscriber without blocking ingest
  5. GET /v1/bins/:id/requests cursor page on (received_at, id) with an ETag
  6. GET /v1/bins/:id/requests If-None-Match not modified, nothing resent
  7. POST /v1/bins/:id/requests/:rid/replay replay queued to the target URL 4-goroutine worker pool on a bounded channel
  8. GET /v1/replays/:id retrying after 1, 2, 4, 8 and 16 s, then dead-letter
  9. sweeper bins older than 24 h removed sweeper goroutine

Scripted from the shared OpenAPI 3.1 contract. The Railway backends are offline, so this is a walkthrough of the API, not live traffic.

Side-by-side: same problem, two languages

Per-bin subscriber set. Publish is non-blocking, so a slow client gets dropped events but cannot stall ingest. The single most important design choice in the project.

Go main.go
type broadcaster struct {
	mu   sync.RWMutex
	subs map[string]map[chan sseEvent]struct{}
}

func (b *broadcaster) subscribe(binID string) (<-chan sseEvent, func()) {
	ch := make(chan sseEvent, 16)
	b.mu.Lock()
	if _, ok := b.subs[binID]; !ok {
		b.subs[binID] = make(map[chan sseEvent]struct{})
	}
	b.subs[binID][ch] = struct{}{}
	b.mu.Unlock()

	cleanup := func() {
		b.mu.Lock()
		delete(b.subs[binID], ch)
		if len(b.subs[binID]) == 0 {
			delete(b.subs, binID)
		}
		b.mu.Unlock()
		close(ch)
	}
	return ch, cleanup
}

func (b *broadcaster) publish(binID string, ev sseEvent) {
	b.mu.RLock()
	defer b.mu.RUnlock()
	for ch := range b.subs[binID] {
		// Non-blocking send: slow subscribers must not stall ingest.
		select {
		case ch <- ev:
		default:
		}
	}
}
TypeScript src/server.ts
class Broadcaster {
	private subs = new Map<string, Set<(e: SSEEvent) => void>>();

	subscribe(binId: string, cb: (e: SSEEvent) => void): () => void {
		let set = this.subs.get(binId);
		if (!set) {
			set = new Set();
			this.subs.set(binId, set);
		}
		set.add(cb);
		return () => {
			const s = this.subs.get(binId);
			if (!s) return;
			s.delete(cb);
			if (s.size === 0) this.subs.delete(binId);
		};
	}

	publish(binId: string, ev: SSEEvent): void {
		const set = this.subs.get(binId);
		if (!set) return;
		for (const cb of set) {
			try {
				cb(ev);
			} catch {
				// One bad subscriber must not stall ingest.
			}
		}
	}
}

Takeaway. Same shape, different idioms. Go uses a buffered channel + non-blocking select; Node uses a callback Set + try/catch. Both deliver bounded latency in the face of slow consumers without backpressure on the producer.

Numbers

Measured · one linear scale per row

Implementation size

Go 944 lines
Node 691 lines
polyhook-bench.json · lines_of_code