Skip to main content

Temporary Storage

In Genezio, a temporary storage partition is mounted to the execution environment by default. This storage can be used for caching data or storing temporary files during function execution. Below, you'll find details on configuring the storage size, its limitations, and considerations for usage.

Default Storage Size

By default, a partition of 128MB is mounted to the execution environment. This storage does not persist between function invocations. It is best suited for temporary operations such as caching files or intermediate data processing.

Writing and Reading Files Using Temporary Storage

Here is an example of how to write to and read from a file in the temporary storage using Node.js with Express. Files should be written in the /tmp directory:

import express from 'express';
import fs from 'fs';
import path from 'path';

const app = express();
const PORT = 3000;

app.use(express.json());

// Write data to a file
app.post('/write', (req, res) => {
const { filename, content } = req.body;
const filePath = path.join('/tmp', filename);

fs.writeFile(filePath, content, (err) => {
if (err) {
console.error('Error writing file:', err);
return res.status(500).send('Error writing file');
}
res.send(`File ${filename} written successfully!`);
});
});

// Read data from a file
app.get('/read/:filename', (req, res) => {
const { filename } = req.params;
const filePath = path.join('/tmp', filename);

fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return res.status(500).send('Error reading file');
}
res.send(data);
});
});

app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});

Customizing Storage Size

Here is an example configuration snippet to configure the storage size to 256MB:

genezio.yaml
backend:
functions:
- name: my-function
path: ./
handler: handler
entry: app.mjs
storageSize: 256 # in MB

You can configure the storage size in increments of 1MB up to a maximum of 128MB.

To increase the maximum value up to 512MB, you can upgrade to a Pro Subscription.

For storage requirements exceeding 512MB, please contact us.

Local Testing

To test your storage configurations locally, use genezio local to start the server execution locally. Ensure that your application behaves as expected with the specified storage size.

Accessing Bundled Local Files

If your source code includes local files, genezio will bundle these files during deployment unless they are explicitly ignored using a .genezioignore file. The source code and any additional files can be found at /tmp/package or process.cwd() within the execution environment.

These bundled files are accessible within the execution environment at the following path:

/tmp/package/<filepath>

For example, if you have a file named config.json in the same directory as your source code, it can be accessed in your function like this:

const configPath = '/tmp/package/config.json';
// or const configPath = path.join(process.cwd(), 'config.json');
fs.readFile(configPath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading config file:', err);
} else {
console.log('Config file contents:', data);
}
});

This allows you to include and use necessary local files without needing to manually upload or manage them separately.

Limitations and Considerations

  • Attempting to write to from directories outside of /tmp will result in a read-only file system error.
  • Ephemeral Storage: The storage is not persistent across requests. It is not suitable for stateful operations or storing data that needs to be retained beyond a single function execution.
  • Impact on Cold Starts: Increasing the storage size may result in longer cold start times for your functions.
  • Best Practices: Use this storage primarily for temporary and disposable data, such as: caching results from external APIs or storing intermediate data.
  • For long-term storage, consider using a dedicated database or file storage service.

Troubleshooting

Error: EROFS: read-only file system

Error writing file: [Error: EROFS: read-only file system, open 'file.txt'] { errno: -30, code: 'EROFS', syscall: 'open', path: 'file.txt' }

This error occurs when attempting to write to a directory outside of /tmp. Ensure that you are writing to the correct path to avoid this error.

Support

If you have any questions or need help, please reach out to us on Discord.