team-telnyx / etcdex

Etcd client in Elixir
Apache License 2.0
9 stars 0 forks source link

How to use this library? #6

Closed tiagodavi closed 1 week ago

tiagodavi commented 1 week ago

Thank you for creating this!

I want to do a simple thing:

1 - Run multiple elixir nodes with libcluster 2 - Each new node will be added to etcd add_member I guess. 3 - Get the leader 4 - If someone dies, someone else will be the new leader.

I have everything up and running, but when I call add_member:

etcdserver: Peer URLs already exists

balena commented 1 week ago

Hi @tiagodavi,

So 1) will require you to discover what's a better fit for the problem you want to solve, as there are different strategies to setup your cluster, from gossip and Kubernetes to Consul.

I'm not sure what you mean by 2). So EtcdEx is an etcd client. Adding itself as a node means the local app has to be member of the cluster, which is not what this library provides.

The etcd protocol allows you to find the leader as you want in 3), but then resiliency is provided by the etcd cluster configuration. With 3 nodes, up to 1 can be down to maintain the cluster fully functional, with 5, up to 2, etc (majority rule).

Regards, Guilherme.

tiagodavi commented 1 week ago

Hi Balena, thank you for your support.

I would like to run multiple phoenix apps locally (initially), each one of them would be part of the cluster, one is a leader, when any node dies it leaves the cluster until it comes back again, only one can be the leader.

I have a phoenix app with this library installed and etcd running separately from it. I though I could use this library to call add_member every time I run one app locally.

Does that make more sense?

tiagodavi commented 1 week ago

Since I want to solve leader election and don't understand etcd completely and I think I am missing something here.

But I am curious to understand what's the purpose of add_member function and why I can't pass a name as argument, I noticed etcd accepts name as argument.

I basically want to first simulate a cluster locally with leader election and then move it to a real cluster in the future.

balena commented 1 week ago

You have a very powerful set of tooling at your hands, but I believe they won't fit your exact needs, or not in the exact intended order.

So add_member can be used to add etcd members to an existing cluster, as in How to Add and Remove Members. Think of etcd as primarily a key-value data store, not a cluster orchestrator. Leadership is for internal use only.

OTP already comes with the very powerful library net_kernel, which permits connecting EVMs and allow them to exchange messages and run remote procedures. Then libcluster can be used to automatically connect nodes, using different strategies: gossip, DNS, etc.

Then for leader election you may use raft indeed, however it may get complex very easily. If all you want to do is to pick up a single node from the cluster, take a look at consistent hashing; if each node of your cluster eventually has the same view of the members, then if all of them perform the exact same hash, they all end up picking up the same node. This approach isn't atomic, and there are chances of multiple leaders, but maybe this is tolerable for the problem you want to solve.

tiagodavi commented 1 week ago

Hi Balena, yes, I ended up solving it with Raft.

Thank you!!