Amazon DevOps Guru for the Serverless applications - Part 10 Anomaly detection on Aurora Serverless v2
Tags: aws, serverless, devops, aiops
Published: 2024-05-20 12:17:48

## Introduction

In the course of the series about Amazon DevOps Guru for the Serverless applications we explored many different anomalies recognized by the DevOps Guru. For my other article series about [Data API for Amazon Aurora Serverless v2](https://dev.to/aws-builders/data-api-for-amazon-aurora-serverless-v2-with-aws-sdk-for-java-part-1-introduction-and-set-up-of-the-sample-application-3g71) I experimented a lot with Aurora Serverless v2 and of course asked myself a question whether anomalies with Aurora (Serverless v2) PostgreSQL database will be recognized by Amazon DevOps Guru service. So, let's explore this. In this article we'll use AWS SDK for Java and focus on the "standard" way to connect Lambda function to the Aurora (Serverless v2) PostgreSQL which is JDBC (Java Database Connectivity). We won't use Amazon RDS Proxy in our example. The same will be true for other programming languages supported by Lambda as they all offer their only functionality for that. We'll explore the anomaly detection on Aurora Serverless v2 with Data API in one of the next articles.

## Anomaly detection on Aurora Serverless v2

Let's look into our [sample application](https://github.com/Vadym79/DevOpsGuruWorkshopDemo) and use [SAM template](https://github.com/Vadym79/DevOpsGuruWorkshopDemo/blob/main/template-Aurora-Serverless-with-and-without-Data-API.yaml) to create infrastructure and deploy the application which architecture is described on the following picture :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ae7t5ev5hzkfq2pfe8o.png)

The application creates products stored in the Aurora Serverless v2 PostgreSQL database and retrieves them by id using JDBC. The relevant Lambda function which we'll use to retrieve product by its id is GetProductByIdViaAuroraServerlessV2WithoutDataApi and its handler implementation is [GetProductByIdViaAuroraServerlessV2WithoutDataApiHandler](https://github.com/Vadym79/DevOpsGuruWorkshopDemo/blob/main/src/main/java/software/amazonaws/example/product/handler/GetProductByIdViaAuroraServerlessV2WithoutDataApiHandler.java).

DevOps Guru suggests enabling [RDS Performance Insights](https://aws.amazon.com/de/rds/performance-insights/) to gain additional insights about the anomaly which we did for AuroraServerlessV2Instance in the [SAM template](https://github.com/Vadym79/DevOpsGuruWorkshopDemo/blob/main/template-Aurora-Serverless-with-and-without-Data-API.yaml). 

```
  AuroraServerlessV2Instance:
    Type: 'AWS::RDS::DBInstance'
    Properties:
      Engine: aurora-postgresql
      DBInstanceClass: db.serverless
      DBClusterIdentifier: !Ref AuroraServerlessV2Cluster
      ....
      EnablePerformanceInsights: true
      PerformanceInsightsRetentionPeriod: 7
```
As in the previous article we use [hey tool](https://github.com/rakyll/hey) to perform the load test like this

```
hey -z 15m -c 300 -H "X-API-Key: XXXa6XXXX" https://XXX.execute-api.eu-central-1.amazonaws.com/prod/productsWithoutDataApi/1   
```
In this example we invoke the API Gateway endpoint with 300 concurrent containers for 15 minutes. Behind the prod/productsWithoutDataApi endpoint Lambda function GetProductByIdViaAuroraServerlessV2WithoutDataApi will be invoked which retrieves the product by id 1 from the Aurora Serverless v2 PostgreSQL database.

We configured in our [SAM template](https://github.com/Vadym79/DevOpsGuruWorkshopDemo/blob/main/template-Aurora-Serverless-with-and-without-Data-API.yaml) Aurora database cluster to scale from minimal capacity 0.5 to maximal capacity 1 ACU (which is very small database size) in case of the increased load for the cost saving purpose only.

```
  AuroraServerlessV2Cluster:
    Type: 'AWS::RDS::DBCluster'
...
      ServerlessV2ScalingConfiguration:
        MinCapacity: 0.5
        MaxCapacity: 1
```
Aurora (Serverless v2) database manages the maximal number of the database connections available proportionally to the database size (in our case the ACU setting). For more information please read the documentation about [Maximum connections for Aurora Serverless v2](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless-v2.setting-capacity.html#aurora-serverless-v2.max-connections). So with the increased number of invocations we expect to reach the maximal number of the database connections available and high database (CPU) load soon, so that database won't be able to respond to the new Lambda function requests to retrieve product by id.  With that we will provoke the anomaly and would like to figure out whether DevOps Guru will be able to detect it. And it will! The following insight was generated:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vogyvyqxhyaowdujx61v.png)

And the following aggregated anomalous metrics have been identified:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6unrtte7n4g2m9kq3cp7.png)

For the Aurora Serverless v2 database these are: DatabaseConnection Sum, DBLoad Average and DBLoadCPU Average.

Graphed anomalies look like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u00gh0jzlt1f7nhsriau.png)

And the RDS Performance Insights metrics for database load sliced by the SQL statement look like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2kps0dql69lxo6rw3b8.png)

So we see that the SQL statements : 
```
select id, name, price from tbl_product where id=?

```

with colors purple and orange caused very high database load.

## Conclusion

In this article learned that DevOps Guru could successfully detect anomalies with Aurora (Serverless v2) PostgreSQL database in case of Lambda function with Java 21 managed runtime connected to it via JDBC. We created a very high load on the database by invoking Lambda function to retrieve product by id several hundred times concurrently for multiple minutes. By doing this Aurora (Serverless v2) PostgreSQL database auto-scaled from 0.5 to 1 ACU which was not enough to sustain such a load. We saw that DevOps Guru correctly pointed to the increased sum of database connections and constantly high database (CPU) load operational anomalies.