Git Product home page Git Product logo

couchdb-locks's Introduction

Locks for CouchDB

This is just experimental locking strategy.

Currently works on single node. Any ideas how to scale?

API

API is simply RESTful, just like Document API.

Acquire a single lock for the scope

curl -X POST http://localhost:5984/DB/_locks?scope=Person&timeout=3000

201 - {"ok":true,"id":"0510eb5409dd5d3cb41fb2773f5e253e"}

Lock already exist:

409 - {"error":"conflict","reason":"Already exist"}

Create document

curl -X POST http://localhost:5984/DB/\_locks/\_db/?lock=0510eb5409dd5d3cb41fb2773f5e253e -d '{"type":"Person","name":"John"}'

CouchDB replies:

201 - {"ok":true,"id":"5ca03037c83797a2457d13efba000c10","rev":"1-ac5e2cfb85fb3ddfc21d91334021b649"}

If lock has expired or does not exist:

400 - {"error":"bad_request","reason":"Lock has expired or does not exist"}

Explicitly release a lock

curl -X DELETE http://localhost:5984/db/_locks/0510eb5409dd5d3cb41fb2773f5e253e

CouchDB replies:

{"ok":true}

List locks

curl -X GET http://localhost:5984/DB/_locks

CouchDB replies:

200 - {"total_rows":1,"rows":[{"scope":"Person","id":"0510eb5409dd5d3cb41fb2773f5e253e"}]}

Use cases

Validate uniqueness:

  • Lock with scope "Person",
  • Ensure that there is no such person with fields intended to be unique (query your views),
  • Create document.

Convention is a key. You can use any token as scope. Use "Person" to lock all documents of type "Person". If you want to update a Person simply don't lock unless email or login fields are dirty.

Every single lock allows only one operation, each POST, PUT, DELETE or Bulk Document API request will release the lock. Each lock has a timeout, this means that you should not be slow, otherwise your update will be rejected. Deadlocks are impossible.

Create an order, reduce Item#units_in_stock:

  • Lock with scope "Item",
  • Check each Item for availability,
  • Create an order document and update each Item via Bulk Document API.

As a bonus, conflict is impossible if all your updates are going through this API with the same scope.

Benefits

  • Simple way to keep data in consistent state, unless you are using offline-replication,
  • Uniqueness is possible,
  • Prevent conflicts.

How it works?

From client's point of view:

  • Try to create a lock,
  • Receive successful response,
  • Perform a number of GET or HEAD requests,
  • Perform your POST, PUT, DELETE or Bulk Document API request.

Failing scenario:

  • Try to create already existing lock,
  • Receive "409 Conflict",
  • Wait and try again.

Installation

See INSTALL.md for installation instructions.

couchdb-locks's People

Contributors

wildchild avatar

Stargazers

Brad Rhoads avatar Michael Gorsuch avatar  avatar Fabien Franzen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.