Runtime Environment
Cloud Functions run in a cloud-based Linux environment1. When handling concurrent requests, a Cloud Function creates multiple Cloud Function instances. These instances are isolated from one another, with no shared memory or disk space. The creation, management, and destruction of Cloud Function instances are handled automatically by the platform. Each Cloud Function instance is provided with 512MB of temporary disk space under the /tmp directory for reading and writing temporary files during a single execution. It is crucial to note that this temporary disk space may be destroyed after the function execution completes. Therefore, you should not rely on or assume that temporary files stored in this disk space will persist. For persistent storage needs, please use the Cloud Storage feature.
Stateless Functions
Cloud Functions should be stateless and idempotent, meaning that the execution of a Cloud Function does not depend on any residual information left in the runtime environment from a previous execution.
To ensure load balancing, the Cloud Function platform controls the number of Cloud Function instances based on the current load and may reuse Cloud Function instances under certain conditions. This means that if two consecutive Cloud Function calls are both handled by the same Cloud Function instance, they will share the same temporary disk space. However, since Cloud Function instances can be destroyed at any time and consecutive requests are not guaranteed to be routed to the same instance, Cloud Functions should not rely on data left in the temporary disk space from previous calls. The overarching principle is that Cloud Function code should be stateless.
Event Model
Cloud function invocations adopt an event-triggered model. Each invocation from the Mini Program side triggers a cloud function invocation event, and the cloud function platform will either create a new instance or reuse an existing one to process the invocation. Similarly, since cloud functions can invoke each other, mutual invocations between cloud functions also trigger invocation events.
Automatic Scaling
Developers do not need to manage the scaling of cloud functions; the platform automatically scales based on load.
Footnotes
- The current runtime environment is CentOS 7.2. Note specifically that the code must not depend on a specific operating system or a specific OS version number. The runtime environment may change, so the code should be as platform-independent as possible.