Skip to content

Commit cc215ed

Browse files
authored
Merge pull request #301 from authzed/expiring-rels-redux
follow ups to document expiring relationships
2 parents 230a49f + 6c662f8 commit cc215ed

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

pages/spicedb/concepts/expiring-relationships.mdx

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,33 @@ import YouTube from 'react-youtube';
77
Expiring Relationships is available from SpiceDB 1.40 onwards.
88
</Callout>
99

10+
A common use case is to model relationships that expire after a certain time.
11+
This is useful for granting temporary access to a resource.
12+
13+
Until now, caveats were the recommended way to support time-bound permissions, but that has some limitations:
14+
15+
- It requires clients to provide the `now` timestamp.
16+
This is additional complexity for clients.
17+
- Expired caveats are not automatically garbage collected.
18+
This can lead to many caveated relationships in the system and increase the costs of loading and evaluating those into the runtime.
19+
20+
SpiceDB supports expiring relationships, which lets users define relationships that expire at a given time.
21+
1022
<Callout type="info">
1123
The clock used to determine if a relationship is expired is that of the underlying SpiceDB datastore.
1224
This gets trickier when using distributed databases like CockroachDB or Spanner, where clocks have an uncertainty range.
1325
When operating your own database, it's key to keep node clocks in sync - we recommend services like [Amazon Time Sync Service](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-time.html).
1426
You should evaluate the impact of clock drift in your application.
1527
</Callout>
1628

17-
A common use-case is to model relationships that expire after a certain amount of time.
18-
This is useful to grant temporary access to a resource.
19-
20-
Until now, caveats was the recommended way to support time-bound permissions, but that has some limitations:
21-
22-
- It requires clients to provide the `now` timestamp.
23-
This is additional complexity for clients.
24-
- Expired caveats are not automatically garbage collected.
25-
This can lead to a large number of caveats in the system and increasing cost for loading those into the runtime and evaluating them.
26-
27-
SpiceDB supports expiring relationships, which lets users define relationships that expire at a given point in time.
28-
2929
## Schema Use
3030

3131
Expiring relationships follow a similar use to caveated subject types.
32-
The novelty here is that, in order to disambiguate between a caveat named `expiration` and the native `expiration` feature,
33-
users would need to add a `use` clause to the schema definition to enable the feature.
32+
The novelty here is that users need to enable the feature using the `use` clause.
33+
This is to disambiguate a caveat named `expiration` from the new expiration feature.
34+
35+
To enable expiration in your schema, add a `use expiration` clause to the top of the file.
36+
Then the relations subject to expiration are marked using `<type> with expiration`:
3437

3538
```zed
3639
use expiration
@@ -44,9 +47,9 @@ definition resource {
4447

4548
## API Use
4649

47-
Expiration of a relationship is [on a per-relationship basis](https://buf.build/authzed/api/docs/63b8911ef2871c56e5048d1f40a8473f98457ca9:authzed.api.v1#authzed.api.v1.Relationship)
50+
The expiration of a relationship is [on a per-relationship basis](https://buf.build/authzed/api/docs/63b8911ef2871c56e5048d1f40a8473f98457ca9:authzed.api.v1#authzed.api.v1.Relationship)
4851
at write time, using `WriteRelationships` or `BulkImportRelationships` APIs.
49-
The expiration is denoted as part of the `OptionalExpiresAt` field in the relationship.
52+
The expiration is denoted with the `OptionalExpiresAt` field in the relationship.
5053

5154
```textproto
5255
WriteRelationshipsRequest {
@@ -72,29 +75,32 @@ WriteRelationshipsRequest {
7275

7376
## Garbage Collection
7477

78+
As soon as a relationship expires, it will no longer be used in permission checks.
79+
However, the row is not deleted right then, but rather is subject to garbage collection.
80+
7581
Reclaiming expiring relationships is governed by the same mechanism (and flags) as the deletion of the history of
76-
relationship changes that powers SpiceDB's own MVCC (Multi-Version Concurrency Control), and heavily depends on
82+
relationship changes that powers SpiceDB's own MVCC (Multi-Version Concurrency Control) and heavily depends on
7783
the datastore chosen.
7884

79-
- Datastores like Spanner and CockroachDB have built-in support for expiring SQL rows, so Garbage Collection is done by the database itself.
80-
In both cases, expired relationships will be reclaimed after 24 hours, and that can't be changed without directly manipulating the SQL schema.
85+
- Datastores like Spanner and CockroachDB have built-in support for expiring SQL rows, so the database does Garbage Collection.
86+
In both cases, expired relationships will be reclaimed after 24 hours, which can't be changed without directly manipulating the SQL schema.
8187
- Datastores like Postgres and MySQL support it using the same GC job that reclaims old relationship versions, which runs every 5 minutes.
82-
Unlike Spanner and CockroachDB, you can govern the GC window with the corresponding flags.
83-
Relationships will be reclaimed after 24 hours by default.
88+
Unlike Spanner and CockroachDB, you can govern the GC window with the corresponding flags.
89+
Relationships will be reclaimed after 24 hours by default.
8490

8591
<Callout type="info">
86-
GC Window should be adjusted based on the needs of the application. How far does you application need to go back in time?
87-
If this is a common use-case, we recommend drastrically reducing the GC window (e.g. 1h, or 30 minutes).
92+
The GC Window should be adjusted according to the application's needs. How far back in time does your application need to go?
93+
If this is a common use case, we recommend drastically reducing the GC window (e.g., 1 hour or 30 minutes).
8894
This means SpiceDB will have to evaluate less data when serving authorization checks, which can improve performance
8995
drastically in large-scale deployments.
9096
</Callout>
9197

92-
## Migrating Off Expirationg With Caveats
98+
## Migrating Off Of Expiration With Caveats
9399

94-
If you implemented expiration using caveats, this section describes the process to migrate to the new expiration feature.
100+
If you implemented expiration using caveats, this section describes migrating to the new expiration feature.
95101

96102
1. Rename your caveat if you had named it `expiration`
97-
2. Add the new subject type to your relation, and add also a combination where both are used:
103+
2. Add the new subject type to your relation, and also add a combination where both are used:
98104

99105
```zed
100106
caveat ttl(timeout duration, now string, timeout_creation_timestamp string) {
@@ -125,12 +131,11 @@ If you implemented expiration using caveats, this section describes the process
125131
```
126132

127133
3. Migrate all relationships to use both the caveat and the new expiration.
128-
This is needed because only one relationship is allowed for a combination of resource/permission/subject.
129-
4. Validate that the new expiration feature works as expected by not providing the needed context for the evaluation
130-
of the `ttl` caveat.
131-
5. Once validated, migrate completely to the new expiration feature by writting all relationships with only expiration
132-
and without caveat.
133-
6. Drop the caveat from your schema once migration is completed
134+
This is needed because only one relationship is allowed for a resource/permission/subject combination.
135+
4. Validate that the new expiration feature works as expected by not providing the context for evaluating the `ttl` caveat.
136+
5. Once validated, migrate completely to the new expiration feature by writing all relationships with only expiration
137+
and without caveat.
138+
6. Drop the caveat from your schema once the migration is completed
134139

135140
```zed
136141
use expiration

0 commit comments

Comments
 (0)