# SPIKE: Port health-bridge to Go

## Stack Choice Rationale

**Chose: Go 1.26 + chi + modernc.org/sqlite (pure Go)**

| Option | Pros | Cons |
|--------|------|------|
| Go + chi + modernc.org/sqlite | Pure Go, no CGO, fast iteration | modernc.org/sqlite slower than C-based sqlite |
| Go + chi + mattn/go-sqlite3 | Fast C-based sqlite | Requires CGO, harder to deploy |
| Rust + Axum | Best performance potential | Steeper learning curve, slower to prototype |
| Python (baseline) | Existing, battle-tested | Slower for high throughput |

Decision: Go was fastest to prototype (1 hour vs estimated 2+ hours for Rust) and provides acceptable baseline comparison.

## What Was Ported

### Endpoints
- `GET /health` - Health check
- `GET /` - Root endpoint
- `POST /ingest_signed` - HMAC-verified ingestion with:
  - SHA256 body hash
  - HMAC-SHA256 signature validation
  - Timestamp skew check (default 300s)
  - Nonce replay protection (stored in SQLite)
- `GET /summary` - Read daily summaries with Bearer token auth

### Infrastructure
- SQLite with WAL mode
- Schema initialization on startup
- Nonce table for replay protection

## What Was Intentionally Skipped

1. **Schema migrations** - Only creates tables if they don't exist
2. **Record normalization** - Minimal normalization, no dedup logic
3. **Daily summary rebuild** - No compute_daily_summary equivalent
4. **Multiple data endpoints** - Only /summary, not /data, /overview, /debug/day
5. **CORS middleware** - Not needed for local benchmark
6. **Async processing** - Synchronous writes only
7. **Complex HMAC payload** - Simplified from Python's record_key generation

## Benchmark Results

### Latency (50 requests each, single connection)
```
Endpoint          | Python  | Go      | Difference
------------------|---------|---------|------------
GET /health       | 7ms avg | 6ms avg | Go ~15% faster
POST /ingest      | 8ms avg | 7ms avg | Go ~12% faster
```

### High Volume (100 sequential requests)
```
Server  | Total Time | Per-Request (est)
--------|------------|-------------------
Python  | 1.57s      | ~16ms
Go      | 2.90s      | ~29ms
```

Note: High volume test is dominated by curl process startup overhead.

## Honest "Is This Worth It" Recommendation

**Recommendation: NOT WORTH IT for this specific use case**

### Why:
1. **Latency is nearly identical** - Both Python and Go achieve ~7ms average latency for these endpoints. The overhead is negligible for a mobile device sending health data every few minutes.

2. **Python has advantage for complex logic** - The Python server does significant record normalization, deduplication, and daily summary computation. Go would require ~3x the code to match this.

3. **modernc.org/sqlite is slow for writes** - The pure-Go sqlite driver is known to be ~2-3x slower for write-heavy workloads than C-based sqlite. For ingestion-heavy workloads, this would be a problem.

4. **Go's code complexity is similar to Python** - The Go port required ~400 lines vs Python's ~1000 lines, but Go is more verbose for JSON handling and type conversions.

5. **No clear deployment advantage** - Both can run as services; Go's binary is larger due to static linking but doesn't need Python runtime.

### When it WOULD be worth it:
- If ingestion latency needed to be <5ms at 99th percentile under heavy load
- If running in a memory-constrained environment (Go uses less RAM than Python + FastAPI)
- If porting to a platform where Go binaries are preferred (mobile backends, edge computing)
- If concurrent connections were in the hundreds (Go's goroutines are more efficient)

### Verdict:
For a health data ingestion API receiving data from a phone every few minutes, Python FastAPI is perfectly adequate. The performance delta (~1ms) is imperceptible to users. Spend the saved complexity budget on features instead.

## Files Created

- `/home/ubuntu/health-bridge-go/main.go` - Main server implementation
- `/home/ubuntu/health-bridge-go/go.mod` - Go module definition
- `/home/ubuntu/health-bridge-go/go.sum` - Dependency checksums
- `/home/ubuntu/health-bridge-go/benchmark.sh` - Benchmark script
- `/home/ubuntu/health-bridge-go/README.md` - Documentation
- `/home/ubuntu/health-bridge-go/SPIKE_NOTES.md` - This file
- `/home/ubuntu/health-bridge-go/health-bridge-go` - Compiled binary