Sub topic 12
Monitoring and Performance Insights
This chapter assumes you have a running RDS instance (such as demo-pg from earlier chapters), can use the AWS CLI, and know basic SQL. It covers the three monitoring layers RDS gives you and how to move from a symptom to a cause.
Three layers of visibility
RDS exposes data at three levels. Each answers a different question.
| Layer | Question it answers | Granularity |
|---|---|---|
| CloudWatch metrics | Is the instance healthy and how loaded is it? | 1 minute, collected automatically |
| Enhanced Monitoring | What is the operating system doing? | 1 to 60 seconds, opt-in |
| Performance Insights | Which queries and waits are causing the load? | Per-second sampling, opt-in |
CloudWatch metrics are collected from the hypervisor, outside your instance. Enhanced Monitoring uses an agent on the instance, so it can show per-process CPU and memory. Performance Insights samples active database sessions and groups them by SQL statement, wait event, host, and user.
Metrics worth watching
Start with a small set and alarm on it:
CPUUtilization: sustained values above about 80% suggest a query or sizing problem.FreeableMemory: a steady fall toward zero means the instance is swapping or the cache is too small for the working set.FreeStorageSpace: running out of space stops writes. Alarm well before it reaches zero.DatabaseConnections: compare against the engine's connection limit.ReadLatencyandWriteLatency: rising latency with flat IOPS often points at storage limits.ReplicaLag: relevant only if you use read replicas.
The following command creates an alarm on low free storage. It assumes an SNS topic already exists; replace the ARN with yours.
aws cloudwatch put-metric-alarm \
--alarm-name demo-pg-low-storage \
--namespace AWS/RDS \
--metric-name FreeStorageSpace \
--dimensions Name=DBInstanceIdentifier,Value=demo-pg \
--statistic Minimum \
--period 300 \
--evaluation-periods 2 \
--threshold 2147483648 \
--comparison-operator LessThanThreshold \
--alarm-actions arn:aws:sns:us-east-1:123456789012:db-alertsThe threshold is in bytes, so this fires when free space stays under 2 GiB for ten minutes.
Turning on Performance Insights
Performance Insights is enabled per instance. Use modify-db-instance to turn it on:
aws rds modify-db-instance \
--db-instance-identifier demo-pg \
--enable-performance-insights \
--performance-insights-retention-period 7 \
--apply-immediatelySeven days of retention is the free tier. Longer retention is billed. Availability depends on engine, version, and instance class, so check the supported list if the call is rejected.
Reading the Performance Insights dashboard
The central chart plots database load, measured in average active sessions (AAS). Compare it to the dashed line showing the instance's vCPU count. Load above that line means sessions are waiting, either for CPU or for something else.
Then slice the load:
- By waits: tells you the kind of bottleneck, such as CPU, I/O, or locks.
- By SQL: names the statements contributing most of the load.
- By host or user: finds the application or account responsible.
Reading the data from code
Performance Insights has an API, so you can feed the data into your own tooling. The resource identifier is the instance's DbiResourceId, not its name.
import boto3
from datetime import datetime, timedelta, timezone
rds = boto3.client("rds")
pi = boto3.client("pi")
instance = rds.describe_db_instances(DBInstanceIdentifier="demo-pg")["DBInstances"][0]
end = datetime.now(timezone.utc)
response = pi.get_resource_metrics(
ServiceType="RDS",
Identifier=instance["DbiResourceId"],
StartTime=end - timedelta(hours=1),
EndTime=end,
PeriodInSeconds=300,
MetricQueries=[{"Metric": "db.load.avg"}],
)
for point in response["MetricList"][0]["DataPoints"]:
print(point["Timestamp"], round(point["Value"], 2))Common mistakes
- Alarming only on CPU. Storage and connection exhaustion cause outages with low CPU.
- Reading AAS without the vCPU line. A load of 4 is idle on 16 vCPUs and saturated on 2.
- Expecting query text beyond the stored limit. Long statements are truncated in the dashboard.
- Forgetting that slow-query logging is a separate setting. The
log_min_duration_statementparameter from the parameter group chapter sends slow statements to the database log, which complements Performance Insights.
Exercise
A CloudWatch alarm shows CPUUtilization at 95%. In Performance Insights, database load is 3 AAS on a 2-vCPU instance, and the wait slice is mostly CPU. What is your next step?
Show answer
Load above the vCPU line with CPU waits means sessions are queuing for CPU. Switch to the "Top SQL" slice, find the statements with the highest load, and tune them (add an index or rewrite the query). If the load is legitimate, move to a larger instance class.
Exercise
Why does the Python example use DbiResourceId and not demo-pg as the identifier?
Show answer
The Performance Insights API identifies a database by its immutable resource ID, which looks like db-ABCDEFGHIJKL. The instance name can be reused or changed, so the API does not accept it.