Skip to content

Commit cddb148

Browse files
committed
add caching explanation
1 parent f027b26 commit cddb148

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

template/how-it-works.mdx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,23 @@ We call this sandbox snapshot a _sandbox template_.
2525
</Note>
2626

2727
## Caching
28-
TODO
28+
The caching concept is similar to [Docker's layer caching](https://docs.docker.com/build/cache/). For each layer command (.copy(), .runCmd(), .setEnvs(), etc.), we create a new layer on top of the existing.
29+
Each layer is cached based on the command and its inputs (e.g., files copied, command executed, environment variables set).
30+
If a layer command is unchanged and its inputs are the same as in any previous build, we reuse the cached layer instead of rebuilding it.
31+
32+
This significantly speeds up the build process, especially for large templates with many layers.
33+
The cache is scoped to the team, so even if you have multiple templates, they can share the same cache if they have identical layers.
34+
35+
### Files Caching
36+
When using the `.copy()` command, we cache the files based on their content. If the files haven't changed since the last build, we reuse them from the files cache.
37+
38+
We differ from Docker here. Because we build the template on our infrastructure, we use improved caching on files level.
39+
Even if you invalidate the layer before `.copy()` (e.g., by changing ENV variables), we'll reuse the already uploaded files.
40+
The `copy()` command will still be re-executed, but the files for the layer will be reused from the files cache, no need to upload them from your computer again.
41+
42+
### Use Case for Caching
43+
You can leverage caching to create templates with multiple variants (e.g., different RAM or CPU) while reusing the common layers.
44+
When building the template, just change the template alias to a specific RAM/CPU configuration (e.g., `my-template-2cpu-2gb`, `my-template-1cpu-4gb`), keep the rest of the template definition the same, and the build process will reuse the cached layers.
45+
46+
### Optimize Build Times
47+
To optimize build times, place frequently changing commands (e.g., copying source code) towards the end of your template definition. This way, earlier layers can be cached and reused more often.

0 commit comments

Comments
 (0)