Skip to content

Implement a singleflight CheckResolver to avoid concurrency evaluation of overlapping subproblems

Open

Description

Checklist

Describe the problem you'd like to have solved

Consider a model such as the following:

model
  schema 1.1

type user

type group
  relations
    define member: [user]

type document
  relations
    define editor: [group#member]
    define viewer: [group#member] or editor

and consider the following tuples:

- document:1#viewer@group:eng#member
- document:1#editor@group:eng#member
- group:eng#member@user:jon

If we call Check(document:1#viewer@user:jon), then this Check may involve a concurrent evaluation of the same subproblem (e.g. Check(group:eng#member@user:jon)). If this overlapping subproblem for group:eng#member@user:jon is evaluated simultaneously, then this is wasted computation since we only need to resolve that subproblem once and share the result for every concurrent evaluation of the same overlapping subproblem.

Describe the ideal solution

We should implement a CheckResolver wrapper similar to the graph#CachedCheckResolver (in the internal/graph package) that uses the singleflight package to de-duplicate in-flight overlapping subproblems. The cache key we use for Check should also be used as the singleflight key that we use to lookup in-flight evaluations of the same subproblem.

The implementation of the singleflight CheckResolver should also introduce a new Prometheus counter metric that counts the number of subproblems that have been de-duplicated so that we can objectively demonstrate the value with our reported metric.

Alternatives and current workarounds

No response

Additional context

Existing start of a POC - implementation is mostly done but needs testing etc..

https://github.com/openfga/openfga/tree/singleflight-check-resolver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions