Operation Mechanism
Cloud Functions is a serverless computing service that enables you to run code without managing servers. CloudBase's Cloud Functions provides a simple, reliable, and efficient code execution environment, automatically handling all underlying infrastructure so you can focus on developing business logic.
Core Features
Stateless Computing
CloudBase dynamically controls the number of cloud function instances and balances request distribution based on real-time load conditions. Consecutive requests may be handled by different instances, therefore:
- Cloud functions must be designed to be stateless: Each execution should be independent of previous ones.
- Functions should be idempotent: The side effects of multiple invocations should be the same as those of a single invocation.
- Should not rely on shared state between instances: Each function instance is an isolated execution environment.
Best practices: If you need to preserve state, use external storage services (such as databases or cloud storage) rather than relying on the memory or local storage of function instances.
Event-Driven Model
Cloud Functions employs an event-driven architecture, where each invocation essentially triggers a cloud function execution event.
Supported Trigger Types
| Trigger Type | Description | Documentation Link |
|---|---|---|
| SDK/API Invocation | Direct invocation via language-specific SDKs or REST API | web Client Invocation node.js Server-Side Invocation |
| HTTP Trigger | Invoked via HTTP requests, can be used to build APIs | Accessing Cloud Functions via HTTP |
| Timer Trigger | Automatically triggers execution based on preset time schedules | Timer Trigger |
Automatic Elastic Scaling
The CloudBase platform automatically handles the scaling of cloud functions, eliminating the need for you to manage infrastructure:
- Scale on demand: Automatically creates more instances when traffic increases
- Automatic scale-in: Releases excess instances when traffic decreases
- Zero-instance cold start: Occupies no resources when there is no traffic, automatically starts up when there are requests
Runtime Environment Details
Containerized Execution Environment
- Cloud functions run in an isolated containerized Unix environment
- Each function instance has an isolated execution environment and is completely isolated from each other.
- The creation, management, and destruction of instances are fully automated by the platform
Temporary Storage
- Each function instance provides 512MB of /tmp temporary storage space
- Suitable for temporary file read/write during a single execution process
- Temporary storage may be cleared after function execution completes
- The current runtime environment is based on CentOS 7.2, but should not depend on a specific operating system or version because the runtime environment may be updated at any time
- Temporary storage is not guaranteed to persist after function execution. For persistent storage, use Cloud Storage
Supported Runtimes
CloudBase Cloud Functions supports multiple programming language runtimes:
| Language | Supported Versions |
|---|---|
| Node.js | 12.16, 10.15, 8.9 |
| PHP | 7.2 |
| Python | 3.6, 2.7 |
| Golang | 1 |
| Java | 8 |
Resource Limits and Performance
Memory Configuration
- Default Memory: 256 MB
- Configurable Range: 128 MB to 2048 MB
- Recommendation: Select an appropriate memory configuration based on function complexity and data processing volume
Performance Tip: Increasing memory allocation typically boosts CPU performance, which is particularly important for compute-intensive tasks.
Concurrency Processing Capability
The concurrency of Cloud Functions refers to the number of function instances executing simultaneously at any given point in time:
- Maximum Concurrent Instances: A single cloud function defaults to 1000 instances.
- Concurrency Throttling Behavior: Calls exceeding the limit will be blocked (and will not be executed).
Concurrency Calculation Formula
Concurrent Function Instances = QPS × Function Execution Time (s)
Instance Calculation
| Scenario | Calculation Process | Result |
|---|---|---|
| Function execution time: 0.2 seconds, 300 requests per second | 300 × 0.2 | 60 concurrent instances |
| Function execution time: 0.2 seconds, to achieve maximum concurrency of 1000 | 1000 ÷ 0.2 | Requires 5000 QPS |
Performance Optimization Suggestions
To ensure cloud functions run efficiently, consider the following optimization suggestions:
Reduce Cold Start Impact
- Avoid loading large dependencies in the global scope
- Separate initialization code from processing logic
- Consider using Provisioned Concurrency
Optimize Execution Time
- Use asynchronous parallel processing for independent tasks
- Avoid unnecessary network requests
- Optimize database queries and file operations
Proper Memory Usage
- Avoid unnecessary large object creation
- Release resources promptly after processing large data
- Allocate sufficient memory for compute-intensive tasks
Handling Errors and Exceptions
- Implement a robust error handling mechanism
- Log critical operations
- Set a reasonable timeout
By understanding the operational mechanisms of cloud functions and following best practices, you can build efficient, reliable, and scalable serverless applications.