Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Variety of persistence options:
3.
Snapshotting
3.1.
Working
4.
Append-only file
5.
Redis Advantages
5.1.
Performance
5.2.
Flexible data structures
5.3.
Ease of use and simplicity
5.4.
Scalability and high availability
5.5.
Free and Open Source Software
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Persistence Manual in Redis

Introduction

This article will explain that writing data to persistent storage, such as a solid-state drive, is referred to as persistence (SSD). Redis has various persistence options: Redis Database (RDB): RDB persistence creates point-in-time snapshots of your dataset at predetermined intervals. It uses various tools.

Variety of persistence options:

The writing of data to persistent storage, such as a solid-state drive, is called persistence (SSD).

  • RDB (Redis Database) persistence takes point-in-time snapshots of your dataset at pre-determined intervals.
     
  • AOF (Append Only File) persistence logs every write action received by the server, replayed upon server restart to recreate the original dataset. Commands are logged in an append-only manner, following the Redis protocol's format. Redis can rebuild it in the background when the log grows too large.
     
  • No persistence: If you want your data to live as long as the server is functioning, you can disable persistence altogether.
     
  • RDB + AOF: Both AOF and RDB can be used together in the same instance. In this situation, the AOF file will be utilized to recreate the original dataset when Redis restarts since it is guaranteed to be the most complete.

Snapshotting

By default, Redis stores snapshots of the dataset to disc in the dump.rdb binary file. If there are at least M changes in the dataset, you may set up Redis to save it every N seconds, or or you can manually issue the SAVE or BGSAVE commands.

For example, if at least 1000 keys change, this setting will cause Redis to dump the dataset to the disc every 60 seconds automatically:

Save 60 1000.


When the following circumstances are met, Redis saves snapshots of your data to the disc in a dump.rdb file:

If 1000 keys were changed per minute,

If 10 keys were replaced every 5 minutes,

If one key was replaced every 15 minutes,

  • So if you're doing a lot of typing and changing a lot of keys, you'll get a snapshot every minute; if you're not changing that much, you'll get a photo every 5 minutes; if you're not changing that much, you'll get a snapshot every 15 minutes.
     
  • If Redis is unable to make a snapshot of your data, it will hang and cease accepting new writes and display an error message to let you know something is wrong.
     
  • You may send your rdb file to Amazon S3 every x interval to backup your data in case of damage.
     
  • You may alter how often these pictures are created, and you can use the "save" command to produce a snapshot on the fly.

Working

This occurs whenever Redis wants to dump the dataset to disc:

  1. Redis splits into forks. We now have a parent and child procedure.
  2. The dataset is written to a temporary RDB file by the child.
  3. The child replaces the old RDB file when it is finished writing the new one.


Redis may now benefit from copy-on-write semantics because of this approach.

Append-only file

This works differently; every command you submit to Redis is saved in a file, which you can subsequently use to re-build the full dataset.

Because this file includes the whole history of every key, it may get rather large over time; as a result, Redis rewrites it now and again to keep it as compact as possible. Instead of keeping the full history of a key, it starts with the most recent state of that key.

// instead of
increment counter
increment counter
increment counter

// it stores
set counter 3.


You may set the frequency of this rewrite based on the current file size and the percentage of growth permitted since the last rewrite.

Although Redis sends the new command to be appended to the file, the OS typically keeps these commands in a buffer and flushes that buffer every X number of seconds, meaning that if something happens while these commands are still in the buffer and not yet written to the disc, you will lose them. As a result, Redis imposes a buffer flush every 1 second to ensure that no data is lost.

You can enable Redis to require flush on every command, not just every second. However, this slows down Redis answers per command and isn't advised, but it is the safest option.

Redis Advantages

Performance

All Redis data is stored in memory, allowing for fast data access with low latency. In-memory data storage, unlike traditional databases, does not require a trip to the disc, decreasing engine latency to microseconds. As a result, in-memory data storage can handle orders of magnitude more operations and provide significantly quicker reaction times. As a result, read and write operations take less than a millisecond, and millions of operations per second are supported.

Flexible data structures

  • Strings — up to 512MB of text or binary data
     
  • Lists are a collection of Strings in the order in which they were added to the database.
     
  • Sets are an unsorted collection of strings that can intersect, unionize, and diff each other. Types to choose from
     
  • Sorted Sets are groups of items that are sorted by a value.
     
  • Hashes are a type of data structure that is used to store a list of fields and values.
     
  • Bitmaps are a form of data that allows for bit-level operations.
     
  • HyperLogLogs is a probabilistic data structure that may be used to estimate the number of unique elements in a data source.
     
  • Streams are a type of data structure seen in log files. The queue of messages
     
  • Geospatial - entries based on longitude and latitude. "Nearby" on maps

Ease of use and simplicity

Redis makes it possible to create more complicated code in fewer, simpler lines. Redis allows you to store, read, and utilize data in your applications with fewer lines of code. The distinction is that Redis developers may utilize a simple command structure instead of typical database query languages. For example, you may migrate data to a data store using the Redis hash data structure with just one line of code. Converting from one format to another on a data store without hash data structures might take many lines of code. Redis has native data structures as well as a plethora of tools for manipulating and interacting with your data. There are more than a hundred open-source clients.

Scalability and high availability

Redis has a primary-replica architecture that may be used in a single node or clustered topology. This enables you to create highly available systems with predictable performance and dependability. When you need to change the size of your cluster, you have the choice to scale up, scale in, or scale-out. This allows your collection to scale to meet your needs.

Free and Open Source Software

Redis is an open-source project with a thriving community, including AWS. Because Redis is built on open standards, supports open data formats, and has a diverse collection of clients, there is no vendor or technological lock-in.  To understand better you may check out this article.

Frequently Asked Questions

1. What happens when the server is restarted?
Your data will be loaded into memory from your backup files using Redis. Redis will utilize the AOF (Append-only file) if you use both Snapshotting and Append-only files since it is assumed to have the most current data.
 

2. What should my persistence approach be?
By default, Redis uses snapshotting; you can keep using it if you're okay with losing a few minutes of data; if you need to guarantee no data loss, use AOF. It's also possible to combine the two strategies to have a clean backup of your data using the snapshot strategy and an append-only file using the AOF strategy.
 

3. Is Redis capable of storing data on disc?
The Redis server periodically keeps all of its data to HDD, giving some permanence. It saves information in one of the following situations: from time to time, automatically when you use the BGSAVE command manually 15.
 

4. When Redis is full, what happens?
If this limit is reached, Redis will begin to respond to write instructions with an error (but will continue to accept read-only commands). You may also set Redis to evict keys when the maximum memory limit is reached.
 

5. Is Redis stored in RAM or on a solid-state drive (SSD)?
You can use REDIS to store key-value pairs in your RAM. It indicates speed since accessing RAM is 150,000 times quicker than accessing a disc and 500 times faster than SSD. However, we already use RAM for the majority of our activities!

Conclusion

We studied how Redis writes data to disc, Snapshots, and operates in this post. We believe that this blog will assist you in grasping the Redis idea. Check out our other blogs if you want to learn more about the database.
We give you ​​a Complete Preparation Guide For Coding Interviews click here to Know more. 
 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass