Guide to Work on Redis Instances and Keys

Category : Redis

Want to learn more about Redis Instances, Keys, and databases? In this guide, we will teach and show some Redis stuff which will increase your knowledge Redis Keys and Instances.

In single Redis instance, there are 16 databases by default. These databases are identified/denoted by a number and not by name and these numbers are called index numbers. Though you can always increase the number of databases. We have covered this at last leg of the tutorial.

  • Check Number of Databases in your current Redis instance configuration
    CONFIG GET databases
    Here 16 is number of databases
    config get databases
  • Check the number of databases set in Redis .conf file
    The path to .conf file /etc/redis/redis.conf
    redis config
  • Check Number of keys and databases holding data
    INFO keyspace
    db0 is database and is index number for the particular database
    db0 has 8 keys inside it.
    info keyspace
  • List all keys in database db0
    key *
    1-8
     are total 8 keys in db0 that we checked on last 
    list all keys
  • Check key type
    type <key>
    Here <key> is name i.e. series of numbers
    In output you will get the type of key. For us its hash
    type of key
    for “string“: get <key>
    for “hash“: hgetall <key>
    for “list“: lrange <key> 0 -1
    for “set“: smembers <key>
    for “zset“: zrange <key> 0 -1 withscores
  • Check content of key type hash
    hgetall <key>
    hgetall key
  • Check the content of key type string
    get string
  • Change current database to db1
    select <db_id>
    Prompt will show db_id within square brackets
    seleclt db
    seleclt db
    seleclt db
  • Check if the database can be switched outside the configuration range
    Our instance has total 16 Databases with the index from 0-15. Here we will try switching to Database with index number 16 which means the 17th database
    select 16
    seleclt db
  • Change current database and add some keys and delete them later
    Change working database
    select 15
    seleclt db
    Check Number of keys and databases holding data
    INFO keyspace
    info keyspace
    Save key ‘anything’ with value abc
    set and get command
    Check Number of keys and databases holding data
    INFO keyspace
    Here in db15 there is one key added
    info keyspace
    Save key ‘hello’ with value world
    set and get command
    Check Number of keys and databases holding data
    INFO keyspace
    Here in db15 there is one more key is added and total keys now are two
    check keyspace
    Remove key ‘hello‘ and check keyspace
    Total one key is left in db15
    del and get and check keyspace
    Remove key ‘anything‘ and check keyspace
    No keys left ins db15
    del and get keyspace
  • Increase Number of databases from 16 as default to 40 or your choice.
    Check current databases.
    CONFIG GET databases
    Here 16 is number of databases
    config get databases
    Edit Redis .conf file located at /etc/redis/redis.conf
    Search for ‘Set the number of databases’
    16 here can differ with any number.databases change to 40
    We will change 16 to 40 and restart redis server.
    databases 16
    Run the following command to shutdown redis-server
    redis-cli shutdown
    Start redis-server using your configuration file.
    redis-server /etc/redis/redis.conf
    Check current databases.
    CONFIG GET databases
    Here 40 is number of databases which means index number 0-39
    40 databases