2022-10-02

Aurora Serverless v1 and v2

What is Amazon Aurora

Amazon Aurora is a relational database developed by AWS that is compatible with MySQL and PostgreSQL, and according to AWS, Aurora is up to 5 times more powerful than MySQL and up to 3 times more powerful than PostgreSQL. It is also used by Amazon's EC sites, Netflix, and other well-known services.

Aurora is organized in clusters, which consist of two layers: an instance layer consisting of one or more DB servers and a storage layer that manages data, separating instances and storage. This architecture provides excellent availability because the database is not affected in the event of network failure or disk corruption.

What is Aurora Serverless

Aurora Serverless is Amazon Aurora that does not require DB instance scaling management; it automatically starts, shuts down, scales up, and scales down according to the volume of requests coming to Aurora Serverless. The unit of scaling performance is called ACU (Aurora Capacity Unit), and 1 ACU is equivalent to about 2GB of memory.

Aurora Serverless v1 vs. v2

Aurora Serverless is available in v1 and v2. v1 became official in October 2018 and v2 became official in April 2022. Below is a table comparing v1 and v2.

Aurora Serverless v1 Aurora Serverless v2
ACU Min:1ACU、Max:128 ACU Min:0.5 ACU、Max:256 ACU
Price 0.10 USD/h per 1ACU 0.20 USD/h per 1ACU
Cluster Serverless Provisioned
DB instance Serverless instance Serverless instance, Provisioned instance
Multi-AZ No Yes
Auto pause Yes No
Data API Yes No
RDS Proxy No Yes
Automatic scaling during query No Yes

Although v1 and v2 are only one version different, they have quite significant specification changes. In particular, the management of clusters and DB instances is completely different, and there are some things that are no longer possible with v2. v1 and v2 should be considered separate services.

The following are two features that will not be available in v2.

  • Data API
    • Aurora Serverless v1 can be integrated with other serverless services using the Data API, which eliminates the need to connect directly to the DB since the API communication is done over HTTPS. With a conventional RDBMS, the communication may become unavailable when the maximum number of connections is reached due to high traffic, but with HTTPS, there is no bottleneck such as a maximum number of connections, allowing for more flexible support of application scalability.
  • Pause Function
    • Aurora Serverless v1 has a configuration item called auto_pause that allows the DB to pause after a certain period of time without connections, without charging for the DB instance. However, it takes about 1 minute to start up after the pause. For example, you can save money by avoiding auto-pause on weekdays between 6:00 and 21:00, and by running Lambda periodically with EventBridge to start the Aurora cluster with 0 ACU on other days (e.g., weekends).

Serverless v1 use cases

Aurora Serverless v1 is suitable for the following use cases

  • Applications and development databases that are used infrequently
    v1 has an auto-shutdown feature that automatically shuts down the DB server when not in use. This is cost-effective, as the server has 0 ACUs during shutdown and only charges for storage.

Serverless v2 use cases

Aurora Serverless v2 is suited for the following use cases

  • Unpredictable workloads in production environments
    The automatic scaling feature makes it useful when the required instance size or traffic is hard to predict.

Although v1 also has an auto-scaling feature, v1 is not suited for production environments due to the following points

  • No multi-AZ capability, which raises concerns about availability and fault tolerance
  • Auto scaling is not available during transaction execution or table locking.

Example of Serverless v1 build with Terraform

Below is an example Terraform code for Aurora Serverless v1.

resource "aws_rds_cluster" "main" {
  cluster_identifier                  = local.aurora_cluster_identifier
  database_name                       = local.aurora_database_name
  master_username                     = local.aurora_master_username
  master_password                     = local.aurora_master_password
  db_cluster_parameter_group_name     = aws_rds_cluster_parameter_group.main.name
  db_subnet_group_name                = data.aws_db_subnet_group.main.name
  deletion_protection                 = true
  skip_final_snapshot                 = false
  engine                              = "aurora-mysql"
  engine_version                      = "5.7.mysql_aurora.2.07.1"
  engine_mode                         = "serverless"
  backup_retention_period             = 7
  storage_encrypted                   = true
  iam_database_authentication_enabled = false
  enable_http_endpoint                = true

  scaling_configuration {
    min_capacity = 2
    max_capacity = 256
    auto_pause   = true
  }

  lifecycle {
    ignore_changes = [
      availability_zones,
      engine_version,
      master_username,
      master_password,
    ]
  }
}

resource "aws_rds_cluster_parameter_group" "main" {
  name        = "cluster-pg"
  family      = "aurora-mysql5.7"
  description = "RDS cluster parameter group"
  parameter {
    name  = "character_set_server"
    value = "utf8"
  }
  parameter {
    name  = "character_set_client"
    value = "utf8"
  }
  parameter {
    apply_method = "immediate"
    name         = "server_audit_events"
    value        = "QUERY"
  }
  parameter {
    apply_method = "immediate"
    name         = "server_audit_logging"
    value        = "1"
  }
  parameter {
    apply_method = "immediate"
    name         = "time_zone"
    value        = "Asia/Tokyo"
  }
}

resource "random_password" "main" {
  length           = 16
  special          = true
  override_special = "_!%^"
}

Example of Serverless v2 build with Terraform

Below is an example Terraform code for Aurora Serverless v2.

resource "aws_rds_cluster" "main" {
  cluster_identifier                  = local.aurora_cluster_identifier
  engine                              = "aurora-postgresql"
  engine_version                      = "13.6"
  engine_mode                         = "provisioned"
  master_username                     = local.aurora_master_username
  master_password                     = local.aurora_master_password
  port                                = 5432
  database_name                       = local.aurora_database_name
  vpc_security_group_ids              = [aws_security_group.aurora.id]
  db_subnet_group_name                = aws_db_subnet_group.main.name
  db_cluster_parameter_group_name     = aws_rds_cluster_parameter_group.main.name
  iam_database_authentication_enabled = true

  serverlessv2_scaling_configuration {
    min_capacity = 0.5
    max_capacity = 1.0
  }

  skip_final_snapshot = true
  apply_immediately   = true
}

resource "aws_rds_cluster_instance" "main" {
  cluster_identifier = aws_rds_cluster.main.id
  identifier         = "${local.aurora_cluster_identifier}-serverless-instance"
  engine                  = aws_rds_cluster.main.engine
  engine_version          = aws_rds_cluster.main.engine_version
  instance_class          = "db.serverless"
  db_subnet_group_name    = aws_db_subnet_group.main.name
  db_parameter_group_name = aws_db_parameter_group.main.name

  monitoring_role_arn = aws_iam_role.aurora_monitoring.arn
  monitoring_interval = 60

  publicly_accessible = true
}

resource "aws_db_subnet_group" "main" {
  name       = local.db_subnet_group_name
  subnet_ids = data.aws_subnet_ids.main.ids
}

resource "aws_rds_cluster_parameter_group" "main" {
  name   = local.cluster_parameter_group_name
  family = "aurora-postgresql13"
}

resource "aws_db_parameter_group" "main" {
  name   = local.db_parameter_group_name
  family = "aurora-postgresql13"

  parameter {
    apply_method = "pending-reboot"
    name         = "shared_preload_libraries"
    value        = "pg_stat_statements,pg_hint_plan"
  }
}

References

https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/aurora-serverless.html
https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/CHAP_AuroraOverview.html
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!