ec2 中的 redis 堆栈性能下降,该怎么办?

问题描述 投票:0回答:1

在 m5.large EC2 实例上部署带有 Redis 堆栈的 Node.js 服务器时,我遇到了严重的延迟问题。在本地,我的响应时间在 40-80 毫秒范围内,但部署后,它们急剧增加到 700-1200 毫秒。

尽管使用HTTP/2进行部署,实现Nginx进行缓存和反向代理,并使用PM2进行集群,但性能并没有提高。值得注意的是,Redis 堆栈托管在同一 EC2 实例上并使用本地主机连接。

我正在寻找有关可能导致响应时间急剧增加的原因以及如何缓解这种情况的见解或建议。以下是我可以根据要求提供的一些具体信息,以更好地诊断问题:

redis-stack.sh

#!/bin/bash

# Root privilege check
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root"
   exit 1
fi

# Adding the GPG key for Redis Stack
echo "Adding the GPG key for Redis Stack..."
curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

# Adding the Redis Stack repository
echo "Adding the Redis Stack repository..."
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list

# Updating package lists and installing Redis Stack
echo "Updating package lists and installing Redis Stack..."
apt-get update
apt-get install redis-stack-server -y

# Configure Redis Stack
echo "Configuring Redis Stack..."
redis_conf="/opt/redis-stack/etc/redis-stack.conf"  # Adjust this path based on your findings
sed -i '/^bind/s/^/#/' $redis_conf
sed -i '/^appendonly/s/^/#/' $redis_conf
echo "maxmemory 2gb" >> $redis_conf
echo "maxmemory-policy allkeys-lru" >> $redis_conf
echo "appendonly no" >> $redis_conf
echo "bind 0.0.0.0" >> $redis_conf

#configure for redis-stack.conf
echo "protected-mode no" >> /etc/redis-stack.conf

# System optimizations
echo "Applying system optimizations..."
sysctl -w net.core.somaxconn=1024
sysctl vm.overcommit_memory=1
echo "fs.file-max = 100000" >> /etc/sysctl.conf
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf

# Enable and start Redis Stack service
echo "Enabling and starting Redis Stack service..."
systemctl enable redis-stack-server
systemctl start redis-stack-server

# Check the status of Redis Stack service
echo "Redis Stack installation and configuration complete. Checking the service status..."
systemctl status redis-stack-server

服务器信息:

root@ip-172-31-22-105:~# free -m
               total        used        free      shared  buff/cache   available
Mem:            7721        4766        2496         144         458        2559
Swap:           5119          39        5080
root@ip-172-31-22-105:~# 

Nginx 配置:

upstream node_backend {
    server 127.0.0.1:8008;
    keepalive 32;
}
server {
    server_name example.domain.com;

    listen 443 ssl http2; # managed by Certbot
    #ssl paths and conf 

    # Security Headers
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    location / {
        proxy_pass http://node_backend;
        proxy_redirect http://localhost:8008/ https://$server_name/;
        
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_http_version 1.1;
        proxy_cache my_cache;
        proxy_cache_valid 200 301 302 60m;
        proxy_cache_bypass $http_cache_control;

        client_max_body_size 5G;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
    }
}

Pm2集群模式:

| id | name            | mode     | status | cpu | memory  |
|----|-----------------|----------|--------|-----|---------|
| 0  | exampl-backend  | cluster  | online | 0%  | 125.0mb |
| 1  | exampl-backend  | cluster  | online | 0%  | 123.9mb |

| PID     | USER   | %CPU | %MEM | COMMAND                  |
|---------|--------|------|------|--------------------------|
| 383595  | root   | 10.0 | 1.6  | node /root/Sand/app.js   |
| 383578  | root   | 0.7  | 1.6  | node /root/Sand/app.js   |
| 3518010 | nobody | 0.3  | 0.5  | redis-server             |

运行Redis堆栈服务器:

redis-stack-server.service - Redis stack server
     Loaded: loaded (/etc/systemd/system/redis-stack-server.service; enabled; vendor preset: enable>
     Active: active (running) since Wed 2024-05-15 11:07:54 UTC; 24h ago
       Docs: https://redis.io/
   Main PID: 3518010 (redis-server)
      Tasks: 13 (limit: 9247)
     Memory: 18.0M
        CPU: 2min 51.842s
     CGroup: /system.slice/redis-stack-server.service
             └─3518010 "/opt/redis-stack/bin/redis-server *:6379" "" "" "" "" "" "" "" "" "" "" "" >

任何有关在此环境中优化我的堆栈性能的建议将不胜感激。

node.js nginx amazon-ec2 redis pm2
1个回答
0
投票

太棒了,它确实可以找到这个解决方案来提高性能。

© www.soinside.com 2019 - 2024. All rights reserved.