Skip to content

Commit 3df2c34

Browse files
andrejtonevmatea16
andauthored
Expand TTL to edges (#1244)
* touch * Edge TTL * PR comments --------- Co-authored-by: Matea Pesic <80577904+matea16@users.noreply.github.com>
1 parent 908b597 commit 3df2c34

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

pages/querying/time-to-live.mdx

+47-15
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,50 @@ import { Callout } from 'nextra/components'
77

88
# Time to live (Enterprise)
99

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.
1111

1212
<Callout type="warning">
1313

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.
1515

1616
</Callout>
1717

1818
<Callout type="info">
1919

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.
2222

2323
</Callout>
2424

2525
## Usage
2626

2727
In order to use the feature the user needs to:
2828
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)
3030

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.
3232

3333
### What is indexed
3434

3535
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.
3637

3738
### Executed query
3839

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:
4041

4142
```cypher
4243
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;
4345
```
4446

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.
4648
The query is batched to limit the serialization errors and lost work that the error might cause.
4749

4850
<Callout type="warning">
4951

5052
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.
5254
The chance of serialization errors can be minimized by limiting the duration of write transactions.
5355
In addition, the user can disable TTL before starting an important write transaction and re-enable it after commit. See [configuration](#configuration).
5456

@@ -101,31 +103,61 @@ DISABLE TTL;
101103

102104
**NOTE:** Once disabled, ttl has to be re-enabled with a new configuration.
103105

104-
## Tagging vertices
106+
## Tagging objects
105107

106108
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+
107111
`ttl` property defines when the vertex has expired as a number of microseconds since POSIX epoch.
108112
POSIX epoch defined as starting from 1st of January 1970. Negative values define time before, while positive numbers the time since.
109113
The user can simply input the number or can use builtin Memgraph functions to define the expiration time.
110114

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:
112122
```cypher
113123
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()}]->();
114131
```
115132

116-
Set vertex to expire now:
133+
Update object's expiration to now:
117134
```cypher
118135
MATCH (n) SET n:TTL, n.ttl=timestamp();
136+
MATCH ()-[e]->() SET e.ttl=timestamp();
119137
```
120138

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:
140+
```cypher
141+
CREATE(:TTL{ttl:timestamp() + timestamp(duration({hour:11, minute:25}))});
142+
CREATE ()-[e:B{ttl:timestamp() + timestamp(duration({hour:11, minute:25}))}]->();
143+
```
144+
145+
Update object's expiration to 11 hours and 25 minutes from now:
122146
```cypher
123147
MATCH (n) SET n:TTL, n.ttl=timestamp() + timestamp(duration({hour:11, minute:25}));
148+
MATCH ()-[e]->() SET e.ttl=timestamp() + timestamp(duration({hour:11, minute:25}));
149+
```
150+
151+
Create object that will expire on July 15th 2024 at 14:15:
152+
```cypher
153+
CREATE (:TTL{ttl:timestamp(LocalDateTime("2024-07-15T14:15:00"))});
154+
CREATE ()-[e:C{ttl:timestamp(LocalDateTime("2024-07-15T14:15:00"))}]->();
124155
```
125156

126-
Set vertex to expire on July 15th 2024 at 14:15:
157+
Update object's so it expires on July 15th 2024 at 14:15:
127158
```cypher
128159
MATCH (n) SET n:TTL, n.ttl=timestamp(LocalDateTime("2024-07-15T14:15:00"));
160+
MATCH ()-[e]->() SET e.ttl=timestamp(LocalDateTime("2024-07-15T14:15:00"));
129161
```
130162

131163
## Authorization
@@ -153,7 +185,7 @@ List of features and their supported status:
153185

154186
Time-to-live configuration is tenant based; meaning that the feature will need to be enabled and configured manually for each tenant.
155187

156-
### Replication
188+
### Replication
157189

158190
Time-to-live background job will be execute only on MAIN and the changes will be replicated.
159191
While the TTL effect is replicated, the configuration is not. TTL needs to be configured manually on every instance that can become MAIN.

0 commit comments

Comments
 (0)