JUNE 2 2025
Exploring namespaces in Dgraph: organizing data with precision
How to use graph namespaces to build multi-tenant applications

The core strength of graph databases is their ability to link data, uncovering insights and revealing meaningful relationships. However, there are scenarios where you might not want all your data interconnected. Whether it's for multi-tenancy, managing different domains, or ensuring data isolation, Dgraph v25 provides the flexibility to organize your data effectively using namespaces.
The power of namespaces
Namespaces in Dgraph represent a significant evolution from traditional multi-tenancy approaches. They offer a standard method for creating isolated data domains, each functioning as a fully scoped graph environment. This means each namespace comes with its own schema and data, allowing for independent provisioning and querying. This capability is particularly beneficial for businesses operating across various domains or serving multiple customers, as it simplifies data management without the overhead of maintaining separate databases for each entity.
Key features
- Isolation and independence: Each namespace operates independently, ensuring that data and schemas don't interfere with one another. This isolation is crucial for maintaining data integrity and security, especially in multi-tenant environments.
- Simplified management: With namespaces, managing different data domains becomes more straightforward. You can create, drop, rename, and list namespaces as needed, providing a flexible approach to data organization.
- Enhanced querying: Namespaces allow for targeted querying within specific data domains. This precision enhances performance and ensures that queries return relevant results without sifting through unrelated data.
- Scalability: By logically separating data, namespaces enable scalable solutions that can grow with your business needs. Whether you're expanding into new markets, onboarding additional customers, or deploying AI agents, namespaces provide the structure to support your growth.
Use cases for namespaces
- Multi-tenancy: Ideal for SaaS providers who need to manage data for multiple clients separately, ensuring privacy and data isolation.
- Domain separation: Useful for organizations that operate in different domains or regions, allowing them to maintain separate data environments tailored to specific operational needs.
- AI agents and memory: Each AI agent can have its own namespace to store specific knowledge and memory. This separation allows agents to operate independently, accessing only the data relevant to their tasks, which enhances efficiency and reduces complexity in managing AI-driven processes.
- New market expansion: Facilitates the entry into new markets by creating isolated environments for market-specific data, enabling targeted strategies and localized operations.
Getting started
Dgraph's v25 release introduces namespaces as part of its v2 APIs, making it easier than ever to implement isolated data environments. Here’s a quick overview of how you can start using namespaces:
Provision a Dgraph instance
Deploy to Hypermode and simply create a Graph or Spin up a local instance.
Connect
Use the Dgraph connection string to connect to your instance.
client, err := dgo.Open("dgraph://<graph-workspace>.hypermode.host?sslmode=verify-ca&bearertoken=<bearer-token>")
// handle error
defer client.Close()
Creating a namespace
Use the CreateNamespace
function to establish a new namespace. This
function allows you to define a unique environment for your data.
// Example of creating a namespace
err := client.CreateNamespace("new_namespace")
if err != nil {
log.Fatal(err)
}
Managing namespaces
Functions like DropNamespace
, RenameNamespace
, and
ListNamespaces
provide the tools you need to manage your namespaces
effectively.
// Example of dropping a namespace
err := client.DropNamespace("old_namespace")
if err != nil {
log.Fatal(err)
}
// Example of renaming a namespace
err := client.RenameNamespace("old_name", "new_name")
if err != nil {
log.Fatal(err)
}
// Example of listing all namespaces
namespaces, err := client.ListNamespaces()
if err != nil {
log.Fatal(err)
}
for _, ns := range namespaces {
fmt.Println(ns)
}
Querying data
With namespaces, you can pass the namespace name to functions like
SetSchema
and RunDQL
, ensuring that your operations are confined to
the intended data domain.
// Example of setting a schema in a namespace
schema := `name: string @index(exact) .`
err := client.SetSchema("target_namespace", schema)
if err != nil {
log.Fatal(err)
}
// Example of running a query in a namespace
query := `{
users(func: has(name)) {
uid
name
}
}`
response, err := client.RunDQL(context.TODO(), "target_namespace", query)
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
Conclusion
By leveraging namespaces, you can ensure that your data is not only connected but also strategically isolated where necessary, providing the best of both worlds in your data infrastructure. Dgraph's namespace feature offers the flexibility to structure your graph database in a way that best suits your needs
For more detailed information on namespaces and how to implement them in your Dgraph setup, refer to the official documentation.