Repository navigation
Description
Problem
Dagger has great caching, but Dagger Functions don't fully benefit from it, because their runtime containers are not cached.
This has several consequences:
- Functions that perform compute-intensive work themselves, via native code, are most affected: they are re-run each time, adding a major performance penalty.
- Functions that are not compute-intensive (the majority) are less affected, but still pay a small performance tax at each execution, because of the overhead of executing the runtime container each time. For complex pipelines this can add up.
- Module initialization is also slower, because SDKs themselves are modules and calling the initialization functions incurs the same performance tax.
The result is that most dagger call commands are noticeably slower than they would be, because of insufficient caching.
Solution
Cache function calls by default!
That would be trivial to do, there's just one problem...
Cache invalidation
Sometimes we want a function call to not be cached, for a variety of reasons:
- Perhaps the function is not "pure" and will not work properly if cached
- Perhaps the function can be cached, but the operator wants to control when to cache it and for how long]
- Perhaps the function requires custom caching behavior - for example a dependency lookup might be useful to persist in a lockfile accessible to the user
In order to cache function calls by default, we need a minimal design to allow disabling that caching. That design should 1) be as simple as possible so we can ship fast - but 2) be future-proof so that we don't need to break it later.
Proposed Design
See this discussion thread for the discussion that led to this proposal.
The design in a nutshell:
- Functions are assumed cacheable by default
- Functions can be labeled as not cacheable
- Future expansions of the design: Cache TTL, lookup functions, more detailed labels
Functions are assumed cacheable by default
If not labeled otherwise, a function is assumed to be cacheable. A cacheable function is not guaranteed to be cached - it simply means that it might be cached.
In practice, this means all function runtimes will be cached by default - eliminating the performance tax.
Functions can be labeled as not cacheable
If a function should not be cached by the engine, the developer can label it as not cacheable.
For example in Go:
// Say hello without every being cached
// +cache=false
func Hello() string {
return "world"
}Future expansions of the design
Additional features are already being discussed, and can be added to the design later. The goal is to fast-track the initial design, while trying to be forward-compatible.
Cache TTL
In this expansion, the TTL of a cacheable function can be configured.
The TTL could be configured callee-side, or caller-side, or both. That point is still in discussion.
Code example (caller-side):
ctr := dag.
WithCacheTTL(10 * time.Second).
Container().
From("my-app-registry:latest")Code example (callee-side):
// +ttl=10s
func Foo() {
...
}CLI example:
dagger call --cache-ttl=10s deployLookup functions
In this expansion, functions can be labeled as lookup functions.
A lookup function is a function that queries an external system, and returns the result for use by other functions. This allows modeling an external dependency in the DAG.
Lookup functions are cached in a special way:
- A special area of the caller's
dagger.jsonis used a general-purpose lock file - The lock file is used as a lookup cache
- The contents of the lock file are visible to the caller (like a regular lock file) and can be committed to source control
- The engine can update the lockfile (UX tbd, perhaps
dagger update) by calling all previously cached lookup functions, and updating the cache with the latest result. - The default TTL of the lock file is TBD. It could be infinite (never expire unless manually updated) or a reasonably high number (1h?) similar to DNS.
- Lookup functions must not have side-effects: the engine may call them any number of times
Open questions:
- Should there be restrictions to the arguments and return values?
- How to avoid abusing lookup functions - ie having functions incorrectly labeled as "lookup"?
More detailed labels
There are different reasons for making a function non-cacheable. It would be interesting to add more labels, to allow developers to specify why their function is non-cacheable. This could perhaps unlock more advanced engine optimizations in the future.
As discussed with @vito and @sagikazarmark
Activity
shykes commentedon May 21, 2024
FYI @sipsma @vito here is a penciled in proposal based on the discord discussion. I kept the word "pure" out of it, because I think it might more loaded than we originally thought (ie. from a functional programming purist's perspective, none of these cacheable functions qualify as "pure". Perhaps best not to kick the hornet nest...)
sipsma commentedon May 21, 2024
In the interest of being able to ship something quickly and then iterate, I think it's worth considering starting with TTL settings as being defined as properties of the function being invoked (i.e. by the author, rather than the caller).
Starting there is relatively straightforward implementation-wise. You could do it without having to go all the way into lock files or anything like that; could just re-use existing cache mechanisms (just need to scope cache to a time interval in the same way you already can do that in "user-space", just automatically now).
Supporting the caller setting the TTL opens up a pandora's box:
Starting out with the TTL being a static property of a function would avoid all that and thus be way quicker to get going.
To be clear, I'm not discounting the value of client-side settings for this, this is more just about what order we stage the features out.
shykes commentedon May 21, 2024
I am happy to cut the TTL feature altogether from the initial implementation. I prefer that to letting the
callercallee define its own TTL.shykes commentedon May 21, 2024
I'm also proposing to leave lock files out of this proposal as well. They would be added later as part of "lookup functions" or some other expansion of the design.
sipsma commentedon May 21, 2024
Yeah starting with no TTL is even simpler, so I'm good with starting there too.
Did you mean you prefer that to letting the callee (as in the module author) define its own TTL? If so I'd disagree since putting the onus always on the caller is going to lead to a lot of repetitive code (a given version of a function often gets called a lot of places, whereas its only authored once) and a lot of situations in which the caller doesn't know what a good TTL is, but the author does. I think a model where authors can define a default TTL if they want and then callers can override it if they want would be the most usable.
shykes commentedon May 21, 2024
🚀
Yes, my bad - I corrected the typo.
Well, the person who knows best what TTL to set, and will most often feel the need to control it, is the end-user. So the most important affordance for setting TTL is actually in the CLI:
dagger call --ttlseems like the simplest implementation.From there, allowing a caller to configure TTL in code (the part you object to) is not really a must-have, but felt like a natural continuity, because the pattern already exists in the wild, with exec cache-busters. So this would be a more general, cleaner version of the cache-buster pattern.
When we discussed it with @sagikazarmark and @vito , we couldn't find good examples of a function developer knowing in advance what TTL to set for their function.
Since we don't yet have consensus on how to handle TTLs, all the more reason to cut it from the first phase of implementation :) I will update the proposal to clarify that.
sipsma commentedon May 21, 2024
Sounds good!
Not relevant in the short term anymore, but for the record in the longer term, here's a generic example:
I'm retrieving some remote resource (downloading a file, whatever), but doing so is an expensive operation and/or want to try to avoid rate limiting. The module author knows the most about the remote resource, so they would be best fit to decide the default TTL in this case. Callers can override if they want but if they are not familiar with the inner details of what they are retrieving (i.e. they are on a different team and just calling another team's module), then they don't have to care.
Basically, one of the fundamental benefits of modules is that it lets experts in one area encode their knowledge into an easily reusable abstraction, which I think would entail cache TTLs for use cases like the above.
shykes commentedon May 21, 2024
But couldn't this example be implemented by the function author setting the TTL to the underlying IO operation? Be it
Container.withExec,http(),git()and so on. That way, the same model would work even for low-level functions - where the callee definitely can't guess of a good TTL (what's the right TTL forwithExecfor example).I think we still achieve the desired outcome (experts can encode their knowledge in an easily reusable abstraction) but in a more consistent and easy-to-explain way.
sipsma commentedon May 21, 2024
Not if they are doing the IO directly in their code (i.e. using some third party SDK or using some protocol we don't support in the core API).
EDIT: or if the overall function is a composition of multiple calls and direct code but should be TTL'd as a whole rather than by parts.
shykes commentedon May 21, 2024
Yeah that makes sense.
shykes commentedon May 21, 2024
Done ✅
sipsma commentedon Jul 9, 2024
I surprisingly ended up needing to do some anticipatory handling of cases that will become possible once we enable this as part of the secret/socket isolation: #7804 (comment)
pjmagee commentedon Dec 19, 2024
i've seem very often in various CLI tools a
--no-cachearg, will this also be something that function developers, or users of the function could use in order to ensure no caching was done?jgsqware commentedon Feb 21, 2025
Hello, this would in fact be really help, any idea when it can be available?
vmaffet-fast commentedon Oct 1, 2025
👋 Being able to set caching TTL on all objects would be very nice. For instance, we have a module that generates GitHub App installation tokens, which are valid for 1h. Currently, if your pipeline takes longer than one hour, the token won't be regenerated because the function call was cached. Hence, we have to resort to adding optional cache-busting parameters :(