AWS Builder Center

Serverless Streaming: No More Loading Spinners

Learn how to use AWS Lambda Response Streaming to improve Time-to-First-Byte and build real-time, stream-first generative AI web applications.

The End of the Loading Spinner: Building Stream-First Serverless UIs with AWS Lambda

The Problem with Buffered Responses

For years, building APIs on AWS Lambda followed a strict, synchronous pattern: a client sends a request, the API Gateway triggers a Lambda function, the function processes the data, and the entire response is buffered in memory before being sent back to the client. While this works perfectly for retrieving a simple database record, it creates a terrible user experience for modern workloads.
If you are querying an external API, generating a complex report, or specifically interacting with a Generative AI model, the client might stare at a loading spinner for 5 to 30 seconds. By the time the buffered response finally arrives, the user may have already abandoned the page.

The Lambda Response Streaming Paradigm

AWS solved this by introducing native Response Streaming for AWS Lambda. Instead of waiting for the entire payload to finish processing, your function can write data chunks to a stream, which are sent immediately to the client.
This unlocks two massive benefits:
  1. Time-to-First-Byte (TTFB): The perceived performance of your application skyrockets because the client begins receiving and rendering data almost instantly.
  2. Bypassing the 6MB Limit: Synchronous Lambda payloads are strictly capped at 6MB. Response streaming bypasses this, allowing you to stream large files or CSV exports up to a soft limit of 20MB. The first 6MB is streamed without constraints, with bandwidth limits applying only to the overflow.

Implementing Stream-First AI Applications

This feature is the missing link for serverless Generative AI. When building applications on Amazon Bedrock or OpenAI, users expect the "typing" effect—seeing the LLM generate the response token by token.
To build this on AWS today without managing servers:
  • The Runtime: Use Node.js (14.x or newer), which supports native streaming via awslambda.streamifyResponse(), or use custom runtimes.
  • The Integration: You can invoke the streaming Lambda directly via Lambda Function URLs. If you need an API layer, you can use API Gateway with WebSockets or AWS AppSync (GraphQL) subscriptions to push generated text back to the client.
  • The Adapter: If you are migrating an existing Express, Next.js, or Flask app, you can use the open-source AWS Lambda Web Adapter to wrap your HTTP 1.1 chunked responses into the Lambda streaming API automatically.
By adopting a stream-first architecture, you eliminate the loading spinner and create the real-time, responsive experiences that modern AI applications demand.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article