Redis : Setup and Usage

Shahwar Alam Naqvi
5 min readJan 31, 2024
Redis

What is it ?

Redis (Remote Dictionary Server).

An open-source ‘in memory data structure store’.

Advantage!

An in-memory data store, like Redis, stores and manages data primarily in the system’s random access memory (RAM). This approach offers significant advantages in terms of speed and low-latency access to data, as reading and writing to memory is faster compared to traditional disk-based storage systems. Basically, an in-memory data store such as Redis is designed to leverage the speed and efficiency of RAM for storing and retrieving data, making it well-suited for scenarios where quick access to data is crucial.

Disadvantages!

Data in memory is volatile and can be lost in the event of a system restart.

The size of the dataset is constrained by the available system memory. Large datasets that cannot fit in memory might not be suitable for in-memory databases.

Setting up!

  1. Have WSL installed.

a. WSL stands for Windows Subsystem for Linux. It’s a compatibility layer for running Linux binary executables natively on Windows. WSL allows users to run a Linux distribution directly on top of the Windows kernel. This enables developers and users to access Linux tools, utilities, and development environments on their Windows machines without needing a separate Linux installation.

wsl --install

b. Choose from the distributions, to install a distribution. So first list the distributions to choose from.

wsl --list --online or wsl -l -o

c. Install eg. Ubuntu

Sometimes installation of Ubuntu or any other distribution might not properly install. So an update command can follow.

d. Create a Username and Password

e. sudo apt update

sudo apt update refreshes the local package index from the repositories, ensuring that your system has the latest information about available packages and their versions.

2. Install Redis

sudo apt-get install redis

3. Start the redis server

sudo service redis-server start
or
redis server
redis-cli
Started redis.

Getting started with Redis:

  1. SET a key — value, then GET value.
  • SET is used to create a key-value pair.
  • GET is used to get value of a specific key in the argument.

2. DEL (delete) key-value.

  • age key is created with value 25
  • we use get to see what’s the value of age key.
  • we use del to delete the age key
  • then we try to get the value of a deleted key, but it’s no where to be found (so nil)

3. EXISTS (for a check).

  • we check if the key ‘age’ exists, ‘0’ comes up, which indicates that key with name ‘age‘ does not exist.
  • but the ‘name’ key we created before still exists, so ‘1’

4. KEYS * (lists all existing keys)

5. flushall.

  • eradicates all the keys that persists.

6. ttl.

  • means ‘time to live’
  • how long does the key live? (until it expires)
  • in below example, ‘-1’ indicates, it is ‘eternal’ or ‘never dies’
  • let’s set an expiry to it (eg 20 seconds & we will keep trying to to see amout of time left to it inorder to expire.)
  • do you think, ‘0’ is the end all ?
  • let’s continue..!
  • we see ‘-2’ persists from now on. It basically means that the key which was set to expiration is now really gone gone.

7. Lists (lpush, lrange)

  • lpush , adds items from the left.
  • lrange, helps list them all.

8. Lists (rpush, lrange).

  • rpush, adds items to the right.
  • lrange. displays all the items.

9. Lists (lpop, rpop)

  • lpop, removes items from the left.
  • rpop, removes items from right.

10. Sets (sadd, smembers).

  • sadd, adds elements to the set.
  • smembers, lists down all members in the set.

11. Sets (srem)

  • srem, removes items from the sets.

12. Hash (hgetall)

  • gets all key-value pairs

13. Hash (hset, hget, hgetall)

  • hset and hget : set and get key-value pairs.
  • hgetall : gets all the key-value pairs.

14. Hash (hdel)

  • hdel : to delete a key-value pair.

15. Hash (hexists).

  • hexists : to check the existence of the key.

References

https://learn.microsoft.com/en-us/windows/wsl/install

https://www.dragonflydb.io/faq/how-to-start-redis-server

--

--