How to Work on Redis Instances and Keys?

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 the number of databases

  • Check the number of databases set in Redis .conf file
    The path to .conf file /etc/redis/redis.conf

  • 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.

  • List all keys in database db0
    key *
    1-8
     are total 8 keys in db0 that we checked on last 

  • Check key type
    type <key>
    Here <key> is name i.e. series of numbers
    In the output you will get the type of key. For us its hash

    for “string“: get <key>
    for “hash“: hgetall <key>
    for “lrange <key> 0 -1
    for “set“: smembers <key>
    for “zset“: zrange <key> 0 -1 withscores
  • Check the content of key type hash
    hgetall <key>

  • Check the content of key type <strong">string

  • Change the current database to db1
    select <db_id>
    Prompt will show db_id within square brackets


    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

  • Change the current database and add some keys and delete them later
    Change the working database
    select 15

    Check Number of keys and databases holding data
    INFO keyspace

    Save key ‘anything’ with value abc

    Check Number of keys and databases holding data
    INFO keyspace
    Here in db15, there is one key added

    Save key ‘hello’ with value world

    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

    Remove key ‘hello‘ and check keyspace
    Total one key is left in db15

    Remove key ‘anything‘ and check keyspace
    No keys left ins db15

  • Increase Number of databases from 16 as default to 40 or your choice.
    Check current databases.
    CONFIG GET databases
    Here 16 is the number of 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.
    We will change 16 to 40 and restart redis server.

    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

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)