<?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>Forem: Wissam A</title>
    <description>The latest articles on Forem by Wissam A (@imwiss).</description>
    <link>https://forem.com/imwiss</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%2F11601%2F524ebca1-af38-4152-9e2d-5cc46507487f.png</url>
      <title>Forem: Wissam A</title>
      <link>https://forem.com/imwiss</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/imwiss"/>
    <language>en</language>
    <item>
      <title>My Best Advice for New Hires</title>
      <dc:creator>Wissam A</dc:creator>
      <pubDate>Wed, 24 Mar 2021 02:28:38 +0000</pubDate>
      <link>https://forem.com/imwiss/my-best-advice-for-new-hires-7nl</link>
      <guid>https://forem.com/imwiss/my-best-advice-for-new-hires-7nl</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lAfxbSL3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://designingforscale.com/content/images/2021/03/bonneval-sebastien-GACjwmEEoeQ-unsplash--1-.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lAfxbSL3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://designingforscale.com/content/images/2021/03/bonneval-sebastien-GACjwmEEoeQ-unsplash--1-.jpg" alt="My Best Advice for New Hires"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I recently hired four new engineers for the API team at GitHub. Starting a new job is never easy, and I feel like it is especially harder when you land at one of your dream jobs.&lt;/p&gt;

&lt;p&gt;As part of their interview process and onboarding, I've been thinking a lot about how to help our new teammates be as successful as possible and what advice I can give without overwhelming them with information. It ended up boiling down to this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do not rush to show value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What does that even mean?
&lt;/h3&gt;

&lt;p&gt;I can't speak for everyone, but I and many other new comers had pretty big &lt;em&gt;imposter syndrome&lt;/em&gt; when we joined GitHub.&lt;/p&gt;

&lt;p&gt;These questions were always top of mind for me: How did I earn this job? Will I live up to expectations? Will they figure out that I'm a fraud? How can I prove myself as fast as possible?&lt;/p&gt;

&lt;p&gt;This mentality is more harmful than anything. Rushing to show value is not helpful, and most people tend to do mistakes when they feel rushed.&lt;/p&gt;

&lt;p&gt;Furthermore, they do &lt;em&gt;not&lt;/em&gt; have to prove themselves. The fact that they successfully went through our interview process and that we extended an offer out to them is proof in and of itself that they deserve it.&lt;/p&gt;

&lt;p&gt;In fact, they should &lt;em&gt;trust&lt;/em&gt; our decision-making process that led to their hiring, and hopefully remove some pressure off of their shoulders.&lt;/p&gt;

&lt;h3&gt;
  
  
  Learn to walk before you run
&lt;/h3&gt;

&lt;p&gt;Every step in the onboarding process is important, and the goal is not to skip them in the spirit of moving fast. New hires should focus on learning to walk before they can run. I understand that it is challenging to not be as productive as you know you can be, but the onboarding process is a great opportunity to solidify your foundation and understanding of your new "company's system and architecture.&lt;/p&gt;

&lt;p&gt;These steps will serve you well in the future:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Taking the time to digest all the information you're reading.&lt;/li&gt;
&lt;li&gt;Asking questions.&lt;/li&gt;
&lt;li&gt;Digging deeper into the code to better understand how the system works.&lt;/li&gt;
&lt;li&gt;Thinking about the big picture, and how the different parts of the system interact with each other.&lt;/li&gt;
&lt;li&gt;Bookmarking important links that you'll want to refer back to in the future.&lt;/li&gt;
&lt;li&gt;Pairing with other engineers.&lt;/li&gt;
&lt;li&gt;Listening and participating in meetings.&lt;/li&gt;
&lt;li&gt;Taking notes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is to set yourself up for future success. If you skip a bunch of important steps, you (and others) will quickly notice the gaps in your understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Imposter syndrome is real, and a big topic on its own, but ultimately it is important to acknowledge that it exists and to also &lt;strong&gt;understand that reacting to it by rushing your onboarding is not the key to success&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead, take a more steady approach by solidifying your foundation and understanding of your new company, and you will be better set up to show all the value that you can bring to them.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@gentlestache?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Bonneval Sebastien&lt;/a&gt; on Unsplash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;PS: If you enjoyed this article, you may enjoy some of my other articles on &lt;a href="https://designingforscale.com/?utm_source=dev-to"&gt;designingforscale.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>juniordev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why Management May Not Be Right For You</title>
      <dc:creator>Wissam A</dc:creator>
      <pubDate>Sat, 16 May 2020 20:51:11 +0000</pubDate>
      <link>https://forem.com/imwiss/why-management-may-not-be-right-for-you-12eh</link>
      <guid>https://forem.com/imwiss/why-management-may-not-be-right-for-you-12eh</guid>
      <description>&lt;p&gt;I often talk to engineers who are interested in making the big jump to management and becoming engineering managers, but they're not sure if it's the right fit for them. They are (understandably so) concerned if they will like it or not, and what their day to day will look like. Here a few points you should consider before making the big jump into management.&lt;/p&gt;

&lt;h2&gt;
  
  
  It is not a promotion
&lt;/h2&gt;

&lt;p&gt;Switching to management is &lt;strong&gt;not&lt;/strong&gt; a promotion. Rather, it is a career transition and you'll be starting at the bottom of the career ladder where you'll feel like a novice at the beginning. Depending on your previous position as an Individual Contributor (IC), this might not even come with a raise. However, you'll have an opportunity to increase your impact on the company and business. You'll be learning something new every day. You'll help people grow in their career and as individuals. There are a lot of pros to being a manager, but don't make the switch if your main motivator is to climb the career ladder and make more money. In this case, you might want to stay on the technical track and grow there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get ready to code much less
&lt;/h2&gt;

