<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Divyanshu Tomar</title>
    <description>The latest articles on DEV Community by Divyanshu Tomar (@divyanshutomar).</description>
    <link>https://dev.to/divyanshutomar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F14742%2Fd1c891b8-fa46-4e43-b5e4-5fe92dcea58f.jpeg</url>
      <title>DEV Community: Divyanshu Tomar</title>
      <link>https://dev.to/divyanshutomar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/divyanshutomar"/>
    <language>en</language>
    <item>
      <title>Introduction to Redis</title>
      <dc:creator>Divyanshu Tomar</dc:creator>
      <pubDate>Sun, 08 Jul 2018 17:27:05 +0000</pubDate>
      <link>https://dev.to/divyanshutomar/introduction-to-redis-3m2a</link>
      <guid>https://dev.to/divyanshutomar/introduction-to-redis-3m2a</guid>
      <description>&lt;p&gt;For a high traffic web service, it becomes a necessity for it to leverage some kind of caching mechanism. Caching is a way of storing computed data in memory so that future requests can be fulfilled right away. It also helps in avoiding any round trips to the data layer and computations on the application side if implemented with the right strategy. Redis and Memcached are two most popular memory-based stores available. In this post, we will explore some key concepts of Redis and go through some basic commands. Besides caching, Redis can be also used for other applications where there is a need for fast and frequent access to data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Redis
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F787xlgwc2hhq3ctzxcvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F787xlgwc2hhq3ctzxcvs.png" alt="redis logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redis is an in-memory data structure store supporting many data types like strings, hashes, sets, sorted sets, etc. Essentially, it is a key-value store.&lt;/p&gt;

&lt;p&gt;Every type of value in Redis is stored against a key that is binary safe and it can be anything from an empty string to long hash string. Every application should follow a predetermined schema for naming Redis keys to avoid any naming conflicts.&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up Redis
&lt;/h4&gt;

&lt;p&gt;Like every database, Redis contains a server for storing data in memory and clients which will execute commands against a server.&lt;br&gt;
For setting up the server on your local machine, I will recommend using Docker as it is easy to get started. If you have Docker daemon running on your machine, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --rm -it --name local-redis -p 6379:6379 redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will run a Docker container with name local-redis on your localhost with port 6379. It uses the official Redis docker image to run the container.&lt;/p&gt;

&lt;p&gt;For the client, we can use the redis-cli for executing commands from a console on the Redis server. Open a new tab, and execute the following command to start a redis-cli session connected to local docker Redis server instance:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -it --link local-redis:redis --rm redis redis-cli -h redis -p 6379
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now we can start executing some basic Redis commands.&lt;/p&gt;
&lt;h3&gt;
  
  
  Commands
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Setting&lt;/em&gt; a value:&lt;/p&gt;

&lt;p&gt;Syntax: &lt;code&gt;SET &amp;lt;key&amp;gt; &amp;lt;value&amp;gt;&lt;/code&gt;&lt;br&gt;
Example: &lt;code&gt;SET firstname Albert&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Retrieve&lt;/em&gt; a value:&lt;/p&gt;

&lt;p&gt;Syntax: &lt;code&gt;GET &amp;lt;key&amp;gt;&lt;/code&gt;&lt;br&gt;
Example: &lt;code&gt;GET firstname&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Check whether a key &lt;em&gt;exists&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;Syntax: &lt;code&gt;EXISTS &amp;lt;key&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Deleting&lt;/em&gt; a key:&lt;/p&gt;

&lt;p&gt;A key can be removed along with its associated memory using:&lt;br&gt;
&lt;code&gt;DEL &amp;lt;key&amp;gt;&lt;/code&gt;&lt;br&gt;
This is a synchronous blocking operation.&lt;/p&gt;

&lt;p&gt;A better way to remove keys will be to unlink them whose associated memory can be collected by a garbage collector later on.&lt;br&gt;
&lt;code&gt;UNLINK &amp;lt;key&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Setting a &lt;em&gt;time to expire&lt;/em&gt; for key:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EXPIRE &amp;lt;key&amp;gt; &amp;lt;seconds&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;PEXPIRE &amp;lt;key&amp;gt; &amp;lt;milliseconds&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Setting&lt;/em&gt; a key with check for &lt;em&gt;existence&lt;/em&gt; and &lt;em&gt;expiry&lt;/em&gt; in one go:&lt;/p&gt;

