• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar
  • Home
  • Projects
  • Products
  • Themes
  • Tools
  • Request for Quote

Vengala Vinay

Having 9+ Years of Experience in Software Development

  • Home
  • WordPress
  • PHP
    • Codeigniter
  • Django
  • Magento
  • Selenium
  • Server
Home » Server Monitoring Best Practices: Keeping Your Magento 2 App and DynamoDB Clusters Alive on AWS

Server Monitoring Best Practices: Keeping Your Magento 2 App and DynamoDB Clusters Alive on AWS

Establishing a Robust Monitoring Baseline for Magento 2 on AWS

Maintaining a high-performance, always-on Magento 2 instance on AWS requires a multi-layered monitoring strategy. This isn’t just about checking if a server is “up”; it’s about understanding resource utilization, application-level performance, and potential bottlenecks before they impact end-users. We’ll focus on key AWS services and practical implementation details.

EC2 Instance Monitoring: CloudWatch Agent and Custom Metrics

AWS CloudWatch is the foundational service for monitoring EC2 instances. While default metrics are useful, deploying the CloudWatch Agent allows for deeper insights, including custom metrics and log collection. For a Magento 2 application, we’re particularly interested in CPU utilization, memory usage, disk I/O, and network traffic. Beyond these, application-specific metrics are crucial.

First, ensure the CloudWatch Agent is installed and configured on your EC2 instances. The configuration file (typically /opt/aws/amazon-cloudwatch-agent/bin/config.json) dictates what metrics are collected. Here’s a sample configuration focusing on system-level metrics and enabling custom metrics for PHP-FPM and Nginx:

{
  "agent": {
    "metrics_collection_interval": 60,
    "run_as_user": "cwagent"
  },
  "metrics": {
    "namespace": "Magento2/EC2",
    "append_dimensions": {
      "InstanceId": "${aws:InstanceId}"
    },
    "aggregation_dimensions": [
      [ "InstanceId" ]
    ],
    "metrics_collected": {
      "cpu": {
        "measurement": [
          "cpu_usage_idle",
          "cpu_usage_iowait",
          "cpu_usage_user",
          "cpu_usage_system"
        ],
        "totalcpu": true
      },
      "disk": {
        "measurement": [
          "free_percent",
          "inodes_free"
        ],
        "resources": [
          "/",
          "/var",
          "/var/log"
        ]
      },
      "mem": {
        "measurement": [
          "mem_used_percent",
          "swap_used_percent"
        ]
      },
      "net": {
        "measurement": [
          "bytes_recv",
          "bytes_sent",
          "packets_recv",
          "packets_sent"
        ]
      },
      "statsd": {
        "service_address": "udp:localhost:8125",
        "metrics_collection_interval": 60
      }
    }
  },
  "logs": {
    "logs_collected": {
      "files": {
        "collect_list": [
          {
            "file_path": "/var/log/nginx/access.log",
            "log_group_name": "Magento2/Nginx/Access",
            "log_stream_name": "{instance_id}/nginx_access"
          },
          {
            "file_path": "/var/log/nginx/error.log",
            "log_group_name": "Magento2/Nginx/Error",
            "log_stream_name": "{instance_id}/nginx_error"
          },
          {
            "file_path": "/var/log/php-fpm/error.log",
            "log_group_name": "Magento2/PHP-FPM/Error",
            "log_stream_name": "{instance_id}/php_fpm_error"
          }
        ]
      }
    }
  }
}

After applying this configuration (using sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s), you’ll see system metrics in CloudWatch under the Magento2/EC2 namespace. The statsd section is key for custom application metrics.

Application Performance Monitoring (APM) with StatsD and CloudWatch Custom Metrics

Magento 2’s performance is heavily influenced by PHP execution time, database queries, and external API calls. We can push custom metrics to CloudWatch via StatsD. A common approach is to use a StatsD client library within your PHP application or to have a separate StatsD daemon that collects metrics from various sources.

For PHP, consider using a library like php-statsd. Here’s a simplified example of how you might instrument a critical part of your Magento 2 code (e.g., a custom module’s controller action or a service):

<?php
// Assuming you have a StatsD client instance available, e.g., $statsdClient

// Measure execution time of a critical operation
$startTime = microtime(true);
// ... perform critical operation ...
$endTime = microtime(true);
$executionTime = ($endTime - $startTime) * 1000; // in milliseconds

$statsdClient->timing('magento2.checkout.process_time', $executionTime);

// Count successful order placements
if ($orderWasPlacedSuccessfully) {
    $statsdClient->increment('magento2.checkout.orders_placed');
} else {
    $statsdClient->increment('magento2.checkout.orders_failed');
}

