DEV Community

Cover image for Redis 2
π™΄πš£πšŽπš”πš’πšŽπš•
π™΄πš£πšŽπš”πš’πšŽπš•

Posted on β€’ Edited on

1 1

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 "Joe@joe.com" # Adds the key-value pair {email : Joe@joe.com} to the hashmap
HSET person phone_number "+2345656655413" # Adds the key-value pair {number : ....} to the hashmap
Enter fullscreen mode Exit fullscreen mode

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

HGET person name # returns "Joe"
Enter fullscreen mode Exit fullscreen mode

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

HGETALL person 

1) "name"
2) "Joe"
3) "email"
4) "Joe@joe.com"
5) "phone_number"
6) "+2345656655413"
Enter fullscreen mode Exit fullscreen mode

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

HDEL person name # removes {name : joe}
Enter fullscreen mode Exit fullscreen mode

the HEXISTS command checks if a key exists in a hashset

HEXISTS person name # returns 0 because we've deleted it before
Enter fullscreen mode Exit fullscreen mode

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

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more