{"id":47590,"date":"2024-12-13T04:21:57","date_gmt":"2024-12-13T04:21:57","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=47590"},"modified":"2024-12-13T04:21:57","modified_gmt":"2024-12-13T04:21:57","slug":"how-to-deal-with-data-persistence-using-redis-while-implementing-cache","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-deal-with-data-persistence-using-redis-while-implementing-cache\/","title":{"rendered":"How to deal with data persistence using Redis while implementing Cache?"},"content":{"rendered":"\n<p><strong>Redis<\/strong> is primarily an <strong>in-memory data store<\/strong>, meaning it stores data in the system&#8217;s memory (RAM) for fast read and write operations. However, Redis also provides options for <strong>data persistence<\/strong> to ensure that data can be recovered after a server restart or crash. Here&#8217;s how Redis handles data storage and persistence:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. In-Memory Storage<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>By default, Redis keeps all data in RAM, which allows it to deliver extremely fast performance.<\/li>\n\n\n\n<li>This is ideal for caching purposes because:\n<ul class=\"wp-block-list\">\n<li>Data retrieval is nearly instantaneous.<\/li>\n\n\n\n<li>It supports high-throughput operations.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Data Persistence Options<\/strong><\/h3>\n\n\n\n<p>Redis offers two primary mechanisms for persisting data to disk for long-term storage:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>a. RDB (Redis Database Backup) Snapshots<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>How It Works<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Redis periodically creates a snapshot of the dataset and saves it to disk in a binary file (e.g., <code>dump.rdb<\/code>).<\/li>\n\n\n\n<li>This happens at predefined intervals (e.g., every X minutes or after Y changes).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Advantages<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Faster recovery times since the file is compact.<\/li>\n\n\n\n<li>Minimal impact on runtime performance.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Disadvantages<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Data loss may occur if the server crashes between snapshots.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>b. AOF (Append-Only File)<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>How It Works<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Redis logs every write operation to a file (<code>appendonly.aof<\/code>) in real-time or at a specified interval.<\/li>\n\n\n\n<li>On recovery, Redis replays the log to restore the dataset.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Advantages<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Ensures minimal data loss.<\/li>\n\n\n\n<li>More reliable for applications requiring high durability.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Disadvantages<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Slower than RDB for large datasets.<\/li>\n\n\n\n<li>Files are larger compared to RDB snapshots.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Hybrid Persistence<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Redis 6.0 introduced <strong>hybrid persistence<\/strong>, which combines RDB and AOF methods.<\/li>\n\n\n\n<li>On recovery, Redis uses the RDB snapshot for fast loading and replays the AOF logs to ensure the dataset is current.<\/li>\n\n\n\n<li>This approach balances speed and durability.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Memory Management<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Redis can evict old keys when memory is full based on its <strong>eviction policies<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Least Recently Used (LRU)<\/strong>: Removes the least recently used keys.<\/li>\n\n\n\n<li><strong>Least Frequently Used (LFU)<\/strong>: Removes keys accessed less frequently.<\/li>\n\n\n\n<li><strong>Allkeys Random<\/strong>: Randomly removes keys, regardless of access.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Use Cases for Persistence<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Caching<\/strong>: No persistence needed (default behavior). Data can be regenerated if lost.<\/li>\n\n\n\n<li><strong>Session Management<\/strong>: Use persistence to avoid losing user session data.<\/li>\n\n\n\n<li><strong>Primary Data Store<\/strong>: Use AOF or hybrid persistence for applications requiring high durability.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Configuration for Persistence<\/strong><\/h3>\n\n\n\n<p>Persistence options can be configured in the <code>redis.conf<\/code> file:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Enable RDB:<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">save <span class=\"hljs-number\">900<\/span> <span class=\"hljs-number\">1<\/span>      <span class=\"hljs-comment\"># Save snapshot every 900 seconds if at least 1 key is changed<\/span>\nsave <span class=\"hljs-number\">300<\/span> <span class=\"hljs-number\">10<\/span>     <span class=\"hljs-comment\"># Save snapshot every 300 seconds if at least 10 keys are changed<\/span>\nsave <span class=\"hljs-number\">60<\/span> <span class=\"hljs-number\">10000<\/span>   <span class=\"hljs-comment\"># Save snapshot every 60 seconds if at least 10,000 keys are changed<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Enable AOF:<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">appendonly yes\nappendfsync everysec   <span class=\"hljs-comment\"># Sync AOF file to disk every second<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Hybrid Persistence:<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">aof-<span class=\"hljs-keyword\">use<\/span>-<span class=\"hljs-title\">rdb<\/span>-<span class=\"hljs-title\">preamble<\/span> <span class=\"hljs-title\">yes<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Does Redis Store Data Somewhere Other Than RAM?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>By Default<\/strong>: Data resides only in memory, making Redis extremely fast.<\/li>\n\n\n\n<li><strong>With Persistence Enabled<\/strong>: Data is also stored on disk (RDB\/AOF) for recovery.<\/li>\n<\/ul>\n\n\n\n<p>If your application requires both fast caching and long-term reliability, enabling <strong>AOF<\/strong> or <strong>hybrid persistence<\/strong> is recommended.<\/p>\n\n\n\n<p>Let me know if you&#8217;d like guidance on configuring Redis for your specific use case!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis is primarily an in-memory data store, meaning it stores data in the system&#8217;s memory (RAM) for fast read and write operations. However, Redis also provides options for data persistence to ensure that data can be recovered after a server restart or crash. Here&#8217;s how Redis handles data storage and persistence: 1. In-Memory Storage 2&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-47590","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=47590"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47590\/revisions"}],"predecessor-version":[{"id":47591,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47590\/revisions\/47591"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=47590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=47590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=47590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}