|
| 1 | +import { Callout } from 'nextra/components' |
| 2 | + |
| 3 | +# Improving Performance |
| 4 | + |
| 5 | +## By enabling cross-node communication |
| 6 | + |
| 7 | +SpiceDB can be deployed in a clustered configuration where multiple nodes work together to serve API requests. In such a configuration, and for the CheckPermissions API, enabling a feature called **dispatch** allows nodes to break down one API request into smaller "questions" and forward those to other nodes within the cluster. This helps reduce latency and improve overall performance. |
| 8 | + |
| 9 | +### How it works |
| 10 | + |
| 11 | +Each SpiceDB node maintains an in-memory cache of permissions queries it has resolved in the past. When a new permissions query is encountered by one node, its answer may be present on another node, so SpiceDB will forward the request onward to the other node to check the shared cache. |
| 12 | + |
| 13 | +For more details on how dispatching works, see the [Consistent Hash Load Balancing for gRPC] article. |
| 14 | + |
| 15 | +[consistent hash load balancing for grpc]: https://authzed.com/blog/consistent-hash-load-balancing-grpc/ |
| 16 | + |
| 17 | +### Configuration in Kubernetes environments |
| 18 | + |
| 19 | +If using the [SpiceDB Operator], dispatching is enabled by default and no additional configuration is necessary. |
| 20 | + |
| 21 | +If not using it, you need to set the following flag: |
| 22 | + |
| 23 | +```sh |
| 24 | +--dispatch-upstream-addr=kubernetes:///spicedb.default:50053 |
| 25 | +``` |
| 26 | + |
| 27 | +where `spicedb.default` is the Kubernetes `Service` in which SpiceDB is accessible. |
| 28 | + |
| 29 | +<Callout type="info"> |
| 30 | + If you are deploying SpiceDB under Kubernetes, it is recommended to use the [SpiceDB Operator], which configures dispatching automatically. |
| 31 | + |
| 32 | + [spicedb operator]: /spicedb/ops/operator |
| 33 | +</Callout> |
| 34 | + |
| 35 | +### Configuration in non-Kubernetes environments |
| 36 | + |
| 37 | +<Callout type="warning"> |
| 38 | + Non-Kubernetes based dispatching relies upon DNS updates, which means it can become stale if DNS is changing. This is not recommended unless DNS updates are rare. |
| 39 | +</Callout> |
| 40 | + |
| 41 | +To enable dispatch, the following flags must be specified: |
| 42 | + |
| 43 | +```sh |
| 44 | +spicedb serve \ |
| 45 | + --dispatch-cluster-enabled=true \ |
| 46 | + --dispatch-upstream-addr=upstream-addr \ |
| 47 | + ... |
| 48 | +``` |
| 49 | + |
| 50 | +or via environment variables with the `SPICEDB_` prefix: |
| 51 | + |
| 52 | +```sh |
| 53 | +SPICEDB_DISPATCH_CLUSTER_ENABLED=true \ |
| 54 | +SPICEDB_DISPATCH_UPSTREAM_ADDR=upstream-addr \ |
| 55 | +spicedb serve ... |
| 56 | +``` |
| 57 | + |
| 58 | +The `upstream-addr` should be the DNS address of the load balancer at which _all_ SpiceDB nodes are accessible at the default dispatch port of `:50053`. |
| 59 | + |
| 60 | +## By enabling Materialize |
| 61 | + |
| 62 | +[Materialize] is a separate service that allows for the precomputation of permission query results. |
| 63 | + |
| 64 | +If Materialize is running, SpiceDB can dispatch sub-queries to Materialize, which can significantly speed up permission checks. |
| 65 | + |
| 66 | +[Materialize]: /authzed/concepts/authzed-materialize |
| 67 | + |
| 68 | +## By enabling the schema cache |
| 69 | + |
| 70 | +The schema cache stores type definitions and caveat definitions to avoid repeatedly fetching schema information from the datastore. |
| 71 | + |
| 72 | +SpiceDB offers two caching modes: |
| 73 | + |
| 74 | +1. **Just-In-Time (JIT) Caching**: The default mode that loads definitions on-demand. Uses less memory, but it incurs a cold start penalty on first access to each definition. |
| 75 | +2. **Watching Cache**: An experimental mode that proactively maintains an always-up-to-date cache. This mode uses more memory but avoids cold start penalties. It is recommended when there are frequent schema changes. |
| 76 | + |
| 77 | +To configure the schema cache, use the following flags: |
| 78 | + |
| 79 | +```bash |
| 80 | +# Enable namespace cache (default: true) |
| 81 | +--ns-cache-enabled=true |
| 82 | + |
| 83 | +# Maximum memory (default: 32 MiB) |
| 84 | +--ns-cache-max-cost=32MiB |
| 85 | + |
| 86 | +# Enable experimental watchable schema cache (default: false) |
| 87 | +# When true: uses watching cache if datastore supports it |
| 88 | +# When false: always uses JIT caching |
| 89 | +--enable-experimental-watchable-schema-cache=false |
| 90 | +``` |
0 commit comments