首页 > 解决方案 > AWS EC2 上 MongoDB Node.JS 系统设计的扩展性能问题

问题描述

我正在 AWS 上使用 MongoDB 进行扩展项目。我想知道如何改善当前的性能问题?我的目标是在不使用 AWS Auto Scaling 的情况下每秒达到大约 5000 个客户端,同时保持 2000 毫秒的平均响应时间、0 错误率、0 超时,以便我可以了解水平和垂直扩展。

在一个 AWS EC2 实例中,我部署了具有 1000 万个唯一 product_id 的 MongoDB 数据库。在另一个 EC2 实例中,我部署了一个服务来连接数据库,这是我的 loader.io 结果: loader io测试结果2 该图显示,当我设置一分钟每秒发出 225 个请求的 get 请求测试时,错误率和超时为0,平均响应时间为1941ms,接近我的目标2000ms。如果我设置任何更高的值,平均响应时间可能会超过 2000 毫秒的限制,这不是我想要的。

然后我开始为 Nginx 负载均衡器创建另一个 EC2 实例,并为服务创建另外 2 个实例以水平扩展我的项目。我希望每秒获得大约 600-700 个客户端,平均响应时间为 2000 毫秒,错误率为 0,超时为 0。不幸的是,这些是我的结果: loader io 280 测试设置 loader io 280 测试结果 loader io 300 测试结果 当我查看 loader.io 结果时,它显示当我设置每秒 280 个请求时,平均响应时间为 186ms,这比我之前没有设置 Nginx 的测试要快得多。错误率和超时也是 0。但是,当我设置为每秒 300 个请求时,它开始构建错误率,大约有 269 个响应和 500 个错误代码。情况不妙。 aws cpu使用情况2 当我查看 AWS Cloudwatch 结果时,我注意到 CPU 使用率非常低。所以我没有CPU问题。 新遗物结果1 新遗物结果2 当我查看 New Relic 结果时,我意识到大部分响应时间来自“MongoDB products toArray”,即棕色阴影区域。因此,我需要弄清楚如何改进数据库。但是,我不确定如何改进。

请注意,我没有在我的编码脚本中的任何地方使用“MongoDB products toArray”,它是由 New Relic 提供的。我认为这意味着基于我当前使用“查找”作为查询的查询,它会返回一个数组作为结果。如果我使用“findOne”作为查询,它将返回一个对象。

我测试的 GET 请求查询:

const getReviewByProductId = (data, callback) => {
    const id = parseInt(data.params.id);
    Model.ProductModel.find({ product_id: id }, (error, results) => {
        if (error) {
            return callback(err);
        } else {
            return callback(null, results);
        }
    });
}

此函数测试的 product_id 是唯一的 id。所以我认为索引不会加快速度。我不确定如何改进“MongoDB 产品 toArray”。

以下是 nginx.conf:

user  nginx;
worker_processes  1;


events {
    worker_connections  1024;
}


http {
    upstream nodejs {
    server 13.57.191.130:3000;
    server 204.236.161.63:3000;
    server 18.216.89.77:3000;
    }
    server {
    listen 80;
    location / {
    proxy_pass http://nodejs;
    }
  }
}

这是我使用的 Nginx 的基本设置。请让我知道是否有任何东西可以提高与“MongoDB products toArray”相关的性能。

如果有兴趣,这是我的 MongoDB 架构:

require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(`${process.env.MONGO_HOST}/reviews`, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });
const Schema = mongoose.Schema;
const productSchema = new Schema({
    product_id: { type: Number, required: true, unique: true },
    product_name: { type: String, required: true, unique: true },
    review: [{
        review_id: { type: Number, required: true, unique: true },
        user: {
            user_id: { type: Number },
            firstname: { type: String },
            lastname: { type: String },
            gender: { type: String, enum: ['Male', 'Female', 'Other'] },
            nickname: { type: String },
            email: { type: String, required: true },
            password: { type: String, required: true },
        },
        opinion: { type: String, required: true },
        text: { type: String },
        rating_overall: { type: Number, min: 1, max: 5, required: true },
        doesRecommended: { type: Boolean, required: true },
        rating_size: { type: String, enum: ['a size too small', '1/2 a size too small', 'Perfect', '1/2 a size too big', 'a size too big'], required: true },
        rating_width: { type: String, enum: ['Too narrow', 'Slightly narrow', 'Perfect', 'Slightly wide', 'Too wide'], required: true },
        rating_comfort: { type: String, enum: ['Uncomfortable', 'Slightly uncomfortable', 'Ok', 'Comfortable', 'Perfect'], required: true },
        rating_quality: { type: String, enum: ['Poor', 'Below average', 'What I expected', 'Pretty great', 'Perfect'], required: true },
        isHelpful: { type: Number, required: true, default: 0 },
        isNotHelpful: { type: Number, required: true, default: 0 },
        created_at: { type: Date, required: true },
        review_photo_path: [{
            review_photo_id: { type: Number },
            review_photo_url: { type: String }
        }]
    }]
});
const ProductModel = mongoose.model('product', productSchema);
module.exports = { ProductModel };

我的数据库目前t2.medium在 EC2 上运行,我的服务和 Nginxt2.micro在 EC2 上运行。

这是Github 中的项目。我计划部署更多服务实例并添加到 Nginx 负载均衡器,但目前我想先修复“MongoDB products toArray”响应时间问题。

标签: node.jsmongodbamazon-ec2horizontal-scaling

解决方案


推荐阅读