DEV Community

Cover image for Redis
Vinoth Kumar
Vinoth Kumar

Posted on • Edited on

Redis

What is Redis?

  • Redis is an open-source, in-memory data structure store used as database, cache and message broker

  • Redis, which stands for Remote Dictionary Server.

  • NoSQL Key/Value Store with some advance features.

  • The different between MongoDB and Redis is Mongo DB is a disk-based document store while Redis is a memory-based.

  • Redis does not support any built-in data encryption


Why Redis?

  • Redis is blazingly fast it's written in C, runs entirely in memory and is optimized to deliver millions of operations with sub-millisecond latency with a single standard server.

  • Pre-built data structures for popular use cases which are used by developer like LEGO building blocks when creating new apps.

  • These built-in data structures include lists, sets, sorted sets, hashes, hyperloglogs, bitmaps, geospatial indexes, bitfields, streams and strings.

  • Redis commands allow data to be processed on the database level rather than the application level, reducing coding effort, code complexity and bandwidth requirements.

  • Redis can be extended infinitely.

  • With Redis Modules that expand Redis infinitely, and it allows to add custom functionality and structures.

  • And Modules that do everything from search to machine learning, security, JSON to graph data processing and more.

  • Has client libraries for every language.

  • The hottest Redis uses includes real-time analytics, High-speed transactions, High-speed data ingest, Message queues, Session storage, In-app social functionality, application job management, Search, Machine learning and caching.

  • Redis has been benchmarked as the world's fastest database.


Data types in Redis

  1. Lists
  2. Sets
  3. Sorted sets
  4. Hashes
  5. Hyperlogs
  6. Bitmaps
  7. Geospatial indexes
  8. Bitfields
  9. Streams
  10. Strings

Installing Redis

Basic commands to install redis.

$ sudo apt update

$ sudo apt install redis-server

$ redis-server # start redis server

$ redis-cli # opens a redis prompt

Basic Redis CLI

  1. SET
  2. SETNX - SET if Not e*X*ists
  3. GET
  4. DEL
  5. EXISTS
  6. MSET - Multi SET
  7. MSETNX - Multi SET if Not e*X*ists
  8. MGET - Multi GET

Note : Other keywords are self-explanatory

SET

  1. Set key to hold the string value.
  2. If the key already holds a value, it is overwritten, regardless of its type.
  3. Time complexity: O(1)
SET key value # Syntax


SET mykey "Hello" # Examples

SETNX

  1. Set key to hold string value if the key does not exist.
  2. In that case, it is equal to SET.
  3. When key already holds a value, no operation is performed.
  4. SETNX is short for "SET if Not e*X*ists".
  5. Time complexity: O(1)
SETNX key value # Syntax


SETNX mykey "Hello" # Examples

GET

  1. Get the value of the key.
  2. If the key does not exist the special value nil is returned.
  3. An error is returned if the value stored at key is not a string, because GET only handles string values.
  4. Time complexity: O(1)
GET key # Syntax


GET mykey # Example

DEL

  1. Removes the specified keys.
  2. A key is ignored if it does not exist.
  3. Time complexity: O(N) where N is the number of keys that will be removed.
  4. Removing a single key that holds a string value is O(1).
DEL key [key ...] # Syntax


DEL key1 key2 key3 # Example

EXISTS

  1. Returns 1 if key exists, 0 if the key does not exist.
  2. Time complexity: O(1)
EXISTS key [key ...] # Syntax


EXISTS key1 key2 # Example

MSET

  1. Sets the given keys to their respective values.
  2. Replaces existing values with new values, just as regular SET.
  3. Time complexity: O(N) where N is the number of keys to set.
MSET key value [key value ...] # Syntax


MSET key1 "Hello" key2 "World" # Example

MSETNX

  1. Sets the given keys to their respective values.
  2. Will not perform any operation at all even if just a single key already exists.
  3. Time complexity: O(N) where N is the number of keys to set.
MSETNX key value [key value ...] # Syntax


MSETNX key1 "Hello" key2 "there" # Example

MGET

  1. Returns the values of all specified keys.
  2. For every key that does not hold a string value or does not exist, the special value nil is returned.
  3. Time complexity: O(N) where N is the number of keys to retrieve.
MGET key [key ...] # Syntax


MGET key1 key2 # Example

Resource

Top comments (0)