Skip to content

Cache function calls by default #7428

Closed

Description

@shykes

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:

  1. Functions that perform compute-intensive work themselves, via native code, are most affected: they are re-run each time, adding a major performance penalty.
  2. 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.
  3. 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:

  1. Functions are assumed cacheable by default
  2. Functions can be labeled as not cacheable
  3. 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 deploy

Lookup 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.json is 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

  1. shykes commented on May 21, 2024

    @shykes
    ContributorAuthor

    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...)

  2. sipsma commented on May 21, 2024

    @sipsma
    Contributor

    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:

    1. For the caching to work as expected for each caller (who may now all call the same function with different TTLs), I think we'd need to support per-client lock files (and some way of specifying which one to use)
      • on top of the previously discussed trickiness of lock files like pruning
    2. Figure out how to implement cases like "multiple calls in the same session that call the same function with different TTLs"
      • To be clear this is tricky but certainly solvable.

    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.

  3. shykes commented on May 21, 2024

    @shykes
    ContributorAuthor

    I am happy to cut the TTL feature altogether from the initial implementation. I prefer that to letting the caller callee define its own TTL.

  4. shykes commented on May 21, 2024

    @shykes
    ContributorAuthor

    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.

  5. sipsma commented on May 21, 2024

    @sipsma
    Contributor

    Yeah starting with no TTL is even simpler, so I'm good with starting there too.

    I am happy to cut the TTL feature altogether from the initial implementation. I prefer that to letting the caller define its own TTL.

    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.

  6. shykes commented on May 21, 2024

    @shykes
    ContributorAuthor

    Yeah starting with no TTL is even simpler, so I'm good with starting there too.

    🚀

    I am happy to cut the TTL feature altogether from the initial implementation. I prefer that to letting the caller define its own TTL.

    Did you mean you prefer that to letting the callee (as in the module author) define its own TTL?

    Yes, my bad - I corrected the typo.

    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.

    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 --ttl seems 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.

  7. sipsma commented on May 21, 2024

    @sipsma
    Contributor

    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.

    Sounds good!

    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.

    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.

  8. shykes commented on May 21, 2024

    @shykes
    ContributorAuthor

    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.

    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 for withExec for 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.

  9. sipsma commented on May 21, 2024

    @sipsma
    Contributor

    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 for withExec for example).

    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.

  10. shykes commented on May 21, 2024

    @shykes
    ContributorAuthor

    Yeah that makes sense.

  11. shykes commented on May 21, 2024

    @shykes
    ContributorAuthor

    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.

    Done ✅

  12. sipsma commented on Jul 9, 2024

    @sipsma
    Contributor

    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)

    • Just worth a read by whoever ends up implementing this, important context on handling cases around secrets especially
  13. pjmagee commented on Dec 19, 2024

    @pjmagee
    Contributor

    i've seem very often in various CLI tools a --no-cache arg, will this also be something that function developers, or users of the function could use in order to ensure no caching was done?

  14. jgsqware commented on Feb 21, 2025

    @jgsqware

    Hello, this would in fact be really help, any idea when it can be available?

  15. vmaffet-fast commented on Oct 1, 2025

    @vmaffet-fast

    👋 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 :(

  16. locked and limited conversation to collaborators on Mar 24, 2026
  17. converted this issue into a discussion #12243 on Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions