Temporary Storage
Overview
Cloud Run service launches an instance upon receiving a request and runs the relevant code logic within the instance. Each instance is equipped with temporary storage space, which can be used to store intermediate data generated during request processing.
Key Features of Temporary Storage
- Easy to Use: Accessible via standard file system interfaces without additional configuration.
- Temporary: Data is only valid during the instance lifecycle.
- Instance Isolation: Temporary storage between different instances is independent of each other.
Application Scenarios
Temporary Storage
- File Upload Processing: Temporarily stores uploaded files, processes them, and then transfers them to persistent storage.
- Request-Level Caching: Stores intermediate results generated during the current request processing.
- Logging: Records business logs to facilitate issue troubleshooting.
- Temporary Compute Data: Stores intermediate results generated during computation.
Usage Limits and Considerations
Important Notice
- Data Non-Persistence: After instance reclamation or destruction, all data in temporary storage will be cleared.
- Memory Occupation: Temporary storage physically occupies the instance's memory space. Excessive usage may lead to insufficient memory.
- Performance Impact: Excessive or frequent file operations may degrade application performance.
Best Practices
- Use Space Judiciously: Store only essential temporary data and promptly clean up unneeded files.
- Control File Size: Avoid storing excessively large files to prevent OOM (Out Of Memory) errors
- Regular Transfer: For data requiring long-term retention, promptly transfer it to persistent storage.
- Monitor Memory Usage: Keep track of instance memory utilization to prevent memory pressure caused by temporary storage.
Code Example
// Node.js Example: Using Temporary Storage to Handle Uploaded Files
const fs = require('fs');
const path = require('path');
// Save uploaded files to temporary storage
app.post('/upload', (req, res) => {
const tempFilePath = path.join('/tmp', `upload-${Date.now()}.dat`);
// Save to temporary storage
fs.writeFileSync(tempFilePath, req.body.fileData);
// Processing file...
// Delete temporary files after processing
fs.unlinkSync(tempFilePath);
res.send('File processing completed');
});
Persistent Storage Solution
For data that needs to be persistently stored, it is recommended to use the following storage solutions:
- COS Object Storage mount: Suitable for file data that needs to be stored for a long time
- Database: Suitable for persistent storage of structured data