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/querying/time-to-live.mdx
+47-15
Original file line number
Diff line number
Diff line change
@@ -7,48 +7,50 @@ import { Callout } from 'nextra/components'
7
7
8
8
# Time to live (Enterprise)
9
9
10
-
Time-to-live allows a user to tag vertices with an expiration time. Once a vertex has expired, the vertex and all associated edges will be deleted.
10
+
Time-to-live allows a user to tag vertices and edges with an expiration time. Once a vertex has expired, the vertex and all its associated edges will be deleted.
11
11
12
12
<Callouttype="warning">
13
13
14
-
The `TTL` label and `ttl` property are reserved names for TTL. See [Tagging vertices](#tagging-vertices) for more info.
14
+
The `TTL` label and `ttl` property are reserved names for TTL. See [Tagging objects](#tagging-objects) for more info.
15
15
16
16
</Callout>
17
17
18
18
<Callouttype="info">
19
19
20
-
Time-to-live is implemented as a background job that periodically gets executed.
21
-
This is a best effort solution; meaning that even if a vertex expires, that does not mean it gets deleted right away, but eventually, once the background job gets executed.
20
+
Time-to-live is implemented as a background job that periodically gets executed.
21
+
This is a best effort solution; meaning that even if an object expires, that does not mean it gets deleted right away, but eventually, once the background job gets executed.
22
22
23
23
</Callout>
24
24
25
25
## Usage
26
26
27
27
In order to use the feature the user needs to:
28
28
1.[Configure TTL](#configuration)
29
-
2.[Tag vertices with an expiration time](#tagging-vertices)
29
+
2.[Tag vertices and edges with an expiration time](#tagging-objects)
30
30
31
-
Once that is done, a background job will periodically delete expired vertices and associated edges.
31
+
Once that is done, a background job will periodically delete expired vertices, its associated edges and expired edges.
32
32
33
33
### What is indexed
34
34
35
35
Time-to-live uses a label `TTL` and property `ttl` to tag vertices. A label+property value index is used to speed up query execution.
36
+
Edges are tagged using only the `ttl` property and are scanned using the global edge property index.
36
37
37
38
### Executed query
38
39
39
-
Time-to-live is implemented as a background job that execute the following query:
40
+
Time-to-live is implemented as a background job that execute the following queries:
40
41
41
42
```cypher
42
43
MATCH (n:TTL) WHERE n.ttl < $now WITH n LIMIT $batch DETACH DELETE n;
44
+
MATCH ()-[e]->() WHERE e.ttl < $now WITH e LIMIT $batch DELETE e;
43
45
```
44
46
45
-
The query DETACH DELETEs all vertices that have expired at this point in time.
47
+
The query DETACH DELETEs all vertices and DELETEs edges that have expired at this point in time.
46
48
The query is batched to limit the serialization errors and lost work that the error might cause.
47
49
48
50
<Callouttype="warning">
49
51
50
52
Since time-to-live is implemented as a query like any other, the user might get serialization errors.
51
-
This can happen if the user is modifying an expired vertex.
53
+
This can happen if the user is modifying an expired vertex or edge.
52
54
The chance of serialization errors can be minimized by limiting the duration of write transactions.
53
55
In addition, the user can disable TTL before starting an important write transaction and re-enable it after commit. See [configuration](#configuration).
54
56
@@ -101,31 +103,61 @@ DISABLE TTL;
101
103
102
104
**NOTE:** Once disabled, ttl has to be re-enabled with a new configuration.
103
105
104
-
## Tagging vertices
106
+
## Tagging objects
105
107
106
108
In order to tag a vertex for expiration, the user needs to add the `TTL` label and `ttl` property.
109
+
Edges are tagged with only the `ttl` property. This allows the user to use TTL on any edge type.
110
+
107
111
`ttl` property defines when the vertex has expired as a number of microseconds since POSIX epoch.
108
112
POSIX epoch defined as starting from 1st of January 1970. Negative values define time before, while positive numbers the time since.
109
113
The user can simply input the number or can use builtin Memgraph functions to define the expiration time.
110
114
111
-
Arbitrary time since epoch:
115
+
Define arbitrary time since epoch when creating an object:
116
+
```cypher
117
+
CREATE (:TTL{ttl:123});
118
+
CREATE ()-[e:E{ttl:123}]->();
119
+
```
120
+
121
+
Mark an already existing object with an arbitrary time since epoch:
112
122
```cypher
113
123
MATCH (n) SET n:TTL, n.ttl=123;
124
+
MATCH ()-[e]->() SET e.ttl=123;
125
+
```
126
+
127
+
Set object expiration to time at creation:
128
+
```cypher
129
+
CREATE (:TTL{ttl:timestamp()});
130
+
CREATE ()-[e:A{ttl:timestamp()}]->();
114
131
```
115
132
116
-
Set vertex to expire now:
133
+
Update object's expiration to now:
117
134
```cypher
118
135
MATCH (n) SET n:TTL, n.ttl=timestamp();
136
+
MATCH ()-[e]->() SET e.ttl=timestamp();
119
137
```
120
138
121
-
Set vertex to expire 11 hours and 25 minutes from now:
139
+
Create an object that will expire 11 hours and 25 minutes after creation:
0 commit comments