&lt;p&gt;As a manager, you'll be coding a lot less (read: almost not at all), and you have to be okay with that. I personally work on side projects a few hours per week to get my coding fix and keep learning new technologies, but I do not contribute meaningful code at my day job and that's 100% fine with me because my focus is on delivering more impact to the team and business by focusing on higher-leverage areas. A key concept you should also aspire to is to stay involved with code, but &lt;strong&gt;stay out of critical paths&lt;/strong&gt; such as being a required reviewer or a blocker for shipping a feature. Instead, you could work on a bug fix or internal tooling to help improve the team's velocity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I38Vyz-9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://designingforscale.com/content/images/2020/05/A82AD4C0-72CA-4B86-B421-FE1E23B7EE1B.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I38Vyz-9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://designingforscale.com/content/images/2020/05/A82AD4C0-72CA-4B86-B421-FE1E23B7EE1B.JPG" alt="Why Management May Not Be Right For You"&gt;&lt;/a&gt;&lt;em&gt;Pictured above are my GitHub contributions as an engineer VS manager&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Your definition of success will change
&lt;/h2&gt;

&lt;p&gt;As a manager, your definition of success will change. Successes are far more spread apart than as an Individual Contributor, and you will not be getting the same adrenaline rush. As an engineer, you might be getting multiple successes per day when you ship a fix or merge a PR — that adrenaline rush that feels good. As a manager, your successes might be helping the team re-align on a project, hitting a quarterly objective, helping an engineer get promoted, or writing a proposal that gets approved by the VP. As you can imagine, these successes take longer to build and are more spread out, and the adrenaline rush you get will also be different.&lt;/p&gt;

&lt;h2&gt;
  
  
  Meetings, meetings, meetings
&lt;/h2&gt;

&lt;p&gt;Your days will mostly be spent in meetings, despite how much you try to reduce the number of hours spent in meetings. I've found certain tricks to help me take better control of my calendar — and it helps a lot — but the bottom line is that you have to attend and even run a lot of important meetings. These meetings include (ideally weekly) 1:1s with your direct reports, team planning meetings, leadership and organization meetings, etc. Because of that, you'll also have less uninterrupted chunks of time so you will be less likely to "be in the zone" unless you are very defensive with your calendar. For instance, I block out large chunks of time on my calendar to ensure that I get regular focus work done and don't jump from a meeting to another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally, this is not a one-way door
&lt;/h2&gt;

&lt;p&gt;It's important to note that making the switch to management should not be a one-way door. I suggest trying it out for 1-2 years to gain experience, a different perspective, and gain insights into how your manager thinks, and then deciding if you want to pursue it further or would prefer switching back to being an engineer. On average, it takes about 2-3 years before your skills erode so you should still be in a relatively good place to jump back to engineering after 1-2 years.&lt;/p&gt;

&lt;p&gt;If these do not sound like blockers to you and are motivating instead, then you should be well prepared for making the switch to management. I hope I was able to provide more insight into what a manager's role entails and that I brought up some good points for you to consider if this is the right fit for you or not.&lt;/p&gt;

</description>
      <category>career</category>
      <category>general</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Understanding Redux Middleware And Writing Custom Ones</title>
      <dc:creator>Wissam A</dc:creator>
      <pubDate>Wed, 06 Sep 2017 16:26:53 +0000</pubDate>
      <link>https://forem.com/imwiss/understanding-redux-middleware-and-writing-custom-ones</link>
      <guid>https://forem.com/imwiss/understanding-redux-middleware-and-writing-custom-ones</guid>
      <description>&lt;p&gt;If you use Redux, you’ve most likely used redux middleware before through - for example - &lt;code&gt;redux-thunk&lt;/code&gt;, &lt;code&gt;redux-promise-middleware&lt;/code&gt;, &lt;code&gt;redux-saga&lt;/code&gt; or &lt;code&gt;redux-logger&lt;/code&gt;. These are important middleware in most apps but yet some of us (including me) take this concept for granted without digging any further.&lt;/p&gt;

&lt;p&gt;With that said, I recently had to implement a caching solution in a large application and, after doing some research and poking one of my colleague’s brain (thank you Rec!), decided that implementing a custom middleware was the best approach for this, mainly because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It makes for cleaner code&lt;/li&gt;
&lt;li&gt;It makes for more maintainable code (think separation of concerns)&lt;/li&gt;
&lt;li&gt;It groups all the caching logic in one location&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, I’d like to explain what a Redux middleware is, and how I implemented a custom middleware.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Middleware?
&lt;/h3&gt;

&lt;p&gt;For backend developers, a Redux middleware is similar to a middleware in Express or in ASP.NET. Although it is not exactly the same thing, it’s similar and represents a good way of thinking of this concept.&lt;/p&gt;

&lt;p&gt;In Redux, a middleware is used to intercept dispatched actions before they make it to the reducer. This means that when you call &lt;code&gt;dispatch&lt;/code&gt; on an action, the action goes through a (or many) middleware before hitting the reducer - if it even makes it that far, but more on that later.&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%2Fdesigningforscale.com%2Fcontent%2Fimages%2F2017%2F09%2FreduxMiddleware.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%2Fdesigningforscale.com%2Fcontent%2Fimages%2F2017%2F09%2FreduxMiddleware.png" alt="This is what the Redux middleware flow looks like"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can apply multiple middleware to a Redux store, which means that the action will have to go through all the middleware before making it to the reducer. The order of execution is actually the order in which you pass the middleware to the store. Also, at any point in a middleware, you can chose to stop forwarding the action, which will end the cycle.&lt;/p&gt;

