redis常见技巧

本文介绍了redis常见的一些技巧。

redis 命令行相关

当搭建好一个redis 或是twemproxy之后,可以直接采用如下命令来操作redis集群

1
2
# 连接上redis server
redis-cli -p port

剩下的就是操作redis的命令了
命令参考 https://redis.io/commands

Error: Protocol error, got “{“ as reply type byte

It is because twemproxy will listen on two ports when it starts, one of them is port 22222(by default, you can set it with ‘-s’), it is used for monitoring, if you connect to it, the error occurs

So, connect to another port

参考:https://stackoverflow.com/questions/27688394/twemproxynutcracker-error-protocol-error-got-as-reply-type-byte

Hash Tags 功能

Hash Tags enables you to use part of the key for calculating the hash. When the hash tag is present, we use part of the key within the tag as the key to be used for consistent hashing. Otherwise, we use the full key as is. Hash tags enable you to map different keys to the same server as long as the part of the key within the tag is the same.

For example, the configuration of server pool beta, also shown below, specifies a two character hash_tag string - “{}”. This means that keys “user:{user1}:ids” and “user:{user1}:tweets” map to the same server because we compute the hash on “user1”. For a key like “user:user1:ids”, we use the entire string “user:user1:ids” to compute the hash and it may map to a different server.

1
2
3
4
5
6
7
8
9
10
11
12
13
beta:
listen: 127.0.0.1:22122
hash: fnv1a_64
hash_tag: "{}"
distribution: ketama
auto_eject_hosts: false
timeout: 400
redis: true
servers:
- 127.0.0.1:6380:1 server1
- 127.0.0.1:6381:1 server2
- 127.0.0.1:6382:1 server3
- 127.0.0.1:6383:1 server4

参考链接:https://github.com/twitter/twemproxy/blob/master/notes/recommendation.md#hash-tags