<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Meuru Muthuthantri on Medium]]></title>
        <description><![CDATA[Stories by Meuru Muthuthantri on Medium]]></description>
        <link>https://medium.com/@meurub?source=rss-74f8e3adf7bb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0u14c4ViqCjkIwvj.jpg</url>
            <title>Stories by Meuru Muthuthantri on Medium</title>
            <link>https://medium.com/@meurub?source=rss-74f8e3adf7bb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 00:21:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@meurub/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Designing for Scale: The Case Against UUIDs as Primary Keys]]></title>
            <link>https://meurub.medium.com/designing-for-scale-the-case-against-uuids-as-primary-keys-db807a6801d1?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/db807a6801d1</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Tue, 06 May 2025 03:39:49 GMT</pubDate>
            <atom:updated>2025-05-06T03:39:49.457Z</atom:updated>
            <content:encoded><![CDATA[<h4>A deep dive into ID strategies, performance trade-offs, and real-world lessons from TailScale</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JVWQ-hEmp5R8tHqi5lWgcw.png" /></figure><p>In a recent <a href="https://www.linkedin.com/posts/trstringer_why-we-stopped-using-uuids-for-primary-keys-activity-7325101383714549760--L5g">LinkedIn post</a>, TR Stringer from TailScale shared a controversial (but increasingly common) decision: <strong>they stopped using UUIDs for primary keys</strong> in favor of auto-incrementing integers. This decision sparked debate across the engineering community, and it’s worth taking a deeper look at the reasoning behind it.</p><p>Let’s unpack why primary key choices matter, the trade-offs between UUIDs and auto-incrementing IDs, and how you might decide what’s best for your system.</p><h3><strong>Why Do We Use IDs in Databases?</strong></h3><p>In relational databases, each table needs a <strong>primary key</strong> — a unique identifier for each row. This key helps:</p><ul><li>Ensure data integrity (no duplicate rows)</li><li>Establish relationships between tables via foreign keys</li><li>Index data efficiently for fast lookups</li></ul><p>While you <em>could</em> use natural keys (like an email address or username), it’s generally best practice to use <strong>surrogate keys</strong> — synthetic identifiers not tied to business logic. This allows the schema to evolve without breaking relationships.</p><h3><strong>Common Types of Primary Keys</strong></h3><p><strong>1. Auto-Incrementing Integers (INT, BIGINT)</strong></p><p><strong>✅ Pros:</strong></p><ul><li>Simple and human-readable</li><li>Fast indexing and retrieval</li><li>Naturally ordered (good for B-tree indexes)</li><li>Easy to reference and debug</li></ul><p><strong>❌ Cons:</strong></p><ul><li>Not globally unique across distributed systems</li><li>Susceptible to enumeration attacks (guessing /users/1, /users/2)</li><li>Can lead to “hot spots” in database writes due to sequential inserts</li></ul><p><strong>2. UUIDs (Universally Unique Identifiers)</strong></p><p><strong>✅ Pros:</strong></p><ul><li>Globally unique — great for distributed systems</li><li>Can be generated client-side (no DB round-trip needed)</li><li>Difficult to guess (better for privacy/security)</li></ul><p><strong>❌ Cons:</strong></p><ul><li>Larger than integers (16 bytes vs 4/8 bytes)</li><li>Slower indexing and comparisons</li><li>Poor cache locality in B-trees (hurts write performance)</li><li>Not human-friendly</li></ul><h3><strong>What TailScale Learned</strong></h3><p>TailScale originally used UUIDs for primary keys, but over time encountered <strong>performance and usability issues</strong>:</p><blockquote><em>“Our MySQL database was getting bloated… queries were slowing down, and we had very poor cache locality. Switching to BIGINT auto-increments brought immediate improvements.” — TR Stringer</em></blockquote><p>The key insight: <strong>the overhead of UUIDs isn’t just theoretical</strong>. In high-scale systems with heavy write operations and frequent lookups, UUIDs introduce real costs — both in terms of storage and query speed.</p><h3><strong>What If You’re Building Distributed Systems?</strong></h3><p>UUIDs are often favored in microservices and distributed systems for their <strong>guaranteed uniqueness without coordination</strong>. But alternatives like <strong>ULIDs</strong>, <strong>KSUIDs</strong>, and <strong>Snowflake IDs</strong> offer a middle ground:</p><ul><li><a href="https://github.com/ulid/spec"><strong>ULID</strong></a>: Timestamp + randomness. Keeps global uniqueness while maintaining sort order.</li><li><a href="https://blog.x.com/engineering/en_us/a/2010/announcing-snowflake"><strong>Snowflake ID</strong></a>: Twitter’s solution — encodes timestamp, region, and machine info.</li><li><strong>KSUID</strong>: Similar to ULID, with better lexicographic sorting and timestamp retrieval.</li></ul><h3><strong>Choosing the Right Key Strategy</strong></h3><p>There’s no one-size-fits-all. Here’s my suggestion:</p><ul><li>Small-scale web apps, CRUD-heavy — Auto-incrementing INT/BIGINT</li><li>Distributed microservices — UUID or Snowflake</li><li>Requires chronological sorting — ULID or KSUID</li><li>Public API with privacy concerns — UUID or obfuscated slug</li></ul><h3><strong>Our Takeaway</strong></h3><p>Primary key choice seems trivial at the beginning — until your system scales or runs into performance cliffs. UUIDs offer flexibility and safety in distributed architectures, but for many applications, <strong>simplicity and performance win</strong>.</p><p>Before committing to UUIDs, ask yourself:</p><ul><li>Do I need globally unique IDs?</li><li>Will this table experience high write volume or frequent queries?</li><li>Are my IDs exposed to end users or clients?</li><li>Is performance critical for joins and indexes?</li></ul><p>Your schema is a foundation. Choose wisely — because it’s hard to change later.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db807a6801d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Learnings: Beware of Stale States]]></title>
            <link>https://meurub.medium.com/react-learnings-beware-of-stale-states-1537a10fbef2?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/1537a10fbef2</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Thu, 30 May 2024 04:25:24 GMT</pubDate>
            <atom:updated>2024-05-30T04:25:24.452Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UMMjBF0Uqbjavhus" /><figcaption>Photo by <a href="https://unsplash.com/@flowforfrank?utm_source=medium&amp;utm_medium=referral">Ferenc Almasi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When working with React, a common scenario involves making API calls or performing time-consuming I/O operations inside a useEffect block. Have you ever encountered a situation where a state update doesn&#39;t seem to take effect after retrieving data? This happened to me recently, and I want to share what I learned.</p><h3>The Problem with Stale States</h3><p>Imagine you have a state called myState with two useEffect blocks:</p><pre>import &quot;./styles.css&quot;;<br>import { useState, useEffect } from &quot;react&quot;;<br><br>export default function App() {<br>  const [myState, setMyState] = useState(0);<br>  useEffect(() =&gt; {<br>   // the time consuming API call<br>    setTimeout(() =&gt; {<br>      console.log(&quot;This is the value after 5 seconds&quot;, myState);<br>      setMyState(myState * 2);<br>    }, 5000);<br>  }, []);<br><br>  useEffect(() =&gt; {<br>    console.log(&quot;myState got updated&quot;, myState);<br>    // regular operation that updates the same state<br>    const timeoutId = setTimeout(() =&gt; {<br>      if (myState &lt; 5) {<br>        setMyState(myState + 1);<br>      }<br>    }, 2000);<br>    return () =&gt; clearTimeout(timeoutId);<br>  }, [myState]);<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;h2&gt;myState value: {myState}&lt;/h2&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>If you run this code, you’ll see the following output:</p><pre>myState got updated 0<br>myState got updated 1<br>myState got updated 2<br>This is the value after 5 seconds 0<br>myState got updated 0<br>myState got updated 1<br>myState got updated 2<br>myState got updated 3<br>myState got updated 4<br>myState got updated 5</pre><p>Notice how the callback function inside the first useEffect block sees myState as 0, which was its value when the useEffect was executed, even though the second useEffect continuously updates myState. This discrepancy can lead to various issues and inconsistencies in your application.</p><h3>Solutions</h3><h4>Solution 1: Using Functional Updates</h4><p>To ensure the state is updated correctly, you can use a functional update inside the setState function:</p><pre>useEffect(() =&gt; {<br>  setTimeout(() =&gt; {<br>    console.log(&quot;This is the value after 5 seconds&quot;, myState);<br>    setMyState((value) =&gt; value * 2);<br>  }, 5000);<br>}, []);</pre><p>This change produces the following output:</p><pre>myState got updated 0<br>myState got updated 1<br>myState got updated 2<br>This is the value after 5 seconds 0<br>myState got updated 4<br>myState got updated 5</pre><h4>Solution 2: Using a Ref</h4><p>Another approach is to use a ref to keep track of the state value. Refs maintain the current value across renders and are visible to callbacks:</p><pre>import &quot;./styles.css&quot;;<br>import { useState, useEffect, useRef } from &quot;react&quot;;<br><br>export default function App() {<br>  const [myState, setMyState] = useState(0);<br>  const myRef = useRef(myState);<br><br>  useEffect(() =&gt; {<br>    setTimeout(() =&gt; {<br>      console.log(&quot;This is the value after 5 seconds&quot;, myRef.current);<br>      setMyState(myRef.current * 2);<br>    }, 5000);<br>  }, []);<br><br>  useEffect(() =&gt; {<br>    myRef.current = myState;<br>    console.log(&quot;myState got updated&quot;, myState);<br>    const timeoutId = setTimeout(() =&gt; {<br>      if (myState &lt; 5) {<br>        setMyState(myState + 1);<br>      }<br>    }, 2000);<br>    return () =&gt; clearTimeout(timeoutId);<br>  }, [myState]);<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;h2&gt;myState value: {myState}&lt;/h2&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>This implementation produces the correct output and ensures the state is up-to-date within the callback functions.</p><h4>Solution 3: Leveraging useReducer</h4><p>Using useReducer can help manage complex state logic and updates, especially for scenarios with multiple state transitions. useReducer provides a more predictable way to manage state updates.</p><pre>import { useReducer, useEffect } from &quot;react&quot;;<br><br>const initialState = { count: 0 };<br><br>function reducer(state, action) {<br>  switch (action.type) {<br>    case &#39;increment&#39;:<br>      return { count: state.count + 1 };<br>    case &#39;double&#39;:<br>      return { count: state.count * 2 };<br>    default:<br>      throw new Error();<br>  }<br>}<br><br>export default function App() {<br>  const [state, dispatch] = useReducer(reducer, initialState);<br><br>  useEffect(() =&gt; {<br>    const timeoutId = setTimeout(() =&gt; {<br>      console.log(&quot;This is the value after 5 seconds&quot;, state.count);<br>      dispatch({ type: &#39;double&#39; });<br>    }, 5000);<br>    return () =&gt; clearTimeout(timeoutId);<br>  }, []);<br><br>  useEffect(() =&gt; {<br>    console.log(&quot;state.count got updated&quot;, state.count);<br>    const timeoutId = setTimeout(() =&gt; {<br>      if (state.count &lt; 5) {<br>        dispatch({ type: &#39;increment&#39; });<br>      }<br>    }, 2000);<br>    return () =&gt; clearTimeout(timeoutId);<br>  }, [state.count]);<br><br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;h2&gt;state.count value: {state.count}&lt;/h2&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>Dealing with stale state values in React can be tricky, but by using functional updates or refs, you can ensure your state remains consistent across asynchronous operations. If you know of any other effective methods to handle this issue, please share them in the comments section. Happy coding, folks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1537a10fbef2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Learnings: Don’t get lost in the State]]></title>
            <link>https://meurub.medium.com/react-learnings-dont-get-lost-in-the-state-bdc5882b0c53?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdc5882b0c53</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Thu, 16 May 2024 03:45:36 GMT</pubDate>
            <atom:updated>2024-05-16T03:45:36.146Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*70rfNGVKYrjZ-6Lc.jpeg" /></figure><p>In my journey with React, I’ve encountered some common pitfalls, especially around managing state. Understanding how to handle the state correctly can save you from a lot of headaches. Here’s a guide to help you avoid these pitfalls and keep your code clean and bug-free.</p><h3>Avoid Direct State Mutations</h3><p>One of the biggest mistakes you can make in React is updating state directly. Let’s look at an example:</p><pre>const [myState, setMyState] = useState([&quot;initial value&quot;]);</pre><p>Suppose you want to add a new value to myState. You might be tempted to do this:</p><pre>myState.push(&quot;newValue&quot;);</pre><p>While this does change the value of myState, it introduces several problems:</p><ol><li>useEffect Won&#39;t Detect Changes: Direct mutations don&#39;t trigger re-renders or updates in useEffect hooks.</li><li>DOM Won’t Reflect Changes: The UI won’t update to reflect the new state.</li><li>Harder Debugging: It becomes challenging to trace what modified the state, making debugging difficult.</li></ol><p>Let’s see this in action:</p><pre>import &quot;./styles.css&quot;;<br>import { useState, useEffect } from &quot;react&quot;;<br><br>export default function App() {<br>  const [myState, setMyState] = useState([&quot;initial value&quot;]);<br><br>  useEffect(() =&gt; {<br>    console.log(&quot;myState got updated&quot;, myState);<br>  }, [myState]);<br><br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;h2&gt;myState value: {myState.join(&quot;, &quot;)}&lt;/h2&gt;<br>      &lt;button<br>        onClick={() =&gt; {<br>          myState.push(&quot;newValue&quot;);<br>          console.log(&quot;myState after edited: &quot;, myState);<br>        }}<br>      &gt;<br>        Press me to unsafely edit myState<br>      &lt;/button&gt;<br>      &lt;br /&gt;<br>      &lt;br /&gt;<br>      &lt;button<br>        onClick={() =&gt; {<br>          setMyState([...myState, &quot;newValue&quot;]);<br>          console.log(&quot;myState after edited: &quot;, myState);<br>        }}<br>      &gt;<br>        Press me to safely edit myState<br>      &lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>}</pre><p>In this example, the first button directly mutates myState, while the second button uses the correct approach.</p><h3>The Correct Approach</h3><p>To update the state properly in React, you should always use the state setter function returned by useState. Here are some guidelines:</p><ol><li>Immutable Updates: Always use immutable patterns to update your state.</li><li>Pure Functions: Ensure functions that process your state are pure functions (no side effects).</li></ol><h3>Example: Correct State Update</h3><p>Instead of mutating the state directly, you should create a new state array:</p><pre>setMyState([...myState, &quot;newValue&quot;]);</pre><p>This approach ensures React knows about the state change, triggering a re-render and updating the UI accordingly.</p><h3>Summary</h3><ol><li>Never mutate state directly: Use the setter function provided by useState.</li><li>Use immutable objects: Ensure your state updates follow immutable patterns.</li><li>Pure functions: When processing complex state, ensure your functions are pure to avoid unexpected changes.</li></ol><p>By following these guidelines, you’ll write more reliable and maintainable React code. Happy coding folks!</p><p><a href="https://codedamn.com/news/reactjs/why-do-you-need-to-use-setstate-instead-of-directly-modifying-the-state">Further Reading</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdc5882b0c53" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Clean Architecture Cheat Sheet]]></title>
            <link>https://meurub.medium.com/my-clean-architecture-cheat-sheet-630d2356f42f?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/630d2356f42f</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Thu, 09 May 2024 04:21:55 GMT</pubDate>
            <atom:updated>2024-05-15T06:10:04.811Z</atom:updated>
            <content:encoded><![CDATA[<p>I am planning to create a live document here to add my findings and understanding of clean architecture.</p><p>Basic components</p><ul><li>Domain (Model)</li><li>UseCases</li><li>Ports</li><li>Adapters/Gateways</li><li>Drivers</li></ul><p><a href="https://orenrose.medium.com/clean-architecture-in-golang-with-go-kit-e5b716a3b881">Clean Architecture with Go</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=630d2356f42f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How React Detects Changes: Dealing with Props and Deep Objects]]></title>
            <link>https://meurub.medium.com/how-react-detects-changes-dealing-with-props-and-deep-objects-da0442d1e9ca?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/da0442d1e9ca</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Tue, 02 Apr 2024 04:10:18 GMT</pubDate>
            <atom:updated>2024-04-02T04:10:18.806Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QUfnS75yI1zk8FOd" /></figure><p>React has become a cornerstone of modern web development, thanks to its efficient ways of rendering and updating components. A critical aspect of its efficiency is how React detects changes in props and decides when to re-render a component. Understanding this process can help developers avoid common pitfalls, especially when dealing with complex or deep objects as props. In this article, we’ll explore how React handles prop changes, the challenges of using deep objects, and best practices to navigate these challenges.</p><h3>How React Component Detects Changes in a Prop</h3><p>React components re-render in response to changes in state or props. For props, React uses a shallow comparison to determine if a re-render is necessary. This means React checks if the prop’s reference has changed, not if the content of the object or array the prop might point to has changed.</p><p>For primitive values (strings, numbers, booleans), this is straightforward; a change in value results in a different reference. However, for objects and arrays, even if their content changes, the reference might remain the same if the object or array is mutated directly. Conversely, if a new object or array is created (even with identical content), its reference will be different, and React will re-render the component receiving it as a prop.</p><h3>Possible Pitfalls of Passing Deep Objects</h3><h4>Unnecessary Re-renders</h4><p>Creating a new object or array on every render, even with identical content, will cause React to perform unnecessary re-renders. This is because the new object/array, despite being “the same” in a deep comparison sense, is a different entity with a new reference.</p><h4>Performance Degradation</h4><p>Unnecessary re-renders can lead to performance degradation, especially in complex applications or components that render large data sets or perform heavy computations.</p><h4>State Management Complexity</h4><p>Managing the state of deep objects can become complex and error-prone, particularly if different parts of the object need to be updated independently but are stored as a single state variable.</p><h3>Best Practices to Avoid Those Pitfalls</h3><h4>Immutable Data Patterns</h4><p>Treat React state as immutable. Instead of mutating objects or arrays directly, use functions like setState, spread operator (...), or utilities from libraries like Immutable.js to update your state. This approach ensures that React&#39;s shallow comparison can accurately detect changes.</p><h4>Memoization</h4><p>Use React.memo for functional components and shouldComponentUpdate lifecycle method for class components to prevent unnecessary re-renders. You can provide a custom comparison function to deeply compare complex props if necessary.</p><h4>Selective Prop Passing</h4><p>Rather than passing the entire deep object to a child component, pass only the properties it needs. This minimizes the risk of unnecessary re-renders due to changes in unrelated parts of the object.</p><h4>Normalization</h4><p>Normalize state shape to ensure that objects and arrays are as flat as possible. This strategy simplifies state management and updates, making it easier to compare changes and reduce unnecessary re-renders.</p><h4>State Splitting</h4><p>Instead of a single, deep state object, consider splitting the state into multiple smaller state variables with useState or into more specific slices with useReducer. This makes state updates more predictable and components more responsive to changes.</p><h4>Use Callbacks and Memoization Hooks</h4><p>For data derived from props or state that require computation, use useMemo to memoize the computed values. Similarly, for callbacks that depend on props or state, use useCallback to ensure they are stable across renders unless their dependencies change.</p><p>Understanding how React detects changes and manages re-renders is crucial for writing efficient React applications. By following best practices for handling deep objects and state, developers can avoid common pitfalls and ensure their applications are both performant and maintainable. Happy coding folks…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da0442d1e9ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing Flexible Filtering Systems in Kotlin]]></title>
            <link>https://meurub.medium.com/designing-flexible-filtering-systems-in-kotlin-1ca39725c136?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ca39725c136</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Tue, 19 Mar 2024 03:33:19 GMT</pubDate>
            <atom:updated>2024-03-19T03:42:18.461Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xB2nryiQSCkyEZukQt_bNA.jpeg" /></figure><p>Creating a flexible and extendable filtering system in Kotlin can greatly enhance the maintainability and scalability of your application. Whether you’re filtering collections of data based on various criteria or need a system that adapts to different filtering needs, Kotlin provides powerful tools and features to help you design such a system. This article explores several approaches to implementing flexible filters, each catering to different requirements regarding the criteria’s type safety, flexibility, and the number of parameters.</p><h3>Understanding the Basics</h3><p>Before diving into specific implementations, let’s clarify the goal: to create an abstract filter class that different implementations can extend, each applying specific logic based on various values and parameters.</p><p>Consider a simple data class for demonstration purposes:</p><pre>data class Person(val name: String, val age: Int)</pre><p>Our objective is to filter a list of Person objects based on different criteria, such as name or age.</p><h3>Approach 1: Varargs and Type Checking</h3><p>The first approach leverages varargs and type checking to allow passing a variable number of arguments of any type to the filter method.</p><pre>abstract class Filter&lt;T&gt; {<br>    abstract fun apply(items: List&lt;T&gt;, vararg criteria: Any?): List&lt;T&gt;<br>}</pre><p>While flexible, this method requires careful type checking and casting inside each filter implementation, potentially leading to runtime errors if not managed correctly.</p><h3>Approach 2: Criteria Object</h3><p>To enhance type safety, the second approach involves defining a criteria object encapsulating all possible filtering criteria.</p><pre>class FilterCriteria(<br>    val name: String? = null,<br>    val ageRange: IntRange? = null,<br>    // Additional fields as required<br>)<br><br>abstract class Filter&lt;T&gt; {<br>    abstract fun apply(items: List&lt;T&gt;, criteria: FilterCriteria): List&lt;T&gt;<br>}</pre><p>This approach is more type-safe but requires all possible criteria to be known and included in the FilterCriteria class, potentially limiting flexibility.</p><h3>Approach 3: Using a Map for Criteria</h3><p>For a dynamic yet flexible solution, the third approach uses a map to pass criteria to the filter.</p><pre>abstract class Filter&lt;T&gt; {<br>    abstract fun apply(items: List&lt;T&gt;, criteria: Map&lt;String, Any&gt;): List&lt;T&gt;<br>}</pre><p>Each filter looks up its relevant criteria in the map, offering great flexibility at the cost of type safety.</p><pre>class NameFilter : Filter&lt;Person&gt;() {<br>    override fun apply(items: List&lt;Person&gt;, criteria: Map&lt;String, Any&gt;): List&lt;Person&gt; {<br>        val nameCriteria = criteria[&quot;name&quot;] as? String ?: return items<br>        return items.filter { it.name.contains(nameCriteria) }<br>    }<br>}</pre><h3>Approach 4: Specific Filters as Functions or Lambdas</h3><p>Leveraging Kotlin’s functional programming capabilities, the fourth approach uses higher-order functions or lambdas directly as filters.</p><pre>fun &lt;T&gt; applyFilter(items: List&lt;T&gt;, filter: (T) -&gt; Boolean): List&lt;T&gt; {<br>    return items.filter(filter)<br>}</pre><p>This approach is maximally flexible and straightforward, ideal for simple or highly specific filtering conditions.</p><pre>val filtered = applyFilter(people) { person -&gt; person.age in 25..30 }</pre><p>You can consider the best approach depending on your requirements.</p><p>Happy coding folks</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ca39725c136" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fluentd: The Swiss Army Knife for Log Management!]]></title>
            <link>https://meurub.medium.com/fluentd-the-swiss-army-knife-for-log-management-94f37945685b?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/94f37945685b</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Mon, 01 Jan 2024 05:02:18 GMT</pubDate>
            <atom:updated>2024-01-01T05:02:18.560Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2OYPUadh5Tq-4YlR" /></figure><p>Hey there, tech enthusiasts! Today, let’s dive into the world of Fluentd — a tool that’s as cool as it is powerful in managing your log data. Whether you’re a developer, a system admin, or just someone fascinated by the intricacies of data management, Fluentd is something you’ll want to get your hands on. So, buckle up, and let’s unravel the magic of Fluentd!</p><h3>What is Fluentd?</h3><p>Imagine you’re a chef in a kitchen with ingredients coming from all over the place. Fluentd is like your sous-chef, masterfully collecting these ingredients (aka log data) from various sources, transforming them into a delicious dish (structured data), and then serving it to the right customer (data storage systems). In technical terms, Fluentd is an open-source data collector, specifically designed for unifying log management across multiple systems.</p><h3>Why is Fluentd So Cool?</h3><ol><li><strong>Unified Logging Layer</strong>: Fluentd offers a unified logging layer, meaning it can collect, process, and deliver logs from disparate sources to multiple destinations. It’s like a universal translator for log data!</li><li><strong>Plug-and-Play</strong>: With over 500 plugins available, Fluentd seamlessly integrates with services like MongoDB, MySQL, and even cloud services like AWS and Azure. It’s all about making your life easier!</li><li><strong>Lightweight and Flexible</strong>: Fluentd is written in Ruby, making it lightweight and easy to install. Plus, its flexibility means it can be tailored to various architectures, from simple to complex.</li><li><strong>Reliability and Efficiency</strong>: Fluentd’s built-in reliability features, like buffering, ensure data integrity during network hiccups. No more data loss nightmares!</li></ol><h3>Basic Principles of Fluentd</h3><ol><li><strong>Data Collection</strong>: Fluentd can collect data from multiple sources, like log files, systems, and even applications.</li><li><strong>Data Enrichment and Processing</strong>: Once the data is collected, Fluentd can enrich, filter, and transform it. Think of it as refining crude oil into high-quality fuel.</li><li><strong>Data Routing</strong>: Fluentd then routes this processed data to the right destinations. This could be databases, monitoring systems, or even analytics platforms.</li></ol><h3>Getting Started with Fluentd</h3><ol><li>Installation: Getting Fluentd up and running is a breeze. With just a few commands, you can have Fluentd installed on your machine.</li><li>Configuration: Fluentd works with a simple configuration file where you define your sources, match patterns, and output destinations. It’s like setting up your playlist for the data stream.</li><li>Scaling: Fluentd scales with your needs. Whether you’re handling gigabytes or terabytes of data, Fluentd stands tall.</li></ol><h3>Real-World Use Cases</h3><ul><li>Centralized Log Management: Companies use Fluentd to aggregate logs from all their services and applications into a single location. It’s like having all your music in one playlist.</li><li>Data Stream Processing: Fluentd can filter and transform data streams in real-time, making it invaluable for real-time analytics.</li><li>Cloud Logging: Fluentd is cloud-friendly, making it an ideal choice for managing logs in cloud-based architectures.</li></ul><h3>Wrapping Up</h3><p>Fluentd is not just a tool; it’s a game-changer in the world of log management. With its flexibility, reliability, and ease of use, it’s no wonder that it’s becoming the go-to choice for businesses big and small. So, why not give Fluentd a spin and see how it transforms your data management strategy? Happy logging, folks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94f37945685b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easy way to back up Neo4j DB hosted in AWS EC2 instance]]></title>
            <link>https://meurub.medium.com/easy-way-to-back-up-neo4j-db-hosted-in-aws-ec2-instance-b4fddef2c8f2?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4fddef2c8f2</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Mon, 26 Jun 2023 10:35:57 GMT</pubDate>
            <atom:updated>2023-06-26T10:35:57.277Z</atom:updated>
            <content:encoded><![CDATA[<h3>Requirement</h3><p>Let’s say you have a Neo4j DB hosted in an AWS ec2 instance which you need to take daily backups. But you want to do it the easiest way.</p><h3>Approach</h3><ul><li>Inside the Neo4j prod DB EC2 Instance, a crone job will be triggered at a given time everyday.</li><li>The crone job will execute a script with root permissions</li><li>The script will<br>- Delete previous db dump<br>- Create a fresh db dump<br>- Copy the db dump to the s3 bucket</li></ul><h3>Implementation</h3><ul><li>Create the S3 bucket and add the required policies to retain the backup for a certain number of days.</li><li>The corresponding EC2 instance should have permission to copy files to AWS S3 bucket. To resolve this issue, you have a few options:</li></ul><ol><li><strong>IAM Role</strong>: The recommended approach is to assign an IAM role to your EC2 instance that has the necessary permissions to access the S3 bucket. You can create an IAM role with the appropriate permissions, and then associate that role with your EC2 instance. When you launch an EC2 instance with an assigned IAM role, the AWS CLI and SDKs automatically retrieve the necessary credentials from the instance metadata.<br>To assign an IAM role to your EC2 instance, follow these steps:<br>- Open the Amazon EC2 console and navigate to the Instances page.<br>- Select your EC2 instance.<br>- Click on “Actions” and choose “Security” &gt; “Modify IAM Role”.<br>- Select the desired IAM role from the list or create a new role (You can give AmazonS3FullAccess).<br>- Once the role is associated with the instance, retry the aws s3 cp command from the EC2 instance, and it should work without the credential error.</li><li>Configure AWS CLI: If you don’t want to use an IAM role, you can configure the AWS CLI on your EC2 instance manually with the appropriate AWS access key and secret access key.<br>- Run the command aws configureon your EC2 instance and provide your AWS Access Key ID and Secret Access Key:<br>- Enter the requested AWS Access Key ID, Secret Access Key, default region, and output format. Ensure that the access key provided has the necessary permissions to access the S3 bucket. After configuring the AWS CLI, retry the aws s3 cp command, and it should now be able to access the S3 bucket.</li><li>Credentials File: If you have an AWS credentials file available on your EC2 instance, you can specify its location using the AWS_SHARED_CREDENTIALS_FILE environment variable. This file should contain the necessary AWS access key and secret access key. Set the environment variable to the credentials file location before running the aws s3 cp command.</li></ol><pre>export AWS_SHARED_CREDENTIALS_FILE=/path/to/credentials/file<br>aws s3 cp db.dump s3://meuru-bucket/</pre><ul><li>Create the script file with the following content inside the EC2 instance</li></ul><pre>#!/bin/bash<br>rm /home/ubuntu/db.dump<br># Check if the script is running as root<br>if [[ $EUID -ne 0 ]]; then<br>  echo &quot;This script must be run as root.&quot;<br>  exit 1<br>fi<br><br># Stop the Neo4j service<br>systemctl stop neo4j.service<br>neo4j-admin dump --to=/home/ubuntu/db.dump --verbose<br>systemctl start neo4j.service<br><br># Get the current date and time in a specific format<br>timestamp=$(date +&quot;%Y-%m-%d_%H:%M&quot;)<br><br># Specify the S3 key with the timestamp<br>s3_key=&quot;${timestamp}.dump&quot;<br>aws s3 cp /home/ubuntu/db.dump s3://neo4j-backup/${s3_key}<br><br>exit 0</pre><ul><li>Save the above script as backupScript.sh</li><li>Add the cron job to execute the script at the given time</li></ul><pre># add execution permissions to the script<br>chmod +x ./backupScript.sh<br><br>sudo su<br>crontab -e<br># select a text editor<br># add the following line<br>0 7 * * * /home/ubuntu/backupScript.sh &gt;&gt; /home/ubuntu/backupScript.log 2&gt;&amp;1<br># save and exit<br># the following would display the scheduled cron job<br>crontab -l</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4fddef2c8f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Receiving values from a AWS Glue Job]]></title>
            <link>https://meurub.medium.com/receiving-values-from-a-aws-glue-job-ebc6b7386008?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/ebc6b7386008</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Fri, 27 Nov 2020 07:37:45 GMT</pubDate>
            <atom:updated>2020-11-27T07:37:45.191Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I wanted to use AWS Glue heavily for some development work at office. AWS Glue is a managed service from AWS which comes handy in processing or computing large amounts of data. My use case was to implement an ETL (Extract — Transform — Load) work flow. Therefore there were multiple glue jobs doing different tasks varied form decompression, data processing, validating, loading, etc. Those glue jobs were managed by a single AWS Step Function in each workflows. Everything was straightforward until there was a requirement to read some values from AWS Glue Job and include them in a SNS Notification. As everybody does, I called Google for my help. I started going through documentations and stackoverflow posts. Then finally it broke my heart when I read <a href="https://stackoverflow.com/questions/58222209/how-to-return-output-from-aws-glue-jobs-back-to-the-calling-step-function-workfl">this</a> post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*SqQuxWa_z-JB_cZ5.png" /></figure><p>It was evident that AWS Glue Jobs are designed not to return values.</p><blockquote><em>“By definition, AWS glue is expected to work on huge amount of data and hence it is expected that output will also be huge amount of data.”</em></blockquote><p>So the expectation was to store the data at the end of the processing. But my use case was to read some small set of value. Following are the approaches I figured out.</p><h3>1. Saving the values to a file using a Lambda function and use it when required</h3><p>You can create a lambda function that would accept the values you want to store and it can either write it to a file and store it in a s3 or can put the value into a DB for future reference. Then that lambda function can be invoked within the Glue Job and pass the required values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/0*c2yLDuZc_P6nHCiA.png" /></figure><h4>Lambda function</h4><pre>import json<br>import boto3</pre><pre>def lambda_handler(event, context):<br> s3_client = boto3.client(‘s3’)<br> bucket_name = ‘myBucket’<br> s3_path = ‘path/to/dir/values.txt’</pre><pre> values_str = json.dumps(event)<br> print(“Received values: %s” % values_str)</pre><pre> try:<br> response = s3_client.get_object(Bucket=bucket_name, Key=s3_path)<br> current_content = response[‘Body’].read().decode(‘utf-8’)<br> print(“Reading content from file at s3:%s key:%s” % (bucket_name, s3_path))<br> content = ‘{}, {}’.format(current_content, values_str)<br> except s3_client.exceptions.NoSuchKey:<br> print(“Created a new file at s3:%s key:%s” % (bucket_name, s3_path))<br> content = values_str</pre><pre> encoded_string = content.encode(“utf-8”)</pre><pre> response = s3_client.put_object(Bucket=bucket_name, Key=s3_path, Body=encoded_string)<br> return {<br> ‘statusCode’: 200,<br> ‘body’: json.dumps(response)<br> }</pre><h4>Lambda invocation</h4><pre>import boto3<br>import json</pre><pre># GLUE JOB CODE</pre><pre>lambda_client = boto3.client(&#39;lambda&#39;)</pre><pre>response = lambda_client.invoke(FunctionName=&#39;myLambdaFnName&#39;, Payload=json.dumps({<br>    &quot;key1&quot;: &#39;val&#39;,<br>    &quot;key2&quot;: a_value_from_glue<br>}))</pre><h3>2. Logging the values with a special pattern and reading it from CloudWatch</h3><p>You can easily log your values. When you log make sure to include a special pattern so that you can easily extract those values. Any other service can fetch those log entries using the CloudWatch API. Refer the following example</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/0*-mQyf7ambFBBQKzI.png" /></figure><p>Logging within the the Glue Job</p><pre># GLUE JOB CODE</pre><pre>print(&quot;[MY_SERVICE] key1: val1&quot;)<br>print(&quot;[MY_SERVICE] key2: %s&quot; % a_value_from_glue)</pre><p>Reading the values</p><pre>import boto3</pre><pre>logs_client = boto3.client(&#39;logs&#39;)<br>repsonse = logs_client.start_query(<br>    logGroupName=&#39;/aws-glue/jobs/output&#39;,<br>    startTime=timestamp,<br>    endTime=int(datetime.now().timestamp()),<br>    queryString=&#39;fields @timestamp, @message | filter @message like /MY_SERVICE/&#39;<br>)</pre><pre>query_id = repsonse[&#39;queryId&#39;]</pre><pre>query_response = None<br>while query_response == None or query_response[&#39;status&#39;] == &#39;Running&#39;:<br>    time.sleep(1)<br>    query_response = logs_client.get_query_results(<br>        queryId=query_id<br>    )</pre><pre>logger.info(&#39;Received results: {}&#39;.format(query_response[&#39;results&#39;]))</pre><pre>results = []<br>for result in query_response[&#39;results&#39;]:<br>    timestamp = next(ele for ele in result if ele[&#39;field&#39;]==&#39;@timestamp&#39;)[&#39;value&#39;]<br>    message = next(ele for ele in result if ele[&#39;field&#39;]==&#39;@message&#39;)[&#39;value&#39;].replace(&#39;\n&#39;, &#39;&#39;)<br>    results.append(&#39;{}: {}&#39;.format(timestamp, message))</pre><pre>print(results)</pre><p>Please note that there can be a small delay when logs are pushed to CloudWatch, therefore make sure to give enough time for the logs to get pushed.</p><p>Happy coding folks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ebc6b7386008" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximizing the amount of work not done]]></title>
            <link>https://meurub.medium.com/maximizing-the-amount-of-work-not-done-ac77403802ed?source=rss-74f8e3adf7bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac77403802ed</guid>
            <dc:creator><![CDATA[Meuru Muthuthantri]]></dc:creator>
            <pubDate>Mon, 04 May 2020 04:21:19 GMT</pubDate>
            <atom:updated>2020-05-04T04:21:19.233Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/660/0*SU6QqT0n8fjtWRd-" /></figure><p>I first heard this phrase during an Agile training program, that sounded really cool, but I was thinking how can we possibly apply that to my software engineering work.</p><p>It’s one of the twelve agile principals.</p><h3>What does it mean?</h3><p>According to my understanding it simply meaning doing only what is necessary and required. For that we have properly understand the user’s requirements and required value and work only to add that value, nothing more nothing less.</p><h4>Why do we do that?</h4><p>Well in simple words you save a lot of time by doing that. Time is money, isn’t it?</p><h4>It will improve your timelines</h4><p>Most of the software engineering projects suffer when meeting timelines. So why do we do something that has not been expected. Yes, it is good to always add more value, but what if that value is not required by the user? Our effort will be lost.</p><p>In order to understand the optimum set of requirements(not minimum) you should have close collaboration between your users(product owners) and you. Identify to the user stories in the optimum granular level, so that product owners/users can clearly set the priorities correct. So that the dev team can deliver working software as soon as possible. It is part of the dev team as well to argue on the priority of the stories. With the collaboration users/product owners and the dev team can come to the optimum set of requirements.</p><p>Working on and delivering the most important things means that adding the most value to the product. Which also means utilizing the budget effectively to maximize the return on investment</p><p>Requirements tend to change with time, so why do you want to just throw away you nice additional feature?</p><blockquote>Simple words, ‘Ask the user, get the feedback, develop the feature only if they required’</blockquote><h4>Code quality will be high</h4><p>We love to build super features, a Todo app which can also save the earth from aliens.</p><p>We have to keep in mind the coding is an art. Simpler and minimal the better.</p><p>If it is not practically required, we don’t need to handle all the hypothetical edge scenarios(but don’t let those to be forgotten as well). Make sure to always have support for future improvements by preserving the <a href="https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle">Open Close Principal</a>.</p><p>Final words..</p><p>Whenever you design, code and implement remember to think twice, or ask someone and maximize the work not done.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac77403802ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>