FastCGI: understand long-running processes and web-server connections
FastCGI is a record-based protocol for exchanging requests and responses between a web server and an application process. Unlike the traditional Unix CGI pattern, where a program is started for a request and then exits, FastCGI is designed for long-lived processes that can handle multiple requests. Using FastCGI alone does not guarantee concurrency or better performance; check the server, application, and process-manager configuration together.
FastCGI is a record-based protocol for exchanging requests and responses between a web server and an application process. Unlike the traditional Unix CGI pattern, where a program is started for a request and then exits, FastCGI is designed for long-lived processes that can handle multiple requests. Using FastCGI alone does not guarantee concurrency or better performance; check the server, application, and process-manager configuration together.
How does FastCGI differ from CGI?
CGI defines an interface for a server to pass request information to a script and receive its output. FastCGI adds a separate communication protocol between the web server and application. A FastCGI application runs as a long-lived process; the server sends request data over a connection and receives response data in return. This design avoids having to repeat process startup and shutdown for every request.
That is an architectural difference, not a promise that FastCGI is always faster in a given environment. Connection type, process count, request capacity, and server/application implementations all affect the outcome. The FastCGI specification describes the interface and record protocol, not the web server's entire process-management feature set.
What is the request flow?
A FastCGI application accepts connections through a listening socket prepared by the web server. Protocol data is exchanged in records. A record contains fields such as version, type, request ID, content length, and padding length; its type determines whether it carries parameters or a data stream.
A Responder request roughly follows this sequence:
The server announces a request and its role with `FCGI_BEGIN_REQUEST`.
It sends CGI-style environment information in the `FCGI_PARAMS` stream and, when present, the request body in `FCGI_STDIN`.
The application sends its result in `FCGI_STDOUT` and may send diagnostic errors separately in `FCGI_STDERR`.
The application signals completion and status with `FCGI_END_REQUEST`.
The protocol can use multiple request IDs on one transport connection, allowing multiplexing at the protocol level. The server and application must both support and configure concurrent request handling. If the server multiplexes requests to an application that only handles one request at a time, the protocol allows the application to reject a request.
What do the roles mean?
The FastCGI specification defines application roles:
| Role | Input and result |
|---|---|
| Responder | Receives HTTP request information and generates an HTTP response, similar in purpose to a CGI program. |
| Authorizer | Returns an allow/deny decision based on request information and can pass values to the server for later processing when access is allowed. |
| Filter | Receives request information plus a server file data stream and generates a transformed response. |
Check the actual server and application documentation for supported roles, stream ordering, and role-specific inputs.
What should you check when deploying or troubleshooting?
**Process management:** Determine who starts and restarts the application and sets worker counts and shutdown policy. The FastCGI protocol does not define all of this management.
**Connection boundary:** Identify the socket or transport used and who can connect to it. Set exposure limits and access controls in the deployment environment.
**Records and request IDs:** Verify supported FastCGI versions and roles, and check record boundaries, lengths, and per-request-ID state handling.
**Concurrency:** Confirm that connection multiplexing matches the application's ability to process concurrent work. Check shared state and database connection pools under load.
**Input, output, and completion:** Verify when parameters and request bodies are fully read, that stdout and stderr are separated, and that `FCGI_END_REQUEST` status is handled. Review server logs alongside the application's error stream.
**Measurement:** Compare latency, throughput, CPU and memory use, queue time, and process restarts under representative load. Do not infer performance from the protocol name alone.
FastCGI provides a way for long-running applications and web servers to communicate, but it does not automatically choose process management, concurrency, security boundaries, or performance targets. Before a migration, verify the records, roles, and transport settings supported by the server module and runtime, then measure load and failure behavior.
Sources
[FastCGI Specification, Version 1.0 (Open Market, 1996)](https://fastcgi-archives.github.io/FastCGI_Specification.html)
[RFC 3875: The Common Gateway Interface (CGI) Version 1.1](https://www.rfc-editor.org/rfc/rfc3875.html)