<?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: Vignesh Muthukumaran</title>
    <description>The latest articles on DEV Community by Vignesh Muthukumaran (@vigneshm243).</description>
    <link>https://dev.to/vigneshm243</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%2F770830%2F85d7fad4-f7fb-4bcd-9588-19c8c185f380.png</url>
      <title>DEV Community: Vignesh Muthukumaran</title>
      <link>https://dev.to/vigneshm243</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vigneshm243"/>
    <language>en</language>
    <item>
      <title>Bloom Filters and Cuckoo Filters</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Mon, 30 Jun 2025 18:37:52 +0000</pubDate>
      <link>https://dev.to/vigneshm243/bloom-filters-and-cuckoo-filters-3hgl</link>
      <guid>https://dev.to/vigneshm243/bloom-filters-and-cuckoo-filters-3hgl</guid>
      <description>&lt;p&gt;Probabilistic data structures are essential tools for efficiently answering questions like “Is this element in my set?” when working with large-scale data. Two of the most popular structures for fast set membership queries, with tunable tradeoffs between space and error rate, are &lt;strong&gt;Bloom Filters&lt;/strong&gt; and &lt;strong&gt;Cuckoo Filters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This article explains both data structures conceptually, and compares their strengths and weaknesses.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where Are Bloom Filters and Cuckoo Filters Used?
&lt;/h2&gt;

