Static fallback

Static fallback

Static fallback is a feature that ties directly into our philosophy on prioritizing safety and developer experience.

Lekkos and code transformation

Lekkos are just regular functions that sit in your codebase. The difference is that at runtime, they can be connected to Lekko’s services and be dynamically updated.

Example lekko
// Whether to use experimental features or not
func getExperimentalEnabled(env string) bool {
  if env == "staging" {
    return true
  }
  return false
}

This is done via code transformation - Lekko provides code parsing and generation tools that take these functions and transform them to code that connect to Lekko. You can read more on these tools in our SDK pages.

Example transformed code
// Whether to use experimental features or not
func (c *LekkoClient) GetExperimentalEnabled(env string) bool {
  ctx := context.Background()
  ctx = client.Add(ctx, "env", env)
  // evaluate locally using dynamic data from Lekko backend
  result, err := c.GetBool(ctx, "default", "experimental-enabled")
  if err == nil {
    return result
  }
  // something is wrong or API key is not set,
  // use static lekko function directly as a fallback
  return getExperimentalEnabled(env, feature, owner, repo)
}

Code transformation is meant for when your project is deployed. During development, you don't need your code to be connected to Lekko - that would introduce an external dependency and the behavior of your app might even change if other people are making lekko changes remotely.

What is a static fallback?

As you can see in the highlighted lines of the above examples, static fallbacks refer to the original lekko code that is used as the return value when an error occurs while trying to reach Lekko or evaluate values.

Because code transformation happens at build time, static fallbacks are also embedded at build time, hence the name static. This means that they're guaranteed to present at runtime, and as long as your code compiles, static fallbacks never fail. This provides a couple of key benefits.

Benefits

Built-in error handling

Static fallbacks act as built-in error handling. Normally, when using external services, you need to check for errors and handle them by providing some default behavior or bubbling them up to be handled elsewhere.

With Lekko, you don't need to worry about any of this. With static fallbacks, your callsites are always safe and thus much cleaner.

- value, err := lekko.GetBool("example", "feature-flag")
- if err != nil {
-   // Need to do something to handle error...
- }
+ value := lekko.Example.GetFeatureFlag()

No hard dependency on Lekko

Because static fallbacks are regular functions that are compiled into your code, they do not introduce any external runtime dependencies. This means that even if Lekko’s services are unreachable or AWS’s data centers burn down simultaneously, your projects will still have their configuration values available (unless, of course, your project runs on AWS!).