Operation Mechanism
Cloud Functions (Cloud Functions) is a serverless computing service that allows you to run code without managing servers. The CloudBase cloud function provides you with a simple, reliable, and efficient code execution environment, automatically handling all underlying infrastructure and allowing you to focus on developing business logic.
Core Features
Stateless Computing
CloudBase dynamically controls the number of cloud function instances and evenly distributes requests based on real-time load conditions. Consecutive requests may be handled by different instances, therefore:
- Cloud functions must be designed as stateless: Each execution should be independent of previous executions.
- Functions must be idempotent: Multiple invocations should produce the same side effects as a single invocation
- Should not rely on shared state between instances: Each function instance is an isolated execution environment
Best Practice: 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 adopt an event-driven architecture. Each invocation essentially triggers a cloud function execution event.
Supported Trigger Types
| Trigger Type | Description | Documentation Link |
|---|---|---|
| SDK/API Call | Direct invocation via SDKs in various languages or REST API | web client invocation node server-side invocation |
| HTTP Trigger | Invoked via HTTP requests, can be used to build APIs | Using HTTP to Access Cloud Functions |
| Timer Trigger | Automatically triggers execution based on preset time schedules | Timer Trigger |
Automatic Elastic Scaling
CloudBase automatically handles the scale-out and scale-in of cloud functions, freeing you from infrastructure management:
- Scale out on demand: More instances are automatically created when traffic increases
- Scale in automatically: Excess instances are released when traffic decreases
- Zero-instance cold startup: Occupies zero resources when there is no traffic, and automatically starts up upon receiving requests
Runtime Environment Details
Containerized Execution Environment
- Cloud functions run in an isolated containerized Unix environment
- Each function instance has an independent execution environment and is completely isolated from others
- The creation, management, and destruction of instances are fully automated and handled by the platform
Temporary Storage
- Each function instance provides 512MB of /tmp temporary storage space
- Suitable for reading and writing temporary files during a single execution
- Temporary storage may be cleared after function execution completes
- The current runtime environment is based on CentOS 7.2, but should not rely on specific operating systems or versions as 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 function supports multiple programming language runtimes:
| Language | Supported Versions |
|---|---|
| Node.js | 16.13 18.15 20.19 |
| PHP | 7.4 8.0 |
| Python | 3.7 3.9 3.10 |
| Golang | 1 |
| Java | 11(Kona JDK) |
Resource Limitations and Performance
Memory Configuration
- Default memory: 256 MB
- Configurable range: 64 MB ~ 3072 MB
- Recommendation: Select an appropriate memory configuration based on function complexity and data processing volume
Performance Note: Increasing memory configuration typically enhances CPU performance as well, which is particularly important for compute-intensive tasks.
Concurrency Processing Capacity
The concurrency of a cloud function refers to the number of function instances executing simultaneously at any given time:
- Maximum concurrent instances: 1000 instances per cloud function by default
- Concurrency Limiting Behavior: Calls exceeding the limit will be blocked (not executed)
Concurrency Calculation Formula
Concurrent function instances = QPS × Function runtime duration (seconds)
Instance Calculation
| Scenario | Calculation Process | Result |
|---|---|---|
| Function execution: 0.2 seconds, 300 requests per second | 300 × 0.2 | 60 concurrent instances |
| Function execution: 0.2 seconds, to reach maximum concurrency of 1000 | 1000 ÷ 0.2 | Requires 5000 QPS |
Performance Optimization Recommendations
To ensure cloud functions run efficiently, consider the following optimization recommendations:
Reduce cold startup 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
Use memory properly
- Avoid unnecessary creation of large objects
- Release promptly after processing large data
- Allocate sufficient memory for compute-intensive tasks
Handle errors and exceptions
- Implement a robust error handling mechanism
- Log key operations
- Set a reasonable timeout period
By understanding the operational mechanisms of cloud functions and adhering to best practices, you can build efficient, reliable, and scalable serverless applications.