Introduction
Response streaming is a new invocation method that enables your functions to progressively stream response payloads back to the clients. Instead of waiting for the entire data to be ready, your applications can now receive chunks of data as soon as they become available. This means faster loading times and a more responsive user experience.
Response Streaming vs. Non-Streaming (Buffering)
To quickly get an idea of how it works, we need to see it in action.
Below, we show two functions that have the simple task of displaying a message in HTML. The non-streaming function introduces a delay of 3 seconds to simulate a process. This will highlight how each approach affects the user experience.
Without Response Streaming: The Waiting Game and the Role of TTFBÂ
In GIF above, we simulate a non-streaming operation. The function waits for three seconds to mimic a process before it returns the message. The result? The browser waits too, displaying nothing until the entire process is completed. We see no output until everything is ready. This is where the concept of TTFB, or Time To First Byte, comes into play. TTFB is the time it takes from when the client makes a request until it receives the first byte of data from the server. In this non-streaming scenario, the TTFB includes the entire three-second processing time. Only after this does the client receive its first byte of data, which can make the application seem unresponsive or slow.
With Response Streaming
In contrast, response streaming can reduce the perceived TTFB by sending chunks of data as soon as they are available, which leads to an immediate and more interactive response. By allowing users to see data earlier in the process, we can enhance the user’s perception of speed and responsiveness in our applications. Let’s see how this works in the following GIF.Â
Putting it into Practice
Let’s see this feature firsthand.
Steps
- Save this YAML file to a location on your computer.Â
- Create a new stack in AWS CloudFormation using the YAML that you’ve saved.
- Wait for your stack to be created. This may take a minute. You can monitor the progress on the CloudFormation dashboard.
- Once the stack creation process has been completed, go to the Outputs section of your stack details in the CloudFormation dashboard. Here, you will find the function URL of the Lambda function.
Verification
Open the function URL in your web browser to see the response streaming in action.
Cleaning up
Once you’re done testing, don’t forget to delete the stack to avoid incurring unnecessary charges and remove any potentially exposed endpoints.