Hands-On with REDIS | Part 1

Welcome to this blog. If you are coming here directly, it’s highly recommended to read through this story first. We shall be looking at following topics in this blog :-

  • Installation of Redis-version-7 at local system.
  • Launching Redis-Server.
  • Simple Demonstration of Redis as Cache.
  • Overview of Data Persistence Options with Redis.
  • Setting up RDB configurations and Snapshotting.
  • Setting up AOF using ZSYNC.

Question :- Kindly guide the process of setting-up Redis, at our local ?

Answer :- Please take note that redis was made for UNIX based systems. So, it works well with Linux, it works well with Mac. It’s not made for Windows. So for those that want to try it with Windows, it’s going to work with that port but be aware that there’s going to be some issues. Sometimes, it may be unsupported at this point. It’s not the supported version by redis themselves. Just a note. Or you can actually do it on Windows as well, with the help of Docker.

Step #1.) Go to redis.io then go into the download section here so check the downloads page then scroll all the way down to the installation section here.

It will download the files that we need. Then we need to extract those files so do a tar and xzf, which is a special command.

Step #2.) So, let’s go ahead and cd inside of our directory that we just extracted :-

Step #3.) And now, we shall do the command “make”. For those that don’t have the apple developer tools, when you enter “make” and you click “enter” it’s going to ask you to install those tools. When you’re done installing those tools, come back and do the “make” command again. Now, I’m going to do the “make” command. I have the apple developer tools already installed and it’s going to do a whole bunch of stuff.

Question :- Demonstrate the launch of Redis-Server ?

Answer:- Let’s start our redis-server. So, the best way to do this, is to do “src/redis-server” like so. And this is going to start our server. So if you see this, our redis server is actually installed.

Question :- Demonstrate the usage of Key-Value pair through Redis-Client ?

Answer:- Let’s open new terminal :-

  • Launch the Redis-Client as shown below.
  • We have set-up a Key-Value pair into the Redis.
  • The Key here is “userName” and value here is “adityagoel786”. Also, we are fetching the value, based upon Key.

Question:- Does Redis provides good options for data persistence ?

Answer:- Although, all operations in Redis are done in-memory, right out of the box, but yes, we can even persist the data as well. Redis offers very good set of options for persisting the data.

Question :- Why data-persistence is needed with Redis ?

Answer:- If a Redis server that only stores data in RAM is restarted, all data is lost. To prevent such data loss, there needs to be some mechanism for persisting the data to disk.

Question :-Are there any data -persistence options available with Redis ?

Answer:- Redis provides two options as follows :-

  • Snapshotting / RDB file → Default mode.
  • Append-only file, or AOF → Needs to be turned-on explicitly.

You can configure your Redis instance to use either of the two or a combination of both.

Question:- Explain bit more about the RDB approach ?

Answer:- RDB stands for Redis-Database-Backup file. This approach deals with snapshot-operations :-

  • It creates point-in-time copies of your data, that you can retrieve in case of a disaster. It’s as simple as that.
  • When conditions are met, for example, a certain number of new items in data or time base, Redis makes a snapshot of the data and writes it into an RDB file, which you can retrieve whenever needed.
  • RDB is already preconfigured, out of the box, when you first install Redis.

Question :- Demonstrate the procedure to setup the RDB Redis-Persistence-Option.

Answer:- We shall demo the RDB option for now :-

Step #1.) Let’s go ahead and open the file : redis.conf, in any IDE :-

Step #2.) First, we want to look for the SNAPSHOTTING. So, in your code editor, use the Command + F, and look for SNAPSHOTTING, like so.

Question :- What happens, when a snapshot is created ?

Answer:- When a snapshot is created, the entire point in time via the data set is written to persistent storage in a compact RDB file.

  • We can set up recurring backups, for example, every one, 12, or 24 hours, and use these backups to easily restore different versions of the data set in case of disasters.
  • We can also use these snapshots to create a clone of the server or simply leave them in place for a future restart.

Question :- What’s the correct way for setting up a process to generate the RDB-file ?

Answer:- Creating an RDB file requires a lot of disk IO.

  • If performed in the main Redis process, this would reduce the server’s performance. That’s why this work is done by a forked child process.
  • But even forking can be time consuming if the data set is large. This may result in decreased performance or in Redis failing to serve clients for a few milliseconds or even up to a second for very large data sets.

Therefore, Understanding this should help you decide whether this solution makes sense for your requirements.

Question :- What all things we can configure, while working with RDB-file ?

Answer:-

  • We can configure the name and location of the RDB file with the dbfilename and dir configuration directives either through the redis.conf file or through the Redis CLI.
  • We can configure how often we want to create a snapshot. Here’s an excerpt from the redis.conf file showing the default values :-

As an example, this configuration will make Redis automatically dump the data-set to disk every 60 seconds if at least 1,000 keys changed in that period.

Question :- Explain the meaning of aforementioned configurations :-

