<?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 Long Ho on Medium]]></title>
        <description><![CDATA[Stories by Long Ho on Medium]]></description>
        <link>https://medium.com/@longho?source=rss-bb3ff7847246------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*H9xeXeYo5nsw6kCi.jpg</url>
            <title>Stories by Long Ho on Medium</title>
            <link>https://medium.com/@longho?source=rss-bb3ff7847246------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 10:02:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@longho/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[Product Platform Pillars for Consumer Web Apps]]></title>
            <link>https://levelup.gitconnected.com/product-platform-pillars-for-consumer-web-apps-52ea5446dafc?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/52ea5446dafc</guid>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Sat, 21 Mar 2026 22:35:43 GMT</pubDate>
            <atom:updated>2026-03-23T15:20:25.506Z</atom:updated>
            <content:encoded><![CDATA[<p>Developing shared features across multiple applications often turns into rebuilding the same things over and over again. This usually happens because there is no shared product platform and the design systems diverge over time. This article lays out foundational pillars that a production-ready feature needs, and uses them as the basis for thinking about a product platform.</p><p>The focus here is on the Web, but the concepts translate to other platforms, which typically have native equivalents. The examples skew toward consumer applications, where client capabilities are more restricted and teams have less control over release cycles and versioning.</p><h3>Application Anatomy</h3><p>At the highest level, an application is a collection of product features. Those features depend on a shared product platform runtime that abstracts common, productionized capabilities. Beneath that sits the “bare metal” provided by the underlying environment (browsers, operating systems, native SDKs).</p><p>The mission of a product platform runtime is to provide a stable foundation for features to evolve across all supported platforms. Without this layer, every feature team ends up solving the same cross-cutting concerns in slightly different ways, which makes consistency, reliability, and iteration speed much harder.</p><p>Here is how the layers stack up:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*lOZfNfV8_pvLN0vokgqVog.png" /></figure><h3>Code Sharing Strategy</h3><p>Any serious platform discussion needs an explicit code sharing strategy. This strategy guides the engineering organization on what should be shared and what should not.</p><p>Two common failure modes:</p><ul><li><strong>Sharing “everything”:</strong> The codebase becomes a tangle of interdependencies and abandonware. Nobody knows which abstractions are safe to depend on, so changes are risky.</li><li><strong>Sharing “nothing”:</strong> Every team re-implements the same primitives, leading to duplication, divergence, and inconsistent behavior.</li></ul><p>A healthier model is tiered:</p><ul><li><strong>Shareable tiers:</strong> Product platform runtime, Design systems, Product-level features, A small core library of foundational utilities (think Google Closure Library or Yahoo’s YUI).</li><li><strong>Non-shareable tiers:</strong> Random small utilities, One-off components optimized for a single feature.</li></ul><p>In a mature setup, static analysis at build time enforces these boundaries: shared tiers can depend “down” but not sideways into other product feature implementations, and non-shareable pieces cannot leak into shared libraries.</p><p>This article assumes a similar model: platform and features are shared; low-level internal components and libraries are not.</p><h3>Representative Capabilities</h3><p>This is not an exhaustive list, but a representative set of platform capabilities that tend to show up repeatedly in consumer web products: ECMA-262 / language features and polyfills, IO (API clients and transport), persistence, telemetry, analytics, localization, configuration management, dynamic image scaling (notable mention). Each of these is a pillar where centralizing the solution in the platform creates leverage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*phw-hf6fZg5mhLnTaO5SjA.png" /></figure><h3>ECMA-262 Features and Polyfills</h3><p>Most consumer products must support older browsers and devices. That often means dealing with older JavaScript engines (e.g., last N versions of Chrome, older Mobile Safari) and incomplete or missing platform APIs.</p><p>Polyfills help level the playing field by re-implementing newer APIs in JavaScript where possible. In practice, web and mobile apps often ship different bundles or code paths for different runtime environments.</p><p>However, polyfills are not “free”: they increase bundle size, slow down bootstrap time, and localization-related polyfills can be especially heavy due to large Unicode CLDR data sets.</p><p>The role of the product platform here: build a semi-static polyfill infrastructure that supports multiple browser versions and handles multiple locales. Provide guardrails — tests and lint rules to prevent usage of non-polyfillable features in both JavaScript and CSS.</p><h4>Prior Art</h4><p>Dropbox packages its bootstrapping layer, including polyfills, into targeted bundles — for example, bundles optimized for specific browsers and locales such as Chrome + English.</p><p>Yahoo used an internal system similar to Polyfill.io to serve dynamic polyfills based on user agent and requested features.</p><h3>IO (API Access)</h3><p>Client IO generally splits into two buckets: GraphQL (or similar) APIs, and non-GraphQL endpoints (REST, RPC, third-party APIs, etc.).</p><p>A product platform should provide a stable IO client that works across platforms (web, native, desktop shell), includes consistent tracing and observability hooks, and handles more restrictive IO environments — for example, when embedded in a third-party frame or a desktop wrapper like Electron or CEF.</p><h4>Prior Art</h4><p>Dropbox and Yahoo have dedicated IO drivers that offer first-class tracing and support constrained environments such as embedded web views and desktop containers.</p><h3>Persistence</h3><p>Browser-side persistence is limited and shared across all apps within the same domain. Without orchestration, features can starve one another trying to use storage. On top of that, some environments (like Mobile Safari’s private browsing) severely restrict APIs like localStorage, causing runtime errors if not handled gracefully.</p><p>The platform’s role: provide an orchestration layer for shared resources that fairly allocates storage across features and offers APIs that are safe across environments. Monitor shared resource usage — track quota usage and provide signals when features must degrade behavior.</p><h4>Prior Art</h4><p>Dropbox, which runs in multiple web contexts (browser, embedded web views, desktop CEF), has a shared storage layer to prevent cross-product starvation and gracefully degrade in restrictive environments.</p><h3>Telemetry</h3><p>Telemetry is critical for understanding how well an app behaves in the real world and for debugging issues. Most serious clients collect Real-User Metrics (RUM) and track them over time to detect regressions and correlate them with UX and business outcomes.</p><p>The platform’s responsibilities: provide a library to collect RUM including page load timings, time to interactive, and key interaction latencies. Provide a remote logging library that automatically attaches application dimensions (app version, browser version, URL, device info, etc.). Offer debug tooling for remote logging from clients, and sampling and filtering signals at scale.</p><h4>Prior Art</h4><p>Dropbox invested heavily in a 2-second Time To Interactive (TTI) goal, which required robust RUM collection, cohorting by device dimensions, and profiling bottlenecks. Frontend logs were mapped back to specific code lines and git SHAs, and error trends were tracked over windows like 5 minutes, 1 hour, and 1 day to distinguish local issues from global incidents.</p><p>Yahoo, as a media company, often treated Above-The-Fold time as a “golden metric,” closely tied to ad revenue and user engagement for content pages.</p><h3>Analytics</h3><p>Where telemetry focuses on application health, analytics focuses on user behavior: understanding funnels, abandonment, time spent, and action cohorts.</p><p>A product platform should provide instrumentation tooling — components that are automatically instrumented. For example, a Button that automatically tags itself based on its location in the render tree. It should also provide tests and lint rules to ensure important flows are properly instrumented and prevent shipping untracked CTAs or key paths.</p><h4>Prior Art</h4><p>Dropbox and Yahoo both integrated analytics into their UI components. When a button is clicked, analytics metadata is aggregated from its parent components, so the event clearly indicates which button in which context was clicked — e.g., a “pay” button inside the billing submenu versus a pay button in a different context.</p><p>For example:</p><pre>&lt;Section role=&quot;billing&quot;&gt;<br>  &lt;Menu role=&quot;submenu&quot;&gt;<br>    &lt;Button role=&quot;pay&quot;&gt;Pay now&lt;/Button&gt;<br>  &lt;/Menu&gt;<br>&lt;/Section&gt;</pre><h3>Localization</h3><p>Browsers do not ship with all locale data out of the box. Scandinavian and other less common locales are often missing or incomplete. As a result, localization needs are similar to the ECMA-262/polyfill story.</p><p>On top of locale data, product features almost always require translations and localizable messaging.</p><p>The platform’s responsibilities: selectively polyfill locale data — only ship the data needed for supported locales and balance bundle size vs coverage. Provide a library to aggregate, package, and distribute translation files. Provide a runtime ingestion library that loads translation bundles efficiently and supports fallbacks and late-loaded locales.</p><h4>Prior Art</h4><p>Dropbox and Yahoo both implemented selective locale polyfills combined with libraries like <a href="https://formatjs.github.io/">FormatJS</a> to handle extraction, packaging, and runtime consumption of translations.</p><h3>Configuration Management</h3><p>Configuration management provides a uniform mechanism for features to resolve configuration while staying consistent with the rest of the app.</p><p>The platform should define a set of ambient variables — region, locale, device type, app variant, environment, etc. It should provide tooling for consuming and serving configuration: feature-level config files keyed by those dimensions, and runtime resolution logic that sends down the right config during bootstrap.</p><h4>Prior Art</h4><p>Yahoo used a dynamic configuration service where features registered config files with dimensions like region, locale, device, and product. During bootstrap, the system resolved these dimensions and delivered the appropriate config to the client.</p><h3>Notable Mention: Dynamic Image Scaling</h3><p>Consumer products typically support many devices and form factors, from low-end phones to high-density displays and large desktop monitors. This creates a recurring need to scale images semi-dynamically.</p><p>The platform can provide a dynamic image scaler service that accepts a canonical asset and parameters like width, height, quality, and format, and produces variants optimized for different devices and network conditions.</p><h4>Prior Art</h4><p>Services like Uber Eats’ image pipeline expose APIs to resize and convert images on the fly (e.g., to WebP) with tunable quality and dimensions.</p><h3>Practical Priority Ordering</h3><p>In a greenfield or early-stage platform effort, it is tempting to build everything at once. In reality, prioritization matters. A pragmatic priority stack:</p><ol><li><strong>Telemetry</strong> — Observability (crashes, alerts, logging, app health) and RUM are essential when supporting consumer apps.</li><li><strong>Localization</strong> — Directly impacts global expansion and improves developer experience for shipping features in multiple markets.</li><li><strong>Analytics</strong> — Enables understanding of traffic cohorts and helps detect regressions in user flows.</li><li><strong>ECMA-262 / Polyfills</strong> — Dependent on telemetry and analytics to derive the actual browser/runtime support matrix.</li><li><strong>IO</strong> — Often there is an existing API client, but consolidating it into a platform layer is valuable.</li><li><strong>Configuration Management</strong> — May be partially solved by existing flagging or experimentation systems, but a unified solution across clients is useful.</li><li><strong>Persistence</strong> — Typically becomes important once multiple products compete for limited storage or when hitting environment quirks at scale.</li></ol><h3>Closing Thoughts</h3><p>These pillars show up repeatedly in mature consumer products, regardless of the domain. A strong product platform gives feature teams a stable, well-lit path for common problems, reduces incidental complexity and duplicated effort, and makes it easier to reason about performance, reliability, and behavior across a portfolio of apps.</p><p>Starting with a few high-leverage pillars — telemetry, localization, analytics — and iterating from there is often the most effective way to grow a platform that genuinely accelerates product development rather than slowing it down.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52ea5446dafc" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/product-platform-pillars-for-consumer-web-apps-52ea5446dafc">Product Platform Pillars for Consumer Web Apps</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Goodbye Lerna (sorta), hello Bazel]]></title>
            <link>https://levelup.gitconnected.com/goodbye-lerna-sorta-hello-bazel-f47ef1a01f73?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/f47ef1a01f73</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[bazel]]></category>
            <category><![CDATA[lerna]]></category>
            <category><![CDATA[node]]></category>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Thu, 16 Jul 2020 04:56:01 GMT</pubDate>
            <atom:updated>2020-07-17T12:49:06.805Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*RnCS09nWSrDjhtYEHZHV9Q.png" /><figcaption>Hello Bazel</figcaption></figure><h3>Context</h3><p>As a maintainer for <a href="https://github.com/formatjs/formatjs">formatjs &amp; react-intl</a>, I use <a href="https://lerna.js.org/">lerna</a> to manage the monorepo, which includes several tasks:</p><ol><li>Manage npm dependencies installation</li><li>Run npm scripts in a topologically-sorted order such as build or test</li><li>Create new GitHub releases</li><li>Generate CHANGELOGs</li><li>Publish to npm</li></ol><p>Lerna is pretty good at doing all of those 5 tasks but with the growth of the monorepo and the increasing amount of build time I figure we might need something that’s better suited for selective build. In the blog post I’ll walk you through my decision to drop <a href="https://lerna.js.org/">lerna</a> as a build system in favor of <a href="https://bazel.build/">bazel</a>.</p><h3>First, the result</h3><p>We now have a GitHub Action workflow with cache:</p><ul><li>CI: we’re on par with the older system with more correctness &amp; reproducibility (~14 minutes).</li><li>Selective Build (local): our build step went from 9 minutes to 50s — 10 minutes (depending on what changed).</li><li>Selective Test (local): our build step went from 4 minutes to 20s — 5 minutes (depending on what changed).</li><li>A lot less “<strong>works on my machine</strong>” issues.</li></ul><h3>Our build setup</h3><p>Our monorepo houses 8 ECMA402 <a href="https://formatjs.io/docs/polyfills">Intl polyfills</a>, each with its own dataset. The majority of our build time is spent on processing <a href="http://cldr.unicode.org/">CLDR</a> &amp; <a href="https://www.iana.org/time-zones">IANA timezone database</a>. The preprocessing step allows our runtime implementation to have high performance while maintaining ECMA402 spec compliance.</p><p>My 1st setup was to process everything from source, including remote artifacts fetching like IANA. This did not scale. On a cold GitHub Action CI build, our build step can take up to 30 minutes, with the bottleneck being zdump taking ~20 minutes for all the IANA timezones. This makes DevEx extremely painful, since a small doc change would result in a long CI wait.</p><p>My 2nd attempt was to commit in several generated artifacts, along with vendoring in IANA database. This is not uncommon in large scale CI build. git has built-in support to mark them differently via .gitattributes and GitHub also has support on top of that via linguist-generated &amp; linguist-vendored as well. This dramatically reduces our build time to roughly 15 minutes on a cold CI build, without a “golden” test to verify generated artifacts are up to date &amp; deterministic (in order to do so we have to re-run generation step which still takes forever).</p><h3>Our test setup</h3><p>Our test setup is quite standard:</p><ul><li>Unit test in jest</li><li>Selenium tests in karma powered by <a href="https://saucelabs.com/">Sauce Labs</a></li><li>Test262 compliance tests in test262-harness</li></ul><p>Our bottleneck is surprisingly not Selenium, but rather test262 since it’s a rather comprehensive test suite. For those who are not familiar with test262 it’s the TC39 compliance test suite that makes sure any spec implementation is spec-compliant. In order to prevent pollution it also spins up new v8 context for every test, making it correct yet slow (we still get timed out every now &amp; then).</p><p>Excluding the occasional timeouts our test clocks in at roughly 4 minutes.</p><h3>Selective Build</h3><p>Selective build is basically the ability to rebuild only part of the system that changed, thus improving performance. The tradeoff is typically correctness due to potential caching issues &amp; differences in build env setup. Lerna itself doesn’t have great (or maybe any) support for this other than <a href="https://github.com/lerna/lerna/tree/master/core/filter-options#--since-ref">--since</a> flag. This uses git ref as the filtering mechanism for what has changed. However, this has several issues:</p><ol><li>The unit of execution is still a package.</li><li>Transitive dependencies are opt-in. This results in missing build of deps that fail the overall build. Including those basically becomes a full rebuild.</li><li>Issues with git aliases that modify git outputs (as 1 of our collaborators had).</li></ol><p>At a high level, there are issues in both the underlying mechanism to detect change (e.g git ) &amp; the rebuild granularity (package).</p><h3>Say hello to <a href="https://bazel.build/">bazel</a>, specifically <a href="https://github.com/bazelbuild/rules_nodejs">rules_nodejs</a></h3><p>Bazel is a hermetic build system that is reproducible, deterministic &amp; fast. At the core of it are rule(how to build things) &amp;file (well, files). The mechanism of the build system is expressed in relationships between <strong>source files</strong> and <strong>generated files</strong>, expressed in <strong>rules</strong>. Bazel maintains its own output directory and does not pollute source tree (unless you tell it to). Since it’s hermetic it can safely cache build output &amp; test results, allowing me to selectively rebuild &amp; retest only that parts that changed.</p><p>Another class of problem that bazel solves in to maximize reproducibility, which effectively eliminates the “<strong>works on my machine</strong>” issue. We’re so used to the typical rm -rf node_modules &amp;&amp; git clean -fd and then reinstalling everything and try to re-run that it becomes the norm. This problem becomes a lot harder with OSS where collaborators communication is a challenge.</p><p>I had not heard of bazel before joining Dropbox and to be honest I was not a fan of it. It has a fairly high learning curve, ranging from the custom language <a href="https://docs.bazel.build/versions/master/skylark/language.html">Starlark</a> to its <a href="https://docs.bazel.build/versions/master/bazel-vision.html">philosophy</a> to its setup. It didn’t interop with the existing web build ecosystem very well, primarily due to implicit directory assumptions from existing build toolchains. However, once I got over <strong>part</strong> of the learning curve, the result was quite astonishing.</p><h3>Final Words</h3><p>Let’s be clear, bazel does not solve every use case out there. If your build/test is reasonably fast and you’re familiar with your existing toolchain, there’s no reason to switch. If you’re dealing with a monorepo with slow build/test loop this might speed you up tremendously.</p><p>Our current setup is over at <a href="https://github.com/formatjs/formatjs">https://github.com/formatjs/formatjs</a>. The community has been super helpful and active as well so feel free to join <a href="https://bazelbuild.slack.com/">bazel’s Slack</a> if you’re interested.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f47ef1a01f73" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/goodbye-lerna-sorta-hello-bazel-f47ef1a01f73">Goodbye Lerna (sorta), hello Bazel</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a TypeScript Custom AST Transformer (Part 3)]]></title>
            <link>https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-3-93b6238ae21f?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/93b6238ae21f</guid>
            <category><![CDATA[abstract-syntax-tree]]></category>
            <category><![CDATA[typescript-with-react]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Tue, 20 Aug 2019 03:04:22 GMT</pubDate>
            <atom:updated>2019-08-20T05:07:10.633Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qD3XTwpaTemydFrC2j0sfQ.jpeg" /></figure><p>This is the continuation of <a href="https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-2-5322c2b1660e">Part 2</a> and a deeper dive into a couple of advanced techniques I’ve used in writing my transformers.</p><h3>Non-TS import</h3><p>Non-TS import allows me to declare explicit dependencies on static resources such as CSS/png/svg, other non-JS files, and even external build toolchain. This is important when working in a large codebase since it’s easier to track down the dependency chain and refactor when anything changes in the dep graph.</p><p>This is the primary reason why I wrote <a href="https://github.com/longlho/ts-transform-css-modules">https://github.com/longlho/ts-transform-css-modules</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d192a7367dfac42195739c1831e20701/href">https://medium.com/media/d192a7367dfac42195739c1831e20701/href</a></iframe><p>The flow is fairly simple, whenever we encounter an import node with a moduleSpecifier ending in .css , we try to convert that node into a const foo = {className: &#39;1nahjk&#39;} hash.</p><p>The interesting part is that this allows you to run the PostCSS build pipeline inline as long as it’s <a href="https://medium.com/@longho/the-postcss-ecosystem-issue-b549c47b1a9">synchronous</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1183b8a4813134870836be858ce47f8/href">https://medium.com/media/b1183b8a4813134870836be858ce47f8/href</a></iframe><p>It’s also recommended to reference external CSS source in sourcemap as well, so that devtools can download it accordingly.</p><h3>Compiler Macro</h3><p>Compiler Macro is a fairly common concept in a traditional compiler. In the babel world, <a href="https://github.com/kentcdodds/babel-plugin-macros">babel-plugin-macros</a> does a fairly good job of introducing this concept. This allows you to do things like tagging a specific piece of code for extraction, pre-eval inline scripts, embedding compile-time variables and such.</p><p>From my perspective, import can be treated as a compiler macro of some sort where resources being imported can be processed differently. Another common example is string extraction for i18n, similar to <a href="https://github.com/longlho/ts-transform-react-intl">https://github.com/longlho/ts-transform-react-intl</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdf0ca668409d559c8de549ed835281b/href">https://medium.com/media/bdf0ca668409d559c8de549ed835281b/href</a></iframe><p>Everytime I encounter the _ symbol imported from ts-transform-react-intl, I marked the parameter as a MessageDescriptor and extract them to be translated. The post-transformed code becomes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eeecfa859b8fdce487cbe7ea2907942b/href">https://medium.com/media/eeecfa859b8fdce487cbe7ea2907942b/href</a></iframe><p>During the process, we can also run a linter against ICU syntax declared in the format to catch things like invalid syntax, emoji, vendor restrictions, and such.</p><h3>Utilizing TypeChecker</h3><p>Checking for _ in the import statement can be inaccurate, especially when aliases are involved: import {_ as _d} from &#39;ts-transform-react-intl&#39;</p><p>However, the TypeChecker API allows access to resolve aliases typeChecker.getAliasedSymbol and various other APIs that become very handy in connecting symbols and types together. This is definitely a lot more powerful than AST-only-based parser like estree.</p><h3>Scope Modification/Injection</h3><p>While the inline mod is very handy, there’re cases where you can optimize your code like hoisting variable to outside scope. While this can be done manually in source, it definitely takes a mental toll during development and code review while things like extracting const React elements can be done automatically: <a href="https://github.com/dropbox/ts-transform-react-constant-elements">https://github.com/dropbox/ts-transform-react-constant-elements</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8abc8c5269dbeae1ad7fe0c22d7f241b/href">https://medium.com/media/8abc8c5269dbeae1ad7fe0c22d7f241b/href</a></iframe><h3>General Flow</h3><h4>Find the correct lexical environment (scope)</h4><p>ctx.startLexicalEnvironment: add a lexical environment in the stack to store locale var/function declaration</p><p>ctx.suspendLexicalEnvironment and ctx.resumeLexicalEnvironment: typically when visiting a parameter list, preventing new scope creation.</p><p>ctx.endLexicalEnvironment: pop the stack.</p><p>ctx.hoistVariableDeclaration: record a var in the current lexical environment in the stack.</p><p>However, a new scope is created often so less control over where it gets hoisted (e.g entering a function body creates a scope and hoist only writes to current scope in the stack so no control to hoist).</p><p>In our use case, injecting to the top is perfectly fine.</p><h4>Find top node to inject this variable</h4><p>#!/env/node : below shebang (not in spec and might crap out in Esprima)</p><p>&quot;use strict&quot;; : below prologue directives (e.g: “use strict”)</p><p>// ts-lint:disable : below top level comments</p><p>import * as React from &#39;react&#39;; : below import React</p><h4>Find constant elements</h4><p>Since we can’t traverse backup, we’d have to do a full pass through the tree, filter out nodes that have <a href="https://github.com/dropbox/ts-transform-react-constant-elements/blob/master/src/transform.ts#L37">immutable attributes</a> and collect them</p><h4>Transform</h4><p>After collecting those nodes, we have to transform them into pre-declared variable names using ts.createUniqueName(prefix) for each hoistable nodes.</p><p>After that, we can append after top React import node by adding[reactNode, …hoistedVarStatements] . Finally, do another traversal and replace those ReactElement with the variables created earlier.</p><h3>Pipeline chaining</h3><p>You can even chain those AST transformers as the below example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bd22f8b7802534b2b192536b9ea4c2e/href">https://medium.com/media/7bd22f8b7802534b2b192536b9ea4c2e/href</a></iframe><p>Hope you guys learn something new! I certainly learned a lot throughout this process including reading the spec and diving into other build pipelines in the ecosystem.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93b6238ae21f" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-3-93b6238ae21f">Writing a TypeScript Custom AST Transformer (Part 3)</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing TypeScript custom AST Transformer (Part 2)]]></title>
            <link>https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-2-5322c2b1660e?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/5322c2b1660e</guid>
            <category><![CDATA[abstract-syntax-tree]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[compilers]]></category>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Sun, 31 Mar 2019 20:03:00 GMT</pubDate>
            <atom:updated>2020-09-05T04:03:07.241Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L51DrHKCIhieSyLJ" /><figcaption>Photo by <a href="https://unsplash.com/@toddquackenbush?utm_source=medium&amp;utm_medium=referral">Todd Quackenbush</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This is the continuation of <a href="https://medium.com/@longho/writing-typescript-custom-ast-transformer-part-1-7585d6916819">Part 1</a> where I talked about high-level TS compiler API boilerplate. This post will concentrate on the necessary tooling and walk through a simple AST transformer that I wrote.</p><h3>Compilation Process</h3><p>You can read more about this in details <a href="https://github.com/Microsoft/TypeScript/wiki/Architectural-Overview#overview-of-the-compilation-process">here</a>. But generally the process looks like this:</p><ol><li>Pre-processing: Read source file, aggregate dependencies, construct dependency trees</li><li>Parsing: Converts SourceFile into Node s.</li><li>Binding: Creates Symbol s from Nodes and construct TypeChecker to consolidate Symbols.</li><li>Emitting: Transforms Node s into JS and print out corresponding output files</li></ol><p>Right now AST transformers can only affect the emitting process without the capability to manipulate Symbol table or add new files into the dependency graph, which would require a more complex plugin system for the TypeChecker</p><h3>High level flow</h3><p>AST transformer operates like any node traversal mechanism (e.g XML parser). Every time the TS compiler visits a Node, it’ll trigger the transformer function to potentially replace it with a new node. The entry point for your transformer looks something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a926baadf9c33d57bd8e9cfa23c4f21b/href">https://medium.com/media/a926baadf9c33d57bd8e9cfa23c4f21b/href</a></iframe><p>where transformNode can recursively traverse down the tree by calling itself in conjunction with ts.visitEachChild . I’ll talk more about this during the walkthrough down below.</p><p>In order to transform a node, I typically follow these steps:</p><ol><li>Find the node I’m interested in (using ts.isFunctionDeclaration or the like)</li><li>Clone it with ts.getMutableClone</li><li>Manipulate certain attributes of the cloned node</li><li>Set source map text range with ts.setSourceMapRange</li><li>Return modified node</li></ol><p><a href="https://astexplorer.net/">https://astexplorer.net/</a> is a vital tool when it comes to figuring out the correct node to look for and transform to.</p><h3>Simple example walkthrough</h3><p>The easiest example is 1–1 node transformation since it typically does not involve scope manipulation, or bridging external compilation process (like PostCSS). <a href="https://github.com/dropbox/ts-transform-import-path-rewrite">https://github.com/dropbox/ts-transform-import-path-rewrite</a> is a transformer I wrote while at Dropbox that handles path rewrite, which allows our code to adapt to different build toolchain (absolute vs relative import/export path, or different npm node_module resolution).</p><p>The nodes I specifically cared about are:</p><pre>import * as foo from &#39;./foo&#39;; // Namespace import<br>import {foo} from &#39;./foo&#39;; // Named import<br>export {foo} from &#39;./foo&#39;; // Named re-export<br>export * as foo from &#39;./foo&#39;; Namespace re-export<br>import(&#39;./foo&#39;); // Dynamic Import</pre><p>The node breakdown looks something like:</p><pre>import * as foo from &#39;./foo&#39;; // ts.ImportDeclaration<br>* as foo --&gt; ts.ImportDeclaration.importClause<br>&#39;./foo&#39; --&gt; ts.ImportDeclaration.moduleSpecifier</pre><p>In this case, since we only care about rewriting the path, we can just manipulate moduleSpecifier in this node. Following the steps outlined above:</p><h4>Step 1</h4><p>Find the interested node</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82866a807fd0fa5e645ed82d27cf081c/href">https://medium.com/media/82866a807fd0fa5e645ed82d27cf081c/href</a></iframe><h4>Step 2</h4><p>Clone it</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb06326aac8ba48fce82e22f751758d3/href">https://medium.com/media/eb06326aac8ba48fce82e22f751758d3/href</a></iframe><h4>Step 3</h4><p>Manipulate its attribute, in this case we can replace the moduleSpecifier with the new path</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d54461692293f984858338e5e2efa47/href">https://medium.com/media/7d54461692293f984858338e5e2efa47/href</a></iframe><p>Couple of things to keep in mind:</p><ol><li>moduleSpecifier includes quotes since this is a AST node (e.g instead of ./foo its value is actually &quot;./foo&quot; )</li><li>You have to convert a regular string to its AST equivalence StringLiteral )</li></ol><h4>Step 4</h4><p>Update source map (optional)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70a84e8af749c4681659cb7d8c52d4ea/href">https://medium.com/media/70a84e8af749c4681659cb7d8c52d4ea/href</a></iframe><p>And finally return the new node.</p><p>In the actual production code, it’s a little more complicated since this has to handle the other 4 cases mentioned above. However, this should serve as the skeleton of how you can manipulate the emitting process.</p><p>Some other super simple but useful AST transformers include <a href="https://github.com/dropbox/ts-transform-react-jsx-source">https://github.com/dropbox/ts-transform-react-jsx-source</a> which injects __source to every JSX node so React can print out a better debug message…</p><p>In the next part I’ll walk through a slightly more complicated transformer and also talk about writing compiler macros!</p><p><a href="https://gitconnected.com/learn/typescript">Learn TypeScript - Best TypeScript Tutorials (2020) | gitconnected</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5322c2b1660e" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-2-5322c2b1660e">Writing TypeScript custom AST Transformer (Part 2)</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing  TypeScript custom AST Transformer (Part 1)]]></title>
            <link>https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-1-7585d6916819?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/7585d6916819</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Sun, 24 Mar 2019 19:42:11 GMT</pubDate>
            <atom:updated>2020-09-05T04:02:50.068Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*z4zjWqaZEHokAgR1" /><figcaption>Photo by <a href="https://unsplash.com/@utsmanmedia?utm_source=medium&amp;utm_medium=referral">Utsman Media</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Writing TypeScript custom AST Transformer (Part 1)</h3><h3>Background</h3><p>Babel’s ecosystem relies heavily on plugins which allows you to do a lot of things ranging from interop with other ecosystems (e.g CSS) to optimization (e.g hoisting/inlining). The TypeScript compiler, on the other hand, is designed to be a battery-included monolith that works great out of the box and thus, requires fewer plugins to build.</p><p>However, that partially changed back in TypeScript 2.3-ish when TS Compiler started supporting custom AST Transformer. This opens up a lot of possibilities to bring similar capabilities from babel to TS without having to chain these tools and also take full advantage of TS typechecker.</p><h3>Why did I care?</h3><p>Primarily if you’re interested in learning how toolchain works under the hood and general AST transformation, this could be interesting to you.</p><p>The real-world interesting aspect to me has been figuring out the boundary between what to write in source &amp; what compiler can take care of for you. This changes how I write code, specifically in manually optimizing certain code paths and enforcing convention. Understanding how this works and what can be done allows me to move some of those from manual code review to compile-time transformation.</p><p>Furthermore, this opens up opportunities to things like compiler macros and interop with other ecosystems while enforcing explicit dependencies in your source code.</p><p>This will be a multi-part series and part 1 specifically talks about the boilerplate.</p><h3>The boilerplate</h3><p>Unfortunately right now tsconfig.json does not allow specifying custom AST transformers. There’re a couple of alternatives you can utilize, each with its own caveat:</p><ol><li><a href="https://github.com/TypeStrong/ts-loader">https://github.com/TypeStrong/ts-loader</a> for webpack ecosystem</li><li><a href="https://github.com/TypeStrong/ts-node">https://github.com/TypeStrong/ts-node</a> for REPL</li><li><a href="https://github.com/cevek/ttypescript">https://github.com/cevek/ttypescript</a> for tsc replacement</li><li>Write your own compiler wrapper</li></ol><p>I’ll be talking about option 4 specifically in this post primarily because it’s more interesting and also has 0 outside dependencies other than typescript itself.</p><h3>Simple compiler wrapper</h3><p>TypeScript wiki already has a decent set of docs surrounding <a href="https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API">how to use the compiler API</a> that I highly recommend. For most of my TS Transformer projects, my boilerplate looks something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/564e68809027b7ae004a232340c4f9c1/href">https://medium.com/media/564e68809027b7ae004a232340c4f9c1/href</a></iframe><p>This wrapper can be broken down into 3 main pieces:</p><h4>Parsing tsconfig.json</h4><pre>ts.getParsedCommandLineOfConfigFile(configFilePath, undefined, host);  </pre><p>At a high level, TS Compiler takes in 2 main params: the list of files to compile &amp; the CompilerOptions to compile them with. You can choose to manually specify the options like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cafde2ee5c5b04c8d58e729630d41b71/href">https://medium.com/media/cafde2ee5c5b04c8d58e729630d41b71/href</a></iframe><p>However, there’re a couple of advantages to using getParsedCommandLineOfConfigFile</p><ol><li>Centralize config around tsconfig.json which means you don’t have to manually glob files to pass into the compiler.</li><li>This handles CLI overwrites.</li><li>There’re subtle differences in the final CompilerOptionsgenerated from this function compared to manually specifying one.</li></ol><h4>Create Program and emit results</h4><p>This is the bulk of the work where TS Program is constructed, starts compilation &amp; emit both .js and .d.ts to the specified output directory. This is also where you’d specify your custom AST transformer, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d588f917d07e83cc3909ed1d7109d847/href">https://medium.com/media/d588f917d07e83cc3909ed1d7109d847/href</a></iframe><p>TS itself comes with <a href="https://github.com/Microsoft/TypeScript/tree/master/src/compiler/transformers">a lot of ESNext -&gt; ES5 transformers</a> by default. The pipeline allows you to order your custom transformer in a specific way:</p><ol><li>before means your transformers get run before TS ones, which means your transformer will get raw TS syntax instead of transpiled syntax (e.g import instead of require or define )</li><li>after means your transformers get run after TS ones, which gets transpiled syntax.</li><li>afterDeclarations means your transformers get run during d.ts generation phase, allowing you to transform output type declarations.</li></ol><p>Most transformers I wrote are before transformers because that’s where most of the use cases have been. after &amp; afterDeclarations are necessary if you’re potentially modifying types. For example:</p><ol><li><a href="https://github.com/longlho/ts-transform-json">https://github.com/longlho/ts-transform-json</a> inlines JSON keys into output file, which requires modifying d.ts as well, otherwise the type declaration would still preserve the original json file import which makes it less useful.</li><li><a href="https://github.com/dropbox/ts-transform-import-path-rewrite">https://github.com/dropbox/ts-transform-import-path-rewrite</a> modifies import &amp; export paths, thus needing to have d.ts matching the new rewritten path or else type checking will fail.</li></ol><p>This is it for Part 1. I’ll start walking through the anatomy of a custom AST transformer &amp; walk through some transformers I wrote in subsequent parts :)</p><p><a href="https://gitconnected.com/learn/typescript">Learn TypeScript - Best TypeScript Tutorials (2020) | gitconnected</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7585d6916819" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-1-7585d6916819">Writing  TypeScript custom AST Transformer (Part 1)</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The PostCSS Ecosystem issue]]></title>
            <link>https://medium.com/@longho/the-postcss-ecosystem-issue-b549c47b1a9?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/b549c47b1a9</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[postcss]]></category>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Fri, 01 Feb 2019 14:28:51 GMT</pubDate>
            <atom:updated>2019-02-01T14:28:51.474Z</atom:updated>
            <content:encoded><![CDATA[<p>This doc serves to explain a fundamental issue in build system interop. This is primarily relevant for people who have to deal with PostCSS and respective build system for JS.</p><h3>Background</h3><p>For those who are not familiar with PostCSS it’s a CSS pre-processor which parses CSS into <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">AST</a> and has a plugin architecture that allows you to do node transformation. PostCSS used to be synchronous until circa 2014 when a bunch of plugins took a very long time to process (e.g img compression), it decided to support an async interface.</p><h3>The 1st core issue</h3><p>However, it does not mandate plugins to support both interfaces (sync &amp; async). The LOE to support both interfaces is also fairly high (almost a complete rewrite in cases where there’re a lot of File IO like postcss-import). Therefore, some has made a stance to only support async.</p><p>Sync obviously takes longer, but as it turns out async opens up a new class of problem.</p><h3>Cross-build system interop</h3><p>There’re a few big build systems that have considerable interop issue with PostCSS: webpack (along with it rollup, parcel… mainly the asset dist packager) &amp; babel (along with it typescript, browserify &amp; even jest).</p><ul><li>webpack is almost completely async from code transformation to chunk split/merge, which benefits from PostCSS async plugins. webpack also walks the dep tree from an entry point, thus having ability to stage transformations.</li><li>typescript and friends do not. These tools do not have an entry point and walks through a glob of files as input. The real problem is that <strong>require &amp; import have to be resolved synchronously</strong>.</li></ul><p>When you initialize PostCSS processor with a bunch of plugins, <strong>everything</strong> has to be sync for the pipeline to be sync, but not for async.</p><h3>The manifest of the issue</h3><p>The real problem is CSS Modules, where we’re allowed to do things like:</p><p>import * as css from ‘foo.css’</p><p>The classnames have to be resolved synchronously if used in typescript, babel or jest, which is not possible if running a live PostCSS pipeline. <a href="https://github.com/css-modules/postcss-modules/issues/74">postcss-modules, however, is async</a>.</p><p>Even if postcss-modules is sync, postcss-import isn’t (which led to forks like postcss-import-sync &amp; postcss-import-sync2 bc the author refused to support both interfaces). postcss-custom-properties is async but postcss-css-variables (which does very similar things) is sync.</p><p>Wait, but I’ve seen pipeline like this work before, how? This is likely due to:</p><ol><li>They have custom watered-down implementations. Take <a href="https://github.com/Connormiha/jest-css-modules-transform">https://github.com/Connormiha/jest-css-modules-transform</a> as an example. The source code literally just walk through the CSS AST tree &amp; pull out the class name manually, which has so many unhandled edge cases.</li><li>They use very old pre-async versions of those plugins (Some people still use postcss-import@7 and latest version is 10)</li><li>They use the underlying libs that those plugins use, but not the plugins themselves. See <a href="https://github.com/css-modules/css-modules-require-hook/blob/master/package.json">https://github.com/css-modules/css-modules-require-hook/blob/master/package.json</a></li></ol><h3>Foolproof solution</h3><p>The most foolproof solution is to run the PostCSS pipeline separately, dumps out all the artifacts you use in your code &amp; then do transformations on top of that. E.g: dump out all class names &amp; vars into a json file &amp; do transformation using those.</p><h3>The 2nd (not really core PostCSS) issue</h3><p>CSS as a standard only support 1 way to import: via a URL. However, some plugins allow you to resolve import locally or Node style (look at node_modules 1st). This turns PostCSS import resolution into kind of a nightmare. Why?</p><ol><li>It complicates the lookup hierarchy.</li><li>Some build system (like webpack) loads &amp; produce chunk in memory, which has no actual backing file, on top of that it supports import &#39;css-loader?foobarcustom syntax.</li><li>Node-style lookup hierarchy is also fairly complex, especially when it comes to symlinks.</li></ol><h3>Foolproof solution</h3><p>This is the reason why a lot of import-related plugins give you an escape hatch in the form of a resolveModuleByYourself callback because it’s sometimes impossible to try to figure out systematically. If the plugin you’re using doesn’t offer, make a suggestion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b549c47b1a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scaling React/Redux in Yahoo Finance]]></title>
            <link>https://medium.com/@longho/scaling-react-redux-in-yahoo-finance-88ef9e89b55c?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/88ef9e89b55c</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Sat, 02 Sep 2017 15:34:57 GMT</pubDate>
            <atom:updated>2017-09-02T15:38:20.471Z</atom:updated>
            <content:encoded><![CDATA[<p>Yahoo started its journey on modernizing its stack across the board roughly 3 years ago. While internally we have a stable framework on top of React &amp; <a href="https://github.com/yahoo/fluxible">Fluxible</a>, we decided to experiment with Redux architecture in Yahoo Finance Mobile Web &amp; potentially expand it to certain pieces of desktop. In terms of RUM, Finance Mobile Web commands roughly 2M DAU and generates 3.3M Daily PVs. It is a big enough space to explore new opportunities yet small enough to manage user-impacting risks.</p><h3>Architecture</h3><p>At a high level, we believe there are a couple of key components in building an isomorphic web application:</p><ul><li><strong>API Layer</strong>: In charge of fetching data from source of truth and update user data accordingly</li><li><strong>Data Massaging Layer</strong>: In charge of massaging data into UI-friendly business model objects</li><li><strong>State Management</strong>: Manages and synchronize states between various pieces of the app, including UI local states, caching layer, upstream API…</li><li><strong>Router</strong>: Handle route changes which include setup/teardown of certain application state</li><li><strong>Rendering Engine</strong>: Renders DOM and manages local UI states (such as selected, active…)</li></ul><h3>API Layer</h3><p>We decided to build our API layer on top of ES6 fetch thanks to its modern API using Promise. While making a simple call isn’t hard, in a production system a lot of complexity comes from a combination of contextualizing the request, measuring, caching and the likes.</p><p>In order to keep the layer lean yet extensible, instead of integrating performance labels, proxies, fallbacks… directly into the layer, we offset the complexity into the message action instead.</p><p>The core action throughout the application is a POJO and has exactly 4 fields:</p><pre>export interface FSAction&lt;T, U&gt; {</pre><pre>  /**</pre><pre>   * Type (required)</pre><pre>   */</pre><pre>  type: string</pre><pre>  /**</pre><pre>   * Payload, if error is true, then payload is the Error</pre><pre>   * Otherwise it&#39;s w/e</pre><pre>   */</pre><pre>  payload?: T</pre><pre>  /**</pre><pre>   * Metadata that comes w/ the payload</pre><pre>   */</pre><pre>  meta?: U</pre><pre>  /**</pre><pre>   * Error obj</pre><pre>   */</pre><pre>  error?: any</pre><pre>}</pre><p>For example, instead of bundling a caching layer into our API layer, we parse and expose Cache-Control into meta field. Instead of bundling performance logging, we capture response timing and expose performance field in meta field also. Both pieces of information will be delegated to another centralized layer that examine the action and determine the appropriate behavior.</p><pre>export interface CacheControl {</pre><pre>  /**</pre><pre>   * Max-age from cache-control spec</pre><pre>   */</pre><pre>  ‘max-age’: number</pre><pre>  /**</pre><pre>   * Stale while revalidate</pre><pre>   */</pre><pre>  ‘stale-while-revalidate’?: number</pre><pre>}</pre><pre>export interface PerformanceTiming {</pre><pre>  label?: string</pre><pre>  start?: number</pre><pre>  stop?: number</pre><pre>  failure?: boolean<br>}</pre><h3>Data Massaging Layer</h3><p>This is primarily Redux reducers so nothing interesting. We do HTML decoding &amp; number manipulation and such in this layer.</p><h3>State Management (Scaling Redux)</h3><p>Thanks to Redux’s centralized middleware flow, we essentially compartmentalize complicated data flow into a bunch of middlewares. An example simplified version of an API call lifecycle is something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HCGb-uiXpIaLYdTfVcNhfQ.png" /><figcaption>Simplified API Flow</figcaption></figure><p>Some of the sample key components:</p><ul><li><strong>API Middleware sandwiched between 2 Cache Middleware (Get/Set)</strong>: An API action flows through CacheGet middleware 1st, then ApiMiddleware, then CacheSet middleware. This allows an opportunity for the Caching layer to modify request params before actually fetching data. E.g: if we ask for 5 things and 3 things are in cache, it’d only fetch 2 things. Caching middleware also provides opportunity to use different cache backends ( localStorage vs memcached vs in-memory)</li><li><strong>Beacon Middleware at the end</strong>: This allows us to beacon not only every action that has an error, but also capture every performance timings happening in between based on the PerformanceTiming payload described above.</li></ul><p>The middleware flow also acts as a gateway for foreign systems to hook into our centralized Store while potentially maintaining its own internal state. For example, our real-time streamer exposes a middleware that allows components to (un)subscribe, while pumping data into the centralized Store.</p><p>In production, we have roughly 10 different Redux middlewares handling a myriad of things:</p><ol><li><strong>Config Middleware</strong>: Pumps config into action</li><li><strong>Contextualize Middleware</strong>: Pumps user-sensitive data into action</li><li><strong>CacheGet</strong></li><li><strong>API</strong></li><li><strong>CacheSet</strong></li><li>Roughly 4 middlewares from foreign systems</li><li><strong>Beacon Middleware</strong></li></ol><h3>Router</h3><p>When we initially started the experiment, our first goal is to introduce an architecture that is isomorphic (universal) to maximize logic reusability. The immediate challenge is the difference between server and client. Server aims to be as stateless as possible and put an emphasis on handling the most number of requests while client is stateful by nature, storing user state locally to facilitate a faster user experience. Replicating all client states on the server would heavily increase memory usage and subsequently put stress on GC. This approach is also prone to memory leaks due to the nature of socket reuse on Node.js. Therefore, we decided to model client app after server request/response lifecycle instead.</p><p>In our implementation, we built a “server shim” layer to normalize the client/server differences. First of all, such layer would establish the practice of treating each page navigation as a request/response cycle. This provides a good level of abstraction and normalize routing methodologies. It would also populate relevant data from window to replicate a basic request object, such as cookie, user-agent, router…</p><p>Once the routing abstraction is established, we declared a straightforward routing life cycle:</p><ul><li><strong>contextualize</strong>: focuses on extracting out information from request headers/cookie to provider a preliminary context</li></ul><pre>export interface Req extends Request {<br>  context: Context<br>}</pre><pre>export interface Context {<br>  // “0/1”<br>  authed: string<br>  bucket: string<br>  devFlags: DevFlags<br>  env: string<br>  intl: string<br>  config: ApiConfigs<br>}</pre><ul><li><strong>preinit</strong>: fetches the initial data for the page, enough to determine the statusCode, certain SEO info and enough information to resolve to a certain layout.</li><li><strong>renderHead</strong>: sets response’s statusCode, headers and potentially render/dehydrate initial data. It will also render out certain &lt;script&gt; to allow pre-fetching content.</li><li><strong>init</strong>: fetches the rest of the data for page and populate corresponding sections, ready to be rendered.</li><li><strong>renderBody</strong>: renders the rest of the page with the rest of dehydrated data and potentially <strong>multi tree React</strong>.</li><li><strong>postInit</strong>: is client-only, in case we defer rendering.</li></ul><p>The clear separation of preInit phase &amp; init phase allow more tuning for Above-The-Fold experience and optimization opportunities. It also allows us to easily shuffle different phases to handle Failsafe/Crawler.</p><p>For example, full server side rendering means changing the order of those phases to:</p><ol><li>contextualize</li><li>preinit</li><li>renderHead</li><li>init</li><li>postInit</li><li>renderBody</li></ol><p>By moving renderBody to the bottom we force all Below Fold API calls to finish before rendering, making it a full server-side render.</p><p>On the other, a full client-side render would be to move renderBody to above init and flush early.</p><h3>Rendering Engine (Scaling React)</h3><p>While React excels on the client side thanks to its DOM-ding mechanism, since server is stateless, its performance has been an issue on the Node.js side. The first thing we decided to do was to measure its performance using a mock application state, thanks to our State Management philosophy. Using this data point, we can safely adjust the ratio between server-rendered components and client-rendered components, thus balancing UX and scalability.</p><pre>Nebula@0.1.457</pre><pre>rehydration x 2,340 ops/sec 6.53% (53 runs sampled)<br>navrail x 2,870 ops/sec 4.36% (57 runs sampled)<br>LoadingBar x 2,813 ops/sec 2.16% (64 runs sampled)<br>UH x 218 ops/sec 5.68% (50 runs sampled)<br>hero x 48.76 ops/sec 6.60% (45 runs sampled)<br>main x 1,174 ops/sec 6.39% (56 runs sampled)<br>lightbox x 2,762 ops/sec 3.19% (59 runs sampled)<br>feedback x 710 ops/sec 2.70% (61 runs sampled)</pre><pre>CPU time for rehydration: 0.42734133643922173ms<br>CPU time for navrail: 0.3483773579622734ms<br>CPU time for LoadingBar: 0.35554301294768764ms<br>CPU time for UH: 4.596207153906486ms<br>CPU time for hero: 20.508138746296293ms<br>CPU time for main: 0.8519489946518364ms<br>CPU time for lightbox: 0.36210397088059915ms<br>CPU time for feedback: 1.407506364826446ms</pre><pre>total CPU time per req: 28.857166937910844ms<br>max theoretical rps/core: 34.65343642886367</pre><p>Based on our max rps/core we can safely do capacity planning and adjust our caching TTL for certain components.</p><p>However, we’ve also made the decision to drastically reduce the scope of React integration within the app itself. This means instead of heavily relying on React component life cycle to trigger certain behaviors, we have been treating React as a pure stateless rendering engine.</p><p>This philosophy allows us to safely and easily mix in multiple rendering engines where it’s best needed, such as D3/Canvas for chart rendering and Ads without having them depend on each other, thus reducing complexity. There have also been several more lightweight Virtual DOM implementations that we would like to explore, in case React’s direction does not match with our own. This mental shift allows us to separate out presentation layer and make it reusable across properties.</p><p>Besides, in production, every sizable component flows through a centralized caching layer that caches the HTML based on a combination of props &amp; unique dimensions that prevents us from having to re-render things.</p><h3>Memory &amp; Garbage Collection</h3><p>Since the server logic is constructed to be stateless, there is always a need to create new application context for every incoming request, and as soon as it finishes the server should be able to clean up all the resources associated with and consumed by that particular request. It will be a significant problem to the heap memory if the server fails to correctly garbage collect the unused memory. By using immutable data structure as well as custom control ow of the application, YFinance architecture minimizes the exposure of the original request object, thus reducing the risk of memory leak. As a result, the typical GC scavenge cycle takes lesser time to run and the frequency of the mark-sweep cycle is lower due to the improved result from the scavenge one. This is crucial to server scalability because the more frequent and the longer each GC cycle takes, the lower the throughout of the server to handle incoming requests because the processes will be blocked during GC cycle due to the single-threaded nature of a Node process.</p><h3>Some Graphs (on 1 box)</h3><p>There’re some graphs that we cannot publish, but our Above-Fold-Time reduces by <strong>2x</strong> compared to our previous legacy stack and our RPS is also <strong>~3x</strong> compared to our legacy stack also.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*POTvtNWmcHIBI0ns3j2Sig.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iEL4MBEMKoMhnWECsDrERA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EifKzjHfulzUIf2vPkgODA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iKiv5QRRYwb2AzTlZxyIQg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88ef9e89b55c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Yahoo Finance Portfolio: Rendering high performance financial data table in React]]></title>
            <link>https://levelup.gitconnected.com/yahoo-finance-portfolio-rendering-high-performance-financial-data-table-in-react-634fe2d6a381?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/634fe2d6a381</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[finance]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Sun, 13 Aug 2017 18:29:36 GMT</pubDate>
            <atom:updated>2020-08-21T01:57:53.011Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*npwjNvplxxjvTzNmCHo74g.png" /><figcaption>Yahoo Finance Portfolio profile</figcaption></figure><p>60 frame per second (fps) is the pinacle of any front end rendering infrastructure. The key to achieve a high frame rate lies in the combination of high-performance rendering functions and workload orchestration on the main thread.</p><p>Rendering large Excel-like tables is not a new problem. However, the constantly changing landscape of Front End browsers and capabilities allows us to explore new venues in solving this problem with a different set of tradeoffs. We’ve exhaustively explored pre-existing solutions from Facebook and Google but were not able to settle on one given our use case. Therefore, we’ve decided to build an in-house solution and establish a set of best practices that can be commonly used across Yahoo.</p><h3>Background</h3><p>Portfolio is a utility within Finance that allows user to create a spreadsheet consisting of a customized set of data columns &amp; a list of user-specified symbols. We started approaching this problem by identifying our 99%-percentile usage pattern. This puts us in the range of 200–250 tickers with roughly 20 columns (4000–5000 cells).</p><p>General user feedbacks also call for support for copy/paste &amp; print of the whole table, making traditional cell reuse/treadmilling too complex. On top of that, as part of the newer experience, 1st column (symbol column) &amp; header row are fixed within viewport, posing a big challenge in architecting composite layers to minimize re-layout during usage.</p><h3>Existing Solutions</h3><p>The most popular open source solution is <a href="https://github.com/facebook/fixed-data-table">Facebook Fixed Data Table</a> and proprietary one is Google Sheets.</p><p>Facebook datatable utilizes <a href="https://en.wikipedia.org/wiki/Treadmilling">treadmilling</a>, a common practice of rendering a fixed set of rows and moving out-of-view front rows to the back as the user scrolls.<br>Since not all cells are actually rendered, this prevents users from copy/paste/print the whole table. Besides, this solution has a low frame rate, hovering at around 20–30fps with heavy reflows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7yQwQWRg0qN6PDb3IDnLJg.png" /><figcaption>Fixed Data Table perf profile</figcaption></figure><p>Google solution uses the same approach but has a better performance profile thanks to using canvas as its backbone. Google Sheets does layer a table of elements on top of its canvas table primarily for cell selection &amp; user interactions. This solution is not open-source and very complex to implement. Its performance under constantly streaming dataset is also unverified.</p><h3>Our general approach</h3><p>At a high level, we identified the key optimization ranging from structure to micro operations:</p><ul><li><strong>Benchmarking/Profiling Infrastructure</strong>: We cannot safely move forward without a stable benchmarking/profiling infrastructure. Browsers such as Chrome offer a great toolset of profiling ranging from CPU/memory usage to flamegraphs of time spent in UI recompositing.</li><li><strong>General Layout</strong>: Due to sticky row/column behavior, these elements require special treatments that not only affects markup structure but also styling.</li><li><strong>Cell Rendering Optimization</strong>: At the end, it comes down to the combination of individual cell rendering performance that mostly affects the whole table. We spent a significant time on tuning each cell type individually.</li></ul><h3>Benchmarking/Profiling Infrastructure</h3><p>There are several Key Performance Indicators (KPIs) that we rely on:</p><ul><li><strong>CPU Time</strong>: This can be measured by repeatedly rendering a cell of a certain type in a controlled environment. This practice is also called micro-benchmarking. We utilize <a href="https://benchmarkjs.com/">benchmark.js</a> along with React string rendering as a proxy to gather this information.</li><li><strong>CPU Usage</strong>: Streaming ticket data points require re-rendering of certain cells. Chrome DevTool profiling in combination with task manager provide us with the approximation to this number.</li><li><strong>Memory Usage</strong>: Since the dataset is changing in realtime, we want to make sure this does not affect GC cycle in the browser, which can cause thread halt. Chrome DevTool offers this capability as well.</li><li><strong>Layout/Composition Time</strong>: Time spent re-calculating layouts which is inherently expensive. This will negatively affect frame rate. Chrome DevTool flamegraph profiling provides us with the breakdown in time spent on UI layout calculation.</li></ul><h3>General Layout</h3><p>Due to header/first column stickiness, we started with cloning those elements into a separate layer and laying them on top of the table.</p><p>The advantage of this approach is control over layout and style recalculation. This allows us to specifically throttle/batch/split these operations based on CPU availability.</p><p>The disadvantage is that we would have to spend extra CPU cycle synchronizing these elements’ dimensions with the core table due to its flexible width nature and synchronizing scroll position.</p><p>However, this can be mitigated with narrowing down the set of events that might affect dimensions change. These are resize, data column change events, ticker change events and navigation events. By knowing when these happen, this gives us control over when to best re-calculating these layouts and minimize the cost.</p><p>However, synchronizing position is extremely costly since debouncing/throttling them will result in out of order glitch and doing them in realtime affects scroll performance. In the end, we decided to only separate out header row and leave first column in as part of the natural flow of the core table using position: sticky with <a href="https://github.com/wilddeer/stickyfill">polyfill</a>. After that, we preventDefault the horizontal scroll behavior of the table and manually calculate scroll position and scroll velocity to ensure columns are always in sync.</p><h3>Cell Rendering Optimization</h3><h4>General Cells</h4><p>We started with the baseline of our current cell rendering CPU Time:</p><pre>Row w/ 62 cells x 190 ops/sec ±3.06% (73 runs sampled)<br>CPU time for Row w/ 62 cells: 5.253441030475995ms</pre><p>We decided to experiment with removing React overhead by changing Component to functional StatelessComponent to minimize the createElement overhead on certain cell types. The assumption is that those cell types have a very high probability of value change during streaming, thus rendering the difference check irrelevant.</p><pre>Row w/ 62 cells x 344 ops/sec ±3.72% (72 runs sampled)<br>CPU time for Row w/ 62 cells: 2.908777648130453ms</pre><h4>Number Cells</h4><p>Since the majority of our cell types are numbers that requires internationalized formatting but have very similar setups, we decided to memoize the formatting mechanism by serializing constructor options into a key. This tremendously reduces the need to reconstruct the formatter.</p><pre>Row w/ 62 cells x 562 ops/sec ±2.52% (80 runs sampled)<br>CPU time for Row w/ 62 cells: 1.7808359339126558ms</pre><h4>Graphic Cells</h4><p>Some of our cells are custom graphics (such as charts and number ranges). These were initially rendered using a combination of regular DOM elements and SVG. However, while SVG is flexible and vector-based, it requires a different composition layer. This constantly gets recalculated during scrolling/resizing/streaming. We decides to rewrite them into canvas-based implementation to take full use of rasterization.</p><p><strong>Colored Streaming Cells</strong></p><p>Colored Streaming Cells are special streaming cells that “flashes” (background-color change) to red/green depending on whether the change is negative or positive. While this might be simple, background-color is not easily <a href="https://www.html5rocks.com/en/tutorials/speed/high-performance-animations/">GPU-accelerated</a> animated but opacity is.</p><p>We decided to explore a “sandwich” approach of creating 3 separately layers for this cell, 1 for green, 1 for red and 1 for the actual content. The 2 background layers are created using pseudo element :before and :after and are animated based on 3 keyframes to mimic bezier-curve-like animation.background-color are also fixed at red/green and opacity is being animated instead. Keyframe as setup similar to as follow:</p><pre><a href="http://twitter.com/keyframes">@keyframes</a> trendAnimUp1 {<br>  25% {<br>    opacity: 1;<br>    display: block;<br>  }<br>  75% {<br>    opacity: 1;<br>  }<br>  100% {<br>    opacity: 0;<br>    display: none;<br>  }<br>}</pre><p>The key techniques here are:</p><ul><li><strong>Manipulate display attribute</strong>: The downside of creating extra layers is the cost of compositing them. By changing display to none we essentially remove those layer once they’ve done animating, thus reducing complexity.</li><li><strong>Rotate animations</strong>: Since keyframes cannot be easily pause/resume/reset, we create duplicate keyframes and use class to switch between them, effectively triggering keyframe reset.</li></ul><p>Using these techniques we were able to convert <strong>32.04ms</strong> CPU time to <strong>9.72ms</strong> (-68%) CPU time &amp; <strong>1.2ms</strong> GPU time.</p><h3>Results &amp; Key Learnings</h3><p>Our final fps for rendering a constantly streaming table for currencies with ~2000 rows roughly <strong>54fps</strong>. In the process, we have also found some key learnings:</p><ul><li><strong>Remove React styling mechanism in favor of pure CSS</strong>: Rendering styles and icon and React introduces overhead in both CPU &amp; GPU time, instead inlining SVG into CSS instead.</li><li><strong>Rasterization helps FPS tremendously</strong>: By switching from vector-based SVG to rasterized canvas we were able to remove a huge chunk of layout composition from our flamegraph. Animations in SVG is also very costly.</li><li><strong>Optimize for GPU-acceleration</strong>: By shifting heavy UI tasks from CPU to a spread of CPU &amp; GPU in our colorized value use case, we were able to shave down CPU time in main thread significantly.</li><li><strong>Memoization as much as possible</strong>: By memoizing as much as possible within our requirements, we were able to reuse costly internationalized formatters.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=634fe2d6a381" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/yahoo-finance-portfolio-rendering-high-performance-financial-data-table-in-react-634fe2d6a381">Yahoo Finance Portfolio: Rendering high performance financial data table in React</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Sorry, but I’m not a Manager”]]></title>
            <link>https://medium.com/@longho/sorry-but-i-m-not-a-manager-e82c9f95ab6f?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/e82c9f95ab6f</guid>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Tue, 10 Mar 2015 19:03:19 GMT</pubDate>
            <atom:updated>2015-03-10T19:03:19.094Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ty3EJ4TcGxRMlbItqvO4-A.jpeg" /></figure><p>Ever since, or even before, stepping foot into the industry, I’ve always been led to believe that going into management is the natural progression of one’s career. I think it somehow stems from the notion of increasing responsibility, in the sense that as I progress career-wise, my level of responsibility also expands. However, since my capability of handling complexity is finite, delegation is the de facto pattern of maintaining a growing matrix of subsystems. I believe that a well-architected system consists of various independent and well-defined subsystems, such as each can function independently within its scope given a set of specific instructions.</p><p>Management, to me, is the implementation of such principle on a personnel level, where the manager acts as the point of entry (or <strong>Main</strong> module) that initializes and manages the whole system. However, this is also where things break down in terms of career advancement. When a system needs re-factoring, I strongly believe it’s not natural to take 1 of the submodules to become the main one. Submodule &amp; main module can have a widly different set of attributes, characteristics and functionalities that sometimes facilitate such transition, but not all the time. I believe this is exceptionally true in fields where the boundary between people skills &amp; technical knowledge is rather clear.</p><p>I have the utmost respect for a good Manager and I believe not everyone possesses such skills to become one. It is literally a separate field in and of itself. There’s a drastic shift in focus, going from technical expertise to people management, which includes understanding how motivation and meritocracy works, managing expectations and politics, etc… Come to think of it, to me, it really boils down to solving very complex problems w/ a large set of undeterministic variables, which are people. Everyone is unique and probably gets motivated in his/her own way, thus balancing all of these factors in theoretically a set of equations is indeed very complex.</p><p>I’ve had my first taste of management and it’s certainly different. Technical knowledge was no longer my #1 priority, my team was. It’s all about protecting them from unnecessary meetings, finding interesting and challenging projects and sometimes settings technical directions. While I tried my best to shift my focus towards such tasks, I realize that those are not the things that motivate me, at all. I want to be the one doing the cool and challenging stuff. I was selfish in that sense and at that moment I know I was not ready to be a manager, right now, or even in the next 5 years.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e82c9f95ab6f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serving 2TB/day of dynamically-scaled images: Part 2]]></title>
            <link>https://medium.com/@longho/serving-2tb-day-of-dynamically-scaled-images-part-2-cc654603cc7b?source=rss-bb3ff7847246------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc654603cc7b</guid>
            <dc:creator><![CDATA[Long Ho]]></dc:creator>
            <pubDate>Sun, 08 Mar 2015 16:48:43 GMT</pubDate>
            <atom:updated>2015-03-08T16:48:43.929Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s all about cache tuning and designing your API design to facilitate such.</p><p>If there’s 1 thing I learnt about scalability, it’s definitely not designing/implementing the fastest algorithms, but rather boosting my cache hit ratio &amp; avoiding cache-fragmentation.</p><p>Soon after building my scaling worker I realize that it is indeed also the bottleneck of the whole system, mainly due to scaling images consume a rather large amount of resources, both on CPU, memory &amp; potentially IO. It’s actually rather interesting to read GM/IM source code to figure that out. It uses PixelCache to, most of the time, cache the full image, unless the image is a stream that supports seeking. This takes a rather heavy toll on memory and has a direct correlation to image size. Unfortunately the majority of images I was deadling with are hi-res ones from record labels, making the worker highly unscalable even in a multi-core environment.</p><p>Taking 100% of raw traffic becomes impossible, or at least w/ the amount of hardware that we have. I know we have to cache, very heavily, in the 99% range. The problem domain turns into cache optimization, which focuses a lot on IO tuning/API design which are what I had little knowledge of. Scary shit!</p><p>In my case the URL serves as my API, and also the cache key. Minimizing its cardinality becomes 1 of the top priotities for me at the time. URL parameters, however, are unordered, which means every enumeration of the supported key/value will potentially result in a cache miss. However, turning them into a ordered path greatly reduces the flexibility and maintainability of the API. Maybe I should have process the URL, order them &amp; hash instead? That’ll have to be implemented on varnish VCL, which affects maintainability also.</p><p>The other challenge I faced was the fact that, among the myriad clients that I supported, some of them doesn’t handle <strong>301/302</strong>. How important can that be? Issuing a redirect is <strong>absolutely</strong> more efficient than telling worker to fetch image, scale &amp; pipe back. Even though the # of clients that do that was small, I couldn’t risk correctness for efficiency. Maybe I should have done user-agent/capability detection to gracefully handle that case? However, on mobile devices bandwidth/connectivity is rather scarce so 301 will result in 2 instead of 1 call, which might be an issue also.</p><p>Having HTTP as the protocol there’re definitely a # of <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html">headers</a> you should consider:</p><ul><li>Cookie/Set-Cookie: I was handling stateless images, not stateful client request so wiping those is definitely the 1st step.</li><li>Cache-Control/Expires: There’re so many ways to configure them. I did tune them to handle client/proxy differently via having <strong>max-age </strong>and <strong>s-maxage</strong> be different. Note that s-maxage affects all subsequent proxy layer so stuff might live longer than expected.</li><li>Vary: This header is powerful, yet can cause serious cache fragmentation. Therefore, a lot of CDNs don’t support them, due to a lot of web frameworks automatically set <strong>Vary: User-Agent </strong>by <a href="http://www.rimmkaufman.com/blog/vary-user-agent/30112012/">default</a>. This affects cache rate and thus takes a toll on CDNs.</li><li>Age: Varnish automatically set this header for cache objects, and surprisingly as I found out affects Akamai also. If your Varnish TTL is greater than Akamai, thus <strong>Age</strong> can be greater than Akamai’s TTL, Akamai will incorrectly invalidate the response. If you still wanna know the Age, set a custom header like <strong>X-Foo-Age = Age</strong> to avoid collision.</li><li>Expires: I didn’t use this and rely on <strong>Cache-Control</strong> instead. There’s no point having multiple headers doing the same job.</li><li>ETag/If-Modified-Since/Last-Modified: This provides an intelligent way of checking for object freshness &amp; hash and fortunately a lot of caching proxies seem to handle those by default.</li></ul><p>Outside of headers, cache-tuning also involves setting TTLs for different status code:</p><ul><li>2xx: This is a no brainer &amp; your default use case. If the request got successfully handled, cache it, cache it gooood.</li><li>301, 302: 301 is permanent redirect so it should have a rather longer TTLs and 302. This is your plan B to minimize cache fragmentation also, but rewriting URLs to a previously-cached one.</li><li>400, 404, 4xx: Cache this, period. 4xx range is Client Error, which basically includes all the gibberish request you cannot understand, and thus cannot service. Caching those ensures a bad/faulty client doesn’t DDOS you just because they send the wrong params, or requesting something that doesn’t exist. In practice I actually cache this as long as 2xx. Once the faulty clients fix their params, the URL will result in a different cache key anw.</li><li>500, 503, 5xx: I generally put a grace cache (short TTL) on this, mainly as a way of throttling requests that trigger a bad path in your application. I also log those the most verbosely for further debugging.</li></ul><p>TTLs are subject to business requirements, because they control how often content gets refreshed. You can be very liberal in your TTL if you have a mean to clear them on demand with great flexibility. I’ll talk about Varnish/IO tuning in the next part.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc654603cc7b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>