Hi everyone, we will be talking about Redis.

What is Redis? 

Why do I need Redis? 

Where do I use Redis?

Redis is an open source NoSql (Not Only SQL) database that is becoming increasingly popular, keeping data as key value.  Redis supports a lot of data types(Json, XML, etc.). Redis stores all data in memory or you can store data in a hard disk.

So What is NoSql database? NoSql database is different from other relaSo What is NoSql database? NoSql database is different from other relational databases. There is no table, table to table link, transaction, nested query.

NoSQL database has a lot of data models including access to data, documents to manage data, graphics, key value, search in memory etc.

Such databases are optimized especially for applications that require large data volumes, low latency, and flexible data models.

So why should we use Redis? 

What are the Redis advantages?

  • Reduces IO operation.
  • Work synchronously.
  • Reduces CPU usage.
  • Simple access to data.
  • Working high performance.
  • Supports popular software programming languages.
  • Great open source technology.
  • Supports commonly data types.
  • Easy used commands. 
  • Organized documents.
  • It has many features like Cluster Sharing, Sentinel, Replication.

What are the Redis disadvantages?

  • Redis doesn’t work asynchronously, you may not be able to reach the performance achieved by asynchronous alternatives.
  • RAM is based on your data size.
  • Complex queries, such as relational databases are not supported.
  • If you are going to query for complex queries, you must properly construct the Redis structure.
  • If a transaction gets an error, there is no going back.

Where can we use Redis?

  • Caching system
  • Session system
  • Counter
  • Queue Operation
  • Caching a web page
  • Caching database query results,
  • Caching frequently used objects such as images, files and metadata

Redis Data Replication

Redis only supports copy data from master nodes to all slave nodes. When any data updates in any Redis master nodes, Redis sends up to date data to all slave nodes. A slave node can be a master node for other slave nodes. Thus, we can create graph structure models.

Redis also supports the publish/subscribe feature. In this way the slave node subscribes to a channel and receives all messages broadcasted to the master node.

Redis Data Persistence

Redis keeps all data in memory. Redis has two ways for data persistence. The First way is called “Snapshotting”. Redis takes a copy of all data at scheduled times and then stores it on the disk. 

The Second way, Redis keeps all data in memory and stores it on disk synchronously.

These two methods provide data persistence. We can disabled data persistence feature. 

Data persistence process will reduce the performance of the Redis master node, we can run these processes in slave nodes.

Redis Security

Redis is designed on secure clients’ access to Redis in a secure network. Redis does not have an access control protocol, it uses a simple authentication protocol instead.

The client authenticates with the password provided by the “AUTH” command. The password is determined by the system administrator and kept in clear text in the file “Redis.conf”.

Redis does not support data encryption, thus the secure client should implement SSL protocol to access redis. 

We can alter any command names.

Redis focuses on performance, not security.

Redis Pipelining

A Request/Response server can be implemented so that it is able to process new requests even if the client hasn’t already read the old responses. This way it is possible to send multiple commands to the server without waiting for the replies at all, and then read the replies in a single step.

This is called pipelining, it is a technique that has been widely used for many decades. For instance many POP3 protocol implementations already support this feature, dramatically speeding up the process of downloading new emails from the server.

Important Note: While the client sends commands using pipelining, the server will be forced to queue the replies, using memory. So if you need to send a lot of commands with pipelining, it is better to send them in batches having a reasonable number. For instance send 10k commands, read the replies, and then send another 10k commands again, and so forth. The speed will be nearly the same, but the additional memory used will be at the max amount needed to queue the replies for these 10k commands.

We can store five different types of data on Redis. Such as: String, Hash, List, Set, Sorted Set.

  1. Atomic Transactions
  2. String:
    • Basic data type. Stores up to 512MB.
    • SET & GET
      • SET adds a string on memory. GET brings a string from memory.
    • GETRANGE key start end
      • Gives us data from the start index to the end index in the value.
    • EXISTS key
      • The command checks the existence of Key. It returns 0 or 1.
    • STRLEN key
      • The command gives us a length of the key.
    • APPEND key value
      • The command adds data to the end of the value pointed by the key.
    • MSET key1 value1 key2 value2 key3 value3
      • Used to add multiple values with one command.
    • MGET key2 key2 key3
      • Used to get multiple values with one command.
    • DEL key1
      • Used to delete data. The commands deletes the key and value.
    • KEY key1*
      • The command lists all keys starting with key1.
    • EXPIRE key minutes
      • The command sets expire time(unit is minute) for the existing key.
    • RENAME oldName newName
      • Used to change key names.
  3. Hash
    • When we had a lot of keys, we should create a map on redis. Thus, we separate the data block by block.
    • HMSET key pointer1 value1 pointer2 value2 pointer3 value3
      • The command used to create a map.
    • HGET key pointer1
      • The command gets to value in the map.
    • HGETALL key
      • The command gets all pointers in the map.
    • HDEL key
      • The command deletes the key’s value.
  4. List
    • It is used to keep the data in a list.
    • LPUSH key list
      • Used to create a list and add data in the list.
    • LRANGE key start end
      • Gives us data from the start index to the end index in the value.
    • LINDEX key index
      • Used to get data from index.
    • LINSERT key BEFORE/AFTER value “new value”
      • Used to add elements before or after the selected element in the list.
  5. SET
    • Used to store data as a cluster.
    • SADD key value
      • Used to create a SET and add value in the SET.
    • SMEMBERS key
      • Used to sort values in the SET.
    • SMOVE key1 key2 value
      • Used to move value from key1 to key2
    • SDIFF key1 key2
      • Used to compare one set with another and to list elements that are not in the first set but not in the second set.
  6. Sorted SET
    • Used to store data as a sorted.
    • ZADD key score value
      • Used to create a Sorted SET and add value.
    • ZRANGE key start end WITHSCORES
      • Gives us data from the start index to the end index in the value with Score value
    • ZSCORE key value
      • Used to get value’s score

How can I run Redis on Windows Operating System?

If you want to use Redis in Windows Operating System HERE.

You should extract the compressed file after downloading Redis. You should open the file that is 32 or 64 bit according to your operating system and then run the redis-server.exe file. Once you have given the necessary operating system permission, the application will stand up. You will see a screen like the one below.

You can run redis-cli.exe file in the same file and test Redis with the following commands.

set user1 "caglartelef.com"
get user1

When you run the redis-cli.exe file, you will see an output like redis-server.exe “1 clients connected (0 slaves), 1188264 bytes in use”.

After running the test commands, you will see a screen like the one below.

You can download the project HERE.

We have done a spring boot project with redis. You can add or get any data on Redis.

To add      : http://localhost:8080/redis/set
To get      : http://localhost:8080/redis/get

Redis official documentation is HERE

Thank you for reading my article. Have a nice day!