Unlocking the Power of Redis: A Comprehensive Introduction to Redis Database Technology
The open-source, in-memory data store used by millions of developers as a cache, vector database, document database, streaming engine, and message broker. — Redis Docs
Key Features of Redis:
In-memory Storage: Redis keeps data in RAM, enabling lightning-fast read and write operations.
Data Structures: Redis supports various data structures like strings, hashes, lists, sets, sorted sets and more, making it versatile for different use cases.
Atomic Operations: Redis ensures atomicity for operations, providing reliability in multi-step operations.
Persistence Options: While primarily an in-memory store, Redis offers persistence options, allowing data to be saved to disk for durability. RDB (Redis Database) and AOF (Append only file). Click to read more
Organisation using redis — Twitter, Github, Snapchat .. many more and Wealthy as well 😃
Getting started with redis-cli:
Installation → brew install redis
Connect → redis-cli -h host_name -p port_number -n database_number
Client Libraries → Redis support more than 50 programming language few client libraries are redis-py, go-redis, Sidekiq
Redis Data Type walkthrough:
Strings: Sasta Dictionary(HashMap/HashTable) 😃, where key will be string and value will be stored as either string or in bytes format.
It contains-
- SET and GET are used to set and retrieve string key/values.
- MSET and MGET are used to set and retrieve multiple string values.
- XX : set if exists
- e.g → set a b xx
- NX: set if not exists
- e.g→ set a b xn
- Counters: To increment value of a key. Useful in page visitor count and similar places.
- Value should be an integer.
- commands-
- incr→ to increment value by 1
- incrby→ to increment value by x
- e.g: set a 1
- incr a → get a #output=2
- incrby a 10 → get a #output=12
Redis lists(DLL):Redis lists are linked lists of string values. Redis lists are frequently used to:
- Implement stacks and queues.
- Build queue management for background worker systems.
- LPUSH adds a new element to the head of a list; RPUSH adds to the tail.
- LPOP removes and returns an element from the head of a list; RPOP does the same but from the tails of a list.
- LLEN returns the length of a list.
- LMOVE atomically moves elements from one list to another.
- LTRIM reduces a list to the specified range of elements.
Hashes:
- A Redis hash is a collection of key-value pairs where the keys and values are strings. It is a versatile data structure that allows you to represent objects or entities in a structured way similar to dictionary in python
Basic commands -
- HSET sets the value of one or more fields on a hash.
- HGET returns the value at a given field.
- HMGET returns the values at one or more given fields.
- HINCRBY increments the value at a given field by the integer provided.
- HExists determines whether the provided key is present or not.
- HDel To delete particular key, return nil for non exists key.
- More hash commands click_here
Sorted sets:
A Redis Sorted Set is like a special kind of set but with an extra feature — each member (element) in the set is associated with a score, which is a numerical value. This score is used to order the elements in the set.
This can be useful in creating the leaderboard may be for top selling mutual funds, top clients etc.
Basic commands-
- ZADD adds a new member and associated score to a sorted set. If the member already exists, the score is updated.
- ZRANGE returns members of a sorted set, sorted within a given range.
- ZREM removes specific element from the sorted set.
- More sorted set command click_here
We’ve covered only few of the data type that is present in redis, however if you’re interested more in exploring click_here to proceed for official redis docs and filter by core data types.
Few More important redis commands that you should know:
- FLUSHALL 💣 → This is very critical command and should be used carefully. Removes all the keys from all the databases.
2. FLUSHDB → Clears all keys and data in the currently selected database.
3. KEYS [pattern] → Helpful to get the matched pattern keys. e.g keys *
4. PUBLISH/SUBSCRIBE → It’s a pub-sub pattern in redis like NATS.io where it can broadcast the messages to the subscribers and publisher can publish their messages.
Let’s understand pub-sub with the example of Mutual Fund:
- Let’s say service_X has created a broadcasting channel named nfos . It will publish all the new NFOs detail which is created in the system.
- Let’s say service_Y has subscribed the nfos channel, now it will get the event as soon as service_X publishes it.
Thank you for taking the time to explore my blog. If you enjoyed the content, I invite you to follow me and show your appreciation by giving it a clap 👏📝. Your support means a lot!”
LinkedIn — https://www.linkedin.com/in/mohdmujtaba/