<?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: Marvelous Akporowho</title>
    <description>The latest articles on DEV Community by Marvelous Akporowho (@superdeveloper).</description>
    <link>https://dev.to/superdeveloper</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%2F925967%2F0580d848-a647-4196-abe1-5756428737dd.png</url>
      <title>DEV Community: Marvelous Akporowho</title>
      <link>https://dev.to/superdeveloper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/superdeveloper"/>
    <language>en</language>
    <item>
      <title>Trust me, Redis really makes your app super fast.</title>
      <dc:creator>Marvelous Akporowho</dc:creator>
      <pubDate>Fri, 16 Sep 2022 11:46:41 +0000</pubDate>
      <link>https://dev.to/superdeveloper/trust-me-redis-really-makes-you-app-super-fast-3ohf</link>
      <guid>https://dev.to/superdeveloper/trust-me-redis-really-makes-you-app-super-fast-3ohf</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Redis is super fast, Trust me. At the end of this article our sample app shows you how we went from a response time of 742 milliseconds to 5 milliseconds using Redis.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is Redis?
&lt;/h2&gt;

&lt;p&gt;Redis, which stands for Remote Dictionary Server, is a fast, key-value data store initially created to improve application scalability and speed. Today, Redis is now used as a database, cache, message broker, and queue.&lt;/p&gt;

&lt;p&gt;Redis provides sub-millisecond response times, allowing for millions of requests per second for real-time applications in a lot of industries Today, Redis is one of the most popular open-source engines, having been named the "Most Loved" database by Stack Overflow for five years in a row and currently ranking second in 2022. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Redis is a popular choice for caching, session management, gaming, leaderboards, real-time analytics, geospatial, ride-hailing, chat/messaging, media streaming, and pub/sub apps due to its fast performance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Benefits of using Redis
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;br&gt;
Redis's memory-based design allows for low latency and high throughput data access. In-memory data stores, as opposed to conventional databases, don't require a trip to the disk, cutting engine latency to microseconds. In-memory data stores can therefore support orders of magnitude more operations and provide quicker responses. As a result, operations like reads and writes typically take less than a millisecond, and millions of operations can be supported per second.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flexible data structures&lt;/strong&gt;&lt;br&gt;
Redis offers a wide range of data structures to satisfy the requirements of your application. Data types in Redis include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strings&lt;/strong&gt; – a sequence of characters, either as a literal constant or as some kind of variable or binary data up to 512MB in size&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lists&lt;/strong&gt; – abstract data type that represents a finite number of ordered values&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sets&lt;/strong&gt; – a collection of elements in an unordered collection of strings with the ability to intersect, union, and diff other set types&lt;/li&gt;
&lt;li&gt;*&lt;em&gt;Sorted Sets *&lt;/em&gt;– well-defined objects or sets ordered by a value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hashes&lt;/strong&gt; – a data structure for storing a list of fields and values&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bitmaps&lt;/strong&gt; – a compact data structure to store binary logic and states. It is a data type that offers bit-level operations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HyperLogLogs&lt;/strong&gt; – a probabilistic data structure to estimate the unique items in a data set&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Streams&lt;/strong&gt; - a log data structure message queue&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Geospatial&lt;/strong&gt; - a longitude-/latitude-based entries Maps&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JSON&lt;/strong&gt; - a named value object that supports nested, semi-structured objects such as numbers, strings, Booleans, arrays, and others.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Simplicity and ease-of-use&lt;/strong&gt;&lt;br&gt;
Redis makes it possible to write traditionally complex code in smaller, more concise chunks. Redis allows you to store, access, and use data in your applications with fewer lines of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Persistence and Replication&lt;/strong&gt;&lt;br&gt;
Redis uses a primary-replica architecture and asynchronous replication, which allows data to be replicated to multiple replica servers so as to improve read performance (as requests can be distributed among the servers) and allows for faster recovery when the primary server fails.&lt;/p&gt;
&lt;h2&gt;
  
  
  Popular Redis Use Cases
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Caching&lt;/strong&gt;&lt;br&gt;
Redis is an excellent choice for building a highly available in-memory cache to reduce data access latency, increase throughput, and relieve load on your relational or NoSQL database and application. Redis can serve frequently requested items in sub-milliseconds and allows you to easily scale for higher loads without expanding the more expensive backend.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Messaging, and Queues&lt;/strong&gt;&lt;br&gt;
Redis is an excellent choice for high-performance chat rooms, real-time comment streams, social media feeds, and server-to-server communication. A lightweight queue can be easily implemented using the Redis List data structure. Lists support atomic operations as well as blocking, making them suitable for a wide range of applications that require a dependable message broker.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Session handling&lt;/strong&gt;&lt;br&gt;
Redis provides the sub-millisecond latency, scalability, and resilience needed to manage session data such as user profiles, credentials, session state, and user-specific personalization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Media streaming&lt;/strong&gt;&lt;br&gt;
Redis can be used to store metadata about video viewing histories alongside user details for millions of users, as well as media manifest files to enable scalable and optimized video streaming to millions of users at once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-time Analytics&lt;/strong&gt;&lt;br&gt;
Redis can be used as an in-memory data store with streaming solutions such as Apache Kafka and Amazon Kinesis to ingest, process, and analyze real-time data with sub-millisecond latency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rate Limiting&lt;/strong&gt;&lt;br&gt;
One of the best ways to implement a rate-limiting mechanism in your application is to use an integer-based Redis key that expires after a set period of time to prevent your users or other malicious agents from abusing server resources.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to use Redis
&lt;/h2&gt;

