Redis is an in-memory database that is generally utilized as a cache in front of PostgreSQL or MySQL. Redis lessens the strain on the primary database and improves application performance by taking advantage of memory speed. It is especially helpful for storing data that is less vital but is updated regularly, as well as data that is frequently queried but changes infrequently. Session or data caches, leaderboard data, and roll-up analytics for dashboards are a few examples of this type of data.

Redis has two persistence models: Append-Only Files (AOF) and Redis Database Files (RBB files) for data durability. In the event that the system crashes or restarts, these persistence techniques make sure that data is not lost. Redis uses RDB as its default persistence model. The dataset is periodically snapshotted and saved as binary RDB files. These files record the Redis database’s status at a particular moment in time. An append-only file is where AOF persistence, an alternate persistence architecture in Redis, logs each write action. Redis can recreate the dataset by replaying the log, which is captured sequentially by AOF.
Understanding Redis Internals:
Redis uses copy-on-write (COW) and forking strategies to effectively enable data persistence in its single-threaded architecture. Redis takes the following actions when it executes a background saving or snapshot (RDB) operation:
1. Forking: Redis creates a child process which is an exact replica of the parent process, using the fork() system call. Since forking makes a copy-on-write copy of the parent’s memory, it is a simple operation.
2. Copy-on-Write (COW): In COW, the parent process and the child process initially share memory pages. COW, on the other hand, kicks in when a memory page is altered by either the parent process or the child process. The operating system makes a new copy of the updated page only when required, rather than copying it instantly.
Through the use of COW, Redis gains the following advantages:
Memory Effectiveness:
The child process and the parent process share the same memory pages when they are first created. The additional RAM required by this shared memory method is negligible. Memory is conserved by just copying the altered pages when needed.
Performance:
Redis can use the COW method to conduct persistence operations without suffering a major performance overhead since only the updated pages are replicated. Large datasets are especially benefiting from this, as it would take a lot of time to duplicate the full dataset for durability.
Fork Safety:
Redis uses fork-based persistence, or “fork safety,” to prevent the main event loop from being blocked while taking snapshots. The parent process can carry on fulfilling client requests while the child process carries out the persistence operation on its own by forking it. High responsiveness and continuous service are thus guaranteed.
It’s necessary to remember that forking and COW have drawbacks even though they improve memory efficiency and performance. If a large number of modified pages need to be duplicated during the copy-on-write procedure, forking may result in increased memory utilisation. In addition, systems with high memory footprints could have slower fork operations.
Overall, Redis’s single-threaded architecture efficiently achieves data durability by utilising copy-on-write and forking techniques. Redis may do background-saving operations and snapshots using these strategies without causing any substantial influence on its memory usage or performance.
Deployment Solutions:
Redis supports different deployment architectures, including a single Redis instance, Redis HA (High Availability), Redis Sentinel, and Redis Cluster. Each architecture has its trade-offs and is suitable for different use cases and scalability needs. AWS has a serverless caching solution that is compatible with Redis and Memcached, Amazon ElastiCache offers modern applications with real-time, cost-effective performance. ElastiCache provides enterprise-grade security and dependability while scaling to hundreds of millions of operations per second with microsecond response times.
All things considered, Redis gives programmers a strong and adaptable data storage solution with a range of deployment choices and features.
Checkout Latest Blogs here