Quick start

After creating a local cluster, follow the instructions below to test YugabyteDB’s Redis-compatible YEDIS API.

redis-cli is a command line interface to interact with a Redis server. For ease of use, YugabyteDB ships with the 4.0.1 version of redis-cli in its bin directory.

1. Initialize YEDIS API & Connect with redis-cli

  • Initialize the YEDIS API.

Setup the redis_keyspace keyspace and the .redis table so that this cluster becomes ready for redis clients. Detailed output for the setup_redis command is available in the yb-ctl Reference.

  1. $ ./bin/yb-ctl setup_redis
  • Run redis-cli to connect to the service.
  1. $ ./bin/redis-cli
  1. 127.0.0.1:6379>
  • Run a Redis command to verify it is working.
  1. 127.0.0.1:6379> PING
  1. "PONG"
  • Initialize the YEDIS API.

Setup the redis_keyspace keyspace and the .redis table so that this cluster becomes ready for redis clients. Detailed output for the setup_redis command is available in the yb-ctl Reference.

  1. $ ./bin/yb-ctl setup_redis
  • Run redis-cli to connect to the service.
  1. $ ./bin/redis-cli
  1. 127.0.0.1:6379>
  • Run a Redis command to verify it is working.
  1. 127.0.0.1:6379> PING
  1. "PONG"
  • The yb-docker-ctl utility initializes the YEDIS API automatically.

  • Run redis-cli to connect to the service.

  1. $ docker exec -it yb-tserver-n1 /home/yugabyte/bin/redis-cli
  1. 127.0.0.1:6379>
  • Run a Redis command to verify it is working.
  1. 127.0.0.1:6379> PING
  1. "PONG"
  • Initialize YEDIS API in the YugabyteDB universe we just setup by running the following yb-admin command.
  1. $ kubectl exec -it yb-master-0 /home/yugabyte/bin/yb-admin -- --master_addresses yb-master-0.yb-masters.default.svc.cluster.local:7100 setup_redis_table
  1. ...
  2. I0127 19:38:10.358551 115 client.cc:1292] Created table system_redis.redis of type REDIS_TABLE_TYPE
  3. I0127 19:38:10.358872 115 yb-admin_client.cc:400] Table 'system_redis.redis' created.
  • Run redis-cli to connect to the service.

You can do this as shown below.

  1. $ kubectl exec -it yb-tserver-0 /home/yugabyte/bin/redis-cli
  1. 127.0.0.1:6379>
  • Run a Redis command to verify it is working.
  1. 127.0.0.1:6379> PING
  1. "PONG"

2. Simple key-value types

Insert a key and a value.

  1. 127.0.0.1:6379> set mykey somevalue
  1. "OK"

Query the value by the key.

  1. 127.0.0.1:6379> get mykey
  1. "somevalue"

Check if the key exists.

  1. 127.0.0.1:6379> exists mykey
  1. (integer) 1

If the value is a number, it can be incremented.

  1. 127.0.0.1:6379> set counter 100
  1. "OK"
  1. 127.0.0.1:6379> incr counter
  1. (integer) 101
  1. 127.0.0.1:6379> incr counter
  1. (integer) 102
  1. 127.0.0.1:6379> get counter
  1. "102"

3. Hash data types

You can create a Redis Hash data type as follows. This models the data for user id 1000 with the following attributes {username : john, birthyear : 1977, verified : 1}.

  1. 127.0.0.1:6379> hmset user:1000 username john birthyear 1977 verified 1
  1. "OK"

You can retrieve specific attributes for user id 1000 as follows.

  1. 127.0.0.1:6379> hget user:1000 username
  1. "john"
  1. 127.0.0.1:6379> hget user:1000 birthyear
  1. "1977"

You can fetch multiple attributes with a single command as follows.

  1. 127.0.0.1:6379> hmget user:1000 username birthyear no-such-field
  1. 1) "john"
  2. 2) "1977"
  3. 3) (nil)

You can fetch all attributes by using the hgetall command.

  1. 127.0.0.1:6379> hgetall user:1000
  1. 1) "birthyear"
  2. 2) "1977"
  3. 3) "username"
  4. 4) "john"
  5. 5) "verified"
  6. 6) "1"

Ask our community