Redis
Vinoth Kumar
Posted on June 20, 2020
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
- Lists
- Sets
- Sorted sets
- Hashes
- Hyperlogs
- Bitmaps
- Geospatial indexes
- Bitfields
- Streams
- 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
- SET
- SETNX - SET if Not e*X*ists
- GET
- DEL
- EXISTS
- MSET - Multi SET
- MSETNX - Multi SET if Not e*X*ists
- MGET - Multi GET
Note : Other keywords are self-explanatory
SET
- Set key to hold the string value.
- If the key already holds a value, it is overwritten, regardless of its type.
- Time complexity: O(1)
SET key value # Syntax
SET mykey "Hello" # Examples
SETNX
- Set key to hold string value if the key does not exist.
- In that case, it is equal to SET.
- When key already holds a value, no operation is performed.
- SETNX is short for "SET if Not e*X*ists".
- Time complexity: O(1)
SETNX key value # Syntax
SETNX mykey "Hello" # Examples
GET
- Get the value of the key.
- If the key does not exist the special value nil is returned.
- An error is returned if the value stored at key is not a string, because GET only handles string values.
- Time complexity: O(1)
GET key # Syntax
GET mykey # Example
DEL
- Removes the specified keys.
- A key is ignored if it does not exist.
- Time complexity: O(N) where N is the number of keys that will be removed.
- Removing a single key that holds a string value is O(1).
DEL key [key ...] # Syntax
DEL key1 key2 key3 # Example
EXISTS
- Returns 1 if key exists, 0 if the key does not exist.
- Time complexity: O(1)
EXISTS key [key ...] # Syntax
EXISTS key1 key2 # Example
MSET
- Sets the given keys to their respective values.
- Replaces existing values with new values, just as regular SET.
- 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
- Sets the given keys to their respective values.
- Will not perform any operation at all even if just a single key already exists.
- 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
- Returns the values of all specified keys.
- For every key that does not hold a string value or does not exist, the special value nil is returned.
- Time complexity: O(N) where N is the number of keys to retrieve.
MGET key [key ...] # Syntax
MGET key1 key2 # Example
Resource
Posted on June 20, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.