Tuesday, February 16, 2016

Redis & Jedis Quickstart

Hi,

In this post I would share some commands to help you get started quickly with Redis and Jedis provider.

To get started, you can download redis installation from:
If you use windows, use MSOpenTech version from:

Jedis (Redis java provider):


Connect to redis server


redis-cli -h [host] -p [port]

(default port is 6379)

Useful commands


INFO - Prints general details about the current server

GET [Key] - Gets a value of a key

SET [Key] [Value] - Sets a value of a key

SETEX [Key] [Seconds] [Value] – Sets key & value with expiration

RENAME [OldKeyName] [NewKeyName] - Renames a key

DEL [Key] [Key] .... - Removes given keys and returns the number of keys removes

APPEND [Key] [Value] – Append a value to a key

EXISTS [Key] - Returns 1 if the given key exists, 0 if not.

KEYS [regex] - Prints all keys matches a given regex pattern

KEYS * - Prints all keys

KEYS hello* - Prints all keys that starts with "hello"

TTL [Key] – Gets the remaining time of a key to live.

TIME  [Key] – Returns the time of the current server.

DBSIZE - Number of keys in db

FLUSHALL - Removes all keys & values

SLAVEOF [host] - Creates a slave to a master

SLAVEOF NO ONE - Promotes slave to master

ROLE - Tells whether current server is master or slave

CLUSTER INFO - Prints details regarding the redis cluster parameters like state, size etc.

CLUSTER NODES - Prints details regarding servers in the cluster 

CONFIG GET  * - Prints all configuration file data

CONFIG SET [Key] [Value] - Sets a value to a config key (Can be use in runtime)

CLIENT SETNAME [This client name] – Assigns a name to the current connection

CLIENT GETNAME – Gets the current connection name

CLIENT LIST - Prints information about client connections to the current server

QUIT - Closes connections to the server


SENTINEL:


"Redis Sentinel is a distributed system:

Sentinel itself is designed to run in a configuration where there are multiple 

Sentinel processes cooperating together. The advantages of having multiple 

Sentinel processes cooperating are the following:

1. Failure detection is performed when multiple Sentinels agree about the 

fact a given master is no longer available. This lowers the probability of 

false positives.

2. Sentinel works even if not all the Sentinel processes are working, 

making the system robust against failures. There is no fun in having a 

fail over system which is itself a single point of failure, after all."

(Taken from official redis documentation http://redis.io/topics/sentinel


Starting a redis server in sentinel mode:

redis-server /path/to/sentinel.conf --sentinel


Setting a sentinel option:

sentinel <option_name> <master_name> <option_value>


Setting a sentinel master:

sentinel monitor <master-group-name> <ip> <port> <quorum>


All these configuration should be placed in "sentinel.conf":

sentinel monitor mymaster 127.0.0.1 6379 2

sentinel down-after-milliseconds mymaster 60000

sentinel failover-timeout mymaster 180000

sentinel parallel-syncs mymaster 1


First config row - Sentinel configuration INCLUDING master (slaves are auto-discovered).
The last value ("2") is the quorum number (number of Sentinels that need to agree about the fact the master is not reachable).

Last config row - parallel-syncs is the number of slaves which will participate in configuring the new master after failover.
(Bigger number = Faster sync, but if the data is old in some of them, it might be written to the new master)


Jedis Java Provider


Common Pool Constructors:

JedisPool(Config poolConfig, String host)


JedisPool(Config poolConfig, String host, int host)


JedisSentinelPool(String masterName, Set<String> sentinels)

Parameters:

masterName – Name of the sentinel master given in "sentinel monitor" line in 

"sentinel.conf". In the example above it's "mymaster".

sentinels - Entire set of IP addresses of all sentinels (Master + Slaves).

For Example:

HashSet<String> sentinels = new HashSet<String>();

sentinels.add("127.0.0.1:1234");

JedisSentinelPool jedisPool = new JedisSentinelPool("mymaster", sentinels);


Get key example: 

Jedis jedis = jedisPool.getResource(); 

String result = jedis.get(key);                 

jedisPool.returnResource(jedis);

Set key example: 

Jedis jedis = jedisPool.getResource(); 

jedis.setex(key,secondsTillExpired, value); // setting a key and a value with expiration

jedisPool.returnResource(jedis);



CLUSTER


Redis Cluster provides a way to run a Redis installation where data is automatically 

sharded across multiple Redis nodes.

(Taken from official redis documentation http://redis.io/topics/cluster-tutorial).

2 comments:

Thank you Blogger, hello Medium

Hey guys, I've been writing in Blogger for almost 10 years this is a time to move on. I'm happy to announce my new blog at Med...