<?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 David Stinnette on Medium]]></title>
        <description><![CDATA[Stories by David Stinnette on Medium]]></description>
        <link>https://medium.com/@dastinnette?source=rss-187f33cde8d2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wMeIngjTotUP_8cm.jpg</url>
            <title>Stories by David Stinnette on Medium</title>
            <link>https://medium.com/@dastinnette?source=rss-187f33cde8d2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 21:31:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dastinnette/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[Optimistic vs Pessimistic Rendering: When should we test a user’s patience?]]></title>
            <link>https://medium.com/@dastinnette/optimistic-vs-pessimistic-rendering-when-should-we-test-a-users-patience-2f6d5251d943?source=rss-187f33cde8d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f6d5251d943</guid>
            <dc:creator><![CDATA[David Stinnette]]></dc:creator>
            <pubDate>Mon, 16 Oct 2023 16:31:03 GMT</pubDate>
            <atom:updated>2023-10-16T16:31:03.178Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jsj1JWTL_--pVlpKyxVBZQ.jpeg" /></figure><p>There are more apps than ever that provide unique bits of value to users and as a result, competition for attention has become increasingly intense. As developers we need to balance functionality with performance and prioritize response times when it makes sense. One such trade off decision involves deciding how to respond to a user request when they interact with your website. Optimistic and Pessimistic rendering are two concepts that field a request and return a response to the user. In this post we’ll dive into each and discuss scenarios where Optimistic Rendering is preferred over Pessimistic and vice versa.</p><p><strong>Optimistic Rendering</strong></p><p>Optimistic Rendering is a design pattern where responses are returned to the user instantly. In short, the UI updates what the user sees before receiving a response from the server. The optimistic result is replaced behind the scenes with the actual response once received and UI response times are much faster. What is an example where this be might be preferred?</p><p>Let’s use Instagram as an example. What happens under the hood when a user likes a post?</p><ul><li>A user double taps the post they like</li><li>Instantly, the heart icon turns red and the like count increases</li><li>Meanwhile, the like update request has been sent to the server</li><li>If the call is successful, the response overwrites the temporary success state</li><li>In the event of a failure, the heart icon and like count revert</li></ul><p><strong>Pessimistic Rendering</strong></p><p>Pessimistic Rendering is a design pattern where the user must wait for the server to respond before seeing if their request was successful. The UI updates once the user sees once it has received a successful response from the server. In the event of a failure, that response is also sent to the user instructing them to try again. What is an example where this pattern is preferred?</p><p>Let’s use Ticketmaster (booo!!) as an example. What happens behind the scenes when a user purchases a concert ticket?</p><ul><li>The user enters all payment info and clicks a confirm purchase button</li><li>A call is sent to the server while the user sees some sort of “processing” animation instructing them to wait for a response</li><li>The server sends a response back to the user</li><li>A successful purchase sends the user to a confirmed purchase page</li><li>A failure does not change the database and alerts the user to try again</li></ul><p><strong>Summary</strong></p><p>When should we implement each rendering pattern and why? Ultimately, it’s up to you to decide which to use. The best choice typically depends on what sort of information is being returned to the user. Are you working on a banking app with an account transfer or building a feature for a user to purchase an airline ticket? In these cases where important requests with sensitive info are being sent to the server, it’s best to use Pessimistic Rendering. Less critical requests such as liking a post on Instagram can use Optimistic Rendering where the stakes of the request are lower and a user simply expects to see their action responded to immediately. As engineers, we need to fully understand our product and what a user expects to see in order to determine how best to handle rendering requests.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f6d5251d943" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React State Management 101]]></title>
            <link>https://medium.com/@dastinnette/react-state-management-101-a4c4da2e87cd?source=rss-187f33cde8d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4c4da2e87cd</guid>
            <dc:creator><![CDATA[David Stinnette]]></dc:creator>
            <pubDate>Mon, 16 Oct 2023 12:51:37 GMT</pubDate>
            <atom:updated>2023-10-16T12:51:37.348Z</atom:updated>
            <content:encoded><![CDATA[<p>I have spent the past several weeks learning React and have gained an understanding of why it’s so popular in the web development community. One of the most powerful tools I’ve picked up is how React manages state compared to other libraries or frameworks. In this post I’ll briefly dig in to why state management is important, what issues developers may face as apps scale, and what are some of the built in state management solutions React provides.</p><p>State management is important in React because it outlines how applications handle data. The library provides various tools to ensure consistency when handling data. Knowing these tools and design best practices improves maintainability as apps scale. Adhering to these recommendations also produces apps that are performant as well as functional.</p><p>Speaking of scaling, maintaining consistency of state across many components can become challenging the larger an app gets. Developers can choose to optimize state updates, avoid unnecessary re-renders, call the database in the correct component and find other performance improvements through React Developer Tools.</p><p>React provides built in state management tools that do much of the heavy lifting for us. A hook is another name for a tool and useState is one of the core hooks that helps manage state. It gives us access to the state variable and setState function that can update the state of a component. Consider a button that adds one to a count every time a button is clicked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*iEgqWqmJi0CheyEm15lLlA.png" /></figure><p>Here we are initializing the state count with the useState hook and setting the starting value to zero. Then we are using the setCount function to update the state by one whenever the Add button is clicked.</p><p>All of this happens without a page reload and requires noticeably less code than solutions built through vanilla Javascript. This improves app performance while also keeping state management simple. As I continue to learn React I want to learn about more hooks like useReducer which help developers work with complex state objects. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4c4da2e87cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DIY Database Performance Improvement]]></title>
            <link>https://medium.com/@dastinnette/make-database-performance-great-again-6f41502d4ce9?source=rss-187f33cde8d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f41502d4ce9</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[David Stinnette]]></dc:creator>
            <pubDate>Mon, 18 Apr 2016 00:25:34 GMT</pubDate>
            <atom:updated>2020-03-18T13:03:53.794Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYKIcNIzs7hRD5cPu2PH9g.png" /><figcaption>I don’t think so, Tim</figcaption></figure><p>I once built a Rails app that took 300 seconds to load. No, I didn’t forget to include the ‘milli’ prefix and I’m well aware an app that slow will never be useful — no matter how interesting the data it renders is. So how did I arrive there? And more importantly, how did I refactor my code to bring database performance back to a satisfactory state?</p><p>Before we dig in, let’s take a step back. On the job you’re likely to encounter database performance issues due to the average web app being so database reliant. When this happens, where do you start? Resist the urge to immediately start rooting out N+1 queries and instead use a service that measures app performance one call at a time. The small time investment required up front will almost assuredly save you time and headaches in the long run versus the guess and check approach. <a href="https://github.com/flyerhzm/bullet">Bullet</a>, <a href="https://github.com/skylightio/skylight-ruby">Skylight</a>, and <a href="https://github.com/newrelic/rpm">New Relic</a> are all good options for Rails apps. I used New Relic and quickly confirmed the problem I suspected — a index page was calling nearly the entire database over 200 times when only a single query was necessary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*Jxa462qmMp-BCktxDqp4oQ.png" /></figure><p>So we’ve found the bottleneck, now what? In my app, the ‘count_tweets’ method queries a database of 10,000 tweets, groups them by user location, and groups them once more according to a hashtag ID. Prior to the refactor you see in the screenshot, the return value from ‘count_tweets’ was called by four other methods in the class and the database was queried each time. A table in the index view called these four methods for each of the 50 states plus DC — 204 database queries in all! Gross, let’s eliminate this absurd traffic jam. In an effort to keep these methods separate to maintain the code’s readability, I created an initialize method that stored the query’s response and passed that as a variable to the rest of the class. 204 fewer database queries later and just like that, a load time of 300 seconds was reduced to less than 300ms.</p><p>What other improvements can be made? The app is hosted on Heroku which enforces a 10,000 record database limit for free plans. Due to my database size and app design, Politweets did not benefit a great deal from indexing or fetching less data through query optimization. However if you’re working with 100,000+ records, these are topics to explore.</p><p><strong>Indexing </strong>— Adding an index to a table will ensure consistent query performance even as many thousands of records get added to it. For example, if a model uses the ‘find_by’ method on a column, the column in question should be indexed. In theory any column you frequently sort by should be indexed. A word of caution though, indices should only be added to columns that are actually queried in the app because each one added creates more data to process. How does one create an index? Study up <a href="http://www.tutorialspoint.com/sql/sql-indexes.htm">here</a>.</p><p><strong>Query optimization </strong>— This can mean many things so let’s focus on simply choosing the right query for the job. Sometimes you need to fetch an entire table but oftentimes that’s not the case. When you don’t need every column returned, use ‘select’ to reduce the size of the response. It will looks something like this: ‘Table.select(:column)’. Taking this concept one step further, use ‘pluck’ as a shortcut to select one or more attributes without loading a bunch of records just to grab the necessary info. ‘Table.pluck(:column)’ returns an array of attribute values typecast to match the column name. The theory above is fairly straight forward — if you don’t need the entire data set, learn how to ask for less and save time.</p><p>Still hungry? I learned a lot from <a href="http://tutorials.jumpstartlab.com/topics/performance/queries.html#query-strategies">this resource</a>. And if you have questions or thoughts on how I could improve my solution, I’d love to hear from you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f41502d4ce9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>