# Query Idempotence in GoCql driver

[Blog](/blog/)&gt;[Technical](/blog/category/technical/)&gt;Query Idempotence in GoCql driver 

Query Idempotence in GoCql driver
=================================

June 26, 2018 | By [ Instaclustr ](https://www.instaclustr.com/blog/author/instaposting/)

 

 

 

 



   [ ](https://x.com/intent/tweet?text=Query%20Idempotence%20in%20GoCql%20driver&url=https://www.instaclustr.com/blog/query-idempotence-in-gocql-driver/) [ ](https://www.linkedin.com/shareArticle?mini=true&url=https://www.instaclustr.com/blog/query-idempotence-in-gocql-driver/&title=&summary=Query%20Idempotence%20in%20GoCql%20driver&source=) 

We have recently helped to implement a new feature in the **gocql** driver for Apache Cassandra that allows a user to mark queries as **idempotent**. In this post we’ll cover what idempotent queries are and how they can be used.

**Query Idempotence**
---------------------

The query is defined as idempotent if the repeated execution would render the same result each time. That is, it can be applied multiple times without changing the result of the initial operation. For example, the following query would be idempotent:

`<span style="font-weight: 400;">UPDATE my_table SET col=2 WHERE pk=1</span>`

On the other hand, the following query would not be idempotent:

`<span style="font-weight: 400;">UPDATE my_table SET col=col+1 WHERE pk=1 (as col value changes with every query)</span>`

Idempotence is important for **retries** and **speculative queries**. If the query is idempotent, it is safe and in some cases could be useful to retry it, knowing that when it succeeds, the result is the expected one.

**Speculative query execution**
-------------------------------

Knowing that idempotent queries can be safely retried, we can run the same query on a multiple nodes in parallel, only waiting for the first response of success. This potentially can speed up query execution, especially if some node is busy, has network issues or is unresponsive. We can send the query to another node if the current is taking too long to respond. This way of query execution is called speculative query execution, and clearly it heavily relies on query idempotence. Unfortunately, it is not yet available in gocql driver.

As a result, we decided to implement query idempotence in the gocql driver, and then implement speculative query execution as a follow up.

**Marking query as idempotent in gocql driver**
-----------------------------------------------

In order to mark the query as idempotent with the gocql driver, pull the latest master if you haven’t for a while, and then use the following example:

For a specific query:































package main import ( "fmt" "log" "github.com/gocql/gocql" ) func main() { // connect to the cluster cluster := gocql.NewCluster("127.0.0.1") cluster.Keyspace = "demo" session, \_ := cluster.CreateSession() defer session.Close() // insert a user query := session.Query("INSERT INTO users (lastname, age, city, firstname) VALUES ('Jones', 35, 'Austin', 'Bob') WHERE email='bob@example.com'") query.Idempotent(true) if err := query.Exec(); err != nil { log.Fatal(err) } ...

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23



  package main



import (

 "fmt"

 "log"



 "github.com/gocql/gocql"

)



func main() {

 // connect to the cluster

 cluster := gocql.NewCluster("127.0.0.1")

 cluster.Keyspace = "demo"

 session, \_ := cluster.CreateSession()

 defer session.Close()



 // insert a user

 query := session.Query("INSERT INTO users (lastname, age, city, firstname) VALUES ('Jones', 35, 'Austin', 'Bob') WHERE email='bob@example.com'")

 query.Idempotent(true)

 if err := query.Exec(); err != nil {

 log.Fatal(err)

 }

 ...



   

 

 For **all** the queries for this cluster:































import ( "fmt" "log" "github.com/gocql/gocql" ) func main() { // connect to the cluster cluster := gocql.NewCluster("127.0.0.1") cluster.DefaultIdempotence = true ...

   1

2

3

4

5

6

7

8

9

10

11

12



  import (

 "fmt"

 "log"



 "github.com/gocql/gocql"

)



func main() {

 // connect to the cluster

 cluster := gocql.NewCluster("127.0.0.1")

 cluster.DefaultIdempotence = true

 ...



   

 

 These examples provide the basis for the future work to implement speculative query execution in gocql driver. By starting to use the idempotency settings now you will be able to take advantage of the speculative execution functions when they are available.

Further reading:

- GoCql tickets: <https://github.com/gocql/gocql/issues/1083> for further related information and for progress on this work.

 

 [ Add Instaclustr as a preferred source on Google ](https://google.com/preferences/source?q=instaclustr.com)



 

 ![mail icon]()#### Get the latest articles for open sourceIn your inbox

 <a class="btn btn-primary btn-popup text-dark" href="">Sign up now</a> 

 

 

 

  ### Related content

 [ Zero Downtime Migration to Instaclustr 

 

 Yes, we can migrate existing Cassandra clusters to Instaclustr without any downtime. Here's what to expect from the process... 

 

 

 

 

 

 

 ](https://www.instaclustr.com/blog/zero-downtime-migration-to-instaclustr/) 

 [ Workflow Comparison: Uber Cadence vs Netflix Conductor 

 

 When choosing what’s right for your company’s opensource workflow needs it is important to know the difference and similarities ... 

 

 

 

 

 

 

 ](https://www.instaclustr.com/blog/workflow-comparison-uber-cadence-vs-netflix-conductor/) 

 [ Will Your Cassandra Database Project Succeed?: The New Stack 

 

 Open source Apache Cassandra® continues to stand out as an enterprise-proven solution for organizations seeking high availability... 

 

 

 

 

 

 

 ](https://www.instaclustr.com/blog/will-your-cassandra-database-project-succeed-the-new-stack/) 

 

  <a class="close-modal" href="">×</a>Sign upto ourNewsletter
-----------------------