&lt;p&gt;Both Bloom Filters and Cuckoo Filters are widely used in real-world systems where memory efficiency and fast lookups are essential and occasional false positives are tolerable. Some common applications include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Databases and Caches:&lt;/strong&gt; Used to quickly check if an item might exist before performing expensive disk or database lookups (e.g., Bigtable, HBase, Cassandra).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Systems:&lt;/strong&gt; Employed in web proxies and routers to filter URLs or packets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distributed Systems:&lt;/strong&gt; Used in distributed hash tables (DHTs), peer-to-peer networks, and blockchain systems to reduce unnecessary data transfers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security and Privacy:&lt;/strong&gt; Utilized in password breach detection, malware detection, and privacy-preserving systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web Analytics and Ad Tech:&lt;/strong&gt; For deduplication, click fraud detection, and audience segmentation.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Bloom Filters
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Bloom Filter&lt;/strong&gt; is a space-efficient, probabilistic data structure used to test whether an element is a member of a set. False positives are possible (an element might appear to be in the set when it isn't), but false negatives are not (an element that is in the set will never be missed).&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Allocate a bit array of size &lt;em&gt;m&lt;/em&gt;, all initialized to 0.&lt;/li&gt;
&lt;li&gt;Use &lt;em&gt;k&lt;/em&gt; independent hash functions.&lt;/li&gt;
&lt;li&gt;To add an element, compute its &lt;em&gt;k&lt;/em&gt; hashes and set the corresponding bits in the array.&lt;/li&gt;
&lt;li&gt;To query membership, compute the &lt;em&gt;k&lt;/em&gt; hashes and check those bits. If any bit is 0, the element is definitely not present. If all are 1, it &lt;em&gt;might&lt;/em&gt; be present (with a certain false positive probability).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pros and Cons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; Very space-efficient, simple to implement, fast insert and lookup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; Cannot delete elements (without counting), false positives possible, no information about element multiplicity.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Cuckoo Filters
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Cuckoo Filter&lt;/strong&gt; is another probabilistic data structure for set membership tests, inspired by Cuckoo Hashing. It supports efficient additions, deletions, and queries, with lower false positive rates and often better performance than Bloom Filters for some applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Divide the filter into buckets, each holding a small number of fingerprints (compact representations of set elements).&lt;/li&gt;
&lt;li&gt;To add an element, compute two possible bucket locations from its fingerprint.&lt;/li&gt;
&lt;li&gt;If either bucket has space, insert the fingerprint.&lt;/li&gt;
&lt;li&gt;If both are full, randomly evict one fingerprint ("cuckoo") and try to re-insert it in its alternate location, repeating as necessary.&lt;/li&gt;
&lt;li&gt;To query, check if the element’s fingerprint is present in either bucket.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pros and Cons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pros:&lt;/strong&gt; Supports deletion, often lower false positive rates, similar or better space efficiency vs. Bloom Filters, good cache performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; More complex, insertions can fail if the filter is too full, slightly higher per-operation overhead.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Comparison Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Bloom Filter&lt;/th&gt;
&lt;th&gt;Cuckoo Filter&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Membership Query&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deletion&lt;/td&gt;
&lt;td&gt;No (unless counting)&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False Positives&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;False Negatives&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Space Efficiency&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Very High&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Complexity&lt;/td&gt;
&lt;td&gt;Simple&lt;/td&gt;
&lt;td&gt;Moderate&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




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

&lt;p&gt;Both Bloom Filters and Cuckoo Filters are invaluable for high-performance, space-efficient set membership queries where occasional false positives are acceptable. The right choice depends on factors like need for deletions, expected query volume, and tolerance for complexity.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Further Reading:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Bloom_filter" rel="noopener noreferrer"&gt;Bloom Filter (Wikipedia)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf" rel="noopener noreferrer"&gt;Cuckoo Filter: Practically Better Than Bloom (Paper)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>systemdesign</category>
      <category>programming</category>
    </item>
    <item>
      <title>Building a RAG application using LangChain and LangSmith</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Sun, 21 Jul 2024 15:30:40 +0000</pubDate>
      <link>https://dev.to/vigneshm243/building-a-rag-application-using-langchain-and-langsmith-1m59</link>
      <guid>https://dev.to/vigneshm243/building-a-rag-application-using-langchain-and-langsmith-1m59</guid>
      <description>&lt;h3&gt;
  
  
  Why RAG?
&lt;/h3&gt;

&lt;p&gt;LLMs have come a long way since their inception. However, one of the main problems with LLMs is that they are trained on publically available data or generic training data. This means they are not able to perform well in domain-specific scenarios. To overcome this we would either need to retrain the model with Domain-specific data which is costly or use something like RAG.&lt;/p&gt;

&lt;p&gt;Another important need is that LLMs are known to &lt;strong&gt;hallucinate&lt;/strong&gt;(generate a response that seems true but is not available in training data/factually incorrect). One of the ways to overcome this is by &lt;strong&gt;grounding&lt;/strong&gt; the response with relevant reliable information.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is RAG?
&lt;/h3&gt;

&lt;p&gt;RAG stands for &lt;strong&gt;Retrieval Augmented Generation&lt;/strong&gt;. The basic principle is that when we query an LLM, we pass in relevant context with the query so that the LLM can easily respond with relevant information. We are able to do this now because the context windows which used to 2048/4096 tokens have now come a long way to around 1 Million for newer models. There are 3 basic steps in RAG,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Indexing&lt;/li&gt;
&lt;li&gt;Retrieval&lt;/li&gt;
&lt;li&gt;Generation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will take a simple use case and see LLMs in action. I am taking a blog post I wrote earlier using it for context, and asking a relevant question for the same. Here's a quick diagram explaining the flow.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3ley5rujtiyj0gkyto0.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3ley5rujtiyj0gkyto0.png" alt="RAG App Flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will use LangChain, HuggingFace(Free API request), and LangSmith(for tracing) to achieve this. First, install the required modules.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;!&lt;/span&gt; pip &lt;span class="nb"&gt;install &lt;/span&gt;langchain_community tiktoken langchainhub chromadb langchain langchain_huggingface


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

&lt;/div&gt;

&lt;p&gt;Create a .env file and add the API keys used in this article.&lt;/p&gt;

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

LANGCHAIN_API_KEY='&amp;lt;langchain-key&amp;gt;'
HF_TOKEN='&amp;lt;hf-key&amp;gt;'


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

&lt;/div&gt;

&lt;p&gt;Initialize a few environment variables, which can be set in the .env file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;LANGCHAIN_TRACING_V2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;LANGCHAIN_ENDPOINT&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://api.smith.langchain.com&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Indexing
&lt;/h3&gt;

&lt;p&gt;The first step in RAG is indexing. We basically need to convert the document into a vector representation called embeddings. The reason behind this is that it's easier to find relevance between similar pieces of text when they are in vector format. There are a few steps involved in Indexing the data.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loading&lt;/li&gt;
&lt;li&gt;Splitting&lt;/li&gt;
&lt;li&gt;Embedding&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Loading
&lt;/h4&gt;

&lt;p&gt;LangChain offers a multitude of ways to load data. We will use a simple &lt;em&gt;WebBaseLoader&lt;/em&gt; to load the article into memory.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# Load blog
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;bs4&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_community.document_loaders&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;WebBaseLoader&lt;/span&gt;
&lt;span class="n"&gt;loader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;WebBaseLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;web_paths&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://vignesh.page/posts/kafka/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,),&lt;/span&gt;
    &lt;span class="n"&gt;bs_kwargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="c1"&gt;# parse_only=bs4.SoupStrainer(
&lt;/span&gt;        &lt;span class="c1"&gt;#     class_=("main-content")
&lt;/span&gt;        &lt;span class="c1"&gt;# )
&lt;/span&gt;    &lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;blog_docs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;blog_docs&lt;/span&gt;


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

&lt;/div&gt;
&lt;h4&gt;
  
  
  Splitting
&lt;/h4&gt;

&lt;p&gt;We need to split the data for Embedding because there is a size limitation to how much data can be embedded in one go. We are using the &lt;em&gt;RecursiveCharacterTextSplitter&lt;/em&gt; for our example.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# Split
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.text_splitter&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;RecursiveCharacterTextSplitter&lt;/span&gt;
&lt;span class="n"&gt;text_splitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;RecursiveCharacterTextSplitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_tiktoken_encoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;chunk_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;chunk_overlap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Make splits
&lt;/span&gt;&lt;span class="n"&gt;splits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text_splitter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split_documents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blog_docs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;
&lt;h4&gt;
  
  
  Indexing
&lt;/h4&gt;

&lt;p&gt;The last step is to index the data into a Vector Store. For this example, we will use an in-memory instance of &lt;em&gt;Chroma&lt;/em&gt;. We are using the &lt;em&gt;HuggingFaceEmbeddings&lt;/em&gt; to embed the data. We set the vector store as a retriever with k = 1 (we are using k nearest neighbors algo to find relevant documents, so 1 returns only relevant documents).&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# Index
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_huggingface&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HuggingFaceEmbeddings&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_community&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;stores&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Chroma&lt;/span&gt;
&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Chroma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_documents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;documents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;splits&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                    &lt;span class="n"&gt;embedding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;HuggingFaceEmbeddings&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="n"&gt;retriever&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vectorstore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_retriever&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search_kwargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;k&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Retrieval
&lt;/h3&gt;

&lt;p&gt;The next step is quite straightforward, we just need to retrieve relevant document chunks whenever we get a query. My query "What is Kafka?" returned 4 documents with relevant data which will be passed as context to the LLM.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;retriever&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_relevant_documents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is Kafka?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;We can see the LangSmith trace letting us know what was retrieved.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4fsa9xonk6mns5smh7ge.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4fsa9xonk6mns5smh7ge.png" alt="Retriever Trace in LangSmith"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Generation
&lt;/h3&gt;

&lt;p&gt;Now, all that is left is to pass the relevant documents to the LLM to get our response to the query. &lt;/p&gt;

&lt;p&gt;We are using the Falcon-7B model from HuggingFace in this example, feel free to swap out with any LLM of your choice.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# LLM
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_huggingface&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HuggingFaceEndpoint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ChatHuggingFace&lt;/span&gt;

&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HuggingFaceEndpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;repo_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tiiuae/falcon-7b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;text-generation&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_new_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;do_sample&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;repetition_penalty&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1.03&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Next, we create a prompt template to pass the query and the context.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.prompts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ChatPromptTemplate&lt;/span&gt;

&lt;span class="c1"&gt;# Prompt
&lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Answer the question based only on the following context:
{context}

Question: {question}
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

&lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ChatPromptTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_template&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;We will use LangChain to chain these together and create a simple chain. Then, we invoke the chain.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# Chain
&lt;/span&gt;&lt;span class="n"&gt;chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;

&lt;span class="c1"&gt;# Run
&lt;/span&gt;&lt;span class="n"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;context&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is Kafka?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;After adding some syntactic sugar the same can be made easier to read. We will also add a default RAG prompt that is available in the LangChain hub.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hub&lt;/span&gt;
&lt;span class="n"&gt;prompt_hub_rag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rlm/rag-prompt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;rag_chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;context&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;retriever&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RunnablePassthrough&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;prompt_hub_rag&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;llm&lt;/span&gt;
    &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;StrOutputParser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;rag_chain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is Kafka?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;We can see the trace of the execution of this chain in LangSmith as well.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftr4sh9r750254p7xnye3.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftr4sh9r750254p7xnye3.png" alt="Sequence flow in LangSmith"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code is available at my &lt;a href="https://github.com/vigneshm243/blogRAG" rel="noopener noreferrer"&gt;github&lt;/a&gt;. Please do give it a go. All the resources used are under the free tier making it easily accessible.&lt;/p&gt;

&lt;p&gt;Thus with a few lines of code, we are able to build a simple RAG application. We have a multitude of tools inside LangChain that can be a separate article.&lt;/p&gt;




&lt;p&gt;Originally posted on &lt;a href="https://vignesh.page/posts/rag_using_langchain_langsmith/" rel="noopener noreferrer"&gt;vignesh.page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you found the article useful please share it. Hope this helps in starting you Gen AI application development journey. &lt;/p&gt;

</description>
      <category>rag</category>
      <category>ai</category>
    </item>
    <item>
      <title>tmux 101</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Wed, 17 Jul 2024 15:00:45 +0000</pubDate>
      <link>https://dev.to/vigneshm243/tmux-101-50lm</link>
      <guid>https://dev.to/vigneshm243/tmux-101-50lm</guid>
      <description>&lt;p&gt;tmux is a terminal multiplexer. We can have multiple terminals inside a single terminal, which is especially useful when we ssh into a remote machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8uu8tb9kfgevoylnqfqp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8uu8tb9kfgevoylnqfqp.png" alt="Image description" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;tmux has 3 levels of hierarchy,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sessions - To have completely different work environments for different concerns&lt;/li&gt;
&lt;li&gt;Windows - To have multiple tabs for what that window represents, basically one for monitoring logs and performance and another to execute the commands required.&lt;/li&gt;
&lt;li&gt;Panes - To have different panes inside a window, like one running top, and another running a couple of tail commands on log files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To start using tmux, we just need to run the command &lt;em&gt;tmux&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;One key concept in tmux is the concept of prefix key. After pressing the prefix key, tmux enters command mode, similar to vim's insert, command, and view modes. The &lt;strong&gt;prefix key&lt;/strong&gt; for tmux by default is &lt;strong&gt;Ctrl + B&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic commands
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Start a new session
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Detach from a session
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ctrl+B d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Attach to the last accessed session
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux attach 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Exit and quit tmux
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Ctrl+B &amp;amp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Session management
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a named session
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux new &lt;span class="nt"&gt;-s&lt;/span&gt; &amp;lt;session_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Attach to a session
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux attach &lt;span class="nt"&gt;-t&lt;/span&gt; &amp;lt;session_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux a &lt;span class="nt"&gt;-t&lt;/span&gt; &amp;lt;session_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Switch sessions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux switch &lt;span class="nt"&gt;-t&lt;/span&gt; &amp;lt;session_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;List sessions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux &lt;span class="nb"&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Kill sessions
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tmux kill-session &lt;span class="nt"&gt;-t&lt;/span&gt; &amp;lt;session_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Window management
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Key&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B C&lt;/td&gt;
&lt;td&gt;Create new window&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B N&lt;/td&gt;
&lt;td&gt;Move to next window&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B P&lt;/td&gt;
&lt;td&gt;Move to previous window&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B L&lt;/td&gt;
&lt;td&gt;Move to last window&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B 0-9&lt;/td&gt;
&lt;td&gt;Move to window by index number&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Pane management
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Key&lt;/th&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B %&lt;/td&gt;
&lt;td&gt;Vertical split (panes side by side)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B "&lt;/td&gt;
&lt;td&gt;Horizontal split (one pane below the other)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B O&lt;/td&gt;
&lt;td&gt;Move to other pane&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B !&lt;/td&gt;
&lt;td&gt;Remove all panes but the current one from the window&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B Q&lt;/td&gt;
&lt;td&gt;Display window index numbers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B Ctrl-Up/Down&lt;/td&gt;
&lt;td&gt;Resize current pane (due north/south)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B Ctrl-Left/Right&lt;/td&gt;
&lt;td&gt;Resize current pane (due west/east)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ctrl+B W&lt;/td&gt;
&lt;td&gt;Open a panel to navigate across windows in multiple sessions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Command mode
&lt;/h3&gt;

&lt;p&gt;Some other key points are we can enter command mode by &lt;/p&gt;

&lt;p&gt;Ctrl + B :&lt;/p&gt;

&lt;p&gt;We can allow access to a mouse by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; mouse
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Tmux config
&lt;/h3&gt;

&lt;p&gt;We have a tmux config file that is a dot config file. We can create it using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vi &lt;span class="nv"&gt;$HOME&lt;/span&gt;/.tmux.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can add the above command to allow mouse usage into the tmux conf file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tmux References
&lt;/h3&gt;

&lt;p&gt;There is a lot more to tmux than this, which we can check out from the  &lt;a href="http://man.openbsd.org/OpenBSD-current/man1/tmux.1" rel="noopener noreferrer"&gt;tmux manual&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally posted on &lt;a href="https://vignesh.page/posts/tmux/" rel="noopener noreferrer"&gt;vignesh.page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hope tmux improves your daily workflow as it did mine. Comment out any cool features or tmux plugins that you find useful.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>archlinux</category>
    </item>
    <item>
      <title>Setup a k3s Cluster at home quickly</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Sun, 14 Jul 2024 13:54:57 +0000</pubDate>
      <link>https://dev.to/vigneshm243/setup-a-k3s-cluster-at-home-quickly-18il</link>
      <guid>https://dev.to/vigneshm243/setup-a-k3s-cluster-at-home-quickly-18il</guid>
      <description>&lt;p&gt;I was setting up a new Fedora Server VM over the weekend and wanted to use it as a kubernetes cluster to run my containerized workloads. I usually install docker and docker-compose and get on with my work. But I wanted to set up a k8s cluster for fun. When looking up online I found a quick and easy way to set it up. &lt;/p&gt;

&lt;h3&gt;
  
  
  Why k3s over k8s?
&lt;/h3&gt;

&lt;p&gt;Rather than going with a full-blown k8s cluster with multiple VMs, I went with a single node k3s setup. k3s is a lightweight k8s, it has a smaller footprint and is great for a home lab.&lt;/p&gt;

&lt;p&gt;I would encourage you to check out their docs for more info based on your requirements, but the gist is as follows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;p&gt;The minimum hardware requirement is &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Spec&lt;/th&gt;
&lt;th&gt;Min&lt;/th&gt;
&lt;th&gt;Recommended&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU&lt;/td&gt;
&lt;td&gt;1 core&lt;/td&gt;
&lt;td&gt;2 core&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RAM&lt;/td&gt;
&lt;td&gt;512 MB&lt;/td&gt;
&lt;td&gt;1 GB  &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I am running this on a Fedora Server VM with 4 cores and 8 GB of RAM, so well above the recommended settings. &lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;The command to install is,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-sfL&lt;/span&gt; https://get.k3s.io | sh -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will ask for the sudo password. Have a look at the script before running it at &lt;a href="https://get.k3s.io" rel="noopener noreferrer"&gt;Installation Script&lt;/a&gt;. Also, they have a lot of options to customize the components installed. Check out the full list at &lt;a href="https://docs.k3s.io/installation/configuration" rel="noopener noreferrer"&gt;Config Options&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verify the install
&lt;/h3&gt;

&lt;p&gt;If everything goes well, you should be able to check the service through systemctl.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;systemctl status k3s
● k3s.service - Lightweight Kubernetes
     Loaded: loaded &lt;span class="o"&gt;(&lt;/span&gt;/etc/systemd/system/k3s.service&lt;span class="p"&gt;;&lt;/span&gt; enabled&lt;span class="p"&gt;;&lt;/span&gt; preset: disabled&lt;span class="o"&gt;)&lt;/span&gt;
    Drop-In: /usr/lib/systemd/system/service.d
             └─10-timeout-abort.conf
     Active: active &lt;span class="o"&gt;(&lt;/span&gt;running&lt;span class="o"&gt;)&lt;/span&gt; since Sun 2024-07-14 18:47:30 IST&lt;span class="p"&gt;;&lt;/span&gt; 21min ago
       Docs: https://k3s.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Check the default k3s objects
&lt;/h3&gt;

&lt;p&gt;Running the following command gives you all the k3s objects deployed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;kubectl get all &lt;span class="nt"&gt;-n&lt;/span&gt; kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
pod/coredns-6799fbcd5-svqqf                  1/1     Running   0          26m
pod/local-path-provisioner-6f5d79df6-d8h5g   1/1     Running   0          26m
pod/metrics-server-54fd9b65b-5xxq4           0/1     Running   0          26m

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT&lt;span class="o"&gt;(&lt;/span&gt;S&lt;span class="o"&gt;)&lt;/span&gt;                  AGE
service/kube-dns         ClusterIP   10.43.0.10 &amp;lt;none&amp;gt;        53/UDP,53/TCP,9153/TCP   26m
service/metrics-server   ClusterIP   10.43.198.179 &amp;lt;none&amp;gt;        443/TCP                  26m

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/coredns                  1/1     1            1           26m
deployment.apps/local-path-provisioner   1/1     1            1           26m
deployment.apps/metrics-server           0/1     1            0           26m

NAME                                               DESIRED   CURRENT   READY   AGE
replicaset.apps/coredns-6799fbcd5                  1         1         1       26m
replicaset.apps/local-path-provisioner-6f5d79df6   1         1         1       26m
replicaset.apps/metrics-server-54fd9b65b           1         1         0       26m

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring k3s
&lt;/h3&gt;

&lt;p&gt;Create and edit the following file to configure the k3s setup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;vi /etc/rancher/k3s/config.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check out all the available config at &lt;a href="https://docs.k3s.io/installation/configuration#configuration-file" rel="noopener noreferrer"&gt;k3s Config&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Post, the creation/updation of the config file, restart using systemctl.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart k3s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stopping k3s
&lt;/h3&gt;

&lt;p&gt;Just use systemctl to stop the service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl stop k3s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Unintalling k3s
&lt;/h3&gt;

&lt;p&gt;Uninstalling is also a breeze with k3s, as it generates an uninstall script as soon as you install k3s at &lt;em&gt;/usr/local/bin/k3s-uninstall.sh&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Just run the script to uninstall.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/usr/local/bin/k3s-uninstall.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Post uninstall and check the status with systemctl.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;systemctl status k3s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Originally published at vignesh.page.&lt;br&gt;
Please comment any workloads you have for a k3s cluster at home.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>kubernetes</category>
    </item>
    <item>
      <title>Improving throughput and latency using Java Virtual Threads in Spring</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Sun, 30 Jun 2024 17:01:29 +0000</pubDate>
      <link>https://dev.to/vigneshm243/improving-throughput-and-latency-using-java-virtual-threads-in-spring-16mp</link>
      <guid>https://dev.to/vigneshm243/improving-throughput-and-latency-using-java-virtual-threads-in-spring-16mp</guid>
      <description>&lt;p&gt;One of the major changes that Java 21 brought about is Virtual threads. There is so much hype around it, but let's see a real-world example with some metrics. Spring introduced the ability to create GraalVM native images that use Spring Boot and Java 21's virtual threads(Project Loom).&lt;/p&gt;

&lt;p&gt;We will look at virtual threads in detail and the many features that come with them in another article. We will focus on a simple project where we can enable the features of Java Virtual Threads and see the difference in performance gains.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why virtual threads are faster than normal threads?
&lt;/h2&gt;

&lt;p&gt;Normal threads in Java are tied to OS threads. So, there is a limitation to the actual number of threads it can create. Also, time is lost waiting for blocking I/O Calls. Normal Threads are called &lt;strong&gt;Platform Threads&lt;/strong&gt;. They are an expensive resource that needs to be managed well.&lt;/p&gt;

&lt;p&gt;Now, when it comes to virtual threads, they are lightweight constructs. Multiple virtual threads can be tied to a single platform thread which in turn gets tied to an OS thread.&lt;/p&gt;

&lt;p&gt;The simple idea behind Virtual Threads&lt;/p&gt;

&lt;h2&gt;
  
  
  The Spring Project used for benchmarking
&lt;/h2&gt;

&lt;p&gt;The project used in this article can be found &lt;a href="https://github.com/vigneshm243/thirukkuralAPI"&gt;here&lt;/a&gt;. It has a simple fetch API that returns the Thirukkural based on the ID sent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fylajwrz96nofdwfwfu61.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fylajwrz96nofdwfwfu61.png" alt="Image description" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, by default the http server in Tomcat can run many threads in parallel and this will not let us test out this feature easily. So let's throttle it to 10 max threads by adding the below property in the &lt;em&gt;application.properties&lt;/em&gt; file. This will allow Tomcat to only use 10 threads at max.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;server.tomcat.threads.max&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will mimic a blocking IO call using &lt;em&gt;sleep&lt;/em&gt;. We will also log the current thread it's using.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sleep&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Running on "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;currentThread&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Benchmarking using hey
&lt;/h2&gt;

&lt;p&gt;We will benchmark this using hey tool.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hey -n 200 -c 30 http://localhost:8080/thirukural/1

Summary:
 Total:        36.1759 secs
 Slowest:      8.0463 secs
 Fastest:      2.0080 secs
 Average:      5.6840 secs
 Requests/sec: 4.9757


Response time histogram:
 2.008 [1]     |
 2.612 [10]    |■■■
 3.216 [0]     |
 3.819 [0]     |
 4.423 [11]    |■■■
 5.027 [0]     |
 5.631 [0]     |
 6.235 [156]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
 6.839 [0]     |
 7.442 [0]     |
 8.046 [2]     |■


Latency distribution:
 10% in 4.0305 secs
 25% in 6.0212 secs
 50% in 6.0263 secs
 75% in 6.0340 secs
 90% in 6.0376 secs
 95% in 6.0387 secs
 99% in 8.0463 secs

Details (average, fastest, slowest):
 DNS+dialup:   0.0007 secs, 2.0080 secs, 8.0463 secs
 DNS-lookup:   0.0005 secs, 0.0000 secs, 0.0041 secs
 req write:    0.0000 secs, 0.0000 secs, 0.0003 secs
 resp wait:    5.6832 secs, 2.0079 secs, 8.0419 secs
 resp read:    0.0001 secs, 0.0000 secs, 0.0005 secs

Status code distribution:
 [200] 180 responses
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few log entries are listed below to see that these requests are using &lt;strong&gt;Platform Threads&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2024-06-30T22:07:29.659+05:30  INFO 2120 --- [thriukural] [nio-8080-exec-7] p.v.thriukural.web.ThirukuralController  : Running on Thread[#45,http-nio-8080-exec-7,5,main]
2024-06-30T22:07:29.659+05:30  INFO 2120 --- [thriukural] [io-8080-exec-10] p.v.thriukural.web.ThirukuralController  : Running on Thread[#48,http-nio-8080-exec-10,5,main]
2024-06-30T22:07:29.659+05:30  INFO 2120 --- [thriukural] [nio-8080-exec-2] p.v.thriukural.web.ThirukuralController  : Running on Thread[#40,http-nio-8080-exec-2,5,main]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to enable the application to use virtual threads, we just add the below property in &lt;em&gt;application.properties&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;spring.threads.virtual.enabled&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's run and see the same benchmark results again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hey -n 200 -c 30 http://localhost:8080/thirukural/1

Summary:
 Total:        12.2478 secs
 Slowest:      2.1747 secs
 Fastest:      2.0086 secs
 Average:      2.0410 secs
 Requests/sec: 14.6965


Response time histogram:
 2.009 [1]     |
 2.025 [149]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
 2.042 [0]     |
 2.058 [0]     |
 2.075 [0]     |
 2.092 [0]     |
 2.108 [0]     |
 2.125 [0]     |
 2.141 [0]     |
 2.158 [0]     |
 2.175 [30]    |■■■■■■■■


Latency distribution:
 10% in 2.0115 secs
 25% in 2.0129 secs
 50% in 2.0158 secs
 75% in 2.0188 secs
 90% in 2.1724 secs
 95% in 2.1739 secs
 99% in 2.1747 secs

Details (average, fastest, slowest):
 DNS+dialup:   0.0015 secs, 2.0086 secs, 2.1747 secs
 DNS-lookup:   0.0014 secs, 0.0000 secs, 0.0093 secs
 req write:    0.0000 secs, 0.0000 secs, 0.0002 secs
 resp wait:    2.0392 secs, 2.0085 secs, 2.1656 secs
 resp read:    0.0003 secs, 0.0000 secs, 0.0026 secs

Status code distribution:
 [200] 180 responses
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few log entries to see them running in virtual threads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2024-06-30T22:12:38.485+05:30  INFO 17700 --- [thriukural] [mcat-handler-48] p.v.thriukural.web.ThirukuralController  : Running on VirtualThread[#103,tomcat-handler-48]/runnable@ForkJoinPool-1-worker-6
2024-06-30T22:12:38.485+05:30  INFO 17700 --- [thriukural] [mcat-handler-66] p.v.thriukural.web.ThirukuralController  : Running on VirtualThread[#121,tomcat-handler-66]/runnable@ForkJoinPool-1-worker-12
2024-06-30T22:12:38.485+05:30  INFO 17700 --- [thriukural] [mcat-handler-69] p.v.thriukural.web.ThirukuralController  : Running on VirtualThread[#124,tomcat-handler-69]/runnable@ForkJoinPool-1-worker-7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Benchmarking results
&lt;/h2&gt;

&lt;p&gt;As evident from the results, we see an increased throughput and better latency as well once virtual threads are enabled.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parameter    &lt;/th&gt;
&lt;th&gt;Without Virtual Threads&lt;/th&gt;
&lt;th&gt;With Virtual Threads&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Requests/sec&lt;/td&gt;
&lt;td&gt;4.9757                  &lt;/td&gt;
&lt;td&gt;14.6965              &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;99% Latency  &lt;/td&gt;
&lt;td&gt;8.0463 sec              &lt;/td&gt;
&lt;td&gt;2.1747 sec          &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;A simple parameter change has enabled us with a lot of improved performance and throughput. We will further explore in future articles some of the considerations that need to be taken before using virtual threads.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://vignesh.page/posts/java_virtual_threads_in_spring/"&gt;vignesh.page&lt;/a&gt;.&lt;br&gt;
Please let me know if any improvements you have experienced using Java Virtual Threads&lt;/p&gt;

</description>
      <category>spring</category>
      <category>java</category>
      <category>performance</category>
    </item>
    <item>
      <title>React refersher</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Sat, 20 Jan 2024 15:57:20 +0000</pubDate>
      <link>https://dev.to/vigneshm243/react-refersher-3fma</link>
      <guid>https://dev.to/vigneshm243/react-refersher-3fma</guid>
      <description>&lt;h1&gt;
  
  
  React
&lt;/h1&gt;

&lt;p&gt;I recently decided to refresh my React skills, since it has been a very long time since I did something with any frontend technologies. React is one of the most used front-end JS libraries that is used to build user interfaces for web apps and native apps(React Native).&lt;/p&gt;

&lt;h3&gt;
  
  
  How does React work?
&lt;/h3&gt;

&lt;p&gt;React maintains a Virtual DOM, similar to the actual DOM. DOM is a Document Object Model, which is essentially a tree. React works fast by updating the virtual DOM first which is cheaper compared to updating the actual DOM. Once it does that, it effectively reconstructs the actual DOM to push only the changes. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is JSX?
&lt;/h3&gt;

&lt;p&gt;React uses JSX which is nothing but syntactic sugar. Everything written in JSX is converted to JS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;para&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi World&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;gets converted to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing we observe with JSX that is different from HTML is that we can't use &lt;em&gt;class&lt;/em&gt;, instead we use &lt;em&gt;className&lt;/em&gt;. This is because &lt;em&gt;class&lt;/em&gt; is a reserved keyword. There are a few subtleties like this in JSX which we need to be careful about.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are components?
&lt;/h3&gt;

&lt;p&gt;React is a component-based front-end library. This allows us to essentially build our UI as small reusable components. Every React app is a tree of React Components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Components
&lt;/h3&gt;

&lt;p&gt;React has 2 types of components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class-based&lt;/li&gt;
&lt;li&gt;Functional&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Class Based Components
&lt;/h4&gt;

&lt;p&gt;Let's see how class-based components look like first&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hi&lt;/span&gt; &lt;span class="nx"&gt;There&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, we create a separate file for a component. Then we need to import React and Component. We create a class called Test(name of our component). Then we use the render method to define what to display. But this is history, it's legacy code now as functional components have started to take precedence.&lt;/p&gt;

&lt;h4&gt;
  
  
  Functional Components
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hi&lt;/span&gt; &lt;span class="nx"&gt;There&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here too, we import a React library, but this can be skipped in the latest versions. Then we just need to create a function(can be named or arrow). That's it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up a project
&lt;/h3&gt;

&lt;p&gt;We can either&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;setup a project manually by using Webpack and Babel &lt;/li&gt;
&lt;li&gt;using create-react-app command&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will go with create-react-app as it's far easier. But if finer control is required better to go with the first approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is create-react-app?
&lt;/h3&gt;

&lt;p&gt;create-react-app is a tool to generate the starter project. It's officially supported by the React team. To create a project just create a project directory and run the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-react-app ./
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates our React Project. We can start the project by running the below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The project will be hosted on port 3000 on localhost.&lt;/p&gt;

&lt;h3&gt;
  
  
  React Project structure
&lt;/h3&gt;

&lt;p&gt;Let's look at the directories the create-react-app had created to understand what files are required for React to run.&lt;/p&gt;

&lt;p&gt;The first file we need to look at is &lt;em&gt;package.json&lt;/em&gt;. Here we will find a list of dependencies for the project. We will find the react libraries as well here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"@testing-library/jest-dom"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.17.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"@testing-library/react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^13.4.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"@testing-library/user-event"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^13.5.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^18.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"react-dom"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^18.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"react-scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"5.0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"web-vitals"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.1.4"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, in the public directory, we find the &lt;em&gt;index.html&lt;/em&gt;. The main thing we need to look at is the root div. React injects the entire app inside the root tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is done in the &lt;em&gt;index.js&lt;/em&gt; file found under the src directory. Below is a sample of the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt;  &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the ReactDOM is imported with React library. We use ReactDOM only once in the entire app. We use ReactDOM.render with the component we want to inject and the target DOM element. React injects the App component into the root element. This is the basis of how React works.&lt;/p&gt;

&lt;p&gt;The next file is &lt;em&gt;App.js&lt;/em&gt; inside the src directory. It's the place where we are defining what to render. &lt;/p&gt;

&lt;h3&gt;
  
  
  React in action
&lt;/h3&gt;

&lt;p&gt;The main feature of React is the power to write HTML inside Javascript and vice versa. Inside a pair of curly braces, we can put and valid Javascript code.&lt;/p&gt;

&lt;p&gt;For instance, we can start with a clean App.js. Remember App.js is a component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi There&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This renders a simple &lt;strong&gt;Hi There&lt;/strong&gt; on the page. Let's first understand what is happening here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We are importing a css file for styling. &lt;/li&gt;
&lt;li&gt;We are defining a functional component called App using the arrow function.&lt;/li&gt;
&lt;li&gt;We export the App function, so it can be used wherever the file gets imported.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, we can start playing with it. We can define a variable and display the same to the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React renders us &lt;strong&gt;Hi John Doe&lt;/strong&gt;. It evaluates the name variable to get the actual value and shows us the result. &lt;/p&gt;

&lt;p&gt;We can make the same conditional based on a parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;John Doe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isNameEnabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isNameEnanled&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;there&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React renders &lt;strong&gt;Hi there&lt;/strong&gt;. Here we have used a ternary operator to display the name conditonally. Similarly, we can perform calculations, computations, etc.&lt;/p&gt;

&lt;p&gt;Now, let's assume we need to render multiple elements, we usually do that with divs. But a cleaner approach to stop the page from having multiple inner divs is using &lt;em&gt;fragments&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MultipleElements&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;There&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note this is a newer React feature, so legacy apps might not support it. If we don't nest them, we run into the error "Adjacent JSX elements must be wrapped in an enclosing tag". So, be careful with this.&lt;/p&gt;

&lt;p&gt;Moving forward, we will start creating components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating and importing components
&lt;/h3&gt;

&lt;p&gt;One of the selling points of React is that we can create smaller components and embed them into parent components. This way we can reuse the components in multiple places.&lt;/p&gt;

&lt;p&gt;We will look at an example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;First Name: John&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Last Name: Doe&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Age: Unknown&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will render us with text defined in the Person Component. This is how we import one Component into another.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are props?
&lt;/h3&gt;

&lt;p&gt;To pass data to our Components we use &lt;strong&gt;props&lt;/strong&gt;. Props are arguments passed into React Components. We pass them as HTML attributes. We use props to pass data from one component to another (Parent to Child). Data passed in props is read-only and cannot be modified by the received component. &lt;/p&gt;

&lt;p&gt;We will look at the same example above with props.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;First Name: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Last Name: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Age: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'John'&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Doe'&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Jane'&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Doe'&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, in the Child component we get the props argument and use the attributes passed from the parent component.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is state?
&lt;/h3&gt;

&lt;p&gt;State is a JS object used by React to represent the component's state(maybe a counter instance). It is completely managed by the Component it's present in. Whenever the state changes, the component re-renders. The state object is where we store property values that belong to the component. State data can be modified by its component, but is private.&lt;/p&gt;

&lt;p&gt;We will look at a simple counter using state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'App'&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prevCounter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevCounter&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;-&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;prevCounter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevCounter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;+&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use state we need to import the react hook called useState. There are a few hooks that React gives, it's better to know of them. &lt;/p&gt;

&lt;p&gt;useState accepts an initial state and returns 2 values. They are the current state and a function to update the state. We should never update the state directly and always use this function.&lt;/p&gt;

&lt;p&gt;So here we have defined the current state as 0, the current state is &lt;em&gt;counter&lt;/em&gt;, and the &lt;em&gt;setCounter&lt;/em&gt; is the function to update the state. This helps us keep track of the counter and as and when the counter gets updated the component gets re-rendered. If we don't use state and directly do the same, the component doesn't get re-rendered and the value remains the same.&lt;/p&gt;

&lt;p&gt;Another important hook is useEffect. It's used to perform side effects on the components. For instance, if we want to show an alert every time the counter is updated, we can do something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Counter has been updated to &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;useEffect takes in 2 arguments. The function it has to call, and what it is dependent on. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If we don't pass the second argument, it gets triggered on every re-render. The below block will always make the counter to 0, whenever it changes. (Just for example)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If we pass an empty array, it gets triggered only on the first render. The below block sets the counter to 100 on the first render.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If we pass the dependent element, it gets triggered when the value gets updated. This block just alerts the counter whenever the counter gets changed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Counter has been updated to &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are more hooks that React offers. It's a good idea to be aware of them.&lt;/p&gt;

&lt;p&gt;In the next article, I write about React and will look at expanding further on it.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://react.dev/learn"&gt;React Quick Start&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=b9eMGE7QtTk"&gt;Youtube Video by JavaScript Mastery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/react/default.asp"&gt;W3Schools React&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Sample project
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://gitlab.com/vigneshm243/entertainment-tracker-app"&gt;Movie Search&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Kafka, The What, Why and How?</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Fri, 17 Nov 2023 14:16:27 +0000</pubDate>
      <link>https://dev.to/vigneshm243/kafka-the-what-why-and-how-3a75</link>
      <guid>https://dev.to/vigneshm243/kafka-the-what-why-and-how-3a75</guid>
      <description>&lt;h2&gt;
  
  
  What is Kafka?
&lt;/h2&gt;

&lt;p&gt;Created by LinkedIn, and was written in Java and Scala, Apache Kafka is a distributed event streaming platform that can scale massive pipelines of real-time data. So, what is Event Streaming? Event streaming is capturing real-time data from event sources(which can be anything from IOT devices, and mobiles to cloud services, software applications, etc); storing them reliably for retrieval; manipulating them if necessary, and routing them to different destinations if necessary. Kafka can do this at scale efficiently. Kakfa is not an in-memory DB like Redis or Memcached, rather it stores the data in the disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Kakfa?
&lt;/h2&gt;

&lt;p&gt;The main reasons to use Kafka are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High throughput - Capable of handling high velocity and volume.&lt;/li&gt;
&lt;li&gt;High scalability - It can scale to thousands of brokers and scale up or down as required.&lt;/li&gt;
&lt;li&gt;Low latency - Can achieve low latency with a cluster setup&lt;/li&gt;
&lt;li&gt;High availability - Since we can have multiple clusters across servers, and geographies, it is extremely fault tolerant with very minimal risk of data loss&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just a side note, when high throughput is required we can go for Kafka. Though Kafka maintains ACID properties, it's not advised to be used as a DB. If we need to look at data, it's faster in a DB than in Kafka. Ksql is currently available which will allow us to query the message stream, to have continuously updated derived tables. &lt;/p&gt;

&lt;h2&gt;
  
  
  How does Kafka work?
&lt;/h2&gt;

&lt;p&gt;Kafka is composed of multiple components. We will list them down first and understand everyone with a sample use case of a food delivery app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Message
&lt;/h3&gt;

&lt;p&gt;A message is the atomic unit of data in Kafka. It can be a JSON, integer, String, etc. Messages can have a key associated with them, which can be used to determine the destination partition.&lt;/p&gt;

&lt;p&gt;For our example, it can be something like this for order info.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"orderId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Cooking"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"food"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Pizza"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"qty"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Coke"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"qty"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"city"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Chennai"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Topics
&lt;/h3&gt;

&lt;p&gt;Topics are logical partitions of events. We can have separate topics for different types of messages. &lt;/p&gt;

&lt;p&gt;For our example, maybe we can have different topics for order status info, delivery partner location info, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Brokers
&lt;/h3&gt;

&lt;p&gt;Kafka instances that store and replicate events. We will try it out with 1 Kafka broker for our example.&lt;/p&gt;

&lt;h3&gt;
  
  
  Producer
&lt;/h3&gt;

&lt;p&gt;A client app that puts events into Kafka. One more key thing the producer must do is to decide which partition it has to put the data in based on the key.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No key - If no key is specified, a random partition is chosen and tries to balance based on the total number of messages in the partitions&lt;/li&gt;
&lt;li&gt;Key specified - When the key is specified, it uses consistent hashing to decide. So, the same key would go to the same partition as consistent hashing ensures the same hash is generated always.&lt;/li&gt;
&lt;li&gt;Partition specified - We can hardcode the partition.&lt;/li&gt;
&lt;li&gt;Custom - We can write rules as per our requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For our example, we will use location(city) as the key to deciding the position.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consumer
&lt;/h3&gt;

&lt;p&gt;A client app that consumes the events from Kafka. Each time the consumer processes a record it updates the offset as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Partition
&lt;/h3&gt;

&lt;p&gt;Partitions are meant to spread the topics into different buckets. This is similar to sharding in a DB. Instead of vertically scaling in a single partition for a single topic, we can scale horizontally to have more partitions. Note that partitions will not have the same data. Based on the key specified in the message it will choose a partition and write the data.&lt;/p&gt;

&lt;p&gt;For our example, we can have 2 partitions for each topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Replication factor
&lt;/h3&gt;

&lt;p&gt;The replication factor specifies the copies of partitions that should exist. It's specified at the topic level. So if we have 1 topic, 2 partitions, and 2 replication factors, we would end up with 4 partitions. Note that the replication factor should be less than equal to several brokers.&lt;/p&gt;

&lt;p&gt;For our example, we can go with a replication factor of 2.&lt;/p&gt;

&lt;h3&gt;
  
  
  Offset
&lt;/h3&gt;

&lt;p&gt;To keep track of the messages already processed, we have something called an offset. So, once a message in a partition has been consumed by a consumer, it increments the offset. If a consumer goes down, we can resume work from the same offset.&lt;/p&gt;

&lt;h3&gt;
  
  
  Zookeeper
&lt;/h3&gt;

&lt;p&gt;Zookeeper is an extra service to track the Kafka brokers, storing offset for all the partitions, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consumer Group
&lt;/h3&gt;

&lt;p&gt;Consumer groups are created to help attain higher consumption rates if multiple consumer groups are consuming from the same topic.&lt;/p&gt;

&lt;p&gt;One thing we need to be clear is that one consumer can consume from multiple partitions but multiple consumers can consume from a single partition, within a consumer group.&lt;/p&gt;

&lt;p&gt;Also, consumer groups are self-balancing. Let's understand this with an example.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Exact match - 4 partitions, 4 consumers in a consumer group result in 1 consumer per partition.&lt;/li&gt;
&lt;li&gt;Less consumers - 4 partitions, 2 consumers in a consumer group results in 2 partitions per consumer.&lt;/li&gt;
&lt;li&gt;More consumers - 4 partitions, 5 consumers in a consumer group results in 1 consumer per partition and 1 idle consumer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Based on this we might wonder about MQ and Pub-Sub. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MQ - If we want Kafka to act as a MQ, where each event is processed exactly once, we can have the same number of consumers and partitions or a lesser number of consumers than partitions.&lt;/li&gt;
&lt;li&gt;Pub-Sub- We can have multiple consumer groups reading from the same topic/partitions. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our example, we can have a single consumer group.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example program
&lt;/h2&gt;

&lt;p&gt;We can try out the sample code from Github if you are interested. &lt;a href="https://github.com/vigneshm243/kafka_food_delivery"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To run Kafka and Zookeeper locally, we presume docker and docker-compose are installed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose &lt;span class="nt"&gt;-f&lt;/span&gt; docker-compose.yaml up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please refer to this &lt;a href="https://github.com/conduktor/kafka-stack-docker-compose"&gt;Github&lt;/a&gt; project for more alternative setups available.&lt;/p&gt;

&lt;p&gt;The sample project has 3 files&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;admin.js is used to create the topics. We can create as many as required.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node admin.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;producer.js opens up a cli interface where we can give the order ID, status, and location.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node producer.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;consumer.js opens up a consumer and displays the message as and when received. We can mention the group which it should be part of. Since we have 2 partitions, it would be ideal if we create 2 consumers with the same consumer group. This will allow us to see all messages sent with location as Bangalore goes to one partition and the rest all to another partition.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node consumer.js group1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are further advanced topics to explore such as Ksql, Kafka streams, etc which we can explore in a future article.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>kafka</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to solve any dynamic programming question?</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Sun, 12 Nov 2023 09:52:38 +0000</pubDate>
      <link>https://dev.to/vigneshm243/how-to-solve-any-dynamic-programming-question-13ce</link>
      <guid>https://dev.to/vigneshm243/how-to-solve-any-dynamic-programming-question-13ce</guid>
      <description>&lt;p&gt;So, let's start with a quote from Confucius.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Study the past if you would define the future.”&lt;br&gt;
    - Confucius&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The quote let's us know all we need to know about dynamic programming. We need to remember the results computed for previous small problems to solve bigger newer problems. &lt;/p&gt;

&lt;p&gt;There are 2 approaches to dynamic programing usually&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tabulation - Bottom Up&lt;/li&gt;
&lt;li&gt;Memoization - Top Down&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can take a common problem "Fibonacci Series" to understand the approaches.&lt;/p&gt;

&lt;p&gt;The first thing we can try to do is solve it by recursion. So, the recursion solution for Fibonacci is as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we consider the above solution and compute the recursion tree we get the following tree for n = 5.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;fib(5) =&amp;gt; 5

&lt;ol&gt;
&lt;li&gt;fib(4) =&amp;gt; 3

&lt;ol&gt;
&lt;li&gt;fib(3)  =&amp;gt; 2

&lt;ol&gt;
&lt;li&gt;fib(2) =&amp;gt; 1

&lt;ol&gt;
&lt;li&gt;fib(1) =&amp;gt; 1&lt;/li&gt;
&lt;li&gt;fib(0) =&amp;gt; 0&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;fib(1) =&amp;gt; 1&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;fib(2) =&amp;gt; 1

&lt;ol&gt;
&lt;li&gt;fib(1) =&amp;gt; 1&lt;/li&gt;
&lt;li&gt;fib(0) =&amp;gt; 0&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;fib(3) =&amp;gt; 2

&lt;ol&gt;
&lt;li&gt;fib(2) =&amp;gt; 1

&lt;ol&gt;
&lt;li&gt;fib(1) =&amp;gt; 1&lt;/li&gt;
&lt;li&gt;fib(0) =&amp;gt; 0&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;fib(1) =&amp;gt; 1&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Memoization
&lt;/h2&gt;

&lt;p&gt;As we can see above, we have overlapping subproblems, so we don't need to solve these problems again and again. We can store these solutions, this is &lt;strong&gt;memoization&lt;/strong&gt;. We have a single parameter, so we would need a 1D array.&lt;/p&gt;

&lt;p&gt;So how do we apply memoization? We do it with 3 steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare the DP array.&lt;/li&gt;
&lt;li&gt;Start adding the solution for computed sub problems into DP array.&lt;/li&gt;
&lt;li&gt;Check if the solutions already exists and use it if it exists.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="cm"&gt;/* Adding the DP array*/&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;//Checking if solution exists and using it&lt;/span&gt;
    &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;//Updating the array if it doesn't exist&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the time complexity would be O(n). This is because we don't call the function again for already computed solutions. But, the space complexity would be O(n) + O(n), one for the dp array and another for the extra stack space.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tabulation
&lt;/h2&gt;

&lt;p&gt;So, now let's try tabulation. As already highlighted above this is a bottom-up approach. For the current problem, let's try to rewrite it with tabulation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fill&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
    &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
        &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We calculated the base cases first and started iterating and solving the bigger solutions with previously computed values. &lt;/p&gt;

&lt;p&gt;The time complexity is the same, i.e. O(n). The space complexity comes down here because we are not using the stack trace which is O(n).&lt;/p&gt;

&lt;h2&gt;
  
  
  Space optimization on Tabulation
&lt;/h2&gt;

&lt;p&gt;In the above tabulation, we can see that we don't need to maintain all the previous values. Just the last two solutions would suffice for this problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++){&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//since computing till n &lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, now we have reduced the space complexity to O(1), while the time complexity stays the same.&lt;/p&gt;

&lt;p&gt;Now, we will move on to some more complex problems to apply the above approach in the coming articles.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>dsa</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Using Git 1</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Sat, 12 Aug 2023 16:02:46 +0000</pubDate>
      <link>https://dev.to/vigneshm243/using-git-1-2f5m</link>
      <guid>https://dev.to/vigneshm243/using-git-1-2f5m</guid>
      <description>&lt;h2&gt;
  
  
  Git
&lt;/h2&gt;

&lt;p&gt;Git is a source control program. It can be used to,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Version control (maintain all version of a project)&lt;/li&gt;
&lt;li&gt;Revert to older version if something breaks&lt;/li&gt;
&lt;li&gt;Collaborate with people (allowing multiple people work on a single project)&lt;/li&gt;
&lt;li&gt;Track all the changes done by multiple people&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we just explore all the features we can use locally. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using Git
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create a git repo
&lt;/h3&gt;

&lt;p&gt;The way to create a git repo is to run the git init command in the project folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git will create a folder called .git in the current working directory. &lt;/p&gt;

&lt;h3&gt;
  
  
  Start tracking
&lt;/h3&gt;

&lt;p&gt;From now on, all the changes done in the folder will be tracked. Try adding a file and just check it out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will show unstaged files i.e files not yet committed in red, committed files in green.&lt;/p&gt;

&lt;h3&gt;
  
  
  Commiting the changes
&lt;/h3&gt;

&lt;p&gt;If we commit the changes, it will create a snapshot of the version. But before that the changes need to be added to staging.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To add the files to staging using the above command. Now git status will return the files in green. Now the changes can be committed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Changes done"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will now create a snapshot with the commit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tracking changes
&lt;/h3&gt;

&lt;p&gt;To checkout the history of the changes, we can use git log.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reverting a commit
&lt;/h3&gt;

&lt;p&gt;To revert to an older version, we choose the version needed from git log and use git reset to go back to previous commit. This will revert the changes and unstage the changes done as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git reset &lt;span class="nv"&gt;$prev_commit_hash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A git status should show us the changes done after the reverted hash in red.&lt;/p&gt;

&lt;p&gt;If we use the flay --hard, it removes the files as well from the working directory, so use with caution if required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git reset &lt;span class="nt"&gt;--hard&lt;/span&gt; &lt;span class="nv"&gt;$prev_commit_hash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stash changes
&lt;/h3&gt;

&lt;p&gt;Suppose we need to temporarily move the changes and get it to previous committed version, we can use git stash.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To bring back from stash, we can bring it back using git pop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash pop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can stash and remove the changes as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash clear 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove just the top of stash changes we can use git stash drop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash drop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Getting the changes back after a git reset --hard
&lt;/h3&gt;

&lt;p&gt;There is another set of logs in git called reference logs. The command to access reference logs is reflog&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git reflog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use this to checkout a branch from here to an older before a hard reset was done to get the changes.&lt;/p&gt;

&lt;p&gt;In part 2, we will explore the feature we can use to collaborate.&lt;/p&gt;

</description>
      <category>git</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>VS Code Snippets</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Sun, 30 Jul 2023 11:11:48 +0000</pubDate>
      <link>https://dev.to/vigneshm243/vs-code-snippets-hb9</link>
      <guid>https://dev.to/vigneshm243/vs-code-snippets-hb9</guid>
      <description>&lt;p&gt;VS Code snippets is an easy way to create a template for recurring code blocks or front matter for certain files. We will look at a small example here for a Zettelkasten header.&lt;/p&gt;

&lt;p&gt;Here, we have a snippet that will be triggered by "---" followed by Ctrl + Space. Intellisense will also pick it up and suggest the same as well. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name of the snippet: We have named it Test&lt;/li&gt;
&lt;li&gt;Prefix: The trigger that would invoke the snippet. "---"&lt;/li&gt;
&lt;li&gt;Body: The conent to be added.&lt;/li&gt;
&lt;li&gt;Description: A small description of what the snippet is for.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"FrontMatter"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"prefix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"---"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"---"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"title: $1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"date: &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"type: ${2|Daily,Literature,Permanent,Index|}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"description: $3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"tags: [$4]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"---"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"FrontMatter"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the body, we have a lot of control. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parameters defined by $1, $2, etc, that can be cycled through quickly using tab.&lt;/li&gt;
&lt;li&gt;Date or time that can be auto populated.&lt;/li&gt;
&lt;li&gt;Set a list of values that can be quickly selected and restricted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A small gif showing the snippet in action.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--agshYD1D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz1jwd525y2v9r5gbqyh.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--agshYD1D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz1jwd525y2v9r5gbqyh.gif" alt="Image description" width="800" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>productivity</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Reliable, Scalable and Maintainable Applications 1</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Mon, 16 Jan 2023 13:16:00 +0000</pubDate>
      <link>https://dev.to/vigneshm243/reliable-scalable-and-maintainable-applications-1-3pho</link>
      <guid>https://dev.to/vigneshm243/reliable-scalable-and-maintainable-applications-1-3pho</guid>
      <description>&lt;p&gt;As I started to go through the book &lt;a href="https://dataintensive.net/" rel="noopener noreferrer"&gt;Designing Data-Intensive Applications&lt;/a&gt;, I thought I would consolidate my notes to provide a quick reference for my future self. In this article, I will consolidate my notes for the first chapter &lt;em&gt;Reliable, Scalable, and Maintainable Applications&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;An awesome quote at the start of the chapter is &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Internet was done so well that most people think of it as a natural resource like the Pacific Ocean, rather than something that was man-made. When was the last time a technology with a scale like that was so error-free? - Alan Kay, in an interview with Dr. Dobb’s Journal (2012)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The quote emphasizes the beauty of building Reliable, Scalable and Maintainable applications and what we should strive for as architects and developers. &lt;/p&gt;

&lt;p&gt;Broadly applications can be classified into 2 types,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compute intensive - Requiring a lot of computing resources&lt;/li&gt;
&lt;li&gt;Data-intensive - Huge amount of data, complex data, and pace at which these data change is high&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of today's applications are data-intensive. Typical Data Intensive application has the following components. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store data (DBs)&lt;/li&gt;
&lt;li&gt;Remember the results of expensive operations(Caches)&lt;/li&gt;
&lt;li&gt;Allow users to search (Search Indexes)&lt;/li&gt;
&lt;li&gt;Send messages to handle asynchronously (Stream Processing)&lt;/li&gt;
&lt;li&gt;Process huge chunks of data at once (Batch Processing)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Major concerns when designing a software system are&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reliablilty&lt;/strong&gt; - System should work correctly even in face of adversity(hardware/software faults, human error, etc)&lt;br&gt;
&lt;strong&gt;Scalability&lt;/strong&gt; - As system grows(in volume, traffic, etc), there should be ways to deal with that growth&lt;br&gt;
&lt;strong&gt;Maintainability&lt;/strong&gt; - Should be easy to maintain the system(make changes to fix bugs, handle more usecases, etc)&lt;/p&gt;

&lt;h2&gt;
  
  
  Reliability
&lt;/h2&gt;

&lt;p&gt;For software, typical reliability expectations are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performs the function user expects&lt;/li&gt;
&lt;li&gt;Tolerate user mistakes&lt;/li&gt;
&lt;li&gt;Performance is good enough for the use cases, under expected load and data volume&lt;/li&gt;
&lt;li&gt;Prevent unauthorized access and abuse&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the expectation is for the system to work correctly when things go wrong(faults). So, we will be building fault-tolerant (resilient) systems. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fault vs Failure&lt;/strong&gt; - A fault is one/more components deviating from expected specifications, whereas a failure is when the system is not able to service the user request. &lt;/p&gt;

&lt;p&gt;Counterintuitively we can increase faults, to know how well the system can handle the faults(Eg: Netflix Chaos Monkey). Also, in some cases, we need to stop the faults, like in cases of data breaches where there is no way to fix the fault or tolerate them if the data is breached.&lt;/p&gt;

&lt;p&gt;Types of faults are,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hardware faults&lt;/li&gt;
&lt;li&gt;Software faults&lt;/li&gt;
&lt;li&gt;Human errors&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Hardware Faults
&lt;/h3&gt;

&lt;p&gt;If any hardware components were prone to failure, the natural thing to do would be redundancy. In this approach, we may set up the drives in RAID config, with dual power supplies and hot-swappable CPUs. Though not completely foolproof these approaches can keep machines running uninterrupted for years.&lt;/p&gt;

&lt;p&gt;Nowadays, with AWS EC2-like VMs running most of the newer applications, where these systems favor elasticity to single VM reliability, applications need to be able to handle entire machine losses as well.&lt;/p&gt;

&lt;p&gt;We go to multi-machine setups which have few operational advantages as well over the traditional systems like rolling upgrades, zero downtime, quick scaling to load, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Software errors
&lt;/h3&gt;

&lt;p&gt;These are errors due to the bugs in the software. There is no quick solution to these errors. Things like thorough testing, process isolation, allowing processes to crash and restart, and monitoring help here. If a system is to provide some guarantee, it can self-check itself constantly and raise an alert in case of discrepancy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Human errors
&lt;/h3&gt;

&lt;p&gt;Humans are the most unreliable part of any software system. Some ways to keep these errors in check are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limit chances to introduce these human errors&lt;/li&gt;
&lt;li&gt;Decouple places where people make most mistakes (Ex: Test env and prod env)&lt;/li&gt;
&lt;li&gt;Testing thoroughly&lt;/li&gt;
&lt;li&gt;Allow quick recovery (Ex: Rollback config if any failures)&lt;/li&gt;
&lt;li&gt;Detailed monitoring &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even in noncritical applications, the system needs to be reliable. There are cases where cost vs reliability sacrifice arises, but we should be careful of the choices.&lt;/p&gt;

&lt;p&gt;Will continue in the next article as I don't want the article to be too long. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>learning</category>
      <category>devops</category>
      <category>career</category>
    </item>
    <item>
      <title>Go Scrapper API</title>
      <dc:creator>Vignesh Muthukumaran</dc:creator>
      <pubDate>Sun, 03 Jul 2022 09:46:26 +0000</pubDate>
      <link>https://dev.to/vigneshm243/go-scrapper-api-2ah0</link>
      <guid>https://dev.to/vigneshm243/go-scrapper-api-2ah0</guid>
      <description>&lt;p&gt;As I was exploring further about Go, I came across APIs in Go, which was very easy to build. So instead of going the traditional way and building a CRUD API, I decided to write a simple scrapper API. The API scraps Goodreads quotes page and returns a JSON of quotes with their authors.&lt;/p&gt;

&lt;p&gt;Here I am using 2 modules,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="//github.com/gin-gonic/gin"&gt;Gin&lt;/a&gt; - For quickly building the API&lt;/li&gt;
&lt;li&gt;
&lt;a href="//github.com/gocolly/colly"&gt;Colly&lt;/a&gt; - For web scrapping.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have built it and deployed it on Heroku. &lt;/p&gt;

&lt;p&gt;Commands I ran from start to finish&lt;/p&gt;

&lt;h3&gt;
  
  
  To initiate the current folder/project as a module
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go mod init vigneshm.me/go-quotes-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a file called go.mod. This has the name of the project and the version of Go it uses.&lt;/p&gt;

&lt;h3&gt;
  
  
  To import modules
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go get github.com/gin-gonic/gin
go get github.com/gocolly/colly
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This imports the modules and creates a go.sum file to add checksums for the modules it downloads. Also, the go.mod page gets filled with all the dependent modules info.&lt;/p&gt;

&lt;p&gt;Next, I filled in the main.go with my code.&lt;/p&gt;

&lt;p&gt;Now, for the part where I deploy the app in Heroku.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Heroku CLI
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; heroku
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Login to Heroku
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;heroku login &lt;span class="nt"&gt;-i&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Initate a Git repo and commit the code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git init
git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"initial commit"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a Procfile for Heroku
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch &lt;/span&gt;Procfile
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"web: bin/go-quotes-api"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Procfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells Heroku to run the command on startup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create Heroku App
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;heroku create go-quote-api
git push heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates the Heroku app and pushes the code.&lt;/p&gt;

&lt;p&gt;Heroku takes this code runs the build and exposes the app at &lt;a href="https://app-name.herokuapp.com/"&gt;https://app-name.herokuapp.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My app is available at &lt;a href="https://go-quote-api.herokuapp.com/quotes/dumbledore"&gt;https://go-quote-api.herokuapp.com/quotes/dumbledore&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last word can be replaced with any search criteria to search for quotes from your favorite character or book. Code can be found at &lt;a href="https://github.com/vigneshm243/go-quotes-api"&gt;https://github.com/vigneshm243/go-quotes-api&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;




&lt;p&gt;Thanks for reading. Leave a ❤️ if you liked it and comment and let me know what you think. &lt;br&gt;
Originally published at &lt;a href="https://vigneshm.me/posts/go_api/"&gt;vigneshm.me&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>api</category>
      <category>webdev</category>
      <category>heroku</category>
    </item>
  </channel>
</rss>
