Kern

Recipe - Performance and Limits

Throughput-oriented Kern configuration patterns for compression, limits, and endpoint-specific controls.

Performance and Limits

Balance performance and safety by applying limits where they matter.

Global performance baseline

app.Use(middleware.Gzip())
app.Use(middleware.Timeout(middleware.TimeoutConfig{Duration: 2 * time.Second}))
app.Use(middleware.RateLimiter())

Route-specific strictness

app.AddConstraints("GET", "/export", kern.Constraints{
    Validate: middleware.ResponseLimit(middleware.ResponseLimitConfig{
        MaxBytes:   1 << 20,
        StatusCode: 413,
        Message:    "response too large",
    }),
}, exportHandler)
app.AddConstraints("POST", "/ingest", kern.Constraints{
    Validate: middleware.RequestGuard(middleware.RequestGuardConfig{
        MaxBodyBytes:      4 << 20,
        AllowContentTypes: []string{"application/json"},
    }),
}, ingestHandler)

Operational checks

  • Monitor response-limit rejections by route
  • Track timeout counts and p95/p99 latency
  • Confirm compression efficacy for large JSON payloads