• Stars
    star
    358
  • Rank 118,855 (Top 3 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 13 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Redis sharding client library

Redis Shard

Build Status Latest Version Supported Python versions License

A Redis sharding implementation.

Redis is great. It's fast, lightweight and easy to use. But when we want to store a mass of data into one single instance, we may encounter some problems such as performance degradation and slow recovery and we need to scale it.

Usage

First, Create an RedisShardAPI instance with multiple nodes, node name must be unique:

from redis_shard.shard import RedisShardAPI

servers = [
    {'name': 'server1', 'host': '127.0.0.1', 'port': 10000, 'db': 0},
    {'name': 'server2', 'host': '127.0.0.1', 'port': 11000, 'db': 0},
    {'name': 'server3', 'host': '127.0.0.1', 'port': 12000, 'db': 0},
]

client = RedisShardAPI(servers, hash_method='md5')

Then, you can access the Redis cluster as you use redis-py:

client.set('test', 1)
client.get('test')  # 1
client.zadd('testset', 'first', 1)
client.zadd('testset', 'second', 2)
client.zrange('testset', 0, -1)  # [first, second]

Hash Tags

If you want to store specific keys on one node for some reason (such as you prefer single instance pipeline, or you want to use multi-keys command such as sinter), you should use Hash Tags:

client.set('foo', 2)
client.set('a{foo}', 5)
client.set('b{foo}', 5)
client.set('{foo}d', 5)
client.set('d{foo}e', 5)

client.get_server_name('foo') == client.get_server_name('a{foo}') == client.get_server_name('{foo}d') \
    == client.get_server_name('d{foo}e')  # True

The string in a braces of a key is the Hash Tag of the key. The hash of a Hash Tag will be treated the hash of the key. So, keys foo, bar{foo} and b{foo}ar will be sotred in the same node.

Note

Hash Tags are not supported with bytes keys in Python 3.

Tag method

Just add tag_ prefix, you can use any of the normal redis method on the same hash tag:

client.tag_mget("{user:1}question1", "{user:1}question2")
client.tag_delete("{user:1}question1", "{user:1}question2")

Multi-keys method

Only support mget, mset and flushdb.

Config Details

There are three parameters servers, hash_method and sentinel in the class redis_shard.shard.RedisShardAPI.

servers is a list. Each element in it should be a dict or a URL schema.

  • dict:

    [
        {'name': 'server1', 'host': '127.0.0.1', 'port': 10000, 'db': 0},
        {'name': 'server2', 'host': '127.0.0.1', 'port': 11000, 'db': 0, 'max_connections': 50},
        {'name': 'server3', 'host': '127.0.0.1', 'port': 12000, 'db': 0},
    ]
    
  • URL schema:

    [
        'redis://127.0.0.1:10000/0?name=node1',
        'redis://127.0.0.1:11000/0?name=node2&max_connections=50',
        'redis://127.0.0.1:12000/0?name=node3'
    ]
    

If the following parameter sentinel is enabled, only name is needed for the servers config.

hash_method is a string which indicate the method of generating the hash key of the consistent hash ring. The default value is crc32. It also supports md5 and sha1.

sentinel is the config for Redis Sentinel. With the sentinel support, redis-shard will do read/write splitting. Config is like this:

{"hosts": [('localhost', 26379)], "socket_timeout": 0.1}

Limitations

  • Redis Shard dose not support all Redis commands.
  • As mentioned above, Redis Shard does not support all multi-keys commands crossing different nodes, you have to use Hash Tag to work with those commands.
  • Redis Shard does not have any replication mechanism.

How it Works

Redis Shard is basically inspired by this article.

More Repositories

1

Matisse

🎆 A well-designed local image and video selector for Android
Java
12,518
star
2

griffith

A React-based web video player
TypeScript
2,501
star
3

kids

Kids Is Data Stream
C++
1,225
star
4

rucene

Rust port of Lucene
Rust
1,005
star
5

cuBERT

Fast implementation of BERT inference directly on NVIDIA (CUDA, CUBLAS) and Intel MKL
C++
522
star
6

RxLifecycle

Bind observables to the lifecycle of Activity or Fragment in a non-invasive way.
Java
515
star
7

zhihu-rxjava-meetup

知乎 x RxJava Meetup
358
star
8

mirror

Yet another Sketch Mirror App for Android.
Java
272
star
9

SugarAdapter

Make RecyclerView.Adapter Great Again!
Java
235
star
10

zetta

Zetta Table Store
Go
130
star
11

norm

An orm library support nGQL for Golang
Go
103
star
12

tache

A tag based invalidation caching library
Python
77
star
13

promate

Graphite On VictoriaMetrics
Go
69
star
14

SERank

An efficient and effective learning to rank algorithm by mining information across ranking candidates. This repository contains the tensorflow implementation of SERank model. The code is developed based on TF-Ranking.
Python
50
star
15

cmdb

Programmable CMDB
Go
18
star
16

chaika

Elastic cache solution on Kubernetes
11
star
17

TLLM_QMM

TLLM_QMM strips the implementation of quantized kernels of Nvidia's TensorRT-LLM, removing NVInfer dependency and exposes ease of use Pytorch module. We modified the dequantation and weight preprocessing to align with popular quantization alogirthms such as AWQ and GPTQ, and combine them with new FP8 quantization.
C++
10
star
18

zetta-proto

Protobuf files for Zetta Table Store
Shell
7
star
19

presto-connectors

Presto Connectors project has been moved to TiBigData at PingCAP Incubator
6
star
20

zetta-client-go

Go client for Zetta Table Store
Go
4
star