<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shravan I.V on Medium]]></title>
        <description><![CDATA[Stories by Shravan I.V on Medium]]></description>
        <link>https://medium.com/@ivshravan?source=rss-b937019d5ff4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lmQycP40bMgxZRKN_PzNTA.png</url>
            <title>Stories by Shravan I.V on Medium</title>
            <link>https://medium.com/@ivshravan?source=rss-b937019d5ff4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 20:25:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ivshravan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Redis in 30mins — Scratching the Surface]]></title>
            <link>https://medium.com/@ivshravan/scratch-the-surface-of-aws-elasticcache-redis-in-30mins-e904766844de?source=rss-b937019d5ff4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e904766844de</guid>
            <category><![CDATA[redis]]></category>
            <dc:creator><![CDATA[Shravan I.V]]></dc:creator>
            <pubDate>Sun, 19 Mar 2023 11:46:41 GMT</pubDate>
            <atom:updated>2023-03-20T00:56:28.100Z</atom:updated>
            <content:encoded><![CDATA[<h3>Redis in 30mins — Scratching the Surface</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ud_jJ27HRAu1DOYZgTBEDw.png" /></figure><p>Are you tired of scouring the internet to learn the basics of Redis? Look no further! This article series is here to provide newbies with a simple and accessible introduction to Redis. Whether you’re evaluating Redis as a potential technology choice for your designs, or simply looking for a basic starter, this article has got you covered.</p><p>As a developer, researching various technology choices and conducting feasibility studies can be a daunting task. With this article, you can quickly and easily grasp the essential concepts and features of Redis, without the need for extensive research and make informed technology choices for your system designs.</p><h3>In a nutshell</h3><p>Redis (Remote Dictionary Server) is an in-memory data structure store, that can be used as a distributed cache, in-memory key-value database, message queue with optional durability.</p><p>Various offerings are well suited for enterprise-grade deployments as they come with high availability through means of replicas, fault tolerant &amp; resilient through self healing capabilities, horizontal scalability through sharding of data and supporting 100 thousands of requests per second, and support of microseconds of read and milliseconds of write latency. Redis can also be configured for consistency &amp; durability.</p><p>As a company, it was founded in 2011 and headquartered in Mountain View, California with a mission to empower builders and innovators to advance their mission through real-time data. Redis supports community and enterprise offerings. The community version is the Opensource Redis licensed under BSD-3 license.</p><h3>Redis Deployments</h3><p>Redis is provided as an offering by various Cloud Service, like AWS <a href="https://aws.amazon.com/elasticache/redis/">Elasticache</a> for Redis, Azure <a href="https://azure.microsoft.com/en-us/products/cache/">Cache for Redis</a>, GCP<a href="https://cloud.google.com/memorystore"> In-memory store</a>. Additionally, one can also deploy Redis for <a href="https://docs.redis.com/latest/kubernetes/deployment/quick-start/">Kubernetes</a> or as simple as on a linux machine by choosing suitable Redis engine version.</p><p>At the time of writing this article, the latest version of Redis engine was 7.0. Redis follows a <a href="https://redis.io/docs/about/releases/">philosophy</a> to release high-stable versions even at cost of slower cycles.</p><p>While in this article we are going to focus upon deployment in AWS Elasticcache for Redis for deployment &amp; topology details, rest of aspects of Redis will remain same regardless of how you choose to deploy them.</p><h3>Cluster Concepts</h3><p>Redis gives you flexibility to configure and deploy the cluster based on your demands. It could be possible that the node or server on which Redis is running could go down and to ensure availability you can have replica nodes for every primary node. Further, as the number of keys grow storing them all in a single node (and corresponding replica nodes) may not be scalable, so you can spawn new shards to partition the data and load balance the requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*FD9eNRRyrJEFrZ4k1rTu3g.png" /></figure><p>The above three modes differ primarily in reliability, availability, and scaling behavior. For a production workload, consider using a configuration that includes replication to enhance protection of your data. With the exception of the primary node, if a node fails for any reason, you do not lose data as it has been replicated to other nodes. If the primary node fails, you may lose some data due to replication latency.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v12q6IQ5X3lONEeLv3T5tw.png" /></figure><h4>Shards &amp; Replicas</h4><p>Shards are used for data partitioning while Replicas for achieving fault tolerence &amp; high availability. With replicas and shards, you can configure your cluster as anywhere starting from a single node with/without replica nodes to a cluster with multiple shards and replicas in each shard.</p><p>A cluster can have one or more shards, and each shard will have one primary node and 0 to 5 replica nodes. With AWS Elasticcache, you can have up till 500 nodes in a cluster with configurations ranging from either 500 shards with 1 primary 0 replicas or 83 shards with 1 primary and 5 replicas in each shard.</p><h4>Data Sharding</h4><p>Redis distributes storage of every key in something called as Hash slot across primary (or master nodes) in the cluster. There can be 16,384 hash slots in Redis Cluster that divides the entire key space. To compute hash slot for a given key, one can simply take the CRC16 of the key modulo 16384.</p><p>For instance in a 3 node cluster,</p><ul><li>Redis can have slots 1–5460 on node #1,</li><li>slot 5461–10,920 on node #2, and</li><li>slot 10,921–16,384 on node #3.</li></ul><p><strong>Re-Sharding/Balancing the cluster<br></strong>Rebalancing or re-sharding of shards is needed whenever you add/remove nodes in your cluster. This includes shard migration, and setting up triggers for auto-balancing without impacting your application. If we skip rebalancing we could end up with an unbalanced cluster. For instance one full node and many empty nodes. Moving hash slots from a node to another does not require stopping any operations; therefore, adding and removing nodes, or changing the percentage of hash slots held by a node, requires no downtime.</p><p>Auto resharding is only supported on Redis Enterprise. If you are running on the Redis Community version, then you need to perform the maintenance operations manually. Even though manual, Redis comes with nice tools/<a href="https://severalnines.com/blog/hash-slot-resharding-and-rebalancing-redis-cluster/">CLIs</a> to achieve these operations.</p><p><strong>Hash-Tags<br></strong>User can also force keys to belong to same hash slot through something called hash-tags. This can be achieved by following Redis conventions ({}) while naming the keys. If there is a substring between {} brackets in a key, only what is inside the string is hashed. For example, the keys K1:{123} and K2:{123} are guaranteed to be in the same hash slot because they share the same hash tag {123}.</p><h4>Replication</h4><p>Redis replication is non-blocking on the master side. Writes are sent from primary to replicas is an asynchronous way (i.e, clients gets an ACK while the replication would still be ongoing from primary to replica). To make sure that when sub-set of master nodes fail the cluster still remains functional (i.e, be able to serve across hash slots), Redis uses master-replica model.</p><p>At the event of failure of any master node, one of the replicas in that shard get promoted to the new master (or primary node). In case both primary and replicas fail at same time, then Redis cluster goes non-functional.</p><p>Redis uses diskless replication since version 2.8.18 in which case the disk on the primary node is not used as intermediary storge before sending the RDB over the wire to replicas, but rather the bit stream is directly sent over wire.</p><p>Its advised that the replicas are read-only so that it avoids synchronization complexity in case writes are supported by both primary and recplias. Since Redis 2.6, replicas support a read-only mode that is enabled by default. Historically writeable replicas were enabled for some use-cases which were found obsolete starting with 7.0 version, and</p><h4>Cluster Networking</h4><p>Every Redis Cluster node requires two open TCP connections —</p><ul><li>A Redis TCP port used to <em>serve clients</em>, e.g., 6379, and</li><li>A port known as the <em>cluster bus port </em>for communication with other nodes in the cluster. By default, the cluster bus port is set by adding 10000 to the data port (e.g., 16379); however, you can override this in the cluster-portconfiguration. The cluster bus port must be reachable from all the other cluster nodes for Redis to work properly.</li></ul><p>Redis uses Gossip protocol to exchange information across nodes in the cluster, discover new nodes and send heartbeats.</p><p><strong>Security<br></strong>It is advised to not expose Redis endpoints to untrusted networks or systems. Access shall be restricted through implementing ACLs introduced since Redis 6. One can also configured user and passwords for anyone accessing the cluster. Redis has optional support for TLS on all communication channels, including client connections, replication links, and the Redis Cluster bus protocol.</p><h4>Disaster Recovery / Persistence</h4><p>Redis supports two kinds of persistence — RDB &amp; AOF.</p><ul><li>RDB (Redis DataBase) performs point in time snapshots of Redis data.</li><li>AOF (Append Only File) maintains transaction log of all the Redis commands and replays them when the failed node comes back.</li></ul><p>You can also disable persistence altogether for simple caching use-cases. Choosing RDB or AOF depends on your use-case. For instance, for large data-sets RDB outperforms AOF in terms of restart time and also the performance in general. Further comparisons can be found <a href="https://redis.io/docs/management/persistence/">here</a>.</p><h3>Fit in CAP Theorem</h3><p>Redis fits more on CP (Consistency &amp; Partition Tolerence). Few things to note —</p><p><strong>Consistency</strong></p><ul><li>Redis Cluster does not guarantee strong consistency. One can do strongly consistent reads from primary node but weak consistent reads from replicas.</li><li>One can force synchronous replications with trade-off on write performance using WAIT command (commands discussed below in this article) to maximise the likelihood of strong read consistency. This could still fail to address more compelx failure scenarios like — say the replica couldn’t receive the writes from primary due to network partition and if partition lasted for longer time following which the replica became the primary node.</li></ul><p><strong>Availability</strong></p><ul><li>Redis<strong> </strong>makes itself highly available through self-healing process within the shard. In case primary fails, then Redis sentinels promote one of the replicas to master.</li></ul><p><strong>Partition Tolerence</strong></p><ul><li>While we just said Redis’ ability to make itself highly available, during the event of network partition the minority partition will be unavailable while the majority partition remains available.</li></ul><p>Hence from CAP perspective, Redis can be said to be a CP system.</p><h3>Cache Eviction</h3><p>Just like any cache Redis supports eviction policies that can be specified in the configuration. AWS ElastiCache for Redis sets the <strong>volatile-lru</strong> eviction policy for your Redis cluster by default. The least recently used keys with TTL set are evicted whenever the cache memory is overfilled or is greater than the <strong>maxmemory</strong> setting for the cache.</p><p>Other eviction policies that are available in Elasticache are listed below —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1ANb-yHbNfEtSGZR8hYtsw.png" /></figure><h3>AWS Elasticache for Redis</h3><h4>Choosing the Instance Type</h4><ul><li>AWS Elastic cache offers Free Tier of instances (<strong>T2/T3 </strong>micro series of EC2) for 750 hours per month. For entry level use-cases you could use T2/T3 family of instances that provide 2000 or less TPS and 10GB or less data. Medium workloads can be achieved using xlarge and above in T3 series of EC2. For moderate to high workloads, use M5/R5 instances that can provide &gt; 20,000 TPS and 100GB of data size workloads. You can expect 300–500 microseconds of latency.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3u3mUkPNQe5MwT2-gxu4ww.png" /></figure><ul><li>Redis can run on x86 based processors and since 4.0 on ARM, Raspberry pi processors. Redis also published performance numbers for Redis on ARM in their <a href="https://redis.io/docs/reference/arm/">website</a>.</li></ul><h4>Topology &amp; Connectivity</h4><p>There are multiple ways of achieving the connectivity from your application to cache.</p><ul><li>Case 1: Your application &amp; Redis cluster are running in the same VPC and same subnet.</li><li>Case 2: Application and Redis cluster are running same VPC but different subnets</li><li>Case 3: Application &amp; Redis cluster are running in different VPCs.</li></ul><p>For Case 1, connectivity is quite simple and it needs setting up security group of Redis’ cluster with correct inbound traffic rules that’d allow TCP traffic from appropriate subnets. The Redis Cluster’s ‘subnet group’ should include the subnet of the application in order for application to make connectivity to Redis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MVvqr3DDO611oPM3_HVy9g.png" /></figure><p>For Case2, in addition to whatever is configured in Case 1, one needs setting route tables that will provide a L3 routing path for traffic to Redis cluster. Additionally, one could use Route 53 to setup DNS entry if at all you plan to access Redis through canonical names.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DFvINGV2kctwT2SP_djmtQ.png" /></figure><p>Case 3 is when the Redis cluster is in a different VPC (same or different AWS account) from the application accessing it. A typical production deployment could follow this case. Connectivity can be bridged either by VPC Peering or by using VPC endpoint service. VPC peering is preferred when both the VPC trust each other to a greater degree. VPC end point servies exposed only the Redis end point from the destination VPC to the source VPC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z93fPfcww5-4jh2g8B5bgA.png" /></figure><h3>Interacting with Redis</h3><h4>CLI</h4><p>To get a flavour of how Redis behaves, you can install Redis client CLI on your EC2 instance and start accessing it using the CLI.</p><p><strong>Client CLI Installation</strong></p><pre>sudo yum install gcc jemalloc-devel openssl-devel tcl tcl-devel clang wget<br>sudo wget http://download.redis.io/redis-stable.tar.gz<br>sudo tar xvzf redis-stable.tar.gz<br>cd redis-stable<br>sudo CC=clang make BUILD_TLS=yes</pre><p>You can connect to your redis endpoint using</p><pre>src/redis-cli -h &lt;primary-endpoint&gt; -p &lt;port number&gt;</pre><p>and start trying out basic commands like below —</p><pre>redis 127.0.0.1:6379&gt; INFO                  [Provides info about the cluster]<br><br>redis 127.0.0.1:6379&gt; SET &lt;key&gt; &lt;value&gt;     [Sets a key with a value]<br><br>redis 127.0.0.1:6379&gt; DEL &lt;key&gt;             [Deletes the key]<br><br>redis 127.0.0.1:6379&gt; EXISTS &lt;key&gt;          [Checks if the key exists]<br><br>redis 127.0.0.1:6379&gt; EXPIRE &lt;key&gt; &lt;time&gt;   [Expires the key after set time]<br><br>redis 127.0.0.1:6379&gt; TTL &lt;key&gt;             [Sets TTL for key in seconds]<br><br>redis 127.0.0.1:6379&gt; MSET &lt;key-1&gt; &lt;value-1&gt; [&lt;key-2&gt; &lt;value-2&gt;]. [Sets multiple keys in single command]<br><br>redis 127.0.0.1:6379&gt; INCR &lt;key&gt;            [Increments the key if its integer]<br><br>redis 127.0.0.1:6379&gt; DECR &lt;key&gt;            [Decrements the key if its integer]<br><br>redis 127.0.0.1:6379&gt; APPEND &lt;key&gt; &lt;value&gt;  [Appens to the key if its string]<br></pre><p>Power of Redis lies in the data structures that it provides. Examples are Hash map, List, Set as show below —</p><pre>-- Hash DS<br>redis 127.0.0.1:6379&gt; HSET &lt;map-name&gt; &lt;field&gt; &lt;value&gt;<br>redis 127.0.0.1:6379&gt; HMSET &lt;map-name&gt; &lt;field-1&gt; &lt;value-1&gt; [&lt;field-2&gt; &lt;value-2&gt;]<br>redis 127.0.0.1:6379&gt; HGETALL &lt;map-key-name&gt;<br>redis 127.0.0.1:6379&gt; HDEL &lt;map-key-name&gt;<br>redis 127.0.0.1:6379&gt; HLEN &lt;map-key-name&gt; <br>redis 127.0.0.1:6379&gt; HKEYS &lt;map-key-name&gt;<br><br>-- List DS<br>redis 127.0.0.1:6379&gt; LPUSH &lt;key&gt; &lt;value-1&gt; [&lt;value-2&gt;]<br>redis 127.0.0.1:6379&gt; LPOP &lt;key&gt;<br>redis 127.0.0.1:6379&gt; LRANGE &lt;key&gt; &lt;start-index&gt; &lt;end-index&gt; [-1 till end]<br>redis 127.0.0.1:6379&gt; LLEN &lt;key&gt;<br>redis 127.0.0.1:6379&gt; LREM &lt;key&gt; &lt;count&gt; &lt;value&gt;<br>redis 127.0.0.1:6379&gt; RPUSH &lt;key&gt; &lt;value-1&gt; [&lt;value-2&gt;]<br>redis 127.0.0.1:6379&gt; RPOP &lt;key&gt;<br><br>--Set DS<br>redis 127.0.0.1:6379&gt; SADD &lt;key&gt; &lt;member-1&gt; &lt;member-2&gt; <br>redis 127.0.0.1:6379&gt; SPOP &lt;key&gt;<br>redis 127.0.0.1:6379&gt; SREM &lt;key&gt; [&lt;member-1&gt; &lt;member-2&gt;]<br>redis 127.0.0.1:6379&gt; SINTER &lt;key-1&gt; &lt;key-2&gt;<br>redis 127.0.0.1:6379&gt; SUNION &lt;key-1&gt; [&lt;key-2&gt;]<br>redis 127.0.0.1:6379&gt; SMEMBERS &lt;key&gt;</pre><p>Few other features like the Redis Queue (message broker) can be accessed —</p><pre>--Publish / Subscribe<br>redis 127.0.0.1:6379&gt; SUBSCRIBE &lt;channel-name&gt;<br>redis 127.0.0.1:6379&gt; PUBLISH &lt;channel-name&gt; &lt;message string&gt;<br>redis 127.0.0.1:6379&gt; UNSUBSCRIBE &lt;channel-name&gt;</pre><p>Transactions can be executed or discarded (rolledback).</p><pre>--Trasactions<br>redis 127.0.0.1:6379&gt; MULTI<br>redis 127.0.0.1:6379&gt; &lt;command-1&gt;<br>redis 127.0.0.1:6379&gt; &lt;command-2&gt;<br>redis 127.0.0.1:6379&gt; EXEC | DISCARD</pre><p>For more queries, refer to <a href="https://redis.io/commands/">Redis command list</a>.</p><p><a href="https://redis.io/commands/">Commands</a></p><h4>Programmatic Access</h4><ul><li>Java — One can use <a href="https://github.com/redis/jedis">Jedis</a> client library for connecting to Redis, which is sponsored by Redis itself. Alternatively, <a href="https://github.com/redisson/redisson">Redisson</a> is also a good option.</li><li>Python — <a href="https://docs.redis.com/latest/rs/references/client_references/client_python/">Redis-py</a> is the standard client library.</li><li>Node JS — <a href="https://redis.io/docs/stack/get-started/tutorials/stack-node/">Redis OM</a>.</li><li>Redis also supports <a href="https://redis.io/docs/manual/programmability/eval-intro/">Lua</a> scripting directly on the console.</li></ul><h3>Competing Technologies</h3><h4>Memcached</h4><p>Redis is feature rich, and Memcached is the simplistic alternate to it. Memcached lacks replication, fault tolerance &amp; disaster recovery, transactions etc. One can opt for Memcached if their use-case is as simple as a cache whose availability &amp; durability are not a concern. Further comparison can be found <a href="https://aws.amazon.com/elasticache/redis-vs-memcached/">here</a>.</p><h3>References</h3><ul><li><a href="https://redis.io/docs/management/">Manage Redis</a></li><li><a href="https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/WhatIs.html">What is Amazon ElastiCache for Redis?</a></li><li><a href="https://stackoverflow.com/questions/59511275/redis-availability-and-cap-theorem">Redis availability and CAP theorem</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e904766844de" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>