# fasthttp — High-Performance HTTP Server Library for Go > fasthttp is a high-performance HTTP implementation for Go that serves as an alternative to net/http, optimized for speed and low memory allocation in high-throughput scenarios. ## Install Save as a script file and run: # fasthttp — High-Performance HTTP Server Library for Go ## Quick Use ```bash go get github.com/valyala/fasthttp ``` ```go package main import "github.com/valyala/fasthttp" func main() { fasthttp.ListenAndServe(":8080", func(ctx *fasthttp.RequestCtx) { ctx.WriteString("Hello, World!") }) } ``` ## Introduction fasthttp is a Go HTTP library designed from scratch for high throughput and low memory allocation. Unlike the standard net/http package, it reuses request and response objects via object pooling, making it a strong choice for proxy servers, API gateways, and any service that handles tens of thousands of concurrent connections. ## What fasthttp Does - Provides a zero-allocation HTTP server and client for Go - Reuses request/response objects with sync.Pool to reduce GC pressure - Supports HTTP/1.1 pipelining and keep-alive connections by default - Offers a RequestCtx-based API for handling requests without per-request heap allocation - Includes built-in helpers for common tasks like file serving, routing, and compression ## Architecture Overview fasthttp avoids the net/http pattern of creating a new goroutine with fresh allocations per request. Instead, it maintains a worker pool and recycles RequestCtx objects. Buffers for headers, URI parsing, and body reading are pre-allocated and grown only when needed. This design trades API familiarity for raw throughput, making it measurably faster under sustained load while using less memory. ## Self-Hosting & Configuration - Install via `go get github.com/valyala/fasthttp` with Go 1.20+ - Configure concurrency limits with `Server.Concurrency` to cap worker goroutines - Set `Server.ReadTimeout` and `Server.WriteTimeout` for connection lifecycle control - Enable TLS by passing cert and key files to `ListenAndServeTLS` - Use `Server.MaxRequestBodySize` to guard against oversized payloads ## Key Features - Up to 10x lower memory allocation per request compared to net/http - Built-in connection pooling for the HTTP client - Streaming request and response body support - Hot path optimizations for header parsing and URI decoding - Compatible with popular routers like fasthttp/router and fiber ## Comparison with Similar Tools - **net/http** — standard library, broader ecosystem, but higher allocation per request - **Fiber** — web framework built on top of fasthttp with Express-like API - **Echo** — net/http-based framework with middleware, slightly lower raw throughput - **Gin** — net/http-based, strong middleware ecosystem, simpler migration from net/http ## FAQ **Q: Can I use fasthttp as a drop-in replacement for net/http?** A: No. The API differs significantly — fasthttp uses RequestCtx instead of http.Request/http.ResponseWriter. Migration requires rewriting handlers. **Q: Does fasthttp support HTTP/2?** A: Not natively. For HTTP/2 you need net/http or a reverse proxy like Nginx in front. **Q: When should I stick with net/http?** A: If your service is I/O-bound rather than CPU-bound, or if you rely heavily on net/http middleware, the standard library is the simpler choice. **Q: Is fasthttp production-ready?** A: Yes. It has been used in production at scale for years and is the engine behind the Fiber framework. ## Sources - https://github.com/valyala/fasthttp - https://pkg.go.dev/github.com/valyala/fasthttp --- Source: https://tokrepo.com/en/workflows/asset-8ef86317 Author: Script Depot