Redis Notes (Redis-CLI, Redis with NodeJS)
Redis (Remote Dictionary Server) is an open source NoSQL in-memory, key-value data store. All Redis data resides in ram memory, which enables low latency and high throughput data access. Unlike traditional databases, In-memory data stores don’t require a trip to disk, reducing engine latency to microseconds.
Redis delivers sub-millisecond response times, enabling millions of requests per second for real-time applications in industries like gaming, ad-tech, financial services, healthcare, and IoT.
Redis store data inside the volatile ram, it can be entirely ephemeral with no persistence at all. Our data is only available while the server is running, and if for some reason the server was terminated or rebooted our data is lost.
The Redis stores the data as key-value pairs. It is similar to a single JSON object. We create keys and store values associated with each key.Connecting to Redis Server
There are many ways to connect to a Redis server, below are some of the ways to connect to one :
- Redis-CLI - The default command-line tool for redis database.
- RedisInsight - A GUI tool to manage the redis database.
Connections in Production
In production environment,a client will be using an application written in any programming language like python,javascript etc. In such cases we wont be using the above mentioned tools,rather we'll use special modules which will simply take in the database info (user,password) and make a connection to execute a given task.
Below we show how to connect to Redis Server using Python and Javascript :
---------------------------------------------------------------------------------------------------------------
Redis-CLI commands
Below we'll see some common commands while using redis-cli tool.
1] Make a Connection
By default redis-cli connects to the server at the address 127.0.0.1 with port 6379. You can change this using several command line options.
2] CRUD Commands
Below we se hwo to perform create,read,update and delete operations with redis key-store.
3] Key Expiration
Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.
The EXPIRE command is used to set an expiration timer to a key. Technically it's not a timer, but a kill timestamp beyond which the key will always return null unless it's set again. Return 1 if timeout is set , and 0 if key does'nt exists.
The TTL command can be used to learn how much time the key has to live. It returns the following values :
- TTL will return
-1if the key exists but doesn't have an expiration - TTL will return
-2if the key doesn't exist - TTL will return time to live in seconds if the key exists and will expire
---------------------------------------------------------------------------------------------------------------
Redis Data Types
Redis stores everything in string or in its string representation, even Integers are stored as strings. Below are some of the common data types in redis :
- Strings
- Lists
- Sets
- Sorted Sets
- Hashes
- Bitmaps
- Geospatial Indexes
1] Strings
We can store strings and Integers in redis using GET and SET methods.
2] Lists
Redis Lists are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis List pushing new elements on the head (on the left) or on the tail (on the right) of the list.
RPUSH: This command adds a new element at the right of the list.LPUSH: This command adds a new element at the left of the list.RPOP: This command removes an element at the right, or tail of the list.LPOP: This command removes an element at the left, or head of the list.LINDEX: This command retrieves the item at the specified position in the list.LRANGE: This command retrieves a subset of list elements based on the provided “start” and “stop” offsets.
3] SETS
Redis Sets are an unordered collection of strings. In Redis, you can add, remove etc members in O(1) time complexity. We use the below methods for sets in redis :
SADD: This command inserts one or more elements inside the set.
SMEMBERS: This command displays all values inside the set.
SREM: This command removes a specified element from the list.
4] Hashes
In Redis we can also have a key-value pair object associated with a key.
---------------------------------------------------------------------------------------------------------------
Redis NodeJS
Below we display some common operations to be performed on redis server hosted on RedisLabs cloud using NodeJs.
1] Connect to Redis-Server
2] Read & Write Values
i] Strings
ii] Hashes
---------------------------------------------------------------------------------------------------------------

Comments
Post a Comment