# Need help with an example of an optional bind

**URL:** <https://discourse.chef.io/t/need-help-with-an-example-of-an-optional-bind/19292>\
**Category:** Habitat Help\
**Created:** [April 4, 2018, 3:26am UTC](https://discourse.chef.io/t/need-help-with-an-example-of-an-optional-bind/19292 "2018-04-04T03:26:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tashadrew](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@tashadrew](https://discourse.chef.io/u/tashadrew)\
**Post date:** [April 4, 2018, 3:26am UTC](https://discourse.chef.io/t/need-help-with-an-example-of-an-optional-bind/19292/1 "2018-04-04T03:26:43Z")

</div>

Cross posted from a github request from @gustovd [https://github.com/habitat-sh/habitat/issues/4855](https://github.com/habitat-sh/habitat/issues/4855)

Found in the documentation ([https://www.habitat.sh/docs/developing-packages/#pkg-binds](https://www.habitat.sh/docs/developing-packages/#pkg-binds))

“”"  
Service group A can optionally bind to “X” or “Y”. If “X” is present, we operate this way, if “Y” is present, we operate this way. The service will start and may fail because “X” and/or “Y” my not be present at start, but it will eventually start. Real world example: An application service group that could use a Redis backend or a PostgreSQL backend, depending on how you are deploying it in different scenarios.  
“”"

Could you show an example of how this works?

---

<div class="post-metadata">

**Author:** ![christophermaier\_1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/christophermaier_1/32/2828_2.png) [@christophermaier\_1](https://discourse.chef.io/u/christophermaier_1)\
**Post date:** [April 4, 2018, 3:03pm UTC](https://discourse.chef.io/t/need-help-with-an-example-of-an-optional-bind/19292/2 "2018-04-04T15:03:35Z")

</div>

Continuing with the example of an application that can either use a Redis or PostgreSQL backend, you could have a `plan.sh` that includes something like this:

```auto
pkg_binds_optional=(
    [redis]="...",
    [database]="..."
)

```

(The `"..."` serve as placeholders for whatever exported data you would need for those binds… they could be as simple as `"port"`, but it depends on the specific things you’re wanting to bind to.)

If you want to start your application with the Redis backend, you would just bind that to a service group, like so:

```auto
hab svc load my/awesome-app --bind=redis:redis.default

```

(assuming you have a `redis.default` service group in your environment)

This will ensure that `my/awesome-app` will not start until members of the `redis.default` group are present; once they are, their exported data are available to use in the templates of the application, which would be written such that they would “do the right thing”, depending on which bind was present. That might look something like this:

```auto
{{ #if bind.redis }}
... do redis stuff
{{ /if }}

{{ #if bind.database }}
... do database stuff instead
{{ /if }}

```

In another deployment scenario, you could deploy your service using the database backend instead. That would look like this:

```auto
hab svc load my/awesome-app --bind=database:postgresql.default

```

Similarly, this will block startup of `my/awesome-app` until members of the `postgresql.default` group are present.

It should be noted that the use of “optional” here means that you can choose to bind to something or not, but your application will henceforth _require_ them to be present in order to start. I’m currently working on a refinement to our binding logic that will make it so binds can be present or not, and your application will still be able to run… this is how properly designed distributed services should behave anyway. This will allow your application to degrade gracefully if one of its dependencies goes away.

---

<div class="post-metadata">

**Author:** ![guskovd](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/guskovd/32/2992_2.png) [@guskovd](https://discourse.chef.io/u/guskovd)\
**Post date:** [April 4, 2018, 3:29pm UTC](https://discourse.chef.io/t/need-help-with-an-example-of-an-optional-bind/19292/3 "2018-04-04T15:29:37Z")

</div>

Thank you for the clarification!
