Azure Container Apps (ACA) are built on a solid open source foundation. Behind the scenes, it runs on a managed Kubernetes cluster and includes several open source components, including Dapr for microservices creation and execution, Envoy Proxy for ingress functions, and KEDA for event-driven autoscaling. Again, you do not have to install these components yourself. All you need to do is enable and/or configure your container app to use these components.
In this blog, we will see how can we scale Container Apps based on Redis Streams with the Azure managed instance of Redis, i.e. Azure Cache for Redis. We will be referring to the KEDA trigger specification for Redis Streams (Scale applications based on Redis Streams) to configure our Container App. This specification describes the Redis-streams trigger that scales based on the Pending Entries List (XPENDING) for a specific Consumer Group of a Redis Stream. In this blog we are going to manually create Redis entities and add data to them using the Redis Console. However, this can also be done through a DAPR PUB/SUB implementation using Redis.
Introduction
ACA's autoscaling feature internally leverages KEDA and gives you the ability to configure the number of replicas to deploy based on rules (event triggers). Apart from HTTP and TCP rule based scaling, container apps also support custom rules giving it the flexibility and opening up a lot more configuration options since all of KEDA's event-based scalers are supported.
Some of the supported event-driven Azure data sources include:
Various scaling examples of Azure Container Apps can be found in this repository.
Lets cut to the chase !
XGROUP CREATE mystream mygroup $ mkstream
Custom Rule Type is different for every KEDA based scaler. For Redis Streams, it is 'redis-streams'. The Trigger Parameter can be set as "password".
"consumerGroup": "mygroup"
"host": "<REDIS-NAME>.redis.cache.windows.net"
"pendingEntriesCount": "2"
"port": "6379"
"stream": "mystream"
The consumerGroup and stream fields, as the name suggests, are the names of the Consumer Group and Redis Stream which we created initially. pendingEntriesCount are the number of messages present in the Pending Entries List (PEL) of that Consumer Group, and are yet to be acknowledged. In this example, for every 2 new messages in the PEL, one instance/replica will be added to the Container App revision.
Lets start by adding two messages to the queue and assign them to a Consumer Group. We will be using XADD and XREADGROUP commands for this.
> XADD mystream * name Alice
> XADD mystream * name Bob
> XREADGROUP GROUP mygroup consumerx COUNT 2 STREAMS mystream >
The above screenshot shows that both the messages have been added to the stream and have been assigned to the consumer group where the consumer is consumerx. The number of messages which are in pending state can be checked by the XPENDING command.
Now, lets have a look at our replica count. Refresh the Console blade and click on the Replica dropdown. As you would see that even though we added 2 messages to the stream, the instance/replica count did not increase, which is as expected.
Lets now add two more messages to the consumer group stream, in the same way we did before.
> XADD mystream * name Derek
> XADD mystream * name Emily
> XREADGROUP GROUP mygroup consumerx COUNT 2 STREAMS mystream >
We will get 4 pending messages in the list now.
Since there are 4 pending messages in the stream, according to our configuration, a total of 2 instances would be needed to cater them. Looking at the replica dropdown on the Console blade, we can see that there are 2 instances currently active.
This shows that our container app has successfully scaled out and will go on to scale as the messages increase further, till the maxReplica count is not reached.
Now coming to the scale in part. Instance scale in will happen when the pending messages in the list will be consumed and acknowledged. Let us now acknowledge 2 messages and check the PEL count again.
> XACK mystream mygroup <MESSAGE_ID>
For this example, let us pick up the two messageIDs from the result of the last XREADGROUP command. In this example, the two IDs are 1674708896170-0 and 1674708901325-0, for Derek and Emily respectively.
In the above result, we can see that the PEL count has come back down to 2, after the two messages got acknowledged. Let us now refresh our Console blade and check the number of replicas present.
As seen in the above screenshot, the replica count has come down to 1 which shows that the scale in has happened successfully.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.