// Gauge current number of items in a cache
$cacheItemCount = $this->cacheManager->getCache('my_custom_cache')->getItemsCount();
$statsdClient->gauge('magento2.cache.my_custom_cache.item_count', $cacheItemCount);

?>

Ensure your StatsD daemon (e.g., a standalone StatsD server or the one bundled with the CloudWatch Agent) is configured to forward metrics to CloudWatch. The CloudWatch Agent’s StatsD collector, when enabled in its configuration, will listen on UDP port 8125 and forward these metrics to the namespace defined in the agent’s config (Magento2/EC2 in our example). You can then create CloudWatch Alarms based on these custom metrics (e.g., high checkout process time, low order success rate).

Nginx and PHP-FPM Monitoring

Web server and application server logs are invaluable. We’ve already configured the CloudWatch Agent to collect Nginx access/error logs and PHP-FPM error logs. Beyond log analysis, specific metrics are essential.

Nginx: Enable the stub_status module for basic Nginx metrics (active connections, requests per second, etc.). Add the following to your nginx.conf or a site-specific configuration file:

location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1; # Restrict access to localhost
    deny all;
}

You can then use a tool like nginx-statsd or a custom script to scrape this endpoint and send metrics to your StatsD daemon. For example, a simple Python script:

import requests
import statsd

try:
    response = requests.get('http://localhost/nginx_status', timeout=5)
    response.raise_for_status() # Raise an exception for bad status codes
    status_text = response.text.split('\n')

    active_connections = int(status_text[0].split(': ')[1])
    requests_total = int(status_text[2].split(': ')[1])
    connections_accepted = int(status_text[3].split(': ')[1])
    connections_handled = int(status_text[4].split(': ')[1])

    c = statsd.StatsClient('localhost', 8125, prefix='magento2.nginx')
    c.gauge('active_connections', active_connections)
    c.gauge('requests_total', requests_total)
    c.gauge('connections_accepted', connections_accepted)
    c.gauge('connections_handled', connections_handled)

except requests.exceptions.RequestException as e:
    print(f"Error fetching Nginx status: {e}")
except Exception as e:
    print(f"Error processing Nginx status: {e}")

PHP-FPM: PHP-FPM exposes its own status page. Ensure it’s enabled and secured. In your PHP-FPM pool configuration (e.g., /etc/php/X.Y/fpm/pool.d/www.conf), set:

pm.status_path = /fpm_status
ping.response = pong

And in your Nginx configuration, add a location block to proxy requests to the PHP-FPM status page:

location ~ ^/fpm_status(&\?.*)?$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME /fpm_status;
    fastcgi_index index.php;
    fastcgi_pass unix:/var/run/php/phpX.Y-fpm.sock; # Adjust path as needed
    allow 127.0.0.1;
    deny all;
}

Similar to Nginx, use a script (e.g., Python with requests) to fetch these metrics and send them to StatsD. Key metrics include pm.max_children, pm.active_children, pm.idle_children, and pm.accepted_conn.

DynamoDB Cluster Monitoring: CloudWatch Metrics and Alarms

DynamoDB, being a managed service, has its own set of CloudWatch metrics that are critical for performance and cost management. Unlike EC2, you don’t install agents; you monitor the service’s performance through its API and CloudWatch integration.

Key DynamoDB Metrics to Monitor

  • ConsumedReadCapacityUnits and ConsumedWriteCapacityUnits: Essential for understanding throughput usage and cost. Monitor these against provisioned capacity.
  • ProvisionedReadCapacityUnits and ProvisionedWriteCapacityUnits: Track your provisioned capacity.
  • ThrottledRequests: A critical indicator that your application is exceeding provisioned throughput. High throttling means requests are being rejected, impacting user experience.
  • SuccessfulRequestLatency: Measures the latency of successful requests. Spikes here indicate potential performance issues, often related to hot partitions or insufficient capacity.
  • SystemErrors: Indicates internal DynamoDB errors.
  • ReturnedItemCount: Useful for understanding the size of query/scan results.

These metrics are available in CloudWatch under the AWS/DynamoDB namespace. You can view them directly in the AWS Management Console or programmatically via the AWS SDKs.

Setting Up DynamoDB Alarms

Proactive alerting is crucial for DynamoDB. Configure CloudWatch Alarms for the following scenarios:

  • High Throttling Rate: Set an alarm when ThrottledRequests exceeds a certain threshold (e.g., > 0 for a sustained period, or a percentage of total requests). This indicates immediate capacity issues.
  • Approaching Provisioned Capacity Limits: Monitor ConsumedReadCapacityUnits and ConsumedWriteCapacityUnits relative to ProvisionedReadCapacityUnits and ProvisionedWriteCapacityUnits. An alarm when consumed capacity is consistently above 80-90% of provisioned capacity can trigger scaling actions or investigations.
  • High Latency: Alarm on SuccessfulRequestLatency when the average or p95/p99 latency exceeds acceptable thresholds (e.g., > 100ms).
  • System Errors: Set an alarm if SystemErrors is greater than 0 for any significant duration.

