Skip to content

Instantly share code, notes, and snippets.

@vmx
Last active December 21, 2015 01:58
Show Gist options
  • Save vmx/6231565 to your computer and use it in GitHub Desktop.
Save vmx/6231565 to your computer and use it in GitHub Desktop.
This is how an implementation of the Read Your Own Writes could look like on the application level.

Read Your Own Write implementation specification

This specification outlines the details on how Read Your Own Write (RYOW). The provided code will be using reflect how an implementation in Python might look like.

Client/server architecture

Throughout the spec there's a common architecture of the system, that consists of three parts:

Client

In a typical web application the client will be the browser. It interacts through HTTP with the Application.

Application

The application is the business logic that runs server-sided and call out to Couchbase. Couchbase itself consists of two parts, the Storage and the Indexer.

Couchbase

Couchbase is the server. The details on what happens within Couchbase can be found in the RYOW specification

Implementation options

There are several options on how to handle the RYOW when you interact with Couchbase. Couchbase will return an opaque token on any mutations like inserts or updates. This token can either be handled by the SDKs Couchbase provides, by the application or by the client.

Passing to application

The application receives the token on a mutation and stores it in a variable. On the next view reuqest it's included.

# `cb` is the connection object
result = cb.set('key', 'value')
token = result.mutation_id
view_result = cb.query('ddocname', 'viewname', block_until=[token])

It gets harder when you want to make sure multiple mutations are included in the next view request, you would need to keep track of all tokens

# `cb` is the connection object
result1 = cb.set('key1', 'value')
result2 = cb.set('key2', 'value')
tokens = [result1.mutation_id, result2.mutation_id]
view_result = cb.query('ddocname', 'viewname', block_until=tokens)

Passing to client

You can easily imagine that the mutation_id as shown in the "Passing to application" section could easily be passed on to the client. The client would then include that token when it requests something from the application that would lead to a view query.

SDK deals with it

The SDKs can provide an API so that the RYOW is opaque to the application and it won't need to care about the details. It will be guaranteed that the most recent mutation is included in the subsequent view request.

# `cb` is the connection object
cb.block_view = True
cb.set('key1', 'value')
cb.set('key2', 'value')
# Internally in the SDK the `mutation_id` of all requests
# will be stored and supplied with the query
# The view request can clear out the supplied mutations as any subsequent
# request is also guaranteed to have seen the mutations.
view_result = cb.query('ddocname', 'viewname')

Changes within Couchbase

To make all that work, not only the view engine needs to change, but also the memcached protocol. An additional field that contains that token will be needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment