"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Redis 2

Redis 2

Published on 2024-08-15
Browse:294

Redis 2

Redis - Lists, Sets and HashMaps

This is a continuation to the previous blogpost

There are 3 other data types available apart from key-value pairs

Lists

This data structure resembles the list in Python or the array in Javascript or C#. They can be used to keep recently used items. The common operations are;

Operations Explanations Examples Further Explanation
LPUSH Adds an Item to the beginning of the list LPUSH friends "Sophia" similar to Array.unshift in javascript
LRANGE Get all the items in a list LRANGE friends 0 -1 similiarly a list in python 0 is the first item and -1 is the last item
RPUSH Adds an item to the end of the list RPUSH friends "Poe" similar to Array.push in javascript
LPOP Removes an item from the start of the list LPOP friends "Poe" Will return 1 if Poe exists in the list and 0 otherwise
RPOP Removes an item from the end of the list RPOP friends "Sophia" Will return 1 if Sophia exists in the list and 0 otherwise

Sets

A data structure that only has unique items. similar to sets in Python and Sets in Javascript and HashSets in C#. Common operations include;

Operations Explanations Examples Further Explanation
SADD Adds a value to the set SADD colors "pink"
SMEMBERS returns the members of the set SMEMBERS colors will return all the items in the set colors
SREM Removes members of the set SREM colors "pink" Will return 1 if pink exists in the list and 0 otherwise

Hashmaps

An hashmap is a group of key value pairs. An hashmap however cannot be nested. Lets take a case scenario of a person with a name, email and phone number

HSET person name "Joe" # Adds the key-value pair {name : joe} to the hashmap
HSET person email "[email protected]" # Adds the key-value pair {email : [email protected]} to the hashmap
HSET person phone_number " 2345656655413" # Adds the key-value pair {number : ....} to the hashmap

the HGET command can be used to get the value of a particular key in a hashmap

HGET person name # returns "Joe"

the HGETALL command all the key-value pair in the hashmap

HGETALL person 

1) "name"
2) "Joe"
3) "email"
4) "[email protected]"
5) "phone_number"
6) " 2345656655413"

the HDEL command deletes a key-value pair by its key

HDEL person name # removes {name : joe}

the HEXISTS command checks if a key exists in a hashset

HEXISTS person name # returns 0 because we've deleted it before

Those are most of the basic commands a developer is required to know.


Common Interview Questions

  1. What is the full meaning of Redis?: Redis stands for REmote DIctionary Server

  2. How does redis differ from traditional databases like MySQL?: Redis operates in main memory and have basic operations for accessing values quickly unlike SQL which resides on disk and has a wide range of crud operations

  3. What are the limitations of Redis regarding data size and type?: Redis is designed to hold values that fits into the machines memory. It is not suitable for complex relational models with joins or for structuring large blobs

  4. How do you handle caching in a distributed environment using Redis?: Handle caching by setting up redis instances as a caching layer in front of the database. using consistent hashing to distribute keys accreoss the cache nodes ensueres even load distribution and reduces cache misses

  5. When should you use List data types in Redis?: Lists are great for durable atomic queues, for job queue, logs, buffers and many other use cases

Release Statement This article is reproduced at: https://dev.to/ezekiel_77/redis-2-fec?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3