| 1 | ## Query timestamps |
| 2 | |
| 3 | In Cassandra, each mutation has a microsecond-precision timestamp, which |
| 4 | is used to order operations relative to each other. |
| 5 | |
| 6 | There are various ways to assign it: |
| 7 | |
| 8 | ### Server-side generation |
| 9 | |
| 10 | If the client does not specify any timestamp, the server will assign one |
| 11 | based on the time it receives the query. |
| 12 | |
| 13 | This can be a problem when the order of the writes matter: with unlucky |
| 14 | timing (different coordinators, network latency, etc.), two successive |
| 15 | requests from the same client might be processed in a different order |
| 16 | server-side, and end up with out-of-order timestamps. |
| 17 | |
| 18 | ### CQL `USING TIMESTAMP` |
| 19 | |
| 20 | You can explicitly provide the timestamp in your CQL query: |
| 21 | |
| 22 | ```java |
| 23 | session.execute("INSERT INTO my_table(c1, c2) values (1, 1) " + |
| 24 | "USING TIMESTAMP 1432815430948040"); |
| 25 | ``` |
| 26 | |
| 27 | This solves the problems of server-side generation: the client has |
| 28 | control over the ordering of the statements it generates. On the other |
| 29 | hand, it puts the burden of generating timestamps on client code. |
| 30 | |
| 31 | ### Client-side generation |
| 32 | |
| 33 | Starting with version 2.1.2 of the driver, and if [native |
| 34 | protocol](../native_protocol/) v3 or above is in use, a *default |
| 35 | timestamp* can be sent with each query. |
| 36 | |
| 37 | To enable this feature, provide an instance of [TimestampGenerator][tsg] |
| 38 | at initialization: |
| 39 | |
| 40 | ```java |
| 41 | Cluster.builder().addContactPoint("127.0.0.1") |
| 42 | .withTimestampGenerator(new AtomicMonotonicTimestampGenerator()) |
| 43 | .build(); |
| 44 | ``` |
| 45 | |
| 46 | The default is still server-side generation. So unless you explicitly |
| 47 | provide a generator, you get the same behavior as previous driver |
| 48 | versions. |
| 49 | |
| 50 | In addition, you can also override the default timestamp on a |
| 51 | per-statement basis: |
| 52 | |
| 53 | ```java |
| 54 | Statement statement = new SimpleStatement( |
| 55 | "UPDATE users SET email = 'x@y.com' where id = 1"); |
| 56 | statement.setDefaultTimestamp(1234567890); |
| 57 | session.execute(statement); |
| 58 | ``` |
| 59 | |
| 60 | [tsg]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/TimestampGenerator.html |
| 61 | |
| 62 | ### Summary |
| 63 | |
| 64 | As shown in the previous sections, there are multiple ways to provide a |
| 65 | timestamp, some of which overlap. The order of precedence is the |
| 66 | following: |
| 67 | |
| 68 | 1. if there is a `USING TIMESTAMP` clause in the CQL string, use that |
| 69 | over anything else; |
| 70 | 2. otherwise, if a default timestamp was set on the statement and is |
| 71 | different from `Long.MIN_VALUE`, use it; |
| 72 | 3. otherwise, if a generator is specified, invoke it and use its result |
| 73 | if it is different from `Long.MIN_VALUE`; |
| 74 | 4. otherwise, let the server assign the timestamp. |
| 75 | |
| 76 | Steps 2 and 3 only apply if native protocol v3 or above is in use. |