&lt;p&gt;When running Redis, you have several options. You can use the Redis CLI on your development or production machines, you can use Redis Stack, which is an extension of Redis that adds modern data models and processing engines to provide a complete developer experience, or you can run Redis on the cloud via Redis Labs, which is a cloud-based solution for running Redis at scale.&lt;/p&gt;

&lt;p&gt;In our example project, we will work with the Redis CLI. &lt;br&gt;
Let’s proceed to installing Redis on a windows machine.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to Install Redis CLI
&lt;/h2&gt;

&lt;p&gt;How you install Redis depends on your development workflow. If you’re installing Redis on your local development machine, you can install natively, or you can install using docker container.&lt;/p&gt;

&lt;p&gt;To install Redis on your computer, &lt;a href="https://redis.io/docs/getting-started/installation/"&gt;click here&lt;/a&gt; to access the official installation guide for your preferred operating system.&lt;/p&gt;

&lt;p&gt;To install Redis on Windows, you must first enable WSL2 (Windows Subsystem for Linux). WSL2 allows you to run Linux binaries on Windows natively. To use this method, you must be running Windows 10 version 2004 or higher, or Windows 11.&lt;/p&gt;

&lt;p&gt;If you're a windows user, follow the steps as outlined below:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt;&lt;br&gt;
Install or activate WSL2 using the instruction &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt;&lt;br&gt;
Open command prompt or power shell and execute the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update

sudo apt-get install redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt;&lt;br&gt;
Start the Redis server like so using command prompt or your preferred terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo service redis-server start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt;&lt;br&gt;
Connect to Redis&lt;br&gt;
You can test that your Redis server is running by connecting with the Redis CLI using your preferred terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;redis-cli
127.0.0.1:6379&amp;gt; ping
PONG
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you entered the command &lt;strong&gt;ping&lt;/strong&gt; and received &lt;strong&gt;PONG&lt;/strong&gt; as response, you're good to go. Redis is now running on your machine.&lt;/p&gt;

&lt;p&gt;Using the Redis CLI, we can send commands to and read replies from the Redis server. This is how we perform a variety of operations using Redis Commands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://redis.io/commands/"&gt;Click here&lt;/a&gt; to see the list of commands curated by the Redis team.&lt;/p&gt;

&lt;p&gt;A quick example of adding and reading replies from the Redis server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET username "marvelous"

GET username

DELETE username
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Practical Example
&lt;/h2&gt;

&lt;p&gt;Setup an expressjs application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# initialise a new node project
npm init -y

# install expressjs, redis and node-fetch
npm i express redis node-fetch

# for the purpose of this example i am using nodejs v18
# open the project in your favorite code editor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a new file called &lt;code&gt;main.js&lt;/code&gt; and add the following code snippet to the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import redis from 'redis';
import fetch from 'node-fetch'
import express, { response } from 'express'
import {itemCache} from './middleware.js'

//set constant express port
const PORT = 3000;

//set constant redis port
const RPORT = 6379;

// initialise node redis client
export const client = redis.createClient({
    legacyMode: true,
    RPORT
})

// connect to redis client
client.connect();

// create express app instance
const app = express()

// set public API base url
const factApiBase = 'https://jsonplaceholder.typicode.com'

// fetch todos from jsonplaceholder api
async function getItems(req, res) {
    try {
        // fetch todos from jsonplaceholder API
        const response = await fetch(`${factApiBase}/todos`)
        const result = await response.json()
        // cache stringified data to redis
        client.set("items", JSON.stringify(result))
        res.send(result)
    } catch (err) {
        console.error(err);
        res.status(404)
    }
}

// express route
app.get(`/items`, itemCache, getItems)

// launch express app
app.listen(PORT, () =&amp;gt; {
    console.log(`App is listening on port ${PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a &lt;code&gt;middleware.js&lt;/code&gt; file, and add the following code expressjs middleware snippet to the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// cache middleware for fetching todos from redis
import {client} from './main.js'
export function itemCache(req, res, next) {
    client.get("items", (err, data) =&amp;gt; {
        if (err) throw err;
        if (data !== null) {
            res.send(JSON.parse(data))
        } else {
            next()
        }
    })
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run your application using 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;node main.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test the performance of our app, open up postman or an alternative API testing/development tool.&lt;/p&gt;

&lt;p&gt;This is our response time without a cache (742 milliseconds)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8hGImhqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onp70jyr12tgu2l5mtlp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8hGImhqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onp70jyr12tgu2l5mtlp.png" alt="Image description" width="880" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is our response time with the cached response from Redis (5 milliseconds)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DF3_yo7b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tfqlpipb61i59grbqu9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DF3_yo7b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tfqlpipb61i59grbqu9.png" alt="Image description" width="880" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Redis is an open-source project with a thriving community.&lt;br&gt;
It has no vendor or technology lock-in, supports open data formats, and has a diverse set of clients.&lt;/p&gt;

&lt;p&gt;We stringified and passed JSON strings in the preceding example; however, when working with JSON in Redis, it is preferable to use Redis JSON via the Redis stack. Redis has numerous practical use cases in modern applications.&lt;br&gt;
While the list above is not exhaustive, I will be writing about the Redis stack and Redis toolings to help speed up your apps and development workflows.&lt;br&gt;
If your application requires fast response times and scalability, you should always consider using an in-memory database.&lt;/p&gt;

&lt;p&gt;This post is in collaboration with Redis.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://redis.info/3NBGJRT"&gt;Try Redis Cloud for free&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redis.info/3Ga9YII"&gt;Watch this video on the benefits of Redis Cloud over other Redis providers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redis.info/3LC4GqB"&gt;Redis Developer Hub - tools, guides, and tutorials about Redis&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redis.info/3wMR7PR"&gt;RedisInsight Desktop GUI&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>redis</category>
      <category>javascript</category>
      <category>opensource</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
