Skip to content

perf(access-control): cache the policy enforcer #450

Closed

Description

@uniqueg

Problem

FOCA 0.17.0 builds a new Casbin enforcer for every request and loads all policy rules from MongoDB each time (get_enforcer() in foca/security/access_control/enforcement.py, Adapter.load_policy() in foca_casbin_adapter/adapter.py). Apps with per-resource rules pay for every rule on every request. For example, proTES stores two g2 rules per task. In the proTES pre-release review, GET /tasks took about 0.01 to 0.03 s with 33 rules and about 1.6 s with 100,000 rules.

Casbin also logs every loaded role rule at startup in a single INFO line. That line holds user identifiers (g2 subjects), so personal identifiers end up in the logs.

Proposed change

  • Cache one enforcer per app and process, shared by its threads (thread-safe under gunicorn threads).
  • Keep the cache consistent across workers and replicas. Every rule write through FOCA bumps a version that is stored in the policy collection (the permissions API, the resource role helpers, the adapter's write paths and configured policies). Each check reads this version (one small read by _id). If it changed, the enforcer applies only the changed rules and falls back to a full reload when needed. A grant or a revocation therefore takes effect from the next request, in any worker or replica.
  • Lower Casbin's policy dump to DEBUG, or suppress it, so that subjects are not logged at INFO.
  • Keep the public API backward compatible and document the consistency trade-off, e.g., that rules changed directly in the database need a version bump or a restart.
  • Add before and after benchmarks in the PR.

Found in the proTES pre-release review; the maintainer approved the FOCA change on 2026-10-24.

Activity

  1. added this to the v0.18.0 milestone on Oct 23, 2026
  2. added
    priority: high
    status: in progress
    type: feature
    workload: days
    on Oct 23, 2026
  3. removed
    status: in progress
    on Oct 24, 2026
  4. hsluoyz commented on Oct 31, 2026

    @hsluoyz

    Two of the upstream items here are now on pycasbin master (b139a8a: apache/casbin-pycasbin@b139a8a):

    • print_policy() and print_roles() now log per-rule details at DEBUG instead of INFO, so creating an enforcer with an adapter no longer writes every g2 subject to the INFO log.
    • DomainManager now keeps its per-domain role-manager cache in an LRU with a configurable max_cached_role_managers (default 1000; pass 0 or None for no limit), so the cache no longer grows without bound.

    The shared enforcer cache and cross-worker invalidation are still application-level concerns; this does not add a shared enforcer cache. The current workaround can stay until a pycasbin release includes b139a8a.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: hightype: featureworkload: days

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions