What is the best practice to make use of relationships?

Hi, I am beginning to explore graph database and I am learning how to use Dgraph database. I wanted to play around so I tried to build a schema as follows

Datacenter → Servers → IpAddress → IpAddressPorts → Service

Each Datacenter can have many Servers
Each Server can have many IpAddresses
Each IpAddress can have many Ports
Each Port has a Service

I came up with the following schema


<HasServers>: [uid] @reverse .
<HasIpAddresses>: [uid] @reverse .
<HasIpPortNumbers>: [uid] @reverse .
<HasService>: uid @reverse .

<IpAddress>: string @index(exact) .
<Name>: string @index(exact) .
<PortNumber>: int .


type <Datacenter> {
	Name
	HasServers
}
type <Server> {
	Name
	HasIpAddresses
}
type <IpAddress> {
	IpAddress
	HasIpPortNumbers
}
type <IpPortNumber> {
	PortNumber
	HasService
}
type <DgraphDatabaseService> {
	Name
}

now to get the services of a Datacenter i can use this


servicesUnderDatacenter (func: type(Datacenter)){  
    uid
    Name
    HasServers{
    	uid
      Name
      HasIpAddresses{
        uid
        IpAddress
        HasIpPortNumbers{
          uid
          PortNumber
          HasService{
            uid
            Name
          }
        }
      }
    }
  }

to get the Datacenter of a Service i can use this

 datacenterOfService(func: type(DgraphDatabaseService)){  
    uid
    Name
  	~HasService {
  		uid
  		PortNumber
      ~HasIpPortNumbers {
        uid
        IpAddress
        ~HasIpAddresses {
          uid
          Name
          ~HasServers {
            uid
            Name
          }
        }
      }
		}
  }

So to go from Datacenter to service i got to traverse all the paths down and to get the Datacenter i traverse up.

The question is, is it the correct way to create the design of the relationships or should i include the “Datacenter” field in the “Service” Type so i have direct access to Datacenter?

Same to know the IpAddress and Port of the Service, should i include in the DgraphDatabaseService (type) the predicates UseIpAddress and UsePortNumber so i can get them more easily?

Am i making use of the “reverse” feature too much?

What is the best practice here?

You could use Recurse Query.

It is fine. If you add a “shortcut” edge, you would have to deal with future changes by yourself. It is fine to rely on just edges.

no, it is also fine.

As far I can see you are doing good from what you have shared.

Cheers.

1 Like

Amazing, thanks for the reply, as far as I researched I found that its not much of concern about performance when traversing through Types for Dgraph but I still was concerned about structure and about the “reverse” feature hitting performance.

Thanks again

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