Git Product home page Git Product logo

nydus's Introduction

Nydus

Generic database utilities, including connection clustering and routing so you can scale like a pro.

The following example creates a Redis connection cluster which will distribute reads and writes based on a simple modulus lookup of the hashed key:

>>> from nydus.db import create_cluster
>>>
>>> redis = create_cluster({
>>>     'backend': 'nydus.db.backends.redis.Redis',
>>>     'router': 'nydus.db.routers.keyvalue.PartitionRouter',
>>>     'hosts': {
>>>         0: {'db': 0},
>>>         1: {'db': 1},
>>>         2: {'db': 2},
>>>     }
>>> })
>>>
>>> res = redis.incr('foo')
>>>
>>> assert res == 1

nydus.db.create_cluster

The create_cluster function is a simple helper to configure a Cluster based on a simple dict config.

There are two required keyword arguments:

  • backend: full path to the backend class, which should extend nydus.db.backends.BaseConnection.

  • hosts: a dictionary, where the key is an ordered numeric value, and the result is a dict of connection options.

    (the keys are numeric values simply for readability in configuration)

  • defaults: a dictionary of default settings for all hosts to inherit from.

Optionally, you may also specify a value for router, which is the full path to the router class, which must extend nydus.db.routers.BaseRouter.

Distributed Queries

In some cases you may want to execute a query on many nodes (in parallel). Nydus has built-in support for this when any routing function returns a cluster of nodes:

>>> from nydus.db import create_cluster
>>>
>>> # by not specifying a router all queries are executed on all hosts
>>> redis = create_cluster({
>>>     'backend': 'nydus.db.backends.redis.Redis',
>>>     'hosts': {
>>>         0: {'db': 0},
>>>         1: {'db': 1},
>>>         2: {'db': 2},
>>>     }
>>> })
>>>
>>> # map the query over all connections returned by the default router
>>> res = redis.incr('foo')
>>>
>>> assert type(res) == list
>>> assert len(res) == 3

You can also map many queries (utilizing an internal queue) over connections (again, returned by the router):

>>> with redis.map() as conn:
>>>     results = [conn.incr(k) for k in keys]

As of release 0.5.0, the map() function now supports pipelines, and the included Redis backend will pipeline commands wherever possible.

Redis

Nydus was originally designed as a toolkit to expand on the usage of Redis at DISQUS. While it does provide a framework for building clusters that are not Redis, much of the support has gone into providing utilities for routing and querying on Redis clusters.

You can configure the Redis client for a connection by specifying it's full path:

>>> redis = create_cluster({
>>>     'backend': 'nydus.db.backends.redis.Redis',
>>>     'hosts': {
>>>         0: {'db': 0},
>>>     },
>>> )}

The available host options are:

  • host
  • port
  • db
  • timeout
  • password

The Redis client also supports pipelines via the map command. This means that all commands will hit servers at most as of once:

>>> with redis.map() as conn:
>>>     conn.set('a', 1)
>>>     conn.incr('a')
>>>     results = [conn.get('a'), conn.get('b')]
>>> results['a'] == 2
>>> results['b'] == None

Simple Partition Router

There are also several options for builtin routing. The easiest is a simple partition router, which is just a simple hash on the key:

>>> redis = create_cluster({
>>>     'backend': 'nydus.db.backends.redis.Redis',
>>>     'router': 'nydus.db.routers.keyvalue.PartitionRouter',
>>>     'hosts': {
>>>         0: {'db': 0},
>>>     },
>>> )}

Consistent Hashing Router

An improvement upon hashing, Nydus provides a Ketama-based consistent hashing router:

>>> redis = create_cluster({
>>>     'backend': 'nydus.db.backends.redis.Redis',
>>>     'router': 'nydus.db.routers.keyvalue.ConsistentHashingRouter',
>>>     'hosts': {
>>>         0: {'db': 0},
>>>     },
>>> )}

Round Robin Router

An additional option for distributing queries is the round robin router:

>>> redis = create_cluster({
>>>     'backend': 'nydus.db.backends.redis.Redis',
>>>     'router': 'nydus.db.routers.RoundRobinRouter',
>>>     'hosts': {
>>>         0: {'db': 0},
>>>     },
>>> )}

nydus's People

Contributors

ageron avatar dcramer avatar dctrwatson avatar fluxx avatar gjcourt avatar mikeclarke avatar northisup avatar tkaemming avatar

Stargazers

 avatar  avatar

Watchers

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