Enabling TTL (Time to Live)
Aerospike Graph Service (AGS) supports TTL on vertices and edges. When an element's TTL interval expires, AGS drops the element from the graph. When a vertex is dropped via TTL, it behaves the same way it would if it were deleted by other means. All edges associated with the dropped vertex are dropped at the same time.
To assign a TTL interval to an element, assign the element a virtual property with
the key ~ttl
and a numeric value indicating how long the element should live, in seconds,
before AGS drops it from the graph.
Graph TTL configuration options
To enable TTL for Graph, use the following configuration options.
You can specify configuration options when you start the Graph Docker
container, or use a .properties
configuration file. See
Graph Installation
for more information about configuring AGS. See
Configuration Options for a complete list of AGS
configuration options.
aerospike.graph.ttl.enabled=true
aerospike.graph.ttl.purge.interval=<TIME_IN_SECONDS>
Usage
To assign a TTL to an element, assign it a property with the key ~ttl
via a
regular Gremlin traversal for adding properties. The value must be numeric and
is the number of seconds which the element should live from creation
until it expires and is dropped from the graph.
// Create a Vertex with a TTL of 10 hours.
g.addV("v1").property("~ttl", 36000).iterate();
// Adding a TTL to an existing Vertex.
g.addV("v2").iterate();
g.V().hasLabel("v2").property("~ttl", 36000).iterate();
// Updating a TTL of an existing Vertex with TTL. (10 hours -> 5 hours)
g.V().hasLabel("v1").property("~ttl", 18000).iterate();
Updating TTL specifications
Under certain conditions, the TTL of an element cannot be updated. These conditions include:
- The original value of the
~ttl
property was set as less than or equal to the value ofaerospike.graph.ttl.purge.interval
at any time, e.g. via creation or update. - If the expiry time of the element is within the current time plus the
duration specified by
aerospike.graph.ttl.purge.interval
.
Examples
The following examples demonstrate situations in which the TTL of an element cannot be updated.
The purge interval is set to one minute.
aerospike.graph.ttl.purge.interval=60
Example 1: Original ~ttl
property set lower than aerospike.graph.ttl.purge.interval
// Create a vertex with a TTL of thirty seconds.
g.addV("v1").property("~ttl", 30).iterate();
// Update the TTL to two minutes from now.
g.V().hasLabel("v1").property("~ttl", 120).iterate();
// Wait thirty seconds and look for v1.
Thread.sleep(30000);
g.V().hasLabel("v1").hasNext(); // Returns false. The vertex expired via TTL despite attempting to update the value.
#### Example 2: Element's expiry time is within the *current* time plus `aerospike.graph.ttl.purge.interval`
```java
// Create a vertex with a TTL of two minutes.
g.addV("v2").property("~ttl", 120).iterate();
// Wait thirty seconds and update the TTL to be two minutes again.
Thread.sleep(30000);
g.V().hasLabel("v2").property("~ttl", 120).iterate();
// Wait ninety seconds and look for v2 (two minutes total since start).
Thread.sleep(90000);
g.V().hasLabel("v2").hasNext().iterate(); // Returns true. First TTL update was successful as expected.
// Note that the expiry time of v2 is now in thirty seconds, which falls under
// condition #2. Attempt to update the TTL anyways.
g.V().hasLabel("v2").property("~ttl", 120).iterate();
Thread.sleep(30000);
g.V().hasLabel("v2").hasNext(); // Returns false. The vertex expired via TTL despite attempting to update the value.