| 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 | ### CQL `USING TIMESTAMP` |
| 9 | |
| 10 | You can explicitly provide the timestamp in your CQL query: |
| 11 | |
| 12 | ```java |
| 13 | session.execute("INSERT INTO my_table(c1, c2) values (1, 1) " + |
| 14 | "USING TIMESTAMP 1432815430948040"); |
| 15 | ``` |
| 16 | |
| 17 | ### Client-side generation |
| 18 | |
| 19 | This is enabled by default if you're using the driver 3.0 and a version |
| 20 | of Cassandra that supports [native protocol](../native_protocol) v3 or |
| 21 | above. |
| 22 | |
| 23 | A client timestamp will be sent by each query. It is generated by a |
| 24 | [TimestampGenerator][tsg]. The default implementation is |
| 25 | [AtomicMonotonicTimestampGenerator][amtsg]. |
| 26 | |
| 27 | You can specify another generator at initialization: |
| 28 | |
| 29 | ```java |
| 30 | Cluster.builder().addContactPoint("127.0.0.1") |
| 31 | .withTimestampGenerator(new MyCustomTimestampGenerator()) |
| 32 | .build(); |
| 33 | ``` |
| 34 | |
| 35 | In addition, you can also override the default timestamp on a |
| 36 | per-statement basis: |
| 37 | |
| 38 | ```java |
| 39 | Statement statement = new SimpleStatement( |
| 40 | "UPDATE users SET email = 'x@y.com' where id = 1"); |
| 41 | statement.setDefaultTimestamp(1234567890); |
| 42 | session.execute(statement); |
| 43 | ``` |
| 44 | |
| 45 | [tsg]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/TimestampGenerator.html |
| 46 | [amtsg]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/AtomicMonotonicTimestampGenerator.html |
| 47 | |
| 48 | |
| 49 | ### Server-side generation |
| 50 | |
| 51 | This is the "legacy" behavior if you're connected to a Cassandra version |
| 52 | that only supports protocol v2 or below. The server will assign a |
| 53 | timestamp based on the time it receives the query. |
| 54 | |
| 55 | This can be a problem when the order of the writes matter: with unlucky |
| 56 | timing (different coordinators, network latency, etc.), two successive |
| 57 | requests from the same client might be processed in a different order |
| 58 | server-side, and end up with out-of-order timestamps. If protocol v3 is |
| 59 | not an option, the only workaround is to add `USING TIMESTAMP` in your |
| 60 | queries. |
| 61 | |
| 62 | |
| 63 | ### Summary |
| 64 | |
| 65 | As shown in the previous sections, there are multiple ways to provide a |
| 66 | timestamp, some of which overlap. The order of precedence is the |
| 67 | following: |
| 68 | |
| 69 | 1. if there is a `USING TIMESTAMP` clause in the CQL string, use that |
| 70 | over anything else; |
| 71 | 2. otherwise, if a default timestamp was set on the statement and is |
| 72 | different from `Long.MIN_VALUE`, use it; |
| 73 | 3. otherwise, if a generator is specified, invoke it and use its result |
| 74 | if it is different from `Long.MIN_VALUE`; |
| 75 | 4. otherwise, let the server assign the timestamp. |
| 76 | |
| 77 | Steps 2 and 3 only apply if native protocol v3 or above is in use. |