&lt;p&gt;Syntax: &lt;code&gt;SET &amp;lt;key&amp;gt; &amp;lt;value&amp;gt; &amp;lt;EX seconds&amp;gt;|&amp;lt;PX milliseconds&amp;gt; NX|XX&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;NX - set only when a key does not exist.&lt;br&gt;
XX - set only when key already exists.&lt;br&gt;
EX - sets expire time for the key in seconds.&lt;br&gt;
PX - sets expire time for the key in milliseconds.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SET firstname Albert EX 10 NX&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will set the key firstname with string value "Albert" with an expiry time of 10 seconds only if the key does not exist.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;Increment&lt;/em&gt; or &lt;em&gt;Decrement&lt;/em&gt; an integer value:&lt;/p&gt;

&lt;p&gt;Redis provides a convenient way to increment or decrement integer values that may be used as counters.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;code&gt;INCR &amp;lt;key&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;DECR &amp;lt;key&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;INCRBY &amp;lt;key&amp;gt; &amp;lt;increment value&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;DECRBY &amp;lt;key&amp;gt; &amp;lt;decrement value&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;code&gt;SET counter 4&lt;/code&gt;&lt;br&gt;
&lt;code&gt;INCRBY counter 6&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;counter key will hold the value 4 initially, and after the second command, it will get incremented to 10.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the above mentioned commands just deal with storing and manipulating of string or integer values. There are other data structure values such as hashes, sets, bit arrays, etc. that can be used to solve complex problems.&lt;/p&gt;
&lt;h4&gt;
  
  
  Real World Example
&lt;/h4&gt;

&lt;p&gt;In a real-world application, you can use various programming language specific &lt;a href="https://redis.io/clients" rel="noopener noreferrer"&gt;redis clients&lt;/a&gt; available for interacting with your Redis server from the application code.&lt;/p&gt;

&lt;p&gt;We will be writing a simple Node based application that exposes an endpoint for getting user info against an userid. A JSON file will act as our datastore to keep things as simple as possible.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, initialise a NPM repository by running &lt;code&gt;npm init&lt;/code&gt; and install &lt;em&gt;express&lt;/em&gt; and &lt;em&gt;redis&lt;/em&gt; as dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now, make a redis helper file that forms an instance of the redis client connected to our Redis server. We are also writing some cache helper methods here for our route handlers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fyvnh0ig59puaqj8xduyn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fyvnh0ig59puaqj8xduyn.png" alt="redis cache helper file"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In main app file, write a route handler that accepts an userid against which the user info is to be retrieved. Next, form a unique redis key using the userid. This key will always be the same for every request for a given userid. Check for existence of this key in the Redis cache, and return the response if found.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F29fg2n4l4900x41gmhgz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F29fg2n4l4900x41gmhgz.png" alt="main file index.js"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Else, we will query the data from our data source and set the response data to Redis cache before sending it back as a response.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To have a look at the full code and tinker around with it, you can clone the following repository:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/divyanshutomar" rel="noopener noreferrer"&gt;
        divyanshutomar
      &lt;/a&gt; / &lt;a href="https://github.com/divyanshutomar/hello-redis" rel="noopener noreferrer"&gt;
        hello-redis
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Express application that uses Redis for caching data
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Hello Redis Example&lt;/h1&gt;

&lt;/div&gt;

&lt;p&gt;An express application that demonstrates how redis can be utilized for caching data
so that recurrent requests can be fulfilled right away.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Requirements&lt;/h3&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Node &amp;gt;= 8.x&lt;/li&gt;
&lt;li&gt;Redis&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;Setup and Running&lt;/h3&gt;

&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Clone this repo.&lt;/li&gt;
&lt;li&gt;Install all the node dependencies using &lt;code&gt;npm install&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Make sure you have local redis server instance running on &lt;code&gt;localhost:6379&lt;/code&gt;. If not, you can easily start one
by running the following command if you have docker daemon running on your machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;docker run --rm -it --name local-redis -p 6379:6379 redis
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Start the node service by running &lt;code&gt;node index.js&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/divyanshutomar/hello-redis" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;Congratulations! You have now learned the basics of Redis. If you'd like to take a deep dive, please have a look at the official &lt;a href="https://redis.io/documentation" rel="noopener noreferrer"&gt;redis docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for following along and I hope this post would have been useful for you.&lt;br&gt;
Do follow me on &lt;a href="https://twitter.com/divyanshutomar" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; to receive updates on such topics.&lt;/p&gt;

</description>
      <category>redis</category>
      <category>node</category>
      <category>caching</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
