string in redis
string in redis

The String is the most basic datatype of Redis

  • Redis string represents a sequence of bytes.
  • One Redis String can have a maximum value of 512MB

if you see the above representation then you can see that values are stored as a string.

  • user_profile information is JSON and it is converted to string and then it can be stored inside Redis
  • site_visit_count is a number that can also be stored as a String.

Commands Used on Redis String Datatype

SET COMMAND

  • SET command is used to set the String value against a key.
  • SET command will create the key automatically
  • If the key and value is already existing then it will replace the value against that key.
  • Response of SET command
    • If the SET command returns OK then it means success
    • If the SET command returns nil then it means failure
SET <key-name> <value>
  • The first parameter is the key name
  • The second parameter is the value to be stored against that key.

GET COMMAND

  • GET command is used to fetch the value stored against a key
  • Response of GET command
    • If the key is existing then it will return the value stored against the key
    • If the key does not exist then it will return a nil
GET <key-name>

GETSET COMMAND

  • It sets the new value in the key and returns the old value
  • Response of GETSET comand
    • If the key already exists then it will return the old value stored against the key
    • If the key doesn’t exist then it will return nil and set the key with the given value
GETSET <key-name>

INCR COMMAND

  • This command is used to increment the current value by 1
  • Response of INCR command
    • If the key doesn’t exist then it will create the key and set the value to 1 and also return 1 as response
    • If the key exists then it will increment the value by 1 and return the current value stored in the key.
INCR <key-name>

MSET COMMAND

  • This command helps you to SET multiple keys and values at a single time
  • Response of MGET command
    • It returns OK if all the keys and values are inserted in the Redis
    • It returns nil if the command fails
MSET <key-1> <value-1> <key-2> <value-2> <key-3> <value-3>

MGET COMMAND

  • This command is used to retrieve values of multiple keys at one time
  • Response
    • Returns all the values of the keys. If any key doesn’t exist then it returns nil for that key

Usage of String Datatype in Redis

  • Caching
    • String Datatype can be used for storing values for example
      • user profile information can be stored as a string
      • shopping cart items can be stored as a string
      • A whole HTML fragment can be stored as a string
  • Counters: You can create counters with the help of string datatype.
    • For example, if you want to know how many users have visited your website in a day then you can create a counter with key site-visit-count and store the value as 0. Then for every new visitor, you can increment the counter value.
      • Note: You don’t need to do string-to-integer conversion before incrementing the counter. This will be automatically handled by Redis

Important Learnings

What will the SET command do if a key with a value already exists?

  • SET command will replace the value that already exists against the key.

Is it possible to fail the SET command if a key with value is already existing?

  • Yes, this is possible.
  • we can pass options to the SET command. One such option is nx which means not exist. If we pass nx as an option then it will fail if the key is already existing.
  • If the SET command returns nil as the response then it means that the command has failed. If it returns ok then it means that the command has been successfully executed.
SET <key-name> <value> nx

Is it possible to fail the SET command if the key does not exist already?

  • This means that the SET command will successfully run if the key already exists
  • Yes this is possible
  • we can pass options to the SET command. One such option is xx which means already exists. If we pass xx as an option then it will be a success if the key is already existing.
  • If the SET command returns nil as the response then it means that the command has failed. If it returns ok then it means that the command has been successfully executed

References

Next: Redis Publish Subscribe

Leave a Reply

Your email address will not be published. Required fields are marked *