How can I make sure the high availability when I use the go client for dgraph?

Hi,
when I use the go client to query and mutate , I have a question about the high availability, the code for example as below:

func NewGrapher(cfg *GrapherConfig) (*BTCGrapher, error) {
	p := new(BTCGrapher)
	p.cfg = cfg
	conn, err := grpc.Dial(p.cfg.Address+":"+p.cfg.Port, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(4<<30),
		grpc.MaxCallSendMsgSize(4<<30)), grpc.WithInsecure())
	if err != nil {
		log.Error("While trying to dial gRPC", err)
		log.DetailError(err)
		return nil, err
	}
	p.grapherClientConn = conn
    ctx := context.Background()
    mu := &api.Mutation{
			CommitNow:  true,
			DeleteJson: pb,
	}

	_, err = dgo.NewDgraphClient(api.NewDgraphClient(p.grapherClientConn)).NewTxn().Mutate(ctx, mu)
		if err != nil {
			log.DetailError(err)
		}
	return p, nil
}

I have three alpha nodes and I have a domain like “test.dgraph.xx.com” point to the three alpha port 9080, I do not clear how can I write codes to relize the high availability like the method GET or POST in HTTP.

Thank You Very Much!

like that ?

func NewGrapher(cfg *GrapherConfig) (*BTCGrapher, error) {
	conn1, err := grpc.Dial(p.cfg.Address1+":"+p.cfg.Port1, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(4<<30),
		grpc.MaxCallSendMsgSize(4<<30)), grpc.WithInsecure())
	conn2, err := grpc.Dial(p.cfg.Address2+":"+p.cfg.Port2, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(4<<30),
		grpc.MaxCallSendMsgSize(4<<30)), grpc.WithInsecure())
	conn3, err := grpc.Dial(p.cfg.Address3+":"+p.cfg.Port3, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(4<<30),
		grpc.MaxCallSendMsgSize(4<<30)), grpc.WithInsecure())
	ctx := context.Background()

	mu := &api.Mutation{
		CommitNow:  true,
		DeleteJson: pb,
	}

	_, err = dgo.NewDgraphClient(api.NewDgraphClient(conn1), api.NewDgraphClient(conn2), api.NewDgraphClient(conn3)).NewTxn().Mutate(ctx, mu)
	if err != nil {
		log.DetailError(err)
	}
	return p, nil
}

but, how can I know which alpha node is alive? If I kill one alpha node , how can I know it?

The recommended way to ensure high availability is to run Dgraph behind a load balancer which would be responsible for keeping track of which nodes are alive.

The sample code

_, err = dgo.NewDgraphClient(api.NewDgraphClient(conn1), api.NewDgraphClient(conn2), api.NewDgraphClient(conn3)).NewTxn().Mutate(ctx, mu)
	if err != nil {
		log.DetailError(err)
	}

will not work as you intend. When a DgraphClient is initialized with multiple connections, each operation uses only one of them at random. You have to check the error and retry if it the node is down.

Thanks for your reply, I understand your mean, I know I need a load balancer, but I do not know how to client an domain in grpc,can you show me an example code? like that?

conn, err := grpc.Dial("dns:///"+ p.cfg.Domain+":"+p.cfg.Port, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(4<<30),
		grpc.MaxCallSendMsgSize(4<<30)), grpc.WithInsecure())

Thank you!

The grpc.Dial address can be a DNS name.

Hi dmai, thanks for your reply, do you have time to look at my another question?

Thanks very much! I have solved the problem, the load balancer I did before is L7 load balancer, now I relize the load balancer in L4. Now it can work well with domain test.dgraph.xx.com in grpc.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.