Tekko

Language

Get in Touch →

Usually respond within 24 hours

Back to BlogArchitecture

Scaling Permissions: Implementing ReBAC with OpenFGA and Zanzibar

7 min read
ReBACOpenFGAZanzibarMicroservicesAuthorization
Scaling Permissions: Implementing ReBAC with OpenFGA and Zanzibar

As applications grow from simple MVPs to complex enterprise ecosystems, the logic governing 'who can access what' inevitably becomes a bottleneck. Most of us start with Role-Based Access Control (RBAC). It’s simple: a user is an Admin or a Member. But then the product manager arrives with a new requirement: 'Users should be able to share individual folders, and anyone with access to a folder should see all its documents, unless the document is specifically restricted.'

Suddenly, your if (user.role === 'admin') checks are replaced by recursive database queries and spaghetti code scattered across multiple microservices. This is where Relationship-Based Access Control (ReBAC) and the Zanzibar model come into play.

In this article, we will explore how to implement ReBAC using OpenFGA, an open-source implementation of Google’s Zanzibar paper, to build an authorization layer that scales to billions of objects and millions of users.

The Limitations of RBAC and ABAC

Traditional Role-Based Access Control (RBAC) works well when permissions are coarse-grained. If you are an 'Editor,' you can edit all articles. However, RBAC struggles with resource-level permissions. To handle 'User A can edit Document B,' you end up creating artificial roles like Document_B_Editor, which leads to role explosion.

Attribute-Based Access Control (ABAC) attempts to solve this by using attributes (e.g., user.department === document.department). While flexible, ABAC is notoriously difficult to audit. It’s hard to answer the question: 'What all does User A have access to?' without scanning every resource in the system.

ReBAC shifts the focus. Instead of looking at roles or attributes, it looks at relationships. If User A is an owner of Folder B, and Document C is a child of Folder B, then User A has a relationship with Document C. This graph-based approach is how modern giants like Google, Slack, and Airbnb handle permissions at scale.

Understanding the Zanzibar Model

In 2019, Google published the Zanzibar whitepaper, describing the system they use to power authorization for Drive, YouTube, and Cloud. Zanzibar's genius lies in its simplicity and its unified data model: the Relationship Tuple.

A relationship tuple is a statement of fact: {user} has {relation} to {object}

For example:

  • (user:alice, viewer, doc:readme.md)
  • (folder:engineering, parent, doc:readme.md)

By chaining these tuples together, Zanzibar can resolve complex permissions by traversing a graph. If the system knows Alice is a member of the engineering team, and the engineering team is a viewer of the folder, it can infer that Alice can view the document.

Enter OpenFGA

OpenFGA (Fine-Grained Authorization) is a CNCF Sandbox project that implements the Zanzibar model while making it developer-friendly. Unlike the original Zanzibar, which relies heavily on Google's internal Spanner database, OpenFGA is designed to be cloud-native and supports multiple storage backends like PostgreSQL and MySQL.

OpenFGA introduces a readable Domain Specific Language (DSL) to define your authorization model. Instead of writing complex SQL joins, you define your types and relations in a configuration file.

Defining the Schema

Let's look at a real-world example for a SaaS document management platform. We have users, groups, folders, and documents.

type user type group relations define member: [user] type folder relations define viewer: [user, group#member] define owner: [user] type document relations define parent: [folder] define viewer: [user, group#member] or viewer from parent define editor: [user] or owner from parent

In this schema:

  1. A document can have a parent folder.
  2. A viewer of a document is either someone explicitly granted access OR anyone who is a viewer of the parent folder (inheritance).
  3. An editor is someone explicitly granted access OR the owner of the parent folder.

This model is declarative. You don't write the logic for the inheritance; OpenFGA’s engine handles the graph traversal for you.

Integrating OpenFGA into Your Architecture

One of the biggest mistakes teams make is trying to store authorization data inside their primary application database. This leads to "Authorization Spaghetti," where every service needs to know the schema of every other service to check permissions.

OpenFGA acts as a centralized Authorization-as-a-Service. Here is the typical workflow:

1. The Write Path

Whenever a relationship changes in your application (e.g., a user is added to a group, a file is moved to a folder), you send a Write request to OpenFGA to update the tuples.

// Example: Adding a user to a group await fgaClient.write({ writes: [{ user: 'user:8721', relation: 'member', object: 'group:engineering' }] });

2. The Check Path

When a user attempts an action, your API gateway or microservice sends a Check request to OpenFGA.

// Example: Can Alice view the internal roadmap? const { allowed } = await fgaClient.check({ user: 'user:alice', relation: 'viewer', object: 'doc:internal-roadmap' }); if (!allowed) throw new ForbiddenError();

Solving the Performance Challenge

Centralizing authorization introduces a network hop for every request. If your API receives 10,000 requests per second, OpenFGA must handle 10,000 checks. Zanzibar (and OpenFGA) solves this through two primary mechanisms:

1. Intelligent Caching and Sub-problem Redundancy

Many authorization checks share sub-problems. If 1,000 users are checking access to different documents in the same folder, the check for "Is the user a member of the folder's parent group?" can be cached. OpenFGA optimizes these recursive lookups internally.

2. Consistency vs. Latency

Zanzibar introduced the concept of "Zookies" (consistency tokens). When you write a permission, you get back a token. By passing this token back during a check, you ensure the check is performed against a version of the database that includes your recent write, avoiding the "New-Enemy Problem" (where a user is removed from a group but still has access for a few seconds due to replication lag).

Real-World Use Case: The Shared Workspace

Imagine a Slack-like application. You have Workspaces, Channels, and Messages.

  • A Workspace has admins and members.
  • A Channel can be public (all workspace members) or private (specific invited members).
  • A Message belongs to a Channel.

With ReBAC, your model looks like this:

type workspace relations define admin: [user] define member: [user] type channel relations define parent_workspace: [workspace] define invited_member: [user] define can_view: invited_member or member from parent_workspace type message relations define parent_channel: [channel] define can_read: can_view from parent_channel

To check if a user can read a message, OpenFGA checks if they can view the channel. If it's a public channel, it checks if they are a member of the workspace. This logic is decoupled from your messages-service and your identity-service. If the product team decides that Workspace Guests can now view specific messages, you update the model, not the code.

Why This Matters for Technical Decision-Makers

Adopting ReBAC via OpenFGA isn't just about technical elegance; it’s about velocity and security.

  • Auditability: You have a single source of truth for all permissions. You can query OpenFGA to ask, "Who has access to this resource?" or "What resources can this user access?" (via the ListObjects API).
  • Decoupling: Your microservices no longer need to share database tables or implement complex join logic for permissions. They just ask OpenFGA for a Yes/No answer.
  • Future-Proofing: As your organizational structure changes, you update the relationship model. Your application code remains untouched.

Conclusion and Actionable Steps

Transitioning to Relationship-Based Access Control is a significant architectural shift, but for distributed systems with complex sharing requirements, it is the only way to maintain sanity at scale.

To get started:

  1. Audit your current auth logic: Identify where you are performing recursive database queries to check permissions. These are your primary candidates for ReBAC.
  2. Model your domain: Use the OpenFGA Playground to draft your schema using their DSL. Start small with one or two resource types.
  3. Decouple the Check: Start by moving the most complex permission check in your system to OpenFGA. You don't have to migrate everything at once.
  4. Think in Tuples: Stop thinking about user attributes and start thinking about how objects relate to one another.

By adopting the Zanzibar model today, you are building an authorization layer that won't just survive your next 10x growth spurt—it will enable it.