Skip to content

Commit 0a335d2

Browse files
Finalize valkey config
- Optimal node size for cpu utilization - Configurable replica count - Include engine logs for deeper insight
1 parent ab83439 commit 0a335d2

File tree

7 files changed

+48
-18
lines changed

7 files changed

+48
-18
lines changed

terraform/app/env/preview.tfvars

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ appspec_bucket = "nhse-mavis-appspec-bucket-preview"
2323
minimum_web_replicas = 2
2424
maximum_web_replicas = 4
2525

26-
valkey_node_type = "cache.t4g.micro"
26+
valkey_node_type = "cache.t4g.micro"
27+
valkey_log_retention_days = 3
28+
valkey_failover_enabled = false

terraform/app/env/sandbox-alpha.tfvars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ good_job_replicas = 1
2323

2424
valkey_node_type = "cache.t4g.micro"
2525
valkey_log_retention_days = 3
26+
valkey_failover_enabled = false
2627
sidekiq_replicas = 1

terraform/app/env/sandbox-beta.tfvars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ good_job_replicas = 1
2424
# Valkey serverless configuration - minimal settings for sandbox
2525
valkey_node_type = "cache.t4g.micro"
2626
valkey_log_retention_days = 3
27+
valkey_failover_enabled = false
2728
sidekiq_replicas = 1

terraform/app/env/test.tfvars

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ appspec_bucket = "nhse-mavis-appspec-bucket-test"
2121
minimum_web_replicas = 2
2222
maximum_web_replicas = 4
2323

24-
valkey_node_type = "cache.t4g.micro"
24+
valkey_node_type = "cache.t4g.micro"
25+
valkey_log_retention_days = 3
26+
valkey_failover_enabled = false

terraform/app/env/training.tfvars

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ appspec_bucket = "nhse-mavis-appspec-bucket-training"
2525
minimum_web_replicas = 2
2626
maximum_web_replicas = 4
2727

28-
valkey_node_type = "cache.t4g.micro"
28+
valkey_node_type = "cache.t4g.micro"
29+
valkey_log_retention_days = 3
30+
valkey_failover_enabled = false

terraform/app/valkey.tf

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,47 @@ resource "aws_elasticache_subnet_group" "valkey" {
3535
}
3636
}
3737

38-
# ElastiCache Replication Group (cluster mode disabled for Sidekiq compatibility)
3938
resource "aws_elasticache_replication_group" "valkey" {
4039
replication_group_id = "mavis-cache-${var.environment}"
41-
description = "Valkey cluster for Sidekiq (cluster mode disabled)"
40+
description = "Valkey cluster for Sidekiq"
4241

4342
engine = "valkey"
4443
engine_version = var.valkey_engine_version
4544
node_type = var.valkey_node_type
4645
port = var.valkey_port
4746
parameter_group_name = aws_elasticache_parameter_group.valkey.name
4847

49-
# Cluster configuration (disabled for Sidekiq compatibility)
50-
automatic_failover_enabled = true
51-
num_cache_clusters = 2
48+
automatic_failover_enabled = var.valkey_failover_enabled
49+
num_cache_clusters = length(local.valkey_cache_availability_zones)
5250
subnet_group_name = aws_elasticache_subnet_group.valkey.name
5351
security_group_ids = [aws_security_group.valkey.id]
54-
preferred_cache_cluster_azs = [aws_subnet.private_subnet_a.availability_zone, aws_subnet.private_subnet_b.availability_zone]
52+
preferred_cache_cluster_azs = local.valkey_cache_availability_zones
5553
snapshot_retention_limit = var.valkey_snapshot_retention_limit
5654
snapshot_window = var.valkey_snapshot_window
5755
maintenance_window = var.valkey_maintenance_window
5856

5957
at_rest_encryption_enabled = true
6058
transit_encryption_enabled = true
6159

62-
# Logging
6360
log_delivery_configuration {
6461
destination = aws_cloudwatch_log_group.valkey_slow_log.name
6562
destination_type = "cloudwatch-logs"
66-
log_format = "text"
63+
log_format = "json"
6764
log_type = "slow-log"
6865
}
6966

67+
log_delivery_configuration {
68+
destination = aws_cloudwatch_log_group.valkey_engine_log.name
69+
destination_type = "cloudwatch-logs"
70+
log_format = "json"
71+
log_type = "engine-log"
72+
}
73+
7074
tags = {
7175
Name = "mavis-cache-${var.environment}"
7276
Purpose = "sidekiq-job-processing"
7377
}
78+
apply_immediately = true
7479
}
7580

