Redis
Cloud Functions support using Redis.
Prerequisites
Have activated Cloud Development.
Features
Provides a Redis-compatible cache database with high availability, high reliability, and high scalability.
Applicable Scenarios
The database in Cloud Development is insufficient to meet business requirements, which requires the use of Redis.
Existing services that use Redis can be migrated to Cloud Development while continuing to utilize Redis.
Installer
! WeChat Mini Program developers, please use [Other Login Methods] - [WeChat Official Account Login] to log in, then select the associated Mini Program account to log in; QQ Mini Program developers can directly log in via the [Cloud Development] button in the QQ Mini Program Developer IDE, or log in via the associated Tencent Cloud account.
You can install and manage this capability via the Tencent Cloud Development Console.
Using the Program
Depending on the actual situation, it is recommended to use redis and ioredis.
Invocation Example
Using in Cloud Function
'use strict';
const redis = require('redis')
let client = redis.createClient({
host: process.env.HOST,
port: process.env.PORT,
// Fill in the actual password.
password: 'xxx'
})
exports.main = async (event, context, callback) => {
let res = await new Promise((resolve, reject) => {
client.get('test', function (err, reply) {
if (err) {
resolve({
err
})
}
resolve({
data: reply.toString()
})
})
})
return {
res
}
}
Other
Operating Mechanism of Cloud Function
In-depth Understanding of Cloud Function - Operating Mechanism of Cloud Function
Virtual Private Cloud (VPC)
In cloud functions, if developers need to access resources such as Tencent Cloud's Redis, it is recommended to use a private network to ensure data security and connection security. For details on private networks, including how to establish a private network and add cloud functions to it, refer to the relevant section in Database Redis Configuration Network.
Cloud Function Concurrency & Redis Connections
In the Cloud Development Console - Environment Overview, you can view the maximum concurrency allowed for cloud functions in the current environment. The maximum concurrency also represents the maximum instances for cloud functions.
It is recommended to read the Redis official blog post Redis Clients Handling regarding Redis connections.
The configuration parameter maxclients for Redis connections defaults to 10000. If modification is required, please contact customer support.
When using Redis in cloud functions, each cloud function instance establishes a connection to the Redis Server. Therefore, the maximum connections between this cloud function and Redis equals the maximum connections per instance * the actual maximum concurrency. When configuring Redis's maxclients, this parameter should be greater than the sum of the maximum connections of all cloud functions using this database.
Therefore, when using Redis in Cloud Functions, it is recommended to consolidate all code that reads from or writes to the same Redis instance within a single cloud function. This approach offers two benefits:
- The probability of cold starts occurring in Cloud Functions is relatively low
- The maximum number of Redis connections is relatively small, thus reducing excessive memory usage caused by multiple connections