You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/main/asciidoc/sqs.adoc
+65-1Lines changed: 65 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -806,7 +806,6 @@ NOTE: The same factory can be used to create both `single message` and `batch` c
806
806
807
807
IMPORTANT: In case the same factory is shared by both delivery methods, any supplied `ErrorHandler`, `MessageInterceptor` or `MessageListener` should implement the proper methods.
808
808
809
-
810
809
==== Container Options
811
810
812
811
Each `MessageListenerContainer` can have a different set of options.
@@ -1977,6 +1976,7 @@ If after the 5 seconds for `maxDelayBetweenPolls` 6 messages have been processed
1977
1976
If the queue is depleted and a poll returns no messages, it'll enter `low throughput` mode again and perform only one poll at a time.
1978
1977
1979
1978
==== Configuring BackPressureMode
1979
+
The default `BackPressureHandler` can be configured to optimize the polling behavior based on the application's throughput requirements.
1980
1980
The following `BackPressureMode` values can be set in `SqsContainerOptions` to configure polling behavior:
1981
1981
1982
1982
* `AUTO` - The default mode, as described in the previous section.
@@ -1987,6 +1987,70 @@ Useful for really high throughput scenarios where the risk of making parallel po
1987
1987
1988
1988
NOTE: The `AUTO` setting should be balanced for most use cases, including high throughput ones.
1989
1989
1990
+
==== Advanced Backpressure management
1991
+
1992
+
Even though the default `BackPressureHandler` should be enough for most use cases, there are scenarios where more fine-grained control over message consumption is required not to overwhelm downstream systems or exceed resource limits.
1993
+
In such a case, it is necessary to replace the default `BackPressureHandler` with a custom one that implements the `BackPressureHandler` interface.
1994
+
A `backPressureHandlerFactory` can be set in `SqsContainerOptions` to configure which `BackPressureHandler` to use.
1995
+
1996
+
===== What is a BackPressureHandler?
1997
+
1998
+
A `BackPressureHandler` is an interface that determines whether the container should apply backpressure (i.e., slow down or pause polling) based on the current state of the system.
1999
+
It is invoked before each poll to SQS and can prevent polling or poll for fewer messages if certain conditions are met, e.g., too many inflight messages, custom resource constraints, etc.
2000
+
2001
+
===== Creating a custom BackPressureHandler
2002
+
2003
+
To implement a custom backpressure logic, the `BackPressureHandler` interface must be implemented.
2004
+
2005
+
A `SqsMessageListenerContainer` can be configured to use the desired `BackPressureHandler` by setting the `backPressureHandlerFactory` on the `ContainerOptions`.
.backPressureHandlerFactory(containerOptions -> new CustomBackPressureHandler())
2011
+
// ... other options
2012
+
)
2013
+
// ... other container settings ...
2014
+
.build();
2015
+
```
2016
+
2017
+
===== Combining Multiple BackPressureHandlers
2018
+
2019
+
If necessary, multiple `BackPressureHandler` can be combined by using the `CompositeBackPressureHandler`.
2020
+
Each of the `BackPressureHandler` (which we'll call delegates) are chained in the order they are provided.
2021
+
The first delegate will be requested the initial amount of permits and will return the number of permits it accepts to grant.
2022
+
The second delegate will get that potentially reduced number of permits as a request and might in turn reduce it further.
2023
+
The process continues until all delegates have been called or one of them returns 0, which will prevent the polling of messages from SQS.
2024
+
2025
+
For example, to implement the `BackPressureMode.ALWAYS_POLL_MAX_MESSAGES` strategy, we can combine a concurrency limiter, an adaptative throughput handler, and a "full batch only" handler.
2026
+
The resulting `CompositeBackPressureHandler` looks like this:
Spring Cloud AWS provides several built-in `BackPressureHandler` implementations:
2042
+
2043
+
- `ConcurrencyLimiterBackPressureHandler`: Limits the number of messages being processed concurrently.
2044
+
- `ThroughputBackPressureHandler`: Switches between high and low throughput modes. In high throughput mode, multiple polls can be done in parallel.
2045
+
In low throughput mode, only one poll is done at a time.
2046
+
- `FullBatchBackPressureHandler`: Ensure polls will always be done with a full batch of messages, meaning that the number of messages polled will always be equal to `maxMessagesPerPoll` if possible or `0` if not possible.
2047
+
This `FullBatchBackPressureHandler` must always be the last in the chain for it to work properly.
2048
+
2049
+
The `BackPressureHandlerFactories` class provides factory methods to create these handlers easily.
2050
+
These handlers can be used directly or combined with custom ones using the `CompositeBackPressureHandler` to fit the application's needs.
2051
+
2052
+
Additionally, the `BackPressureHandlerFactories#adaptativeThroughputBackPressureHandler` factory method combines the `ConcurrencyLimiterBackPressureHandler`, `ThroughputBackPressureHandler`, and `FullBatchBackPressureHandler` as per the desired `BackPressureMode`.
2053
+
1990
2054
=== Blocking and Non-Blocking (Async) Components
1991
2055
1992
2056
The SQS integration leverages the `CompletableFuture`-based async capabilities of `AWS SDK 2.0` to deliver a fully non-blocking infrastructure.
Copy file name to clipboardExpand all lines: spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/listener/AbstractPipelineMessageListenerContainer.java
0 commit comments