&lt;p&gt;For instance, in my caching middleware, I first check if the &lt;em&gt;same action&lt;/em&gt; is already in progress. If it is, I cache the latest payload and interrupt the flow by returning out of the middleware. Since I’m not calling &lt;code&gt;next&lt;/code&gt; or &lt;code&gt;dispatch&lt;/code&gt;, the action flow will not continue.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Use a Middleware?
&lt;/h3&gt;

&lt;p&gt;As expressed above, actions go through middleware before getting to the reducers, which gives us a great way of applying logic or filters to all actions. This means that the logic is grouped in one place instead of being spread across reducers, that we can easily identify where to investigate if a bug occurs, and we can swap that code out if we ever need to.&lt;/p&gt;

&lt;p&gt;Some use-cases that benefit from using middleware:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logging: every action goes through this middleware, so we can log its type and payload for debugging or tracking purposes.&lt;/li&gt;
&lt;li&gt;Error tracking: if any asynchronous action returned an error, this middleware can display a notification.&lt;/li&gt;
&lt;li&gt;Caching: Only call your API for the same action once, and cache the result for future calls.&lt;/li&gt;
&lt;li&gt;Auth requests: For API calls, apply an authentication token before sending out the request.&lt;/li&gt;
&lt;li&gt;So much more :)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Writing a Middleware
&lt;/h3&gt;

&lt;p&gt;To define your own middleware, you need to write a function with the following signature:&lt;br&gt;
&lt;code&gt;store =&amp;gt; next =&amp;gt; action =&amp;gt; result&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This looks very confusing at first glance - I hear you - so let’s break it down a little bit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;store&lt;/code&gt; is the Redux store instance that will be passed to your middleware.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;next&lt;/code&gt; is a function that you need to call with an action when you want to continue the flow execution, which means passing the action to the next in line: either the following middleware or a reducer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;action&lt;/code&gt; is the action that was originally dispatched so that you can access it, apply logic based on the action, and eventually pass it on using &lt;code&gt;next&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;result&lt;/code&gt; is the value used as the result of the dispatch call.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, to apply this middleware to the Redux store, you need to call &lt;code&gt;applyMiddleware&lt;/code&gt; when creating the store through &lt;code&gt;createStore()&lt;/code&gt;. Here’s a nice example from the official Redux documentation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createStore, combineReducers, applyMiddleware } from 'redux'

