<p>So I want to use Cachex to share temporary data across the cluster. Cachex takes a <code>:nodes</code> list in the config file. That worked great in testing, as I could just hard code <code>[:"a@My-Laptop", :"b@My-Laptop"]</code> and it worked, but with Kubernetes the names are dynamic. How would I set these?</p>

<p>I looked at this for my day job a while back and Cachex doesn't have a solution for dynamic node membership. It expects you to have all of your node names predefined and static. You can do this in Kubernetes with stateful sets, but you'll throw away all future possibility of autoscaling if you want to keep your cache distributed across all nodes.</p>

<p>A better solution IMO is <a href="https://hexdocs.pm/nebulex/Nebulex.html" rel="noreferrer">Nebulex</a>. Nebulex is not concerned about managing the connection to your nodes or their membership. It is only concerned with keeping a cache distributed across <code>[Node.self() | Node.list()]</code> (all of the nodes in your cluster). It all runs over PG2 which is a distributed pubsub implementation. It has the ability to transfer data as nodes join or leave and will shard it so that you have duplicate data across your nodes. It even allows you to match on node names to store your cached data.</p>

<p>To manage connecting your nodes to each other in a dynamic way, there's a library called <a href="https://hexdocs.pm/libcluster/readme.html" rel="noreferrer">libcluster</a> that actually has Kubernetes support built in. Libcluster will either use Kubernetes DNS or you can create a <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="noreferrer">headless service</a> that will serve the IP addresses of all your elixir pods. Libcluster will ping this service on a polling interval and try to join the erlang cluster.</p>

<p>When you have both set up, libcluster will manage keeping your nodes in a cluster and Nebulex will manage keeping your data synced across all of them. It's pretty cool and we haven't had any problems with it yet.</p>

<p>I'm not going to write any source here because the documentation for all of these things is really well written. Let me know if you have any questions though.</p>