7681
resource "aws_elasticache_parameter_group" "valkey" {
@@ -80,7 +85,7 @@ resource "aws_elasticache_parameter_group" "valkey" {
8085
# Optimize for Sidekiq workload
8186
parameter {
8287
name = "maxmemory-policy"
83-
value = "noeviction" #TODO: Optimize for sidekiq
88+
value = "noeviction"
8489
}
8590

8691
tags = {
@@ -96,3 +101,12 @@ resource "aws_cloudwatch_log_group" "valkey_slow_log" {
96101
Name = "mavis-cache-slow-log-${var.environment}"
97102
}
98103
}
104+
105+
resource "aws_cloudwatch_log_group" "valkey_engine_log" {
106+
name = "/aws/elasticache/valkey/${var.environment}/engine-log"
107+
retention_in_days = var.valkey_log_retention_days
108+
109+
tags = {
110+
Name = "mavis-cache-engine-log-${var.environment}"
111+
}
112+
}

terraform/app/variables.tf

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,15 @@ variable "valkey_engine_version" {
354354

355355
variable "valkey_node_type" {
356356
type = string
357-
default = "cache.t2.medium"
358-
description = "ElastiCache node type for Valkey (use cache.t3.micro for sandbox, cache.r6g.large+ for production)"
357+
default = "cache.m7g.large"
358+
description = "ElastiCache node type for Valkey (use cache.t3.micro for sandbox, cache.m7g.large+ for production)"
359+
nullable = false
360+
}
361+
362+
variable "valkey_failover_enabled" {
363+
type = bool
364+
default = true
365+
description = "Enable automatic failover for Valkey cluster"
359366
nullable = false
360367
}
361368

@@ -372,14 +379,14 @@ variable "valkey_snapshot_retention_limit" {
372379

373380
variable "valkey_snapshot_window" {
374381
type = string
375-
default = "03:00-05:00"
382+
default = "00:00-02:00"
376383
description = "Daily snapshot window for Valkey (HH:MM-HH:MM format, UTC)"
377384
nullable = false
378385
}
379386

380387
variable "valkey_maintenance_window" {
381388
type = string
382-
default = "sun:05:00-sun:06:00"
389+
default = "sun:02:00-sun:04:00"
383390
description = "Weekly maintenance window for Valkey (ddd:HH:MM-ddd:HH:MM format, UTC)"
384391
nullable = false
385392
}
@@ -396,6 +403,7 @@ variable "valkey_log_retention_days" {
396403
}
397404

398405
locals {
399-
ecs_initial_lb_target_group = var.active_lb_target_group == "green" ? aws_lb_target_group.green.arn : aws_lb_target_group.blue.arn
400-
ecs_sg_ids = [module.web_service.security_group_id, module.good_job_service.security_group_id, module.sidekiq_service.security_group_id]
406+
ecs_initial_lb_target_group = var.active_lb_target_group == "green" ? aws_lb_target_group.green.arn : aws_lb_target_group.blue.arn
407+
ecs_sg_ids = [module.web_service.security_group_id, module.good_job_service.security_group_id, module.sidekiq_service.security_group_id]
408+
valkey_cache_availability_zones = var.valkey_failover_enabled ? [aws_subnet.private_subnet_a.availability_zone, aws_subnet.private_subnet_b.availability_zone] : [aws_subnet.private_subnet_a.availability_zone]
401409
}

0 commit comments

Comments
 (0)