I see an increase in my AWS Lambda function's IteratorAge metric.
Short description
A Lambda function's iterator age increases when the function can't efficiently process the data that's written to the streams that invoke the function. To decrease your function's IteratorAge performance metric, you must increase your stream processing throughput.
The following factors influence a function's IteratorAge metric:
Reconfigure your function or data stream to decrease your function's iterator age based on your configuration.
Note: For Amazon DynamoDB streams, see Why is the Lambda IteratorAge metric increasing for my Amazon DynamoDB stream?
Resolution
Decrease your function's runtime duration
A high runtime duration increases a function's iterator age. A decrease in the runtime duration increases a function's throughput, which decreases a function's iterator age.
To decrease your function's runtime duration, do one or both of the following:
1. Increase the amount of memory allocated to the function. Lambda allocates CPU power in proportion to the memory.
2. Optimize your function code to process records faster.
Note: To get more information on your Lambda duration and performance, see Visualize Lambda function invocations using AWS X-Ray.
Increase your stream's batch size
If your function's runtime duration is independent from the number of event records, then an increase to the batch size decreases the iterator age.
To increase your function's batch size, follow the instructions in Using Lambda to process records from Amazon Kinesis Data Streams.
Note: If your function's duration is dependent on the number of event records, then an increase to your function's batch size doesn't decrease the iterator age. For more information, see Working with streams.
Make sure that your function gracefully handles invocation errors
Invocation errors can cause a Lambda function to take longer to process an event, or process the same event multiple times. Because a function reads event records sequentially, it can't progress to later records if a record batch causes an error each time it's retried. In this situation, the iterator age increases linearly as those records age.
If your function returns an error, Lambda continuously retries the batch. The batch retries continue until your function processes the batch successfully, reaches the maximum retry attempts, or the data expires from the stream.
Make sure that your function gracefully handles records written to the stream. As you develop your function, logging and instrumenting your code can help you diagnose errors.
Manage your throttle occurrence
Because a function reads event records sequentially, it can't progress to the next record if Lambda throttles the current invocation. In this situation, Iterator age increases when Lambda retries the throttled invokes.
To manage throttling for your Lambda function, request a concurrency increase.
Evenly distribute records in the stream
Partition keys in each data record determine which shards in the stream receive the records. An increase in traffic to your stream with records that contain the same partition key causes a shard to receive a disproportionate number of records. This increase results in a hot shard.
Kinesis Data Streams enhanced shard-level metrics allow you to verify the ingestion rate into each shard and troubleshoot a hot shard.
For more information, see Under the hood: scaling your Kinesis data streams.
Increase your stream's shard count
A low number of shards in a stream increases a function's iterator age. An increase to the number of shards in a stream also increases the number of concurrent Lambda functions that consume from your stream. This increase in shards then decreases a function's iterator age.
For more information, see How do I change the number of open shards in Kinesis Data Streams?
Note: Shard splitting doesn't have an immediate effect on a function's iterator age. Existing records remain in the shards where the stream originally wrote them. Those shards must catch up on their backlog before the iterator age for the shards decreases. For more information, see Working with streams.
Increase your stream processing throughput
Test parallelization factor settings
You can improve stream processing by configuring your function's parallelization factor to increase the number of concurrent Lambda invocations for each shard of a stream. Specify the number of concurrent batches that Lambda polls from each shard.
If your parallelization factor is set to 10, then you can have up to 50 concurrent Lambda invocations to process 5 data shards.
For example, concurrent runs = number of shards x concurrent batches per shard.
For more information, see Using AWS Lambda with Amazon Kinesis and New AWS Lambda scaling controls for Kinesis and Amazon DynamoDB event sources.
Use enhanced fan-out
Create a data stream consumer with enhanced fan-out to reduce latency and increase read throughput. Stream consumers get a dedicated connection to each shard that doesn't impact other applications that read from the stream.
For more information, see Using Lambda to process records from Amazon Kinesis Data Streams.
Related information
Use resharding, scaling, and parallel processing to change the number of shards