HTTP and gRPC server contracts
The HTTP and gRPC transports give clients protocol entry points for native Helix queries and for Cypher and SQL statements. They validate transport input, apply request options, delegate execution to shared server state, and translate service outcomes into protocol-specific success or error forms. See Server and transports for the wider server lifecycle and Server configuration and state for bootstrap and ownership.
Transport role
The transport layer is the boundary between a network client and the database query service. It owns wire parsing, size limits, health and diagnostics replies, option checks, response encoding, and error mapping. It does not define a second query language or execute a query independently.
Request lifecycles
Native requests carry the Helix query JSON directly. Cypher and SQL requests enter through language-specific endpoints, are compiled to the shared query form, and then follow the same execution and durability rules. Both transports can also plan a statement without executing it through their explain operations.
This diagram shows how a request moves from a protocol boundary into shared state and back to the client.
flowchart LR
Client[Client request] --> Boundary[HTTP or gRPC boundary]
Boundary --> Decode[Decode native, Cypher, or SQL input]
Decode --> Validate[Validate body and options]
Validate --> Plan[Compile or plan request]
Plan --> Service[HelixQueryService]
Service --> State[ServerState and database]
State --> Durable{Durability requested?}
Durable -- Yes --> Flush[Flush writer]
Durable -- No --> Result[Build public result]
Flush --> Result
Result --> Client
HTTP query lifecycle
The HTTP router exposes native query, Cypher, SQL, explain, health, readiness, and diagnostics routes. Native POST /v2/query accepts serialized QueryRequest JSON. The language routes accept their statement request, compile it, and use the same shared service after option validation. Successful query responses use JSON with content type application/json; a requested durable write is acknowledged only after the server flushes its writer.
HTTP request options use these headers:
| Header | Purpose | Valid request kind or effect |
|---|---|---|
x-helix-warm | Run a read through the warm path | Read only |
x-helix-require-writer | Reject routing to a read-only server | Any request; returns unavailable on a reader |
x-helix-await-durable | Wait for writer flush before success | Write only; an explicit false skips the wait |
x-helix-tenant-id | Attach a tenant identity to query telemetry | Does not select a storage namespace |
User-Agent | Attach a client identity to query telemetry | Does not select a storage namespace |
The HTTP server also exposes GET /healthz, GET /readyz, and GET /v2/diagnostics. The health routes use shared state and return database mode, index runtime status, and the count of index entities held back by the worker. The diagnostics route reports read-only server setup information.
References: crates/…/http.rs• L40-84 crates/…/http.rs• L86-130 crates/…/http.rs• L142-269 crates/…/http.rs• L337-395
gRPC query lifecycle
The generated HelixDbServer service accepts QueryJsonRequest for native, Cypher, and SQL operations. Native ExecuteQuery parses the query body directly. The language methods compile their request and then share the same option checks and query service. Explain methods plan without executing, so they use read rules even when the statement would change data.
The request fields mirror the HTTP options. gRPC metadata can carry x-helix-tenant-id and user-agent for telemetry identity, but these values do not change the database storage scope. An unset await_durable waits for durability; only an explicit false skips that wait. The Health method returns the same readiness facts as the HTTP health endpoints. A successful operation returns its JSON bytes in QueryJsonResponse.body.
References: crates/…/grpc.rs• L52-120 crates/…/grpc.rs• L124-255
Generated protocol boundary
The .proto contract is the stable gRPC boundary. Code generation turns the service and message definitions into the client and server types used by the transport, while the query body remains an explicit byte field so native, Cypher, and SQL methods can share JSON request bodies.
gRPC message contract
QueryJsonRequest carries the query body and three execution controls: warm_only, require_writer, and await_durable. QueryJsonResponse carries the encoded public result. HealthResponse carries readiness, database mode, index runtime state, and the count of index entities held back by the worker; the old text-index field remains reserved and must not be reused.
The build step generates the gRPC types from the protocol file and preserves the query body fields as byte arrays. Changes to these fields affect clients and servers together, so the protocol file is the source of the wire contract rather than the generated output.
References: crates/…/helixdb.proto• L1-29 crates/…/build.rs• L3-14 crates/…/grpc.rs• L15-20 crates/…/grpc.rs• L52-57
Response and error contracts
The query service converts native execution results into a public JSON object before either transport writes a response. Cypher and SQL operations return their language API result or plan shape. Each transport preserves the public content and adds the protocol features needed to report failures, including whether a client may retry a failure.
Query response shape
Successful native queries serialize named returns as a top-level JSON object. Simple graph elements expose public identity objects such as {"$id": 7}. Ranked search results can include $distance or $score, while rows with visible bindings, paths, or sacks retain an annotated row shape. Empty declared returns keep their public shape rather than becoming an absent field.
Cypher and SQL execution return the result shape defined by their language API. Explain operations return a plan description instead of running the statement. This keeps execution output and planning output distinct even though both use the same transport response container.
Planner diagnostics stay separate from the public native result JSON. This keeps query results stable and lets higher layers expose safe diagnostics without placing internal plan data or query literals in the result body.
References: crates/…/http.rs• L188-209 crates/…/http.rs• L241-291 crates/…/grpc.rs• L93-120 crates/…/transport_contracts.rs• L225-295
Error translation
The query service assigns each public failure a stable machine-readable QueryErrorCode and keeps a human-readable message. HTTP places both values in an {"error": ..., "msg": ...} JSON envelope and adds retryable only when the failure class has an explicit retry policy. gRPC places the stable code in helix-error-code metadata and keeps the human message in the status message. Language query errors also carry language-specific detail in their native protocol form.
The protocol status differs where each transport has a native convention, but the public cause stays aligned:
| Failure class | HTTP result | gRPC result |
|---|---|---|
| Invalid JSON, request, plan, or client vector input | 400 Bad Request | InvalidArgument |
| Retryable transaction conflict | 409 Conflict with retryable: true | Aborted |
| Definitive state conflict | 409 Conflict with retryable: false | FailedPrecondition |
| Retryable index backpressure | 429 Too Many Requests with retryable: true | ResourceExhausted |
| Writer-fenced commit with unknown outcome | 503 Service Unavailable with retryable: false | Unavailable |
| Write required by the service on a reader | 503 Service Unavailable | FailedPrecondition |
| Other database or response serialization failure | 500 Internal Server Error | Internal |
Malformed or oversized input fails before query execution. Both public transports accept at most 16 MiB of query JSON. gRPC also permits a small envelope allowance at its message boundary, while the query body itself keeps the same limit. A malformed body carries the invalid-query code, while an oversized body carries the invalid-request-body code.
References: crates/…/http.rs• L271-335 crates/…/grpc.rs• L257-352 crates/…/transport_contracts.rs• L297-405 crates/…/transport_contracts.rs• L718-961
Health and readiness
Health reporting lets an operator separate process liveness from database readiness. HTTP healthz always returns an OK response with the current state, while readyz returns OK only when the index runtime reports ready and otherwise returns service unavailable. gRPC Health returns the state fields directly in its response.
The health payload contains four facts:
ready: whether the index runtime is ready.mode: whether the database handle is a writer or reader.index_runtime: the stable readiness code.blocked_index_entity_count: the number of index entities the worker currently holds back.
Health output does not expose tenant or entity details. It reports only the count needed for service health without making the unauthenticated health routes a data access path.
References: crates/…/http.rs• L56-79 crates/…/grpc.rs• L223-233 crates/…/transport_contracts.rs• L1229-1285 crates/…/transport_contracts.rs• L1464-1621
Shared service delegation
Both handlers use ServerState as their shared entry point. The state provides one query service and one database handle, so HTTP and gRPC use the same execution rules, database mode, durability behavior, and response conversion below the wire layer.
The query service is the contract between transports and the planner and interpreter stack. It separates transport concerns from query execution, supports execute, warm, and explain modes, records optional telemetry, and can run with a storage scope when a caller needs tenant-scoped execution. The server transports pass tenant and client identity for telemetry; they do not use those metadata values as storage selectors.
When a request asks for durability, the transport passes the durability choice into the shared execution mode. The service completes the writer wait before it reports success, so the client receives an acknowledgement only after the requested durability condition is met.
References: crates/…/state.rs• L7-53 crates/…/query_service.rs• L27-135 crates/…/http.rs• L119-129 crates/…/http.rs• L241-269 crates/…/grpc.rs• L202-220
Transport test model
The transport tests check that protocol adapters preserve one shared behavior model rather than allowing HTTP and gRPC to drift. A common query corpus runs through the embedded database, the query service, the HTTP router, and a real loopback gRPC server, then compares the observed results.
The test model covers these contract groups:
- Shared query results for reads, writes, empty returns, nested values, paths, and ranked public metadata.
- Native, Cypher, and SQL routing, including explain behavior.
- Protocol-specific rejection of malformed and oversized requests.
- Request option validation and reader/writer routing.
- Health and readiness reporting, including held-back index entity counts.
- Stable classification of vector input, index errors, backpressure, conflicts, deadlines, and commit-outcome failures.
- gRPC deadlines, connection churn, restart, and clean shutdown.
- Transport shutdown ordering before database close.
- Scoped service execution matching direct scoped database execution.
The tests therefore validate semantic parity and wire-specific behavior. They also check that stable error codes and retry hints survive translation, definitive write conflicts are not retried, and internal response fields such as diagnostics do not leak into public query JSON.
References: crates/…/transport_contracts.rs• L44-213 crates/…/transport_contracts.rs• L225-405 crates/…/transport_contracts.rs• L540-717 crates/…/transport_contracts.rs• L718-1084 crates/…/transport_contracts.rs• L1086-1415 crates/…/transport_contracts.rs• L1464-1761
Change checklist
When these files or sections are changed, remember to consider these:
- Keep proto/helixdb.proto, generated gRPC types, and both request adapters aligned when a query option, RPC method, or health field changes.
- Preserve the shared 16 MiB query-body limit when changing HTTP body reads or gRPC message limits.
- Update native and language-query error mappers when a
QueryServiceError, language error class, or stableQueryErrorCodechanges. Keep the HTTP envelope and gRPC metadata contract compatible with existing clients. - Keep explain operations on read rules and keep durability acknowledgement after successful shared execution and the requested writer wait.
- Keep
x-helix-tenant-idandUser-Agenttelemetry identity separate from database storage scope. - Extend the shared transport corpus when a result shape or language route changes, then retain protocol-specific checks for status codes, retry hints, and error encoding.
- Keep health field names limited to aggregate readiness facts. Do not expose tenant or entity details through unauthenticated health routes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Query body exceeds 16 MiB | HTTP and gRPC reject it before execution with the invalid-request-body contract | Keep the body limit and the gRPC envelope allowance aligned with the public limit |
warm is used for a write | The request is rejected as an invalid option | Validate the request kind after decoding or compiling the body |
| A write reaches a reader handle | The database cannot provide writer authority | Preserve writer-mode checks in the shared service and transport option checks |
| Retryable backpressure loses its retry hint | Clients may fail to retry a temporary capacity failure | Keep HTTP retryable: true and gRPC ResourceExhausted tied to the same failure class |
| A fenced commit is treated as retryable | A client can duplicate a write whose outcome is unknown | Preserve the terminal error code and retryable: false contract |
| A definitive unique-value conflict is treated as retryable | The same write fails again and may cause unwanted client retries | Keep its HTTP 409 false retry hint and gRPC FailedPrecondition status |
| Error mapping omits the stable code | Clients lose machine-readable failure handling | Keep HTTP error and gRPC helix-error-code sourced from the public error code |
| Diagnostics are added to native result serialization | Internal plan facts can become part of the public result contract | Keep diagnostics separate from top-level result JSON |
| A transport closes without joining its peer | Requests can outlive the database close | Use the server supervisor to stop and join both transports before database close |
| Health reports per-entity details | An unauthenticated endpoint can leak data | Expose only aggregate readiness facts and the held-back entity count |
Key files
| File | Why to read it |
|---|---|
| crates/…/http.rs | Defines the HTTP routes, native and language query lifecycles, headers, health replies, diagnostics, and JSON error envelope. |
| crates/…/grpc.rs | Defines the gRPC service methods, generated type boundary, option checks, telemetry metadata, and status metadata contract. |
| crates/…/helixdb.proto | Defines the public gRPC service and message wire shape. |
| crates/…/build.rs | Shows how the protocol file becomes the generated gRPC boundary. |
| crates/…/state.rs | Shows the shared state and the query and durability boundaries used by both transports. |
| crates/…/query_service.rs | Defines the common query execution, response, diagnostics, and stable error model below the transports. |
| crates/…/transport_contracts.rs | Shows cross-transport result parity, language routing, failure classification, health contracts, and protocol contract tests. |
| crates/…/tests.rs | Shows transport supervision and database-close ordering tests. |