Example CloudWatch Alarm configuration for throttled requests (using AWS CLI):

aws cloudwatch put-metric-alarm \
    --alarm-name "DynamoDB-High-Throttled-Requests-MyTable" \
    --alarm-description "High number of throttled requests on MyTable" \
    --metric-name ThrottledRequests \
    --namespace "AWS/DynamoDB" \
    --statistic Sum \
    --period 300 \
    --threshold 10 \
    --comparison-operator GreaterThanThreshold \
    --dimensions Name=TableName,Value=MyTable \
    --evaluation-periods 2 \
    --datapoints-to-alarm 2 \
    --treat-missing-data notBreaching \
    --alarm-actions arn:aws:sns:us-east-1:123456789012:MySNSTopic

Remember to replace MyTable, us-east-1, 123456789012, and the SNS topic ARN with your specific values. For tables using On-Demand capacity, monitor ConsumedReadCapacityUnits and ConsumedWriteCapacityUnits against the account-level limits and consider alarms for ThrottledRequests if they occur frequently.

Log Analysis and Centralized Logging

Beyond basic log collection, a robust monitoring strategy involves centralizing logs for easier analysis and correlation. AWS CloudWatch Logs is the natural choice. The CloudWatch Agent, as configured earlier, pushes logs to CloudWatch Logs. You can then create Metric Filters to extract metrics from log data (e.g., count specific error messages) and set alarms based on these metrics.

For example, to create a metric filter for PHP-FPM errors:

aws logs put-metric-filter \
    --log-group-name "Magento2/PHP-FPM/Error" \
    --filter-name "PHP-FPM-Critical-Errors" \
    --filter-pattern "[*, timestamp, level, message]" \
    --metric-transformations metricName=PHP-FPM-Errors,metricNamespace=Magento2/PHP-FPM,metricValue=1,defaultValue=0

This example is simplistic; a more sophisticated pattern would be needed to specifically identify critical errors. You can then create a CloudWatch Alarm on the PHP-FPM-Errors metric. For advanced log analysis, consider integrating with services like Amazon OpenSearch Service (formerly Elasticsearch Service) for full-text search and complex querying capabilities.

Proactive Health Checks and Synthetic Monitoring

While metrics and logs tell you what’s happening, synthetic monitoring simulates user interactions to proactively identify issues. AWS offers CloudWatch Synthetics Canaries for this purpose.

Create Canaries to:

  • Periodically hit critical Magento 2 endpoints (e.g., homepage, product page, add-to-cart, checkout steps).
  • Verify page load times and check for specific content on the page (e.g., presence of “Add to Cart” button, absence of error messages).
  • Test API endpoints used by your frontend or mobile apps.

Configure alarms on Canary failures or increased execution times. This provides an external perspective on your application’s availability and performance, independent of internal server metrics.

Conclusion: A Unified Observability Strategy

Effective server monitoring for a complex application like Magento 2, especially when coupled with managed services like DynamoDB, requires a holistic approach. It’s not just about collecting data; it’s about transforming that data into actionable insights. By combining detailed EC2 metrics, application-level custom metrics via StatsD, comprehensive log analysis, and external synthetic checks, you build a resilient observability strategy that keeps your Magento 2 application and DynamoDB clusters performing optimally and reliably on AWS.

Primary Sidebar

A little about the Author

Having 9+ Years of Experience in Software Development.
Expertised in Php Development, WordPress Custom Theme Development (From scratch using underscores or Genesis Framework or using any blank theme or Premium Theme), Custom Plugin Development. Hands on Experience on 3rd Party Php Extension like Chilkat, nSoftware.

Recent Posts

  • Step-by-Step: Diagnosing thread pools deadlock during concurrent ActiveRecord transaction processing on Linode Servers
  • Securing Your E-commerce APIs: Preventing SQL Injection (SQLi) in customized checkout queries in WooCommerce Implementations
  • Disaster Recovery 101: Architecting Auto-Failovers for MySQL and Ruby Deployments on Linode
  • High-Throughput Caching Strategies: Scaling MySQL for Perl Application APIs
  • Disaster Recovery 101: Architecting Auto-Failovers for DynamoDB and Laravel Deployments on DigitalOcean

Copyright © 2026 · Vinay Vengala