# Something similar to Gigalixir's HOST\_INDEX

**URL:** <https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435>\
**Category:** Questions / Help\
**Tags:** distributed\
**Created:** [December 16, 2021, 3:02pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435 "2021-12-16T15:02:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartblast](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/bartblast/32/1912_2.png) [@bartblast](https://community.fly.io/u/bartblast)\
**Post date:** [December 16, 2021, 3:02pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435/1 "2021-12-16T15:02:47Z")

</div>

I’m generating Snowflake ID’s: [Snowflake ID - Wikipedia](https://en.wikipedia.org/wiki/Snowflake_ID) and I need an integer 1-1023 identifying the node. In Gigalixir I could use the HOST\_INDEX environment variable for this when starting a node, but for Fly I only found FLY\_ALLOC\_ID, which is a UUID. What can I do without resorting to some kind hashing of FLY\_ALLOC\_ID, is there an env var similar to HOST\_INDEX?

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [December 16, 2021, 3:05pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435/2 "2021-12-16T15:05:48Z")

</div>

`FLY_ALLOC_ID` is actually a hex, so you should be good if you just convert it to an int. Something like:

```auto
System.fetch_env("FLY_ALLOC_ID") |> Integer.parse(16)

```

---

<div class="post-metadata">

**Author:** ![bartblast](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/bartblast/32/1912_2.png) [@bartblast](https://community.fly.io/u/bartblast)\
**Post date:** [December 16, 2021, 3:11pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435/3 "2021-12-16T15:11:06Z")

</div>

> [@kurt](#):
>
> `System.fetch_env("FLY_ALLOC_ID") |> Integer.parse(16)`

Thanks @kurt, but as I said I don’t want to resort to hashing. Integer.parse will just convert the hex representation of the UUID into its representation in base 10, and then I would have to hash it. This means collisions when I have only 1023 possible values.

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [December 16, 2021, 3:19pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435/4 "2021-12-16T15:19:49Z")

</div>

Oh, I missed a piece of this. The first segment of the alloc ID is unique. So this should work without hashing, I think:

```auto
System.fetch_env("FLY_ALLOC_ID")
|> String.split("-")
|> List.first
|> Integer.parse(16)

```

The alloc id is the only thing in the environment you can really use to uniquely identify a VM.

---

<div class="post-metadata">

**Author:** ![bartblast](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/bartblast/32/1912_2.png) [@bartblast](https://community.fly.io/u/bartblast)\
**Post date:** [December 16, 2021, 3:24pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435/5 "2021-12-16T15:24:50Z")

</div>

Isn’t FLY\_ALLOC\_ID a UUID? If so, the first segment is not guaranteed to be unique. Besides, converting it to an integer would give 16^8 (4294967296) possible values, which again means collisions when hashing to a 10 bit values (1024 possible values).

---

<div class="post-metadata">

**Author:** ![jerome](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jerome/32/17_2.png) [@jerome](https://community.fly.io/u/jerome)\
**Post date:** [December 16, 2021, 3:39pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435/6 "2021-12-16T15:39:53Z")

</div>

The first segment on its own is not guaranteed to be unique, no. Internally we deal with the whole UUID to ensure uniqueness.

For users, the probability of the first segment colliding with another _for the same app_ is very unlikely. Still, it is not a guarantee.

Our next iteration of instances IDs are short and guaranteed to be unique. Unfortunately that’s not ready yet.

Does Gigalixir’s `HOST_INDEX` represent the server on which your app is running or a unique index representing the instance of your app?

---

<div class="post-metadata">

**Author:** ![bartblast](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/bartblast/32/1912_2.png) [@bartblast](https://community.fly.io/u/bartblast)\
**Post date:** [December 16, 2021, 3:44pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435/7 "2021-12-16T15:44:23Z")

</div>

@jerome HOST\_INDEX is a unique index representing the running instance.  
From the Gigalixir docs:

> HOST\_INDEX contains the index of the replica. The hostname for each replica is randomly generated which can be a problem for services like DataDog and NewRelic who charge by the host. We also keep a sort of ordered list of your replicas that you can use to report hostnames to keep your number of hosts low. Each replica currently running will have a different HOST\_INDEX, but once a replica is terminated, its HOST\_INDEX can be re-used in another replica.

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [December 16, 2021, 4:13pm UTC](https://community.fly.io/t/something-similar-to-gigalixirs-host-index/3435/8 "2021-12-16T16:13:08Z")

</div>

The only way I can really imagine getting reliable 0-1024 values is with DNS and a bunch of logic.

You can run the equivalent of `dig AAAA <app>.internal` to get a list of all IPs. That will return them in age order (somewhat reliably, not a guaranty). You could map those to an autoincrementing integer in a database, or do something clever in memory. The IPs will change on deploy, though, so be aware of that.

Also, as an aside, we do enforce uniques of the first segment of the alloc ID _while VMs are running_. They’re not unique forever, but they’re unique at any given point in time.
