[{"data":1,"prerenderedAt":91},["ShallowReactive",2],{"content-guides-lambda-kafka-scaling-throughput":3},{"markdown":4,"frontMatterAttributes":5,"bodyRaw":8,"contributorPaths":9,"menuType":11,"contentName":12,"slug":13,"readingTime":14,"menu":15,"nextDocItem":67,"previousDocItem":54},"\u003Cp>Kafka uses partitions to increase throughput and spread the load of records to all brokers in a cluster. The Lambda ESM can scale up automatically to handle additional load.\u003C\u002Fp>\n\u003Ch3>Kafka event source mapping auto scaling\u003C\u002Fh3>\n\u003Cp>The Event Source Mappings for Kafka have two modes that have different performance characteristics, the first is On-demand mode which is the default, the second is Provisioned mode. \u003C\u002Fp>\n\u003Ch4>On-demand mode\u003C\u002Fh4>\n\u003Cp>The Lambda \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Fdg\u002Finvocation-eventsourcemapping.html\">event source mapping\u003C\u002Fa> resource includes pollers and processors. Pollers have consumers that read records from Kafka partitions. The poller assignors send them to processors which batch the records and invoke your function.\u003C\u002Fp>\n\u003Cp>When you initially create a Kafka event source mapping, Lambda allocates a single consumer with multiple processors to process all partitions in the Kafka topic. Records in each partition are only assigned to a single processor to maintain order.\u003C\u002Fp>\n\u003Cimg src=\"\u002Fassets\u002Fexternal\u002Fguides\u002Flambda-kafka\u002Fassets\u002Fimages\u002Fkafka-lambda-scaling-initial.png\">\n\n\u003Cp>Lambda automatically scales up or down the number of consumers and processors, based on workload. Lambda samples the consumer offset lag of all the partitions in the topic every minute. If the lag is increasing, this means Lambda can&#39;t keep up with processing the records from the partition. The scaling algorithm takes into account the current offset lag, as well as the rate of new messages added to the topic. \u003C\u002Fp>\n\u003Cp>The Kafka broker distributes partitions across consumers as part of a process called rebalance. During this rebalance, the consumer does not wait for in-flight records, which could lead to record duplication. Functions should process records idempotently.\u003C\u002Fp>\n\u003Cp>Total processors for all pollers can only scale up to the total number of partitions in the topic.\u003C\u002Fp>\n\u003Cp>After successful invokes, the poller periodically commits offsets to the respective brokers.\u003C\u002Fp>\n\u003Cimg src=\"\u002Fassets\u002Fexternal\u002Fguides\u002Flambda-kafka\u002Fassets\u002Fimages\u002Fkafka-lambda-scaling-up.png\">\n\n\u003Cp>If your target Lambda function is overloaded, Lambda reduces the number of consumers. For example, if your target Lambda function returns a throttle, the ESM reduces the number of consumers, which reduces the number of Lambda invocations.\u003C\u002Fp>\n\u003Cp>You can monitor the throughput of your Kafka topic using consumer metrics \u003Ccode>consumer_lag\u003C\u002Fcode> and \u003Ccode>consumer_offset\u003C\u002Fcode>.\u003C\u002Fp>\n\u003Cp>To check how many function invocations occur in parallel, you can also monitor the \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Fdg\u002Fmonitoring-metrics.html#monitoring-metrics-concurrency\">concurrency metrics\u003C\u002Fa> for your function. The concurrency is approximately equal to the total number of processors across all pollers, depending on processor activity. For example, if three pollers have five processors running for a given ESM, the function concurrency would be approximately 15 (5 + 5 + 5).\u003C\u002Fp>\n\u003Ch4>Provisioned Mode\u003C\u002Fh4>\n\u003Cp>The easiest way to increase the performance of your Kafka consuming Lambda application is to enable Provisioned Mode on the ESM. With Provisioned Mode you specify the \u003Ccode>MinimumPollers\u003C\u002Fcode> and \u003Ccode>MaximumPollers\u003C\u002Fcode> and then the ESM quickly autoscales between the min and max as message volume and offset lag changes. In Provisioned Mode each Provisioned Poller is a compute unit capable of handling up to 5MBps of throughput and 5 concurrent Lambda invocations. Provisioned Mode does have a cost component, \u003Ca href=\"https:\u002F\u002Faws.amazon.com\u002Flambda\u002Fpricing\u002F#Provisioned_Mode_for_Event_Source_Mapping_.28ESM.29_Pricing\">pricing details\u003C\u002Fa>, so we recommend starting with the minimum number of Provisioned Pollers(MinimumPollers = 1) and baselining the performance of your workload. Then adjust the minimum upwards if your workload needs it, such as use cases with sudden surges in message volume and strict performance needs. \u003C\u002Fp>\n\u003Cp>For the low latency(sub 100ms) processing, set the \u003Ccode>MaximumBatchingWindowInSeconds\u003C\u002Fcode> to 0. Using this setting, combined with Provisioned Mode, tells the ESM to invoke the next batch of records immediately after the previous invocation completes.\u003C\u002Fp>\n\u003Ch3>Increasing data processing throughput\u003C\u002Fh3>\n\u003Cp>It is important that your function can keep pace with the rate of traffic. A growing offset lag means the function processing cannot keep up. If the age is high in relation to the stream&#39;s retention period, you can lose data as records expire from the stream. This value should generally not exceed 50% of the stream&#39;s retention period. When the value reaches 100% of the stream retention period, data is lost. One temporary solution is to increase the retention time of the stream. This gives you more time to resolve the issue before losing data.\u003C\u002Fp>\n\u003Cp>There are several ways to improve processing throughput.\u003C\u002Fp>\n\u003Cul>\n\u003Cli>You can avoid processing unnecessary records by using \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Fdg\u002Finvocation-eventfiltering.html\">content filtering\u003C\u002Fa> to control which records Lambda sends to your function. This helps reduce traffic to your function, simplifies code, and reduces overall cost.\u003C\u002Fli>\n\u003Cli>Lambda allocates processors across all pollers based on the number of partitions up to a maximum of one concurrent Lambda function per partition. You can increase the number of processing Lambda functions by increasing the number of partitions.\u003C\u002Fli>\n\u003Cli>For compute intensive functions, you can increase the \u003Ca href=\"https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Foperatorguide\u002Fcomputing-power.html\">memory\u003C\u002Fa> allocated to your function, which also increases the amount of virtual CPU available. This can help reduce the duration of a processing function.\u003C\u002Fli>\n\u003Cli>Lambda polls Kafka with a configurable batch size of records. You can increase the batch size to process more records in a single invocation. This can improve processing time and reduce costs, particularly if your function has an increased init time. A larger batch size increases the latency to process the first record in the batch, but potentially decreases the latency to process the last record in the batch. There is a tradeoff between cost and latency when optimizing a partition’s capacity and the decision depends on the needs of your workload.\u003C\u002Fli>\n\u003Cli>Ensure that your producers evenly distribute records across partitions using an effective partition key strategy. A workload is unbalanced when a single key dominates other keys, creating a hot partition, which impacts throughput.\u003C\u002Fli>\n\u003C\u002Ful>\n",{"title":6,"order":7},"Scaling and throughput",6,"Kafka uses partitions to increase throughput and spread the load of records to all brokers in a cluster. The Lambda ESM can scale up automatically to handle additional load.\n\n### Kafka event source mapping auto scaling\n\nThe Event Source Mappings for Kafka have two modes that have different performance characteristics, the first is On-demand mode which is the default, the second is Provisioned mode. \n\n#### On-demand mode\n\nThe Lambda [event source mapping](https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Fdg\u002Finvocation-eventsourcemapping.html) resource includes pollers and processors. Pollers have consumers that read records from Kafka partitions. The poller assignors send them to processors which batch the records and invoke your function.\n\nWhen you initially create a Kafka event source mapping, Lambda allocates a single consumer with multiple processors to process all partitions in the Kafka topic. Records in each partition are only assigned to a single processor to maintain order.\n\n\u003Cimg src=\"\u002Fassets\u002Fexternal\u002Fguides\u002Flambda-kafka\u002Fassets\u002Fimages\u002Fkafka-lambda-scaling-initial.png\">\n\nLambda automatically scales up or down the number of consumers and processors, based on workload. Lambda samples the consumer offset lag of all the partitions in the topic every minute. If the lag is increasing, this means Lambda can't keep up with processing the records from the partition. The scaling algorithm takes into account the current offset lag, as well as the rate of new messages added to the topic. \n\nThe Kafka broker distributes partitions across consumers as part of a process called rebalance. During this rebalance, the consumer does not wait for in-flight records, which could lead to record duplication. Functions should process records idempotently.\n\nTotal processors for all pollers can only scale up to the total number of partitions in the topic.\n\nAfter successful invokes, the poller periodically commits offsets to the respective brokers.\n\n\u003Cimg src=\"\u002Fassets\u002Fexternal\u002Fguides\u002Flambda-kafka\u002Fassets\u002Fimages\u002Fkafka-lambda-scaling-up.png\">\n\nIf your target Lambda function is overloaded, Lambda reduces the number of consumers. For example, if your target Lambda function returns a throttle, the ESM reduces the number of consumers, which reduces the number of Lambda invocations.\n\nYou can monitor the throughput of your Kafka topic using consumer metrics `consumer_lag` and `consumer_offset`.\n\nTo check how many function invocations occur in parallel, you can also monitor the [concurrency metrics](https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Fdg\u002Fmonitoring-metrics.html#monitoring-metrics-concurrency) for your function. The concurrency is approximately equal to the total number of processors across all pollers, depending on processor activity. For example, if three pollers have five processors running for a given ESM, the function concurrency would be approximately 15 (5 + 5 + 5).\n\n#### Provisioned Mode\n\nThe easiest way to increase the performance of your Kafka consuming Lambda application is to enable Provisioned Mode on the ESM. With Provisioned Mode you specify the `MinimumPollers` and `MaximumPollers` and then the ESM quickly autoscales between the min and max as message volume and offset lag changes. In Provisioned Mode each Provisioned Poller is a compute unit capable of handling up to 5MBps of throughput and 5 concurrent Lambda invocations. Provisioned Mode does have a cost component, [pricing details](https:\u002F\u002Faws.amazon.com\u002Flambda\u002Fpricing\u002F#Provisioned_Mode_for_Event_Source_Mapping_.28ESM.29_Pricing), so we recommend starting with the minimum number of Provisioned Pollers(MinimumPollers = 1) and baselining the performance of your workload. Then adjust the minimum upwards if your workload needs it, such as use cases with sudden surges in message volume and strict performance needs. \n\nFor the low latency(sub 100ms) processing, set the `MaximumBatchingWindowInSeconds` to 0. Using this setting, combined with Provisioned Mode, tells the ESM to invoke the next batch of records immediately after the previous invocation completes.\n\n### Increasing data processing throughput\n\nIt is important that your function can keep pace with the rate of traffic. A growing offset lag means the function processing cannot keep up. If the age is high in relation to the stream's retention period, you can lose data as records expire from the stream. This value should generally not exceed 50% of the stream's retention period. When the value reaches 100% of the stream retention period, data is lost. One temporary solution is to increase the retention time of the stream. This gives you more time to resolve the issue before losing data.\n\n\nThere are several ways to improve processing throughput.\n\n* You can avoid processing unnecessary records by using [content filtering](https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Fdg\u002Finvocation-eventfiltering.html) to control which records Lambda sends to your function. This helps reduce traffic to your function, simplifies code, and reduces overall cost.\n* Lambda allocates processors across all pollers based on the number of partitions up to a maximum of one concurrent Lambda function per partition. You can increase the number of processing Lambda functions by increasing the number of partitions.\n* For compute intensive functions, you can increase the [memory](https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Foperatorguide\u002Fcomputing-power.html) allocated to your function, which also increases the amount of virtual CPU available. This can help reduce the duration of a processing function.\n* Lambda polls Kafka with a configurable batch size of records. You can increase the batch size to process more records in a single invocation. This can improve processing time and reduce costs, particularly if your function has an increased init time. A larger batch size increases the latency to process the first record in the batch, but potentially decreases the latency to process the last record in the batch. There is a tradeoff between cost and latency when optimizing a partition’s capacity and the decision depends on the needs of your workload.\n* Ensure that your producers evenly distribute records across partitions using an effective partition key strategy. A workload is unbalanced when a single key dominates other keys, creating a hot partition, which impacts throughput.",[10],"content\u002Fcontributors\u002Fjulian-wood.json","LESSON","Using AWS Lambda to process Apache Kafka streams","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Fscaling-throughput","5 min",[16,25,34,42,51,59,64,73,82],{"title":17,"content":18},"Introducing streaming",[19],{"title":17,"order":20,"time":21,"path":22,"id":23,"link":24},1,"4 min","introduction","introduction.md","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Fintroduction",{"title":26,"content":27},"Using Kafka to build your streaming application",[28],{"title":26,"order":29,"time":30,"path":31,"id":32,"link":33},2,"6 min","using-kafka","using-kafka.md","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Fusing-kafka",{"title":35,"content":36},"Using Lambda to consume records from Kafka",[37],{"title":35,"order":38,"time":21,"path":39,"id":40,"link":41},3,"using-lambda","using-lambda.md","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Fusing-lambda",{"title":43,"content":44},"Configuring Kafka and Lambda",[45],{"title":43,"order":46,"time":47,"path":48,"id":49,"link":50},4,"9 min","configuring-kafka-lambda","configuring-kafka-lambda.md","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Fconfiguring-kafka-lambda",{"title":52,"content":53},"Processing Kafka streams using the Lambda ESM",[54],{"title":52,"order":55,"time":47,"path":56,"id":57,"link":58},5,"processing-esm","processing-esm.md","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Fprocessing-esm",{"title":6,"content":60},[61],{"title":6,"order":7,"time":14,"path":62,"id":63,"link":13},"scaling-throughput","scaling-throughput.md",{"title":65,"content":66},"Monitoring and observability",[67],{"title":65,"order":68,"time":69,"path":70,"id":71,"link":72},7,"2 min","monitoring-observability","monitoring-observability.md","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Fmonitoring-observability",{"title":74,"content":75},"Troubleshooting",[76],{"title":74,"order":77,"time":78,"path":79,"id":80,"link":81},8,"3 min","troubleshooting","troubleshooting.md","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Ftroubleshooting",{"title":83,"content":84},"Resources",[85],{"title":83,"order":86,"time":87,"path":88,"id":89,"link":90},9,"1 min","resources","resources.md","\u002Fcontent\u002Fguides\u002Flambda-kafka\u002Fresources",1792578866075]