简体   繁体   English

AWS Lambda:Redis ElastiCache 连接超时错误

[英]AWS Lambda: Redis ElastiCache connection timeout error

I have a lambda function using Node 12.我有一个使用节点 12 的 lambda function。

I need to add a new connection to a Redis database hosted in AWS ElastiCache.我需要添加到 AWS ElastiCache 中托管的 Redis 数据库的新连接。

Both are in one private VPC and the security groups/subnets are configured properly.两者都在一个私有 VPC 中,并且安全组/子网配置正确。

Solution:解决方案:

globals.js: globals.js:

const redis = require('redis');
const redisClient = redis.createClient(
  `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/${process.env.REDIS_DB}`,
);
redisClient.on('error', (err) => {
  console.log('REDIS CLIENT ERROR:' + err);
});
module.exports.globals = {
  REDIS: require('../helpers/redis')(redisClient),
};

index.js (outside handler): index.js(外部处理程序):

const { globals } = require('./config/globals');
global.app = globals;

const lambda_handler = (event, context, callback) => { ... }
exports.handler = lambda_handler;

helpers/redis/index.js:助手/redis/index.js:

const get = require('./get');
module.exports = (redisClient) => {
  return {
    get:  get(redisClient)
  };
};

helpers/redis/get.js:助手/redis/get.js:

module.exports = (redisClient) => {
  return (key, cb) => {
    redisClient.get(key, (err, reply) => {
      if (err) {
        cb(err);
      } else {
        cb(null, reply);
      }
    });
  };
};

Function call: Function 致电:

app.REDIS.get(redisKey, (err, reply) => {
  console.log(`REDIS GET: ${err} ${reply}`);
});

Problem: When increasing lambda timeout to a value greater than Redis timeout, I get this error:问题:当将 lambda 超时增加到大于 Redis 超时的值时,我收到此错误:

REDIS CLIENT ERROR:Error: Redis connection to... failed - connect ETIMEDOUT... REDIS 客户端错误:错误:Redis 连接到...失败 - 连接 ETIMEDOUT...

Addition:添加:

I tried quiting/closing the connection after each transaction:我尝试在每次交易后退出/关闭连接:

module.exports = (redisClient) => {

  return (cb) => {

    redisClient.quit((err, reply) => {
      if (err) {
        cb(err);
      } else {
        cb(null, reply);
      }
    });
  };
};
app.REDIS.get(redisKey, (err, reply) => {
  console.log(`REDIS GET: ${err} ${reply}`);
  if (err) {
    cb(err);
  } else {
    if (reply) {
      app.REDIS.quit(() => {
        cb()
      });
    }
  }
})

Error:错误:

REDIS GET: AbortError: GET can't be processed. REDIS GET:AbortError:GET 无法处理。 The connection is already closed.连接已经关闭。

Extra Notes:额外说明:

  • I have to use callbacks, this is why I pass ones in the above examples我必须使用回调,这就是我在上面的示例中传递回调的原因
  • I'm using "redis": "^3.0.2"我正在使用"redis": "^3.0.2"
  • It's not a configuration issue as the cache was accessed hundred of times in a small period of time but it then started giving the timeout errors.这不是配置问题,因为缓存在短时间内被访问了数百次,但随后开始出现超时错误。
  • Everything works normally locally在本地一切正常

It's not a configuration issue as the cache was accessed hundred of times in a small period of time but it then started giving the timeout errors.这不是配置问题,因为缓存在短时间内被访问了数百次,但随后开始出现超时错误。

i think it is origin of issue, probably redis database size hit the size limit, and it cannot process new data?我认为这是问题的根源,可能是 redis 数据库大小达到大小限制,它无法处理新数据?

Can you delete old data in it?可以删除里面的旧数据吗?

Also it is possible Elastic Cache has limits on new TCP clients' connections, and if its depleted, new connections are refused with similar error message you mentioned.此外,Elastic Cache 可能对新的 TCP 客户端的连接有限制,如果其耗尽,新连接将被拒绝并出现您提到的类似错误消息。

If redis client in aws lambda function cannot establish connection, aws lambda function fails - and new one is started. If redis client in aws lambda function cannot establish connection, aws lambda function fails - and new one is started. New lambda function makes one more connection to redis, redis cannot process it, and one more lambda function is started... New lambda function makes one more connection to redis, redis cannot process it, and one more lambda function is started...

So, at one moment, we hit the limit on active redis connections, and system is in deadlock.因此,在某一时刻,我们达到了活动 redis 连接的限制,系统陷入了死锁。

I think you can temporary stop all lambda functions, and scale up Elastic Cache redis database.我认为您可以暂时停止所有 lambda 功能,并扩展 Elastic Cache redis 数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM