You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/best-practices/best-practices.mdx
+75-11Lines changed: 75 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,16 +18,53 @@ In most cases, these rules should be followed.
18
18
The recommended rules reflect how we would run our own systems, but may not apply to every use case and may not make sense in every situation.
19
19
Follow them if you can and ignore them if you can’t.
20
20
21
+
<BestPracticesSelector />
22
+
21
23
## Priority A Rules: Essential
22
24
23
-
### Make sure your schema fails closed
25
+
### Make Sure your Schema Fails Closed
24
26
25
27
Tags: **schema**
26
28
27
29
This is related to the idea of using negation sparingly, and of phrasing your schema additively.
28
30
Give thought to what happens if your application fails to write a relation: should the user have access in that case?
29
31
The answer is almost always `no`.
30
32
33
+
This example is very simple, but illustrates the basic point:
34
+
35
+
#### Avoid
36
+
37
+
This schema starts with everyone having access and reduces it as you add users to the deny list.
38
+
If you fail to write a user to the deny list, they'll have access when they shouldn't:
39
+
40
+
```zed
41
+
definition user {}
42
+
43
+
definition resource {
44
+
relation public: user:*
45
+
relation deny: user
46
+
47
+
permission view = public - deny
48
+
}
49
+
```
50
+
51
+
#### Prefer
52
+
53
+
By contrast, this schema defaults to nobody having access, and therefore fails closed:
54
+
55
+
```zed
56
+
definition user {}
57
+
58
+
definition resource {
59
+
relation user: user
60
+
61
+
permission view = user
62
+
}
63
+
```
64
+
65
+
This is an admittedly simple example, but the concept holds in more complex schemas.
66
+
This will also sometimes require a conversation about the business logic of your application.
67
+
31
68
### Tune Connections to Datastores
32
69
33
70
Tags: **operations**
@@ -36,6 +73,25 @@ To size your SpiceDB connection pools, start by determining the maximum number o
36
73
37
74
Use these values to set the `--datastore-conn-pool-read-max-open` and `--datastore-conn-pool-write-max-open` flags, and set the corresponding min values to half of each, adjusting as needed based on whether your workload leans more heavily on reads or writes.
38
75
76
+
#### Example
77
+
78
+
Let's say you have a database instance that supports 200 connections, and you know that you read more than you write.
79
+
You have 4 SpiceDB instances in your cluster.
80
+
A starting point for tuning this might be:
81
+
82
+
```sh
83
+
spicedb serve
84
+
# other flags here
85
+
--datastore-conn-pool-read-max-open 30
86
+
--datastore-conn-pool-read-min-open 15
87
+
--datastore-conn-pool-write-max-open 20
88
+
--datastore-conn-pool-write-min-open 10
89
+
```
90
+
91
+
This reserves 50 connections per SpiceDB instance and distributes them accordingly.
92
+
93
+
The `pgxpool_empty_acquire` metric can help you understand if your SpiceDB pods are starved for connections if you're using Postgres or Cockroach.
94
+
39
95
### Test Your Schema
40
96
41
97
Tags: **schema**
@@ -44,7 +100,7 @@ You should be testing the logic of your schema to ensure that it behaves the way
44
100
45
101
- For unit testing and TDD, use test relations + assertions and [zed validate](https://authzed.com/docs/spicedb/modeling/validation-testing-debugging#zed-validate).
46
102
- For snapshot testing, use test relations + expected relations and [zed validate](https://authzed.com/docs/spicedb/modeling/validation-testing-debugging#zed-validate).
47
-
- For integration testing, use the SpiceDB test server with spicedb[serve-testing](https://authzed.com/docs/spicedb/modeling/validation-testing-debugging#integration-test-server).
103
+
- For integration testing, use the SpiceDB test server with SpiceDB[serve-testing](https://authzed.com/docs/spicedb/modeling/validation-testing-debugging#integration-test-server).
48
104
49
105
### Prefer Relations to Caveats
50
106
@@ -53,15 +109,15 @@ Tags: **schema**
53
109
If an authorization concept can be expressed using relations, it should be.
54
110
We provide caveats as an escape hatch; they should only be used for context that’s only available at request time, or else ABAC logic that cannot be expressed in terms of relationships.
55
111
112
+
This is because caveats come with a performance penalty.
113
+
A caveated relationship is both harder to cache and also slows down computation of the graph walk required to compute a permission.
114
+
56
115
Some examples:
57
116
58
117
- A banlist - this could be expressed as a list in caveat context, but it can also be expressed as a relation with negation.
59
118
- A notion of public vs internal - boolean flags seem like an obvious caveat use case, but they can also be expressed using self relations.
60
119
- Dynamic roles - these could be expressed as a list in caveats, and it’s not immediately obvious how to build them into a spicedb schema, but our [Google Cloud IAM example](https://authzed.com/blog/google-cloud-iam-modeling) shows how it’s possible.
61
120
62
-
This is because caveats come with a performance penalty.
63
-
A caveated relationship is both harder to cache and also slows down computation of the graph walk required to compute a permission.
64
-
65
121
### Make Your Writes Idempotent
66
122
67
123
Tags: **application**
@@ -88,12 +144,18 @@ In Postgres, the implementation of MVCC depends on the internals of the transact
88
144
89
145
Tags: **operations**
90
146
91
-
While designing your authorization, it’s important to plan ahead for how quickly an update is guaranteed, while trading off performance via cache effectiveness.
92
-
By default, SpiceDB sets the Quanitzation Interval to 5s; check operations are cached within this window.
93
-
To change this value, set `--datastore-revision-quantization-interval` longer or shorter.
147
+
SpiceDB gives the user the ability to make tradeoffs between cache performance and up-to-date visibility using [its consistency options](https://authzed.com/docs/spicedb/concepts/consistency).
148
+
In addition to these call-time options, there are also some flags that can provide better cache performance if additional staleness is acceptable.
149
+
For example, by default, SpiceDB sets the Quantization Interval to 5s; check operations are cached within this window when using `minimize_latency` or `at_least_as_fresh` calls.
150
+
Setting this window to be larger increases the ability of SpiceDB to use cached results with a tradeoff of results staying in the cache longer.
151
+
More details about how these flags work together can be found in our [Hotspot Caching blog post](https://authzed.com/blog/hotspot-caching-in-google-zanzibar-and-spicedb).
152
+
To change this value, set the `--datastore-revision-quantization-interval` flag.
94
153
95
-
When it comes to write consistency, SpiceDB defaults to high safety, especially in distributed database writing scenarios, guaranteeing a visibility order.
96
-
Individual datastores may also allow a relaxation of this guarantee, based on your scenario; for example, [setting CockroachDB’s overlap strategy](https://authzed.com/docs/spicedb/concepts/datastores#overlap-strategy), trading some authorization visibility safety across domains for greatly increased write throughput.
154
+
When it comes to write consistency, SpiceDB defaults to high safety,
155
+
especially in distributed database writing scenarios, guaranteeing a visibility order.
156
+
Individual datastores may also allow a relaxation of this guarantee, based on your scenario;
157
+
for example, [setting CockroachDB’s overlap strategy](https://authzed.com/docs/spicedb/concepts/datastores#overlap-strategy),
158
+
can let you trade some ordering and consistency guarantees across domains for greatly increased write throughput.
97
159
98
160
### Use GRPC When Possible
99
161
@@ -278,7 +340,7 @@ Tags: **schema**
278
340
As a schema grows in size and complexity, it can become difficult to navigate and grok.
279
341
We implemented [Composable Schemas](https://authzed.com/docs/spicedb/modeling/composable-schemas) to solve this problem, allowing you to break down a schema into multiple files and definitions into multiple problems.
280
342
281
-
### When In Doubt, Add Another Permission
343
+
### Don't Re-Use Permissions Across Use Cases
282
344
283
345
Tags: **schema**
284
346
@@ -291,3 +353,5 @@ Tags: **schema**
291
353
292
354
Expiration is a common use case – at some future time, a permission is revoked.
293
355
It’s so common, it’s now [a built-in feature](https://authzed.com/docs/spicedb/concepts/expiring-relationships), and is far more efficient for SpiceDB to handle than doing the same with caveats!
Copy file name to clipboardExpand all lines: pages/spicedb/concepts/datastores.mdx
+2-13Lines changed: 2 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -274,19 +274,8 @@ Because this counter is instance-specific, there are ways in which the data in t
274
274
Two concrete examples are the use of `pg_dump` and `pg_restore` to transfer data between an old instance and a new instance and setting up
275
275
logical replication between a previously-existing instance and a newly-created instance.
276
276
277
-
If you encounter this, SpiceDB can behave as though there is no schema written, because the data (including the schema) is associated with a future transaction ID and therefore
278
-
isn't "visible" to Spicedb.
279
-
You may see an error like this:
280
-
281
-
```
282
-
FAILED_PRECONDITION: object definition `some_definition` not found
283
-
```
284
-
285
-
If this occurs, you can use `spicedb datastore repair` (along with your usual datastore flags) to roll the transaction counter of the database forward until SpiceDB is able to resume normal operation:
If you encounter this, SpiceDB can behave as though there is no schema written, because the data (including the schema) is associated with a future transaction ID and therefore isn't "visible" to Spicedb.
278
+
If you run into this issue, the fix is [documented here](https://authzed.com/docs/spicedb/concepts/datastores#transaction-ids-and-mvcc)
0 commit comments