Redis
Supports using Redis in cloud functions.
Prerequisites
CloudBase has been provisioned.
Features
Provides a caching database compatible with the Redis protocol, featuring high availability, high reliability, high scalability, and other characteristics.
Applicable Scenarios
The CloudBase database cannot meet the business requirements, so it is necessary to use Redis.
Existing services that have been using Redis wish to continue utilizing it when migrating to CloudBase.
Installer
! WeChat Mini Program developers should use [Other login methods] - [Official Account Login] to log in, then select the associated mini program account. QQ Mini Program developers can log in directly via the [CloudBase] button in the QQ Mini Program IDE, or through an associated Tencent Cloud account.
You can install and manage this capability via the CloudBase Console.
Using the Program
Depending on the actual situation, it is recommended to use redis and ioredis.
Invoke Example
Usage in Cloud Functions:
'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
Operation Mechanism of Cloud Functions
Deep Dive into Cloud Functions - Operation Mechanism of Cloud Functions
Virtual Private Cloud (VPC)
In cloud functions, if developers need to access Tencent Cloud resources such as Redis, it is recommended to use a Virtual Private Cloud (VPC) to ensure data security and connection security. For information on VPC, how to set up a VPC, and how to add cloud functions to a VPC, refer to the relevant section in TencentDB for Redis Network Configuration.
Cloud Function Concurrency & Redis Connections
In the CloudBase Console - Environment Overview, you can view the maximum concurrency allowed for cloud functions in the current environment. The maximum concurrency is also the maximum number of instances for cloud functions.
Regarding Redis connections, it is recommended to read the article Redis Clients Handling on the official Redis blog.
The configuration parameter maxclients
for Redis connections has a default value of 10000. If modification is required, please contact customer service.
When using Redis in cloud functions, each cloud function instance establishes a connection with the Redis Server. Therefore, the maximum number of connections between this cloud function and Redis is the maximum connections per instance * the maximum actual concurrency. When configuring the maxclients
parameter for Redis, this value 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 centralize all code that reads from and writes to the same Redis instance into a single cloud function. This approach offers two benefits.
- Cloud functions have a relatively low probability of experiencing cold starts
- With a relatively small maximum number of Redis connections, this reduces memory usage from excessive connections