let todoApp = combineReducers(reducers)
let store = createStore(
  todoApp,
  // applyMiddleware() tells createStore() how to handle middleware
  applyMiddleware(logger, crashReporter)
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, the middleware &lt;code&gt;logger&lt;/code&gt; will be called first, followed by the &lt;code&gt;crashReporter&lt;/code&gt; middleware since this is the order in which they were passed to &lt;code&gt;applyMiddleware&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Caching Middleware
&lt;/h3&gt;

&lt;p&gt;As mentioned in this article’s outline, I implemented a caching middleware recently to solve a very specific issue. I know that there are existing caching middleware out there, but I needed something small and specific to the issue at hand, so I wrote a few lines of code instead of using an existing library.&lt;/p&gt;

&lt;p&gt;For this issue, I had to make sure that a WYSIWYG editor only called the backend sequentially when saving the content. For instance, if auto-save kicked in while a save was already occurring, I did not want to send the text to the backend until the previous call completed. The same concept also applies if the user hit the &lt;strong&gt;Save&lt;/strong&gt; button multiple times.&lt;/p&gt;

&lt;p&gt;Here’s what my middleware looks like:&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;textUpdatesMiddleware&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="nx"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;action&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;UPDATE_TEXT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Check if the new text in the payload is different from what we already have in the store&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;shouldSaveText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;

      &lt;span class="c1"&gt;// Are we currently saving?&lt;/span&gt;
      &lt;span class="c1"&gt;// isUpdatingText is set to `true` in a reducer&lt;/span&gt;
      &lt;span class="c1"&gt;// A reducer listens to CACHE_TEXT_UPDATE and will store the payload into `pendingTextUpdate`&lt;/span&gt;
      &lt;span class="c1"&gt;// We only cache the latest content, not all of them&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getState&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;isUpdatingText&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="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CACHE_TEXT_UPDATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// This uses `redux-promise-middleware`&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UPDATE_TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;patch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiEndpoint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&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="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// This uses the `redux-promise-middleware` convention of _PENDING, _FULFILLED, _REJECTED&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;UPDATE_TEXT_FULFILLED&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;pendingTextUpdate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getState&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;pendingTextUpdate&lt;/span&gt;
      &lt;span class="c1"&gt;// If we had a pending update&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pendingTextUpdate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// A reducer listens to UNCACHE_TEXT_UPDATE and will clear `pendingTextUpdate`&lt;/span&gt;
        &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UNCACHE_TEXT_UPDATE&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="c1"&gt;// Allow the fulfilled action to continue on to the reducers&lt;/span&gt;
        &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// Dispatch the update with the cached content&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UPDATE_TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pendingTextUpdate&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="c1"&gt;// Nothing to do here - keep calm and carry on&lt;/span&gt;
    &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&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;p&gt;Based on the code above, it’s worth noting that Redux applies some magic when you call &lt;code&gt;store.dispatch&lt;/code&gt; from within a middleware and the action will travel through all the middleware again, including the current middleware that dispatched it. However, when you call &lt;code&gt;next&lt;/code&gt;, the action moves on to the next middleware in the flow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This middleware solves a specific issue I was experiencing, but we could just as well make it more generic so that it applies the same concept to all (or a subset of) actions. At this point, I don’t have any need to make it generic so I did not want to over-engineer it, but it’s worth noting that it’s definitely doable.&lt;/p&gt;

&lt;p&gt;If I hadn’t applied this logic in a middleware, I would have had to validate that an API call is not currently in-progress from a reducer, then dispatch calls from the reducer to cache the content, and also listen for the &lt;code&gt;FULFILLED&lt;/code&gt; action from the reducer or the &lt;code&gt;then&lt;/code&gt; on the http call, and then re-dispatch the action. This gets messy real quick and doesn’t scale well if we need to make it more generic.&lt;/p&gt;

&lt;p&gt;I hope this was a good introduction to middleware and that it covered enough of the basics to get you started if you ever need to write your own custom one.&lt;/p&gt;

&lt;p&gt;My final piece of advice is that research and discussions are very valuable. I am very glad I decided to not go with my original (bad) approach because something felt wrong and that I did more research, discussed it with a colleague, and ended up settling on using a middleware because the final result is a better solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Credit
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The official Redux documentation section on &lt;a href="[Middleware%20%C2%B7%20Redux](http://redux.js.org/docs/advanced/Middleware.html)"&gt;Middleware&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="[You%20Aren%E2%80%99t%20Using%20Redux%20Middleware%20Enough%20%E2%80%93%20Jacob%20Parker%20%E2%80%93%20Medium](https://medium.com/@jacobp100/you-arent-using-redux-middleware-enough-94ffe991e6)"&gt;You Aren’t Using Redux Middleware Enough&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://designingforscale.com/?utm_source=redux-middleware&amp;amp;utm_medium=guest-post&amp;amp;utm_campaign=devto" rel="noopener noreferrer"&gt;Designing for Scale&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Business Rules Must Be Enforced by the API</title>
      <dc:creator>Wissam A</dc:creator>
      <pubDate>Tue, 15 Aug 2017 18:38:27 +0000</pubDate>
      <link>https://forem.com/imwiss/business-rules-must-be-enforced-by-the-api</link>
      <guid>https://forem.com/imwiss/business-rules-must-be-enforced-by-the-api</guid>
      <description>&lt;p&gt;An important concept to keep in mind when designing web applications is that business rules &lt;strong&gt;must&lt;/strong&gt; be enforced on the backend, by the API. In this article, I will demonstrate why this is crucial and how avoiding to do so will open up your application to security issues and bugs.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Are Business Rules
&lt;/h3&gt;

&lt;p&gt;Before jumping in to the technical stuff, let me clarify what I mean by &lt;em&gt;business rules&lt;/em&gt; so that we are all on the same page.&lt;/p&gt;

&lt;p&gt;Business rules, sometimes referred to as &lt;em&gt;business logic&lt;/em&gt; or &lt;em&gt;application logic&lt;/em&gt;, are what governs how your app &lt;strong&gt;should&lt;/strong&gt; function and what validations to apply. For instance, a business rule at Twitter is that tweets cannot contain more than 140 characters (if we dumb it down for the sake of this example and remove logic such as URL shorteners, replying to someone, etc). This means that before sending a tweet, Twitter needs to ensure that it respects the character limit.&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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.08.29-PM.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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.08.29-PM.png" alt="Max character count"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;I'm over the character limit ðŸ‘†.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Where&lt;/em&gt; this validation should be made is exactly the topic of this article. Let's dive in!&lt;/p&gt;

&lt;h3&gt;
  
  
  Client-Side Validations
&lt;/h3&gt;

&lt;p&gt;Client-side validations are useful in order to provide &lt;em&gt;immediate&lt;/em&gt; feedback to the user, which makes for better user experience. For instance, when writing a tweet, we can instantly see if we went over the 140-character limit or not.&lt;/p&gt;

&lt;p&gt;However, client-side validations are not enough on their own since they are easily bypassed. Browsers (and mobile apps for that matter) can be "hacked" or toyed with in order to bypass certain restrictions put in by the application's developers. In fact, browsers allow you to inspect and modify the html and JavaScript quite easily, so it's a bad idea to count only on frontend validations.&lt;/p&gt;

&lt;p&gt;For instance, it's easy to re-enable the &lt;code&gt;Tweet&lt;/code&gt; button in the browser even if we went over the 140-character limit. By doing so, we are bypassing the client-side validation and sending the longer tweet to the backend, as so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Re-enabling the &lt;code&gt;Tweet&lt;/code&gt; button:&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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.20.55-PM.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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.20.55-PM.png" alt="Re-enabling the Tweet button"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Sending the long tweet to the backend, as seen in this POST request:&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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.21.48-PM.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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.21.48-PM.png" alt="HTTP POST Request"&gt;&lt;/a&gt;&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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.22.00-PM.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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.22.00-PM.png" alt="Data sent to the backend"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This is gracefully handled on the server and a friendly error message is displayed to the user:&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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.21.20-PM.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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F08%2FScreen-Shot-2017-08-11-at-10.21.20-PM.png" alt="Error returned by the server and displayed to user"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;To protect the integrity of your data and app, it is essential to enforce your business rules on the backend as well.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Backend Validations
&lt;/h3&gt;

&lt;p&gt;Backend validations allow you to take another look at the data - now that it's made its way from the client to the server - and ensure that it respects the business rules. In our previous example, this is where you would ensure that the tweet being sent is indeed 140 characters or less. If not, the server should return an appropriate error to the client so that it is handled gracefully and the user knows what to do next.&lt;/p&gt;

&lt;p&gt;The backend is also where you can ensure data integrity by validating the information being saved to the database, making sure it does not contain any ill-intented information, that the types are good, etc. This should also be done at a lower level than the API, such as an ORM or even the database, but I include those in the big &lt;em&gt;backend&lt;/em&gt; bubble. Separating concerns and where &lt;strong&gt;exactly&lt;/strong&gt; to apply these validations is a discussion for another article.&lt;/p&gt;

&lt;p&gt;One of the reasons why validations on the backend are better suited for business logic is that you have control over your server and the code executed on it, as opposed to client-side code. This is not being run in a browser or on a mobile device and is not being manipulated by a user. It is code that you wrote and know how it will behave.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;One important thing I'd like to clarify here is that both frontend and backend validations are important but their main goal is different.&lt;/p&gt;

&lt;p&gt;Frontend validations are great for giving feedback to the user. Imagine having a user filling up a long form and submitting it, only for it to be rejected by the server because of an empty input, and prompting the user to re-fill the form. This would obviously be frustrating to users and is avoidable by using frontend validations.&lt;/p&gt;

&lt;p&gt;Backend validations, on the other hand, are meant to ensure that the business rules are indeed applied and respected, and for data integrity.&lt;/p&gt;

&lt;p&gt;I hope this was helpful and clarified the differences between the two parties. Let me know if you have any questions or comments. Thank you for reading.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://designingforscale.com/?utm_source=business-rules&amp;amp;utm_medium=guest-post&amp;amp;utm_campaign=devto" rel="noopener noreferrer"&gt;Designing for Scale&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Hoisting in JavaScript</title>
      <dc:creator>Wissam A</dc:creator>
      <pubDate>Fri, 28 Apr 2017 17:34:00 +0000</pubDate>
      <link>https://forem.com/imwiss/understanding-hoisting-in-javascript</link>
      <guid>https://forem.com/imwiss/understanding-hoisting-in-javascript</guid>
      <description>&lt;p&gt;You know JavaScript, but do you really &lt;em&gt;know&lt;/em&gt; JavaScript? It's a great language, even though some may argue otherwise. Sure, it's got some bad parts, but it has improved a lot in past years and developers are getting much better at using JavaScript &lt;em&gt;correctly&lt;/em&gt; and at following best practices. Strict mode is also getting better at preventing newer developers from making some bad JavaScript mistakes and unfortunately running into unwanted behaviours.&lt;/p&gt;

&lt;p&gt;However, not everyone has heard of the term &lt;strong&gt;Hoisting&lt;/strong&gt; or knows what it means. In this article, I'll explain what hoisting is and show different examples so that you can better understand what it's all about.&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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F03%2FlaptopAndBooks.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%2Fwww.designingforscale.com%2Fcontent%2Fimages%2F2017%2F03%2FlaptopAndBooks.png" alt="Learning"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  The JavaScript Interpreter
&lt;/h4&gt;

&lt;p&gt;When you execute your JavaScript code, the interpreter goes through the code twice. &lt;/p&gt;

&lt;p&gt;The first run through the code is where it does a safety check and small optimizations of your code. Safety checks such as making sure that the syntax is right, if there are any calls to &lt;code&gt;eval&lt;/code&gt; or &lt;code&gt;with&lt;/code&gt;, etc. Then, it optimizes the code as best as it can to ensure better performance when it is executed. This is also where hoisting occurs (more on this soon), and is referred to as the &lt;em&gt;compile&lt;/em&gt; run.&lt;/p&gt;

&lt;p&gt;The second run is where it actually executes your code by going through it line by line, doing the assignments, calling the functions, and so on.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is Hoisting?
&lt;/h4&gt;

&lt;p&gt;Hoisting is when the JavaScript interpreter moves all variable and function declarations to the top of the current scope. It's important to keep in mind that only the actual declarations are hoisted, and that assignments are left where they are.&lt;/p&gt;

&lt;p&gt;Hoisting is done during the interpreter's first run through the code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Variable Declarations
&lt;/h4&gt;

&lt;p&gt;Let's start with a basic example and look at the following code:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'bar'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, you may think that the sample code would throw a &lt;code&gt;ReferenceError&lt;/code&gt; on line 3 (&lt;code&gt;console.log(bar);&lt;/code&gt;) because &lt;code&gt;bar&lt;/code&gt; has not been declared yet. However, with the magic of hoisting, it won't throw a &lt;code&gt;ReferenceError&lt;/code&gt; but the value of &lt;code&gt;bar&lt;/code&gt; will be &lt;code&gt;undefined&lt;/code&gt; at that point. This is because the JavaScript interpreter does a first run through the whole code and declares all variables and functions at the top of the current scope, and then, on the second run, will execute the code.&lt;/p&gt;

&lt;p&gt;Here's what the same code would look like after the interpreter's first run:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'bar'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how &lt;code&gt;bar&lt;/code&gt; is now declared at the top (&lt;code&gt;var bar&lt;/code&gt;) but is not yet assigned at that point? It's a subtle but important difference, and this is why &lt;code&gt;bar&lt;/code&gt; is logged as &lt;code&gt;undefined&lt;/code&gt; instead of throwing a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Function Declarations
&lt;/h4&gt;

&lt;p&gt;Hoisting also applies to &lt;em&gt;function declarations&lt;/em&gt; (not &lt;em&gt;function expressions&lt;/em&gt;). Let's analyze the following sample code:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bam&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bam&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bam&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: bam is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this sample code, we are able to successfully call the function &lt;code&gt;foo&lt;/code&gt; since it's a &lt;em&gt;function declaration&lt;/em&gt; and therefore it is hoisted as-is to the top of the current scope. Then,  &lt;code&gt;foo&lt;/code&gt; will output &lt;code&gt;undefined&lt;/code&gt; when calling it since, as in the previous example, &lt;code&gt;bam&lt;/code&gt; is hoisted to the top of its current scope, which is &lt;code&gt;function foo()&lt;/code&gt;. This means that &lt;code&gt;bam&lt;/code&gt; was declared before calling &lt;code&gt;console.log(bam)&lt;/code&gt; but it has not yet been assigned a value (&lt;code&gt;bam = 'bam'&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;However, the important thing to note here is that &lt;code&gt;bam&lt;/code&gt; was hoisted at the top of its &lt;strong&gt;current&lt;/strong&gt; scope. This means that it was &lt;strong&gt;not&lt;/strong&gt; declared in the global scope but in the function's scope instead.&lt;/p&gt;

&lt;p&gt;Here's what the same code would look like after the interpreter's first run:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bam&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bam&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
    &lt;span class="nx"&gt;bam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bam&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="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bam&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: bam is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how &lt;code&gt;foo()&lt;/code&gt; was moved to the top, and &lt;code&gt;bam&lt;/code&gt; is declared in &lt;code&gt;foo()&lt;/code&gt;? This means that, when you call &lt;code&gt;console.log(bam)&lt;/code&gt; on line 10, it will not find the variable &lt;code&gt;bam&lt;/code&gt; in the general scope and will throw a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Function Expressions
&lt;/h4&gt;

&lt;p&gt;Next, the third use case I'd like to cover is how &lt;em&gt;function expressions&lt;/em&gt; are not hoisted as opposed to &lt;em&gt;function declarations&lt;/em&gt;. Instead, it's their variable declarations that are hoisted. Here's some sample code to demonstrate my point:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bam&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bam&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code throws a &lt;code&gt;TypeError: foo is not a function&lt;/code&gt; error since &lt;strong&gt;only&lt;/strong&gt; the variable declaration &lt;code&gt;var foo&lt;/code&gt; is hoisted to the top of the file, and the assignment of the function to &lt;code&gt;foo&lt;/code&gt; is done on the interpreter's second run only.&lt;/p&gt;

&lt;p&gt;Here's what the same code would look like after the interpreter's first run:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// `foo` has not been assigned the function yet&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bam&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bam&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  What Takes Precedence?
&lt;/h4&gt;

&lt;p&gt;Finally, the last use case I'd like to cover is that &lt;em&gt;function declarations&lt;/em&gt; are hoisted &lt;strong&gt;before&lt;/strong&gt; variables. Let's look at the following code:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'function'&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bam&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;In this example, &lt;code&gt;typeof foo&lt;/code&gt; returns &lt;code&gt;function&lt;/code&gt; instead of &lt;code&gt;string&lt;/code&gt;, even though the function &lt;code&gt;foo()&lt;/code&gt; is declared after the variable. This is because &lt;em&gt;function declarations&lt;/em&gt; are hoisted before &lt;em&gt;variable declarations&lt;/em&gt;, so &lt;code&gt;foo = 'foo'&lt;/code&gt; is executed on the second run, &lt;strong&gt;after&lt;/strong&gt; we call &lt;code&gt;typeof foo&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;On the first run, the interpreter will hoist &lt;code&gt;foo()&lt;/code&gt; at the top of the current scope, and then will get to the &lt;code&gt;var foo = 'foo'&lt;/code&gt; line. At that point, it realizes that &lt;code&gt;foo&lt;/code&gt; was already declared so it doesn't need to do anything and will continue its first run through the code.&lt;/p&gt;

&lt;p&gt;Then, on the second run (which basically &lt;em&gt;executes&lt;/em&gt; the code), it'll call &lt;code&gt;typeof foo&lt;/code&gt; before it gets to the assignment &lt;code&gt;foo = 'foo'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's what the same code would look like after the interpreter's first run:&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use strict&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bam&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bam&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 'function'&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&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;h4&gt;
  
  
  ES6
&lt;/h4&gt;

&lt;p&gt;ES6 is the future and is what most developers will be using moving forward, so let's see how hoisting applies for ES6 code.&lt;/p&gt;

&lt;p&gt;Hoisting doesn't apply the same way for &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables compared to &lt;code&gt;var&lt;/code&gt; variables, as we saw above. However, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables are still hoisted, the difference being that they cannot be accessed until the assignment is done at runtime.&lt;/p&gt;

&lt;p&gt;From &lt;a href="https://tc39.github.io/ecma262/#sec-let-and-const-declarations" rel="noopener noreferrer"&gt;ES6's documentation&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The variables are created when their containing Lexical Environment is instantiated &lt;strong&gt;but may not be accessed in any way until the variable’s LexicalBinding is evaluated.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At the end of the day, it's a small technicality where the interpreter applies hoisting to these variables on the compile run but they'll throw reference errors when accessed before the assignment happens, essentially preventing us from accessing these variables before their assignment.&lt;/p&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;I hope this clarifies how hoisting works in JavaScript. It's definitely not as tricky or complicated as it sounds, but it does require us to breakdown the different use cases and trying different scenarios to understand how things work under the hood.&lt;/p&gt;

&lt;p&gt;Do not hesitate to leave me comments or questions if you have any - I'd love to hear your feedback.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://designingforscale.com/?utm_source=hoisting&amp;amp;utm_medium=guest-post&amp;amp;utm_campaign=devto" rel="noopener noreferrer"&gt;Designing for Scale&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>How Facebook Live Scales</title>
      <dc:creator>Wissam A</dc:creator>
      <pubDate>Wed, 26 Apr 2017 01:33:07 +0000</pubDate>
      <link>https://forem.com/imwiss/how-facebook-live-scales</link>
      <guid>https://forem.com/imwiss/how-facebook-live-scales</guid>
      <description>&lt;p&gt;I was recently watching Facebook's F8 conference - which was streamed on Facebook Live - and it got me curious as to how Facebook Live scales so seamlessly to millions of people around the world.&lt;/p&gt;

&lt;p&gt;How does Facebook Live scale? Let's dig in and analyze!&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%2Fdesigningforscale.com%2Fcontent%2Fimages%2F2017%2F04%2Ff8VR.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%2Fdesigningforscale.com%2Fcontent%2Fimages%2F2017%2F04%2Ff8VR.png" alt="F8 2017 was focused mostly around AR/VR"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;F8 2017 was focused mostly around AR/VR&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Challenges
&lt;/h2&gt;

&lt;p&gt;Before analyzing the architecture and how Facebook Live can scale to that extent, let's look at some of the challenges that building such a product comes with. &lt;/p&gt;

&lt;p&gt;Chris Cox, the company's Chief Product Officer, had an interview with &lt;em&gt;Wired&lt;/em&gt;&lt;sup id="fnref1"&gt;1&lt;/sup&gt; back in April of 2016, where he enumerated the following challenges, considering that there were &lt;strong&gt;over 1.86 billion monthly active users on Facebook&lt;/strong&gt; by the end of 2016:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Needs to be able to serve up millions of &lt;strong&gt;simultaneous&lt;/strong&gt; streams&lt;/li&gt;
&lt;li&gt;Needs to be able to support millions of users on the &lt;strong&gt;same&lt;/strong&gt; stream&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To quote Chris Cox, this "&lt;em&gt;turns out it’s a really hard infrastructure problem&lt;/em&gt;", which makes it even more exciting to deconstruct and analyze.&lt;/p&gt;

&lt;p&gt;Another unique challenge with live video is that traffic comes in spikes, and your infrastructure must be able to handle huge traffic spikes. For instance, when a celebrity starts a live video stream, there will be a huge traffic spike when users join and while the video is still streaming live, and then it'll slow down drastically when the video ends but it will still by viewable by users, even if it is no longer streaming live. The main challenge will be to gracefully handle the spikes while it's streaming live.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Architecture
&lt;/h2&gt;

&lt;p&gt;For the purpose of this article, I'll be exclusively focusing on the high traffic spikes that occur when a user with millions of followers (e.g. a celebrity) starts a live broadcast. In cases like this, more than a million people could be watching the broadcast at the same time, and when they all join around the same time, this causes major stress on the servers and infrastructure. &lt;/p&gt;

&lt;p&gt;When all these requests come in at once - which is called &lt;em&gt;the thundering herd problem&lt;/em&gt;&lt;sup id="fnref2"&gt;2&lt;/sup&gt; - it can cause streaming issues such as lag, dropped packets, or even preventing users from joining the broadcast.&lt;/p&gt;

&lt;p&gt;When there's a possibility you may run into such a problem, the first thing you should do is prevent all the requests from hitting your streaming server directly, &lt;strong&gt;which could be fatal and drop the server all together&lt;/strong&gt;. Instead, you should aim at adding multiple layers that will filter these requests and ensure that only the necessary requests make it to the streaming server.&lt;/p&gt;

&lt;p&gt;This can be achieved in many different ways, depending on the size of your product and the number of users. In Facebook's case, they chose the following architecture:&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%2Fdesigningforscale.com%2Fcontent%2Fimages%2F2017%2F04%2FfacebookLiveArchitecture_1-1.jpg" 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%2Fdesigningforscale.com%2Fcontent%2Fimages%2F2017%2F04%2FfacebookLiveArchitecture_1-1.jpg" alt="Facebook Live Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's important to note here that Edge Cache servers are spread out across the globe to support Facebook's global reach. There's also a &lt;strong&gt;one-to-many&lt;/strong&gt; relationship between the Origin Server and the Edge Cache, where multiple Edge Cache servers can send requests to the same Origin Server.&lt;/p&gt;

&lt;h3&gt;
  
  
  The flow works as follow:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The requests first hit the Edge Cache server closest to the user in order to reduce the latency between the user and the server. The Edge Cache servers are &lt;strong&gt;essentially a cache layer&lt;/strong&gt; and do not do much processing.&lt;/li&gt;
&lt;li&gt;If the requested packets are in the Edge Cache, then they are returned to the user.&lt;/li&gt;
&lt;li&gt;If they are not in the Edge Cache, then the request is forwarded to the Origin Server, which is another cache server and has a similar architecture to the Edge Cache server.&lt;/li&gt;
&lt;li&gt;If the requested packets are in the Origin Server, then they are returned to the Edge Cache who caches them before returning them to the user.&lt;/li&gt;
&lt;li&gt;If they are not in the Origin Server, then the request is forwarded to the specific Streaming Server handling that live broadcast. The Streaming Server then returns the packets to the Origin Server, who caches them before returning them to the Edge Cache. The Edge Cache caches the response and returns the packets to the user.&lt;/li&gt;
&lt;li&gt;Future requests for the &lt;strong&gt;same packets&lt;/strong&gt; will then simply be handled by the Edge Cache and will not travel further than that layer, which speeds up the process incredibly and reduces the load on the Streaming Server.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As we can see, this architecture greatly reduces the number of requests that make it to the Streaming Server. For instance, if 5 requests make it to the Edge Cache &lt;strong&gt;sequentially&lt;/strong&gt;, only the first one will go all the way to the Streaming Server and back, while the 4 others will immediately get a response from Edge Cache since it'll have cached it already.&lt;/p&gt;

&lt;p&gt;However, this is still not enough for Facebook's reach. In fact, according to an article released by Facebook&lt;sup id="fnref3"&gt;3&lt;/sup&gt;, this architecture still leaks about 1.8% of requests to the Streaming Server. At their scale, with millions of requests, 1.8% is a huge amount to leak and puts a lot of stress on the Streaming Server.&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%2Fdesigningforscale.com%2Fcontent%2Fimages%2F2017%2F04%2FfacebookGlobalReach.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%2Fdesigningforscale.com%2Fcontent%2Fimages%2F2017%2F04%2FfacebookGlobalReach.png" alt="Facebook's Global Reach"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Facebook's Global Reach: Edge Cache servers are in pink, and Origin Servers are in yellow. Source: &lt;a href="http://cs.unc.edu/xcms/wpfiles/50th-symp/Moorthy.pdf" rel="noopener noreferrer"&gt;http://cs.unc.edu/xcms/wpfiles/50th-symp/Moorthy.pdf&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is it Leaking so many Requests?
&lt;/h2&gt;

&lt;p&gt;The first flaw in the architecture above is that &lt;strong&gt;simultaneous&lt;/strong&gt; requests to the same Edge Cache will make it through to the Origin Server. Since the Edge Cache forwards all requests for missing packets to the next layer, if simultaneous requests ask for packet A and it is not in the cache, all those requests will be forwarded. Extrapolating this at Facebook's scale, we can quickly see how so many requests are leaked.&lt;/p&gt;

&lt;p&gt;The second flaw in the architecture is that &lt;strong&gt;multiple Edge Cache&lt;/strong&gt; boxes can send the same packet request to the Origin Server, which will in turn forward all those simultaneous requests to the Streaming Server since the packet is not in its cache. This is similar to the first flaw but it's happening at the Origin Server level.&lt;/p&gt;

&lt;p&gt;Again, 1.8% may not seem like a lot for normal products, but at Facebook's scale, it is indeed an important issue that needs to be resolved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adjusting the Architecture for High Scaling
&lt;/h2&gt;

&lt;p&gt;Facebook's solution to this problem is creative, yet simple to understand. When multiple requests for the same packet hit the Edge Cache, they are grouped together in a request queue and only one goes through to the Origin Server. This is called &lt;em&gt;request coalescing&lt;/em&gt;. Once the response comes from the server, it is stored in the cache and then the requests in the queue are responded from the cache.&lt;/p&gt;

&lt;p&gt;For instance, when 10 simultaneous requests make it to the Edge Cache, they are all added to the same request queue and only one goes through, which helps reduce the leaking of requests. If we compare this scenario to the original setup, where all 10 simultaneous requests would have made it through, we see a huge increase in performance.&lt;/p&gt;

&lt;p&gt;The same concept was also applied at the Origin Server level, where all requests for the same packet - coming from one or many Edge Cache endpoints - are grouped together and only one of them makes it through to the Streaming Server. Needless to say that this greatly reduces the overhead on the Streaming Server, especially consider the amount of requests a live broadcast may receive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Load Balancing
&lt;/h2&gt;

&lt;p&gt;Another important tweak that Facebook implemented was load balancing for the Edge Cache servers. In some cases, with huge traffic spikes, Edge Cache servers can get overloaded and no longer perform consistently, despite the use of request coalescing. To avoid such situations, the load balancer redirects requests to the &lt;strong&gt;closest Edge Cache with availability&lt;/strong&gt; for the request. For instance, if the Edge Cache closest to you is already serving 200 000 requests, then the load balancer might forward you to an Edge Cache a little further but that is serving half the number of requests, which would mean you would still be getting a response much faster.&lt;/p&gt;

&lt;p&gt;Facebook determines which Edge Cache is available by constantly measuring the load on these servers and keeping track of its performance. It goes even further than that and uses an algorithm to predict the load &lt;strong&gt;before it happens&lt;/strong&gt;, which is incredible. The basis of the algorithm is complex enough that it would require its own article so I will not be getting into that at the moment, but it is a very interesting topic that I may cover in the near future.&lt;/p&gt;

&lt;h2&gt;
  
  
  Request Coalescing Using Nginx
&lt;/h2&gt;

&lt;p&gt;For those of you using &lt;em&gt;Nginx&lt;/em&gt;, request coalescing can be enabled by setting the &lt;code&gt;proxy_cache_lock&lt;/code&gt; config to &lt;code&gt;on&lt;/code&gt; (default value is &lt;code&gt;off&lt;/code&gt;), as explained in the docs &lt;a href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock" rel="noopener noreferrer"&gt;here&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When enabled, only one request at a time will be allowed to populate a new cache element identified according to the &lt;code&gt;proxy_cache_key&lt;/code&gt; directive by passing a request to a proxied server. &lt;strong&gt;Other requests of the same cache element will either wait for a response to appear in the cache or the cache lock for this element to be released&lt;/strong&gt;, up to the time set by the proxy_cache_lock_timeout directive.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;I had a lot of fun researching and dissecting Facebook Live's architecture and how the engineers were able to make the product scale to millions of users. Although we may never build products that reach such a high amount of users, there are great lessons here that we can use in our own products, such as how adding multiple layers in the architecture helps filtering the number of requests that make it through to the processing server. This, in turn, improves the performance and availability of the server, which provides a better product to users and helps scaling it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://designingforscale.com/?utm_source=scalingfacebooklive&amp;amp;utm_medium=guest-post&amp;amp;utm_campaign=devto" rel="noopener noreferrer"&gt;Designing for Scale&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Credits
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://atscaleconference.com/videos/scaling-facebook-live/" rel="noopener noreferrer"&gt;Scaling Facebook Live &lt;/a&gt; at &lt;em&gt;@Scale Conference&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://atscaleconference.com/videos/scaling-facebook-live-2/" rel="noopener noreferrer"&gt;Scaling Facebook Live (2)&lt;/a&gt; at &lt;em&gt;@Scale Conference&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://www.wired.com/2016/04/facebook-really-really-wants-broadcast-watch-live-video/" rel="noopener noreferrer"&gt;Zuckerberg Really Wants You to Stream Live Video on Facebook&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Thundering_herd_problem" rel="noopener noreferrer"&gt;Wikipedia: Thundering herd problem&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://code.facebook.com/posts/1653074404941839/under-the-hood-broadcasting-live-video-to-millions/" rel="noopener noreferrer"&gt;Under the hood: Broadcasting live video to millions&lt;/a&gt; ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>scale</category>
      <category>programming</category>
      <category>design</category>
      <category>tech</category>
    </item>
  </channel>
</rss>
