{"id":4028,"date":"2019-04-29T06:37:08","date_gmt":"2019-04-29T06:37:08","guid":{"rendered":"https:\/\/cloudkul.com\/blog\/?p=4028"},"modified":"2019-04-29T13:20:32","modified_gmt":"2019-04-29T13:20:32","slug":"getting-started-with-redis","status":"publish","type":"post","link":"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/","title":{"rendered":"Getting started with Redis"},"content":{"rendered":"<h2><span style=\"color: #3366ff\"><b>Introduction<\/b><\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>Redis is a NoSQL or non-relational, advanced key-value store and in-memory database server. It&#8217;s open source with BSD Licensing.<\/p>\n<p>Redis is written in C. And as it supports wide range of Data Structures, it\u2019s also known as Data Structure server. The supported data types include strings, lists, hashes, sets &amp; sorted sets.<\/p>\n<p>Redis supports pipelining which means the client can send multiple requests to the server without having to wait for the replies at all, and finally reads the replies in a single step.<\/p>\n<p>Since Redis is NoSQL, there are no tables &amp; no database-defined.<\/p>\n<p>&nbsp;<\/p>\n<h2><\/h2>\n<h2><span style=\"color: #3366ff\"><b>Features<\/b><\/span><\/h2>\n<p>&nbsp;<\/p>\n<ul>\n<li>\u00a0NoSQL database makes the management of data easier. So, both read &amp; write operations are fast.<\/li>\n<li>\u00a0Being written in C makes it even faster to operate.<\/li>\n<li>\u00a0Combines the adavantages of in-memrory &amp; schema-less design patterns.<\/li>\n<li>\u00a0Wide range of Data Structure support with additional support for range queries, bitmaps, etc.<\/li>\n<li>\u00a0Best suited for Caching. Can work as Primary Database too.<\/li>\n<li>\u00a0Wide API support for language like Java, Python, C, C++, etc.<\/li>\n<li>\u00a0Master\/Slave support, read replication and client-side sharding to scale write performance.<\/li>\n<li>\u00a0Message queue support and can work as a Channel Layer.<\/li>\n<li>\u00a0Ahead of file(AOF) &amp; Point in time support for replicating data to persistant storage.<\/li>\n<li>\u00a0Atomic operations &amp; supports pipe-lining which reduces network round trip time in case multiple operations need to be performed<\/li>\n<\/ul>\n<p>Above features make Redis a viable candidate for Caching solution.<\/p>\n<p>But there can be limitations as mentioned below.<\/p>\n<ul>\n<li>Data stored in Redis server would be lost if server crashes unexpectedly. Redis accomodates this problem by allowing you to write the data to persistent storage regularly.<\/li>\n<li>Since Redis manages in-memory databases, the RAM size restricts the database size.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><strong><span style=\"color: #3366ff\">Supported Data Types <\/span><\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2019\/04\/redis-ds.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4030 aligncenter\" src=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2019\/04\/redis-ds.png\" alt=\"\" width=\"823\" height=\"332\" \/><\/a><\/p>\n<p><strong>\u00a0 \u00a0Source<\/strong>:- https:\/\/redislabs.com<\/p>\n<p>&nbsp;<\/p>\n<h2><\/h2>\n<p>&nbsp;<\/p>\n<h2><strong><span style=\"color: #3366ff\">Backup Policy<\/span><\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Redis offers two ways to write\/backup data to persistent storage.<\/p>\n<ul>\n<li><strong>Snapshot<\/strong>:- It offers Point in Time backup i.e Redis can save current status of DB to disk. At any time one can restore to this point by restoring the backup file. Use command <strong>SAVE<\/strong> or <strong>BGSAVE<\/strong> to start a snapshot. <strong>BGSAVE<\/strong> is <strong>SAVE<\/strong>, running in background instead.<\/li>\n<li><strong>AOF<\/strong>(Ahead of file):- In this, only write queries are written to a file as they hit the Redis server. So, one needs to replay all the write queries to restore the DB.<\/li>\n<\/ul>\n<p>Restoring from Snapshot is really fast. In case the DB crashes before the next Snapshot is made, you can only restore what last successful snapshot has saved. So, the changes in data after the last backup cannot be restored.<\/p>\n<p>Restoring from AOF can be a time intensive process as every single write query will be executed. You may have queries which took longer time so they would cause more delay. Redis doesn&#8217;t write queries to disk straightaway. Rather it flushes the queries\/data to buffers and then it&#8217;s the responsibility of OS to write them to disk.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><strong><span style=\"color: #3366ff\">How Redis compares with other databases<\/span><\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2019\/04\/redis-cmp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4031\" src=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2019\/04\/redis-cmp.png\" alt=\"\" width=\"819\" height=\"522\" \/><\/a><\/p>\n<p><strong>Source<\/strong>:- https:\/\/redislabs.com<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><strong><span style=\"color: #3366ff\">Setup<\/span><\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Install the Redis-server using below command.<\/p>\n<pre class=\"lang:sh decode:true\">apt-get update\r\napt install redis-server -y<\/pre>\n<p><b>Redis-Cli <\/b>is a command line redis client interface used to connect to local\/remote redis-server.<\/p>\n<p>Type \u201credis-cli\u201d on terminal to start the client interface.<\/p>\n<p>By default it connects to local redis-server on default port(6379). These default values can be changed by using command line options.<\/p>\n<p>Redis-cli lets you manage server and its configuration.<\/p>\n<p>Some examples:-<\/p>\n<pre class=\"lang:sh decode:true\">127.0.0.1:6379&gt; SET name \"myfirstkeypair\"\r\nOK\r\n127.0.0.1:6379&gt; GET name\r\n\"myfirstkeypair\"\r\n<\/pre>\n<p>Above we map the key \u201cname\u201d to value \u201cmyfirstkeypair\u201d<\/p>\n<pre class=\"lang:sh decode:true\">127.0.0.1:6379&gt; sadd myfirstset value1\r\n(integer) 1\r\n127.0.0.1:6379&gt; sadd myfirstset value2\r\n(integer) 1\r\n127.0.0.1:6379&gt; sadd myfirstset value2\r\n(integer) 0\r\n127.0.0.1:6379&gt; SMEMBERS myfirstset\r\n1) \"value1\"\r\n2) \"value2\"<\/pre>\n<p>Above we added values to set \u201cmyfirstset\u201d.<\/p>\n<p>Notice the return integer. Integer 1 means new value added to SET and int 0 means value already present.<\/p>\n<p>Similarly many other operations can be peformed.<\/p>\n<p>Run \u201c<strong>redis-benchmark<\/strong>\u201d to benchmark your redis installation, it performs 100000 operations on different data types.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #3366ff\"><b>APIs Support<\/b><\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>Redis has a great support for APIs. Check <a href=\"https:\/\/redis.io\/clients\">https:\/\/redis.io\/clients<\/a> for available options.<\/p>\n<p>Now, we will check how Redis DB can be managed using Python API.<\/p>\n<p>We need install Python library \u201credis\u201d first. We are using Python3.<\/p>\n<pre class=\"lang:sh decode:true\">pip3 install redis<\/pre>\n<p>Note:- Use <b>apt install python3-pip<\/b> to install pip3 if needed.<\/p>\n<p>Below code snippet creates a set &amp; then deletes the set.<\/p>\n<pre class=\"lang:python decode:true\">#!\/usr\/bin\/python3\r\nimport redis\r\n\r\nclient = redis.Redis(host='127.0.0.1',db=0,port=6379) #Connection Object with defualt values\r\nfor _ in range(5):\r\n\tprint(\"Trying to add value%s to \\\"myfirstset\\\"\"%str(_),end = \" \")\r\n\tprint(\"Returned integer is %d\"%(client.sadd('myfirstset', str(\"value\"+str(_))))) #adding values to set\r\n\r\n\r\nprint(\"The SET \\\"myfirstset\\\" is: \", client.smembers('myfirstset')) \r\nclient.delete(\"myfirstset\")  #deleting the values\r\nprint(\"Deleted the SET \\\"myfirstset\\\"\")\r\n<\/pre>\n<p>The output of file is:-<\/p>\n<pre class=\"lang:sh decode:true\">Trying to add value0 to \"myfirstset\" Returned integer is 1\r\nTrying to add value1 to \"myfirstset\" Returned integer is 1\r\nTrying to add value2 to \"myfirstset\" Returned integer is 1\r\nTrying to add value3 to \"myfirstset\" Returned integer is 1\r\nTrying to add value4 to \"myfirstset\" Returned integer is 1\r\nThe SET \"myfirstset\" is:  {b'value3', b'value2', b'value1', b'value4', b'value0'}\r\nDeleted the SET \"myfirstset\"\r\n<\/pre>\n<p>As the output shows, five values(numbered from 0 to 4) were successfully added.<\/p>\n<p>&nbsp;<\/p>\n<p>In the next blog, we will see how Redis can be used for Session management.<\/p>\n<p>&nbsp;<\/p>\n<p>Thank you for Reading this!! Hope it helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction &nbsp; Redis is a NoSQL or non-relational, advanced key-value store and in-memory database server. <a class=\"text-primary\" title=\"read more\" href=\"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":23,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,86,25],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Getting started with Redis - Cloudkul<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Getting started with Redis - Cloudkul\" \/>\n<meta property=\"og:description\" content=\"Introduction &nbsp; Redis is a NoSQL or non-relational, advanced key-value store and in-memory database server. [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudkul\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-29T06:37:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-29T13:20:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2019\/04\/redis-ds.png\" \/>\n<meta name=\"author\" content=\"Manish Bisht\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/\",\"url\":\"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/\",\"name\":\"Getting started with Redis - Cloudkul\",\"isPartOf\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\"},\"datePublished\":\"2019-04-29T06:37:08+00:00\",\"dateModified\":\"2019-04-29T13:20:32+00:00\",\"author\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/bb2c4fc497998b7483f066285b186dc7\"},\"breadcrumb\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Getting started with Redis\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\",\"url\":\"https:\/\/cloudkul.com\/blog\/\",\"name\":\"Cloudkul\",\"description\":\"Host your eCommerce Store on AWS with Optimized Performance\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudkul.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/bb2c4fc497998b7483f066285b186dc7\",\"name\":\"Manish Bisht\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1e80c17ca6bb0845750b842faeb3f4e7?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1e80c17ca6bb0845750b842faeb3f4e7?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"caption\":\"Manish Bisht\"},\"url\":\"https:\/\/cloudkul.com\/blog\/author\/manish-bishtck91\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Getting started with Redis - Cloudkul","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/","og_locale":"en_US","og_type":"article","og_title":"Getting started with Redis - Cloudkul","og_description":"Introduction &nbsp; Redis is a NoSQL or non-relational, advanced key-value store and in-memory database server. [...]","og_url":"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/","og_site_name":"Cloudkul","article_published_time":"2019-04-29T06:37:08+00:00","article_modified_time":"2019-04-29T13:20:32+00:00","og_image":[{"url":"https:\/\/cloudkul.com\/blog\/wp-content\/uploads\/2019\/04\/redis-ds.png"}],"author":"Manish Bisht","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/","url":"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/","name":"Getting started with Redis - Cloudkul","isPartOf":{"@id":"https:\/\/cloudkul.com\/blog\/#website"},"datePublished":"2019-04-29T06:37:08+00:00","dateModified":"2019-04-29T13:20:32+00:00","author":{"@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/bb2c4fc497998b7483f066285b186dc7"},"breadcrumb":{"@id":"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cloudkul.com\/blog\/getting-started-with-redis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Getting started with Redis"}]},{"@type":"WebSite","@id":"https:\/\/cloudkul.com\/blog\/#website","url":"https:\/\/cloudkul.com\/blog\/","name":"Cloudkul","description":"Host your eCommerce Store on AWS with Optimized Performance","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudkul.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/bb2c4fc497998b7483f066285b186dc7","name":"Manish Bisht","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/1e80c17ca6bb0845750b842faeb3f4e7?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1e80c17ca6bb0845750b842faeb3f4e7?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","caption":"Manish Bisht"},"url":"https:\/\/cloudkul.com\/blog\/author\/manish-bishtck91\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/4028"}],"collection":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/comments?post=4028"}],"version-history":[{"count":48,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/4028\/revisions"}],"predecessor-version":[{"id":4079,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/4028\/revisions\/4079"}],"wp:attachment":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media?parent=4028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/categories?post=4028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/tags?post=4028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}