Skip to content

pdm: evaluate API Gateway REST response streaming for record fetches #138

Backlog

Description

@prdai

Problem

The PDM /records endpoint pages records with a fixed item limit, and the executor fetches pages sequentially. At 150k records that is 300 round trips of 500 records each, and the data-retrieval phase measures 34-38 s on rads and 37-42 s on secure-rads. DynamoDB latency per page is small; the sequential API Gateway + Lambda round trips dominate. #137 increases the page size, but the 6 MB Lambda synchronous response limit bounds how far that goes.

Proposal

Evaluate moving pdm-service from API Gateway HTTP API to a REST API with Lambda proxy response streaming (responseTransferMode: STREAM, InvokeWithResponseStream). The handler would stream the requested records in one response instead of paginating. Compression can be done inside the stream (gzip) since content encoding is not applied by API Gateway in stream mode.

Relevant limits:

  • Response streaming is only supported for REST APIs.
  • Lambda streamed responses can reach 200 MB, compared with 6 MB buffered.
  • The first 10 MB of a streamed response is unrestricted; data beyond 10 MB is capped at 2 MB/s.
  • Streams can run for up to 15 minutes, with a 5-minute idle timeout for regional endpoints.
  • Stream mode disables endpoint caching, VTL response transformation, and API Gateway content encoding.

Constraints

  • Migrating the stack from HTTP API to REST API changes the API Gateway resources, custom domain mapping, authorizer wiring, and IAM.
  • The secure handler must verify the access token before streaming, and the stream must not begin before authorization succeeds.
  • The enclave and rads host tools need to read a chunked response incrementally and handle mid-stream failures and retries.
  • The 2 MB/s cap beyond 10 MB means compression matters. Uncompressed ehealth is ~80 MB at 150k records; gzip should bring most of it under or near the unrestricted range.
  • Worth measuring: stream startup time, throughput with gzip, and whether the migration is cheaper than the alternatives.

Alternatives

  • Parallel fetch by RecordId range (zero-padded ids allow range queries), which avoids the REST API migration.
  • One immutable S3 object per algorithm, authorized by PDM, which removes pagination and the 6 MB limit entirely.
  • The page-size change in perf(pdm): batch record queries into larger pages #137, which is already a smaller version of the same idea.

Ask

Decide whether a REST response-streaming prototype is worth the migration cost, or whether parallel range fetching should come first. If accepted, verify against the 150k ehealth dataset and report the data-retrieval phase alongside the current numbers.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions