Giter VIP home page Giter VIP logo

cb-blog-timed-tasks's Introduction

Timed tasks with Go and Couchbase

NOTE: This code's discussion and overview can be found in Couchbase Blog's post Timed tasks using Couchbase and Go.

The goal of the project is to demonstrate how to use specific Couchbase features (pessimistic-locking, indexing, N1QL) to implement a distributed timed tasks system.

The project consists of the following programs:

  • Producer - inserts tasks into Couchbase.
  • Consumer - retrieves tasks and processes them.
  • Consumer cluster - runs multiple consumers in parallel.

Setup

Couchbase

  1. cd into docker folder.
  2. Run docker compose up to deploy a local Couchbase server.
  3. Wait ~20 seconds for complete initialization.

Timed tasks

  1. Install govendor: go get -u github.com/kardianos/govendor.
  2. Install dependencies with govendor sync.
  3. Edit cmd/shared/config.go producer and consumer variables as desired.

Note: a big enough amount of samples (>= 1000) is suggested, to gather correct statistics.

Run

  1. Start the producer.
  2. Start the consumer cluster.
  3. Wait for both programs to automatically terminate (if configured), or press Enter and wait for them to exit.

Note: execution could take up to 1-2 minutes, depending on configuration.

Run with Windows PowerShell:

go build cmd\producer\producer.go ; .\producer.exe ; rm producer.exe
go build cmd\consumer_cluster\consumer_cluster.go ; .\consumer_cluster.exe ; rm consumer_cluster.exe

Run with bash:

go build cmd/producer/producer.go && ./producer && rm producer
go build cmd/consumer_cluster/consumer_cluster.go && ./consumer_cluster && rm consumer_cluster

Statistics

When the consumer cluster execution terminates gracefully, a statistics table will be printed.

Low concurrency

consumerProcessingTime = 500
consumerProcessingTimeRandomMultiplier = 0.25

When consumers have a low concurrency factory, there will be less failed locking attempts.

  totFound|  totLockedFound|  totProcessed|  couldNotLock|  loopEfficiency (%)|  lockEfficiency (%)|
      1060|             108|           106|             0|          100.000000|           49.532711|
      1050|             121|           105|             0|          100.000000|           46.460175|
      1050|             110|           105|             0|          100.000000|           48.837208|
      1060|              94|           106|             0|          100.000000|           52.999996|
      1050|             105|           105|             0|          100.000000|           50.000000|
      1050|              96|           105|             0|          100.000000|           52.238804|
      1050|             113|           105|             0|          100.000000|           48.165138|
      1050|             106|           105|             0|          100.000000|           49.763031|
      1060|             101|           106|             0|          100.000000|           51.207726|
      1060|             103|           106|             0|          100.000000|           50.717705|

High concurrency

consumerProcessingTime = 100
consumerProcessingTimeRandomMultiplier = 1.0

When consumers have a high concurrency factory, there will be more failed locking attempts.

  totFound|  totLockedFound|  totProcessed|  couldNotLock|  loopEfficiency (%)|  lockEfficiency (%)|
      2540|            1043|           253|             1|           99.606300|           19.521605|
      2600|            1056|           259|             1|           99.615387|           19.695816|
      2540|            1067|           254|             0|          100.000000|           19.227858|
      2540|            1080|           254|             0|          100.000000|           19.040480|
      2560|             985|           255|             1|           99.609375|           20.564516|
      2530|            1044|           252|             1|           99.604744|           19.444445|
      2570|            1029|           257|             0|          100.000000|           19.984447|
      2580|            1052|           258|             0|          100.000000|           19.694656|
      2550|            1009|           255|             0|          100.000000|           20.174049|
      2510|            1069|           251|             0|          100.000000|           19.015152|

cb-blog-timed-tasks's People

Contributors

cmaster11 avatar

Stargazers

 avatar  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.