Answer:- Redis will save the DB (i.e. Redis shall generate the snapshot), if the given number of seconds elapsed and it surpassed the given number of write operations against the DB. For example :- By default, Redis will save the DB’s snapshot :-

  • After 3600 seconds (an hour) if at least 1 change was performed.
  • After 300 seconds (5 minutes) if at least 100 changes were performed.
  • After 60 seconds if at least 10,000 changes were performed.

Note that, we can add as many configurations, as we want.

Example :- Can you explain the row “save 5 100000” ?

Answer :- Redis shall generate the snapshot of DB after 5 seconds, if at-least 100,000 changes were performed into the Redis. Note that :-

  • This configuration is for large, large database when there’s a lot of changes.
  • So, if you have a lot of transactions, then maybe you should use some configuration like it.

Question :- How do we set these properties ad-hoc i.e. on the fly, in case restart of redis-server is not really possible ?

Answer:- Snapshotting can be enabled on the fly through the below command as well, using the Redis-CLI tool :-

Afore-mentioned configuration would start generating the snapshot of DB after 60 seconds, if at-least ONE change was performed into the Redis.

Question :- How do we totally dis-able the Snapshotting ?

Answer:- Snapshotting can be completely disabled with a single empty string argument as in following example, but note that, this is a very dangerous situation to be in :-

save “”

Question :- What is the downside while working with RDB / Snapshotting ?

Answer:- While snapshotting it is a great strategy for the use cases, it leaves a huge possibility for data loss. You can configure snapshots to run every few minutes or after X writes against the database. But if the server crashes, you lose all the writes since the last snapshot was taken.

Question :- What option Redis provides for scenarios where NO data-loss is acceptable @ all ?

Answer:- Redis offers the AOF persistence option. AOF, or Append Only File, works by logging every incoming write command to disk as it happens.

  • These commands can then be replayed to server startup to reconstruct the original data set.
  • Commands are logged using the same format as the Redis protocol itself in an append only fashion.

Question:- Lets now proceed to explain more about the AOF approach ?

Answer:- AOF stands for → Append-Only-File.

  • You can turn on an option called append-only file, or AOF, which logs every operation in the system file.
  • What it’ll do is basically take all the transactions that happened and then create a log file with all those transactions inside of this particular file.
  • So, when the Redis server is restarted, it rebuilds a database from this file.

Question :- What happens, if the size of this file becomes too big ?

Answer:- This file can get very big, very quickly. But Redis is smart enough that if it gets too big, it’ll start over using the latest version of the data set, shrinking the file back down to a manageable size i.e. compact format.

Question :- What does this compacted file format contain?

Answer:- The compacted file contains minimal set of commands required to reconstruct the dataset at the time the file was created.

Question :- Demonstrate the procedure to setup the AOF Redis-Persistence-Option.

Answer:- We shall demo the AOF option for now :-

Step #1.) Again, let’s go ahead and open the file : redis.conf, in any IDE :-

Step #2.) First, we want to look for the APPEND ONLY. So, in your code editor, use the Command + F, and look for APPEND ONLY like so.

  • By default, this option is off. All we need to do is say, yes, and you have AOF set. The only thing we need to do is restart our server with the configuration file.
  • And then, to check if AOF is officially started, go back into our directory here, and you should see a file that’s called appendonly.aof. So, that’s basically the file that’s been built when you start your server and you have append-only as, yes.
  • The AOF approach provides greater durability than snapshotting and allows you to configure how often the syncs happen.

Question :- What are the various options to setup AOF Option i.e. FSYNC ?

Answer:- FYSNC policy can be set using various possible options :-

  • FYSNC every write, the safest policy → The write is acknowledged to the client only after it has been written to the AOF file and flushed to disk. Since in this approach, we are writing to disk synchronously, we can expect a much higher latency than usual.
  • FYSNC every second, the default policy → FYSNC is performed asynchronously in a background thread. So, write performance is still high. Choose this option if you need high performance and can afford to lose up to one second worth of writes.
  • No FYSNC → In this case, Redis will log the command to the file descriptor but will not force the OS to flush the data to disk. If the OS crashes, we can lose a few seconds of data. Normally, Linux will flush data every 30 seconds with this configuration. But it’s up to the kernel’s exact tuning.

Question:- Now that, we have seen both the approaches, what’s the best strategy to use, for Data-Persistence with Redis ?

Answer:- The best strategy for setting up a Redis database, is to use :-

  • RDB for its disaster-recovery features i.e. Snapshotting.
  • AOF for its speed and availability.

Using both will make your database ready for almost any kind of scenario.

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next part of this series.

Reference :-

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CSS: Day 48

Dummy text wrapped in a border using CSS

RISC V — VECTOR INSTRUCTIONS : level 1 (Basics Summarized)

Ruby & Active Record Associations

Building a Port Scanner in 16 Lines of Code

Reluctant to communicate Joomla case is gaining momentum —

CRUD operations and Message Transformation with Ballerina

Brickworks — A Truly Customizable Front-end Framework

What Enterprises Need to Know About Low-Code Software Development Platforms

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

Hands-On with REDIS | Part 2

Avengers! Assemble Docker Swarm Mode!

PostgreSQL Cheat Sheet

postgresql cheat sheet

Redis — Introduction