<?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 Pat Migliaccio on Medium]]></title>
        <description><![CDATA[Stories by Pat Migliaccio on Medium]]></description>
        <link>https://medium.com/@pat_migliaccio?source=rss-209d6628cb2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*N4ipCBkPjDuyhdIaCn-Blw.jpeg</url>
            <title>Stories by Pat Migliaccio on Medium</title>
            <link>https://medium.com/@pat_migliaccio?source=rss-209d6628cb2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:21:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pat_migliaccio/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[Code Collaboration: Persisted State Sharing]]></title>
            <link>https://medium.com/@pat_migliaccio/code-collaboration-persisted-state-sharing-86e08971effa?source=rss-209d6628cb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/86e08971effa</guid>
            <category><![CDATA[collaboration]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[developer-tools]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Pat Migliaccio]]></dc:creator>
            <pubDate>Tue, 31 May 2022 00:17:50 GMT</pubDate>
            <atom:updated>2022-05-31T00:19:26.389Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*d69slwOKUEGEBEU7jUiY-w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@jancanty?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Jan Canty</a> on <a href="https://unsplash.com/s/photos/persistence?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Opening a new writing segment called “Something I should build one day” that allows me to expound on (with a bit of ambiguity) a product/project/thing that could be brought into existence. With the ethos being something that I should do (probably won’t), it allows me to shout into the void and flex some creative thought patterns in product development. Think of it as a loose technical or product memo and not a pitch deck. (Look away, there may actually be no market here.) There’s also the potential to write a few more of these or not. Anything’s a dice throw at this point.</p><p>With much of my time spent writing and sharing code artifacts in various languages and forms, I’ve felt devoid of the ability to test, share and execute “snippets” (awful term) with others and plainly myself at a future point in time. Many development tools converge in this area, but are frankly focusing on different outcomes. Thinking about the notion for several months now, I might ultimately have some modicum of material to explain.</p><h3>Pat Migliaccio on Twitter: &quot;Does it save the state of the shell? / Twitter&quot;</h3><p>Does it save the state of the shell?</p><h3>Looking Back</h3><p>Historically, collaborative software development has progressed through different mediums characterized in the following non-exhaustive list.</p><p><strong>Plugboard wiring</strong></p><ul><li>Introduction of manual reprogramming</li><li>e.g. ENIAC</li></ul><p><strong>Punched cards and tapes</strong></p><ul><li>Brought about portability, with static reproducibility</li><li>e.g. IBM100</li></ul><p><strong>Files</strong></p><ul><li>Promoted sharing and iterative development</li></ul><p><strong>Version Control Systems (VCS)</strong></p><ul><li>Offered change management and distributed collaboration</li><li>e.g. svn, git</li></ul><p><strong>Online or cloud development environments</strong></p><ul><li>Real-time, multiplayer collaboration with a low-barrier to entry and minimal compute resources required</li></ul><p>Each being a natural progression beyond the prior model, innovating on the basic notion of an engineer defining a set of logic that can then be read, manipulated and executed by the next.</p><h3>A Background Scenario</h3><p>Let’s start with a problem to align on the current state of production software development. We have X, a full-stack engineer, probably at an early to mid-stage organization, who works across a variety of team functions. Over the course of a general working day, they may write some code, review some code, collaborate on a feature or fix a bug. Most of their toolset lives locally in their interactive development environment (IDE) or collaboratively in a relatively static venue like their shared code repository (e.g. GitHub). They augment their workflow with a messaging application (e.g. Slack), project management tools and some note-taking or documentation software.</p><p>Suppose that X would like the opinion of Y (a second engineer) on a bug they are working to resolve. For effect, Y has never worked in this part of the system before but is knowledgeable in the coding paradigm and may provide valuable insight to X. The process of sharing can happen via a few paths, each with their own deficiencies.</p><p><strong>X copies an isolated segment of code and messages it to Y</strong></p><ul><li>The code lacks context within the file and less importantly syntax highlighting</li></ul><p><strong>X shares the full code file to Y</strong></p><ul><li>The area in question isn’t isolated and lacks context in the broader application</li></ul><p><strong>X shares a reference to the line via permalink within their shared repository</strong></p><ul><li>The working example must be pushed to the repository</li></ul><p><strong>X shares their screen with Y using a video sharing or live sharing application in a pair programming model</strong></p><ul><li>The collaboration is synchronous and requires Y to present an answer on the spot</li></ul><p>All of these are missing two key characteristics in that they do not allow the second engineer to execute the code in it’s current context on their own machine, nor provide a seamless sharing experience between the two engineers.</p><h3>Persisted State Sharing</h3><p>Imagine a model that was networked, with dynamic reproducibility. Say persistence at runtime, shareable via hyperlink for the recipient to engage at the current state of execution. For analogous intent, a video link (e.g. YouTube) at the 23:22 minute mark but for code. Indefinite persistence.</p><p>Thought similarly to orthogonal persistence in a virtual machine hibernation model, but with the backing of a state language for reproducibility.<a href="https://archive.cs.st-andrews.ac.uk//papers/download/ABC+83b.pdf">1</a></p><p>Suppose the interactive development environment is streamed to the local machine with a single source of state persisted in the cloud. A fixture in time within the debugging execution could theoretically be shared, allowing the next individual to capture that specific moment and continue processing.</p><p>Retaining the state indefinitely poses challenges in both execution time and memory allocation but through a model of on-demand persistence and runtime checkpointing, it is certainly feasible.<a href="https://dl.acm.org/doi/10.1145/331119.331420">2</a> Moving from machine to machine and being persistent at runtime without having to stop or redeploy the code.<a href="https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.188.7895&amp;rep=rep1&amp;type=pdf">3</a></p><p>Availability for other perceivable enhancements like “time-travel debugging”, where an engineer can step backwards through time to understand the prior execution path in the stack, is also evident.<a href="https://livebook.manning.com/book/redux-in-action/chapter-3/13">4</a></p><h3>Productization</h3><p>Adoption for cloud-based development environments is gaining momentum, but for large scale projects there is still quite the dependence on the localized model. Presented in a minimum viable product form, an IDE extension that tracks file manipulations and synchronizes changes up to the cloud may be a start to bridging the gap. But there still poses the problem of syncing the more cumbersome runtime execution data.</p><p>Presented as a streaming application, where all interaction is happening in the cloud, would exhibit a more seamless experience. On execution, the runtime is persisted in a state file that would be preserved when a link is generated and contain a reference to the fixed point in time. If no link is shared, the persisted state would simply be ephemeral and removed when the execution is terminated, conserving system memory.</p><p>The developer experience might look like a right-click on a line of code that was just modified in the local environment. When selecting to share, a link is generated and copied to the clipboard. The developer would paste the link into their messaging application. A screenshot of the breakpoint with the segment of code is displayed, showing some values for the observed variables inline. The recipient developer would click the link and be brought into their own local environment at the exact point in time of the runtime execution.</p><p>Engineering this process might contain some combination of a compute layer that requires the application to be containerized with some available test data, like that of an integration test, to present identical reproducibility. Alternatively, the environment can be backed by a database snapshot for further process execution.</p><h3>Objections</h3><p>Being quite inspired by <a href="https://www.inc.com/audacious-companies/issie-lapowsky/square.html">Dorsey and Square’s 140 reasons why it will fail</a>, I have given myself four.</p><p>Where is the market differentiation?</p><ul><li>Present a solution that allows developers to engage in this model without changing their current development workflow.</li></ul><p>What is to stop one of these companies from just complementing their existing offering?</p><ul><li>Nothing. But as mentioned earlier, the desired outcome is different. Most tooling is at the stage of the hobbyist and entry-level engineer.</li></ul><p>Is it not the assumption that online development environments become the defacto standard?</p><ul><li>Maybe. There is always room to adopt the full-SaaS model, but client-server streaming presents the opportunity to take existing applications and start developing for production now.</li></ul><p>Doesn’t persisting the state of innumerous application links pose a significant technical hurdle?</p><ul><li>It is well within the limits of physics.</li></ul><p>The minute this is shared, someone may produce another “what about” that I welcome with full exposure.</p><h3>Go-to-Market</h3><p>Candidly, at this juncture, I care very little about the nature of bringing an idea such as this to market. But if I were to back it by some language, I would suggest that a combination of a bottom-up B2B SaaS approach, mixed with a freemium model for developers, followed by some organic network effects of sharing code in their messaging ecosystem could be a decent first round strategy.</p><p>As always, open and welcome to comments.</p><p><em>This article was originally posted on my own site, </em><a href="https://patmigliaccio.com/persisted-state-sharing/"><em>patmigliaccio.com</em></a></p><h4>References</h4><p><a href="https://archive.cs.st-andrews.ac.uk//papers/download/ABC+83b.pdf">1</a> Atkinson, M.P., Bailey, P.J., Chisholm, K.J., Cockshott, W.P. &amp; Morrison, R. “PS-algol: A Language for Persistent Programming”. In Proc. 10th Australian National Computer Conference, Melbourne, Australia (1983) pp 70–79.</p><p><a href="https://dl.acm.org/doi/10.1145/331119.331420">2</a> Liu, Z. (1996). A persistent runtime system using persistent data structures. Proceedings of the 1996 ACM Symposium on Applied Computing — SAC ’96, 429–439. <a href="https://doi.org/10.1145/331119.331420">https://doi.org/10.1145/331119.331420</a></p><p><a href="https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.188.7895&amp;rep=rep1&amp;type=pdf">3</a> Nicoara, A., &amp; Alonso, G. (2007). Making applications persistent at run-time. 2007 IEEE 23rd International Conference on Data Engineering. <a href="https://doi.org/10.1109/icde.2007.369013">https://doi.org/10.1109/icde.2007.369013</a></p><p><a href="https://livebook.manning.com/book/redux-in-action/chapter-3/13">4</a> Garreau, M., &amp; Faurot, W. (2018). Chapter 3. Debugging Redux applications — Redux in Action. Redux in Action. Retrieved May 22, 2022, from <a href="https://livebook.manning.com/book/redux-in-action/chapter-3/12">https://livebook.manning.com/book/redux-in-action/chapter-3/12</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86e08971effa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Lessons from Deploying a Machine Learning Model with Kubernetes to Google Cloud Platform]]></title>
            <link>https://medium.com/@pat_migliaccio/lessons-from-deploying-a-machine-learning-model-with-kubernetes-to-google-cloud-platform-ab84f71ab311?source=rss-209d6628cb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab84f71ab311</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Pat Migliaccio]]></dc:creator>
            <pubDate>Tue, 29 Oct 2019 15:51:07 GMT</pubDate>
            <atom:updated>2019-10-29T15:51:07.005Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*CFmS3psUfCf9I_ADky_-pA.png" /><figcaption>Photo by <a href="https://unsplash.com/@hynpurpose?utm_medium=referral&amp;utm_campaign=photographer-credit&amp;utm_content=creditBadge">Scoot Johnson</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p><strong>Deployments can be hard.</strong> Especially when working with resource intensive processes like machine learning models and ensuring they run optimally. After quite a bit of challenges, the following is an example illustration of a few actions taken to deploy a containerized model to <strong>Google Cloud Platform (GCP)</strong> in a relatively framework-agnostic manner using <strong>Kubernetes</strong>.</p><h3>A Quick Brief</h3><p>In the case outlined, the model and associated API are written in <strong>Python</strong> with the <a href="http://www.deeplearning.net/software/theano/"><strong>Theano</strong></a> library and served using the <a href="https://palletsprojects.com/p/flask/"><strong>Flask</strong></a> web application framework. The model itself requires a few large asset files to be loaded into memory for it to encode and process the input data.</p><p>Delegating most of the steps necessary for deploying a standard container with <strong>Google Kubernetes Engine (GKE)</strong> to the <a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app">official documentation</a> on the matter, this article mostly focuses on the techniques associated with overcoming the major pain points throughout the deployment process. Techniques, by which, have been organized below in a manner aligned with areas of concern rather than sequential steps.</p><h3>Preparing the Application</h3><p>Before deploying the container to GCP, two methods or routes must be added to the Flask application to ensure our model is loaded on startup or the time in which the pod is provisioned when all Kubernetes health checks pass.</p><p>The first method _load_model() is triggered prior to the execution of the first request made to the API. In this example, the Kubernetes Ingress, discussed later, will perform a health check readiness probe on the /healthz endpoint.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ebaebfc1d9a6dd4757877432976b603/href">https://medium.com/media/6ebaebfc1d9a6dd4757877432976b603/href</a></iframe><p>By storing the model returned from the load_model method in a global variable, all subsequent requests to this pod will utilize it preloaded from memory, reducing request times to the API by approximately 60sec.</p><h3>Storing the Model Assets</h3><p>Since the load_model method reads in a few sizeable <strong>Pickle</strong> (.pkl) files (approx. 2.5 GB) used for encoding the input data, granting the application with file system access to these assets is required.</p><p>Preceding storage setups included the following schemes, yet neither seemed to provide a successful solution.</p><ul><li>Attempting to mount a <strong>Google Cloud Storage (GCS)</strong> <strong>bucket</strong> containing the files from within the Dockerfile using <a href="https://cloud.google.com/storage/docs/gcs-fuse">gcsfuse</a> resulted in a connectivity nightmare.</li><li>Downloading the same model assets on application start using the <a href="https://cloud.google.com/python/getting-started/using-cloud-storage">Python library for Cloud Storage</a> was sluggish, provided limited reliability, and was required with each restart or provisioning of a new pod.</li></ul><h4>Persistent Volumes, Docker exec, and gsutil</h4><p>As a result of a bit of trial-and-error, the remedying solution included mounting a persistent volume in Kubernetes, using docker exec to run <a href="https://cloud.google.com/storage/docs/gsutil">gsutil</a> within the container, and manually conducting a one-time transfer of the model files from the bucket to the volume.</p><p><strong>Persistent Volumes</strong></p><p>Provisioning a persistent volume requires creating a <a href="https://cloud.google.com/kubernetes-engine/docs/concepts/persistent-volumes">PersistentVolumeClaim</a> as a prerequisite to creating the pod that will inevitably utilize it. By employing <a href="https://kubernetes.io/docs/reference/kubectl/overview/">kubectl</a> to deploy new resources, executing the below command on the following pvc.yaml configuration file will accomplish the desired result.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d59d45f4bebb9eb4ae2bd00bbf69dc74/href">https://medium.com/media/d59d45f4bebb9eb4ae2bd00bbf69dc74/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7ca7c6f09b58116b0c5220ed5187c73/href">https://medium.com/media/c7ca7c6f09b58116b0c5220ed5187c73/href</a></iframe><p><strong>Pod Deployment</strong></p><p>Creating a pod for the container application within the <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/creating-a-cluster">Kubernetes cluster</a> follows the same provisioning step but against a separate api-deployment.yaml configuration file.</p><p>Potential configuration setups may <a href="https://kubernetes.io/docs/concepts/configuration/overview/#general-configuration-tips">benefit</a> from the usage of a single deployment file for all objects. In the instance discussed, they are split into separate files for clarity.</p><p>Generated initially from a docker-compose.yaml file using <a href="https://kompose.io/">kompose</a>, the further modified and slightly reduced example declares:</p><ul><li>Labels and selectors to point the NodePort service, to be defined later, to the new Deployment pod</li><li>Environment variables representing the bucket name containing the model files</li><li>Environment variables representing the path by which the model files will be copied to and later read from</li><li>A persistent volume claim and the mountPath for which to mount said volume</li><li>The resources to be allocated to each pod</li><li>A route for the readiness probe in which GKE will perform the health checks on</li></ul><p>…along with additional, more standard deployment configuration settings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/665933139519e138d58d7b01c305a6f4/href">https://medium.com/media/665933139519e138d58d7b01c305a6f4/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57204801c43ada085b1abf01a6a1fab0/href">https://medium.com/media/57204801c43ada085b1abf01a6a1fab0/href</a></iframe><p><strong>CUDA-Enabled Container Image</strong></p><p>As an additional note, the Dockerfile used for building my-image is a derivative of the <a href="https://hub.docker.com/r/nvidia/cuda/">nvidia/cuda:9.0-cudnn7-runtime</a> container image with Python and the associated package requirements installed.</p><h4>Transferring the Model Files</h4><p>With the initial pod and volume created, transferring the models, currently stored locally, to the GCS bucket and then to the persistent volume can all be done with the following sets of commands.</p><p><strong>Local Machine to Bucket</strong></p><p>Recursively copying the large files in <a href="https://cloud.google.com/storage/docs/gsutil/commands/cp">a multithreaded manner</a> to ensure the most efficient transfer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6868f4d062cf9b5fa8289910c3dd968e/href">https://medium.com/media/6868f4d062cf9b5fa8289910c3dd968e/href</a></iframe><p><strong>Get Pod Name</strong></p><p>Retrieving the name of the created pod for which to execute the transfer in.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb7e6905cb2624af4ab8f70bcca58d9f/href">https://medium.com/media/eb7e6905cb2624af4ab8f70bcca58d9f/href</a></iframe><p><strong>Bucket Through Pod to Persistent Volume</strong></p><p>Running bash within the pod to install gsutil and to copy the files from the bucket to the persistent volume.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98057abfc730035b8503c8c716fb0ee9/href">https://medium.com/media/98057abfc730035b8503c8c716fb0ee9/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d8d3873e5367f849e40f9103e381fb0/href">https://medium.com/media/9d8d3873e5367f849e40f9103e381fb0/href</a></iframe><p>Now, the model files stored in the persistent volume are accessible by all current and future pods that are provisioned.</p><h3>Exposing the API</h3><p>The combination of a NodePort service, Ingress service, and global static IP will reliably expose the cluster publicly, with essential load balancing and SSL encryption.</p><h4>NodePort Deployment</h4><p>Creating a NodePort service for the API uses the same deployment process from prior but with the following api-service.yaml configuration file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2c54937b90cfd7f73bc89d55e2ff51e/href">https://medium.com/media/b2c54937b90cfd7f73bc89d55e2ff51e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e5abb1d81cc3c323b90cc1e458087fe/href">https://medium.com/media/4e5abb1d81cc3c323b90cc1e458087fe/href</a></iframe><p>Notice the metadata and selector fields match that of the previously deployed pod to establish a connection between the two resources.</p><h4>Static IP Addressing</h4><p>A new global IP address may <a href="https://github.com/kelseyhightower/ingress-with-static-ip">be created</a> to configure and expose the cluster to the outside.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30838e327c604e76dd738309493fb2da/href">https://medium.com/media/30838e327c604e76dd738309493fb2da/href</a></iframe><h4>Ingress Deployment</h4><p>Including the <a href="https://kubernetes.io/ingress.global-static-ip-name">kubernetes.io/ingress.global-static-ip-name</a> annotation in the ingress-service.yaml deployment file, the newly created global static IP address will be linked to the ingress resource.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdb98a423b7615e414ae4cd7528149da/href">https://medium.com/media/bdb98a423b7615e414ae4cd7528149da/href</a></iframe><p>Naturally, the annotation with a key containing pre-shared-cert will <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/ingress-multi-ssl#specifying_certificates_for_your_ingress">specify the SSL/TLS certificate</a> by the name of ingress-cert.</p><p>If <strong>Cloudflare</strong> happens to be the DNS provider of choice, an Origin CA certificate <a href="https://support.cloudflare.com/hc/en-us/articles/115000479507">can be created,</a> exported, <a href="https://www.ryadel.com/en/openssl-convert-ssl-certificates-pem-crt-cer-pfx-p12-linux-windows/">converted using OpenSSL</a>, and then added to GCP with the equivalent ingress-cert name.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f38bca080cb8cb926b38b3977db4b48/href">https://medium.com/media/3f38bca080cb8cb926b38b3977db4b48/href</a></iframe><p>Deploying the Ingress will trigger the readiness probe hitting the health check endpoint and loading the model into memory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6cae7043da89dbf2500f6c4e1c6d5a90/href">https://medium.com/media/6cae7043da89dbf2500f6c4e1c6d5a90/href</a></iframe><p>A final step would be to include pointing the domain’s A records in Cloudflare to the global static IP address.</p><h3>Pricing, Machine-Types, and Provisioning Resource Pools</h3><p>Often the cost associated with operating these services is omitted when discussing deployment approaches. Including it as a consideration is imperative context for choosing the most optimal machine-types.</p><h4>Understanding Instance Billing</h4><p>One of the more precarious areas to navigate is the <a href="https://cloud.google.com/compute/vm-instance-pricing">instance pricing</a>. Running N1 machine-types with “Predefined vCPUs” costs approximately $0.038 / vCPU hour and $0.005 / GB hour for “Predefined Memory.” By these combined measures, an n1-standard-1 (1 vCPU and 3.75 GB) machine-type will run at approximately $0.057 / hour.</p><h4>High-Memory Machine-Types</h4><p>Monitoring memory usage of the application running locally, may establish the minimum resources necessary to provision the initial pool for sustainable use.</p><p>By the nature of how this particular example application operates, a higher system-memory-to-vCPU ratio will help the model to perform more efficiently while not underutilizing paid for CPU resources.</p><p>Through creating a pool with an n1-highmem-2 (2 vCPU and 13 GB memory) machine-type, above standard memory is allocated to each node provisioned in the cluster running at approximately $0.1421 / hour.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b3a0e7ea98a3c6bc6e95a1b0aa5fd40/href">https://medium.com/media/8b3a0e7ea98a3c6bc6e95a1b0aa5fd40/href</a></iframe><p>Note: Although untested directly, if the budget will support a node pool <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/gpus">equipped with a GPU accelerator</a> such as nvidia-tesla-t4 (1 GPU and 16 GB GDDR6 memory) at $0.95 / hour, processing time on the model may see a significant reduction when applied in conjunction with CUDA using the appropriate <a href="https://developer.nvidia.com/cuda-zone">Theano configuration</a> (.theanrc).</p><h4>Migrating Resource Pools</h4><p><a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/migrating-node-pool">Migrating from one resource pool to a new</a> will require cordoning and evicting the deprecated pool, providing a seamless transition when upscaling or downscaling without incurring any downtime.</p><p>Touching on it briefly the following two commands will accomplish this task, where default-pool is the name of the deprecated pool.</p><p><strong>Cordon Pool</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a60731a282d59ae19103e5fe027d7c27/href">https://medium.com/media/a60731a282d59ae19103e5fe027d7c27/href</a></iframe><p><strong>Evict Pool</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4447022db5da7cb977bd6416d4a79ca3/href">https://medium.com/media/4447022db5da7cb977bd6416d4a79ca3/href</a></iframe><h3>Other Solutions?</h3><p>Certainly, managed services, dedicated to machine learning (e.g. <a href="https://cloud.google.com/ml-engine/">Google ML Engine</a>) are improving by the moment, but in the current state, the aforementioned is believed to be one of the few reliable strategies for production deployments.</p><p>Quite a lot of ground has been covered rather quickly, but I hope to have provided insight into the decision-making process of deploying a containerized application running an ML model on Google Cloud Platform.</p><p>As always, open and welcome to questions and comments!</p><p><em>This article was originally posted on my own site, </em><a href="https://patmigliaccio.com/lessons-ml-gcp/"><em>patmigliaccio.com</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab84f71ab311" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Resolving Nested Promise Arrays]]></title>
            <link>https://medium.com/@pat_migliaccio/resolving-nested-promise-arrays-77a161f5570a?source=rss-209d6628cb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/77a161f5570a</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Pat Migliaccio]]></dc:creator>
            <pubDate>Thu, 04 Apr 2019 20:22:02 GMT</pubDate>
            <atom:updated>2019-04-04T20:24:07.296Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*BfumQz2N8pc2CHnn5gGXMQ.jpeg" /><figcaption>Photo credit: <a href="https://visualhunt.co/a1/ea34a1e2">Mr.TinDC</a> on <a href="https://visualhunt.com/re3/c0945cfb">Visualhunt</a> / <a href="https://creativecommons.org/licenses/by-nd/2.0/">CC BY-ND</a></figcaption></figure><p>Often times, <strong>considerable asynchronous complexity</strong> can arise when making requests to multiple resources in JavaScript in an effort to merge disparate data into a single cohesive data object.</p><p>Instances, where this may occur, could include aggregating various APIs, retrieving data from multiple databases, fetching or processing data with service workers, working with a service-oriented architecture and the list continues.</p><p>As to follow, an example situation is outlined with a suggested set of compounding solutions that may lead to additional insight into potentially <em>solving</em> this type of problem.</p><h3>A Situation</h3><p>Take for hypothetical, a developer is looking to retrieve all comments made by users who are considered <em>active</em> on a particular article post; active as defined by any sort of engagement metric.</p><p>First, an asynchronous request is made to retrieve the active users of the specific post. Utilizing await, one can <em>wait</em> until a Promise is fulfilled before continuing execution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28cc4116b26270e50dd5f2251c1cd34a/href">https://medium.com/media/28cc4116b26270e50dd5f2251c1cd34a/href</a></iframe><p>The response data structure contains an array of User objects with their last few comments. Instead of returning the full Comment object for each, most likely to reduce the response payload size, only the corresponding commentIds are provided by the resource.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75ff0cccb1f8016a9995ccca5f0aff5c/href">https://medium.com/media/75ff0cccb1f8016a9995ccca5f0aff5c/href</a></iframe><p>To retrieve the comments tied to the users, another call may be made to fetch each individual comment based on the commentId and then assign it back to the corresponding User object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af4aaee0930775ee4c3b02364ad3a2eb/href">https://medium.com/media/af4aaee0930775ee4c3b02364ad3a2eb/href</a></iframe><p>But now the resulting data structure contains a nested array of Promises for each set of comments (hereafter referred to as Array&lt;Promise&gt;). Even though included in the map function’s return is the await operator, the comments are not resolved.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7454584d93d03e4bcecfcf2421eea24f/href">https://medium.com/media/7454584d93d03e4bcecfcf2421eea24f/href</a></iframe><h3>A Solution</h3><p>One solution would be to use Promise.all to <a href="https://www.ecma-international.org/ecma-262/6.0/#sec-promise.all">return a single Promise for the entire iterable of unresolved comments passed to it</a>. Prior to assigning it back to the User object, it’s essentially merging the Array&lt;Promise&gt; into a single Promise.</p><p>Moving it to a separate function, resolveComments, may also improve clarity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f32242479eb5a85d8a46e7dde8d383b6/href">https://medium.com/media/f32242479eb5a85d8a46e7dde8d383b6/href</a></iframe><p>The activeUsersFullComments variable would contain a single Array&lt;Promise&gt; with each item resolving subsequently to a User object containing the nested array of resolved Comment objects.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e525ad6556d5d39f928adffd12e256ea/href">https://medium.com/media/e525ad6556d5d39f928adffd12e256ea/href</a></iframe><p>Following this approach, a next step could be to, once again, combine the Array&lt;Promise&gt; into a single Promise by passing it to the Promise.all method and calling it a day.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/532b1733510bf742475778afccfe7ef2/href">https://medium.com/media/532b1733510bf742475778afccfe7ef2/href</a></iframe><p>All of the comments are resolved for each User and may be utilized as desired within the application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e3151940f090c591fc178add45e1714/href">https://medium.com/media/7e3151940f090c591fc178add45e1714/href</a></iframe><h3>A Better Solution</h3><p>But say one wanted to make it a bit more abstract and perform the same logic on any type of User, not just the active ones.</p><p>They could have a resolveUsers method that calls the resolveComments method and wrapping each Array&lt;Promise&gt; in a Promise.all.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edeb14ade6e132c0c17e2ad9f4025168/href">https://medium.com/media/edeb14ade6e132c0c17e2ad9f4025168/href</a></iframe><p>Notice how resolveComments requires an await operator before the Promise.all in the user.comments assignment and resolveUsers does not. Simply because the Promise is being returned and not assigned.</p><h3>A Better, Little More Performant Solution</h3><p>What if there were comments that more than one user contributed to or possibly a comment thread containing references to multiple users (i.e. multiple users having the same commentId value in their User object)?</p><p>It wouldn’t make sense to make a request for that resource more than once, would it?</p><h4>Caching</h4><p>A way to handle this would be through the use of <strong>caching</strong>, by storing the comment in memory for subsequent use when resolving another User object (see also: <a href="https://medium.freecodecamp.org/understanding-memoize-in-javascript-51d07d19430e">memoization</a>). As long as the comment is <strong>immutable</strong> or updates are unlikely to occur over the course of the execution, iterating and resolving all users, the following approach should suffice.</p><p>By creating a cache object, existing comments can be stored then checked for existence and, if existing, retrieved from memory rather than making an additional request to an external resource.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59f6b6da74a7289a6fa3104442997d1f/href">https://medium.com/media/59f6b6da74a7289a6fa3104442997d1f/href</a></iframe><p>The <a href="https://github.com/getify/You-Dont-Know-JS/blob/f0d591b6502c080b92e18fc470432af8144db610/scope%20%26%20closures/ch2.md#chapter-2-lexical-scope"><em>lexical scope</em></a> of the <strong>closure</strong> made by createCommentsResolver maintains the state of the cache across each iteration of users array.</p><h3>Situational Caveats</h3><p>An issue with the example case is that the number of requests or queries made grows proportionally with each additional comment included. As in for every new comment created by a user, the number of requests increases.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0b2d66a1635239be3aea956738ba38c/href">https://medium.com/media/d0b2d66a1635239be3aea956738ba38c/href</a></iframe><h4>n+1 Problem</h4><p>This may lead to an <a href="https://www.infoq.com/articles/N-Plus-1"><strong>n+1 problem</strong></a>, “when a request to load one item turns into n+1 requests since the item has n associated items.” It can potentially be mitigated by <a href="https://secure.phabricator.com/book/phabcontrib/article/n_plus_one/">batching the queries</a> for the nested property, ahead of time, before making the requests.</p><h4>Rate Limiting</h4><p>Another scenario where it may perform poorly would be working with a rate limited resource, such as a third-party API. On each iteration, there may be a significant delay between resolving each subset of data, culminating in one long wait period before the data is accessible.</p><h4>Fault Tolerance</h4><p>One thing to note is the lack of <a href="https://en.wikipedia.org/wiki/Fault_tolerance"><strong>fault tolerance</strong></a> applied above. As discussed in another article of mine, <a href="https://patmigliaccio.com/async-fishing/">Asynchronous Fishing: The Multi-Promise Resolution</a>, when using Promise.all for merging, <strong>total rejection</strong> occurs if one of the requests made to a resource fails, causing the entire Array&lt;Promise&gt; to not resolve.</p><h3>Other Solutions</h3><p>Plenty of libraries exist in the wild to help subjugate some these issues, for instance, <a href="https://github.com/Reactive-Extensions/RxJS">RxJS</a> and the usage of Observables may be a better approach for merging the two example resources.</p><p>GraphQL and <a href="https://www.apollographql.com/docs/graphql-tools/resolvers.html"><strong>nested resolvers</strong></a> are intended for doing just that, <em>resolving nested data</em>. Certainly, a better option if the resource is a GraphQL server.</p><p>In addition to these, <strong>Facebook’s</strong> <a href="https://github.com/facebook/dataloader"><strong>DataLoader</strong></a> is designed specifically for handling most of the aforementioned caveats by being “used as part of your application’s data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.”</p><h3>Final Note</h3><p>No solution is perfect. Iteration and optimization, as needed, is as always the most pragmatic approach and the above is just one way this can be <em>solved</em>, albeit with noted caveats.</p><p>Let me know your thoughts, questions, considerations; always interested in hearing feedback!</p><p><em>This article was originally posted on my own site, </em><a href="https://patmigliaccio.com/"><em>patmigliaccio.com</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77a161f5570a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Misconceptions in Client-Side Security: Reverse Engineering Obfuscation & Disguised Endpoints]]></title>
            <link>https://medium.com/@pat_migliaccio/misconceptions-in-client-side-security-reverse-engineering-obfuscation-disguised-endpoints-dddfdafbc60e?source=rss-209d6628cb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/dddfdafbc60e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[base64]]></category>
            <category><![CDATA[obfuscation]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[web-application-security]]></category>
            <dc:creator><![CDATA[Pat Migliaccio]]></dc:creator>
            <pubDate>Fri, 28 Jul 2017 02:44:46 GMT</pubDate>
            <atom:updated>2017-09-02T01:56:41.646Z</atom:updated>
            <content:encoded><![CDATA[<p><em>The information provided in the following is for educational purposes only and should not be used to wrongfully infiltrate or manipulate an application without the legal consent of its owner.</em></p><p><strong>Client-side security is never safe.</strong> No matter the practices followed, deterrents used, or tricks added, once content is presented to the user, on <em>their</em> machine, they have access to manipulate it in any form they desire. With that being said, understanding how certain techniques can be reverse-engineered will help a developer comprehend the concept of relying on access control and the server as a gateway to protected content.</p><p>A multitude of practices may be used to improve security, a few of which potentially beneficial, but never more than a trivial obstacle for a hacker. With many of the methods causing more issues in a developer’s workflow than they’re worth, it may be unproductive to use these strategies in production.</p><h4>OWASP Top 10 &amp; False Security</h4><p><a href="https://www.owasp.org/index.php/Top_10_2013-A6-Sensitive_Data_Exposure"><strong>A6-Sensitive Data Exposure</strong></a> is listed as one of the Top 10 Most Critical Web Application Security Risks cautioned by the <a href="https://www.owasp.org/">Open Web Application Security Project (OWASP)</a>. In regards to building secure applications, each of the following practices do not conform to this principle and should be understood as such.</p><p>What are methods of <strong>false security</strong> that may be introduced to make the developer feel as though they are implementing a secure solution?</p><h3>Masking API Endpoints</h3><p>At the foundation of the modern web is the asynchronous http request. In most cases, understanding how these are architected within an application can tell a infiltrator a lot about the state of the program to find ways in manipulating it.</p><p>If following a <a href="https://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> or possibly a <a href="http://graphql.org/">GraphQL</a> API implementation, an endpoint is the location in which a request is made for information. The URL appropriately defines the resource which is being retrieved or modified. With this in mind, does abandoning these standard conventions through disguising the target location sound appropriate?</p><p>Hiding <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest">XMLHttpRequest</a> (XHR) calls in obscure JavaScript files with names irrelevant to the feature or the misnaming of the endpoint itself are two of the many futile attempts to deter snoopers from digging into responses, headers, and other metadata.</p><h4>Network Request Sniffing</h4><p><a href="https://developers.google.com/web/tools/chrome-devtools/network-performance/reference">Chrome DevTools’s Network panel</a> (or alternatively <a href="https://www.wireshark.org/">Wireshark</a>) can quickly expose these techniques. Especially with the preview feature, XHR calls returning JSON are becoming increasingly easier to examine.</p><p>Making use of a misnomer for the location of sensitive data may seem ideal, but in actuality it can confuse developers working with the codebase in the future. Additionally, performing other techniques such as hiding query parameters in the Referer header URL is unnecessary. With the availability to quickly sniff the network calls, these approaches are ultimately frivolous.</p><p><em>Note: Methods using WebSockets have been thought to be preventative of these insecurities but </em><a href="https://media.blackhat.com/bh-us-12/Briefings/Shekyan/BH_US_12_Shekyan_Toukharian_Hacking_Websocket_Slides.pdf#page=34"><em>“if you can sniff </em></a><a href="https://media.blackhat.com/bh-us-12/Briefings/Shekyan/BH_US_12_Shekyan_Toukharian_Hacking_Websocket_Slides.pdf#page=34"><em>http: you can sniff </em></a><a href="https://media.blackhat.com/bh-us-12/Briefings/Shekyan/BH_US_12_Shekyan_Toukharian_Hacking_Websocket_Slides.pdf#page=34"><em>ws:”</em></a><em>. JSONP has also been used through script injection, but this is </em><a href="https://securitycafe.ro/2017/01/18/practical-jsonp-injection/"><em>a very unsecure practice</em></a><em> and should not be utilized.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/0*fuJUfg95BSx1gBPi.png" /></figure><p>As shown above, a seemingly inconspicuous REST call (/assets/css/svg/generate) returns an encoded Base64 string. Now although there is the possibility that <a href="https://codepen.io/tigt/post/optimizing-svgs-in-data-uris">an actual SVG is being optimized here</a>, it is always of interest for a attacker to <a href="https://www.sans.org/reading-room/whitepapers/detection/base64-pwned-33759">dissect the response data when it is encoded in Base64</a>.</p><h3>Base64 Encoding &amp; Modification</h3><p><strong>Base64 is not encryption.</strong> By design it is an ASCII string representation of binary data and should be thought of as such.</p><p>Encoding data on the server in a Base64 sequence and then modifying the string before exposing it to the client-side has been used by developers to restrict the plain view visibility of raw payload data.</p><h4>An Impractical Approach</h4><p>An example of a modification technique could be as simple as <strong>rearranging each character of the string in reverse order and moving the last </strong><strong>n characters to the front</strong>. By performing this step, or any similar process that the developer uses at their discretion, network sniffing for the encoded string would lead to the inability to decode the response payload.</p><p>JavaScript’s native <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob">atob()</a> decoding method would throw an error, while other tools found online might be partially more successful. <a href="https://www.base64decode.org/">Base64 Decode and Encode</a> may possibly decode the Base64 string, but certainly into a set of undecipherable characters.</p><h4>Decoding the Request</h4><p>Sifting through the source files of a web application becomes possibly the most difficult part in debunking client-side security solutions. But through basic find and search techniques, like querying for the Request URL header value or looking for where a response’s property values are used, an intruder can and will eventually find the source of where the request is called from.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c547d1708212b82cd3f31fbacd9c74d3/href">https://medium.com/media/c547d1708212b82cd3f31fbacd9c74d3/href</a></iframe><p>Based on the need to decode the response payload value on the client-side, this approach gives any meddler a direct blueprint of the exact design of this “pseudo-encryption algorithm”. If it is not backed by <a href="https://math.berkeley.edu/~kpmann/encryption.pdf">mathmatical principle</a> and can be cracked in a relatively short period of time, <a href="http://spectrum.ieee.org/tech-talk/computing/hardware/encryptionbusting-quantum-computer-practices-factoring-in-scalable-fiveatom-experiment">without the use of quantum computing</a>, <strong>it is not secure and should never be considered a viable substitute for proper endpoint security</strong>.</p><p>After implementing all of these erroneous data manipulation techniques, the XHR call may then be <strong>obfuscated</strong> to further the level of ambiguity of the processing methods used by the application.</p><h3>Vulnerabilities in Obfuscation</h3><p>Obfuscation is an approach used to prevent an individual from easily gaining insight into the logic running behind an application. Through manipulation of the source code, formatting, variable-naming, and other declarations become virtually unreadable.</p><h4>JavaScript Obfuscator &amp; Build Tools</h4><p>Various build tools are available to developers to perform obfuscation, with a few of the most prominent being the <a href="http://www.javascriptobfuscator.com/">JavaScript Obfuscator</a>, <a href="https://developers.google.com/closure/compiler/">Google Closure Compiler</a>, <a href="http://yui.github.io/yuicompressor/">YUI Compressor</a>, and <a href="https://github.com/mishoo/UglifyJS2">UglifyJS</a>. Arguably, these processors are just <strong>minifiers with added optimizations to mangle code</strong>.</p><p>After processing the example request and parsing methods through the JavaScript Obfuscator, one can see the source become <em>almost</em> impossible to digest.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a696d1ac7aef69eed1899cd1b9b6d6dd/href">https://medium.com/media/a696d1ac7aef69eed1899cd1b9b6d6dd/href</a></iframe><p>Renaming variables as <strong>hexadecimal integer literals</strong> and replacing property values with <strong>array references to hex value representations of character codes for ASCII characters</strong>, this obsfucator claims to convert JavaScript into a “completely unreadable form, preventing it from analysing and theft.”</p><h4>Pretty-Printing Obfuscation in Browser</h4><p>Through the formatting functionality of Chrome DevTools, in-browser source code becomes easier to visualize but with obfuscation, dissecting these methods are far from a menial task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/0*edqMmMuPWOMqFENF.png" /></figure><p>Being a just-in-time compiled and interpreted language, JavaScript virtual machines, like <a href="https://developers.google.com/v8/">Google’s V8 engine</a>, present very high-performing applications by optimizing and compiling the <em>human-readable source code</em> at runtime. Unless <a href="https://github.com/nwjs/nw.js/wiki/protect-javascript-source-code-with-v8-snapshot">unrealistically compiled down to machine code</a>, the source of any client-side JavaScript application can be reverse-engineered.</p><h4>Reverse Engineering Obfuscation</h4><p>With a simple script, similar to the one I’ve quickly developed below, an individual can parse obfuscated JavaScript into a more human-readable format for further inspection.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afbec9e71172e9462a66e7ac25f597d6/href">https://medium.com/media/afbec9e71172e9462a66e7ac25f597d6/href</a></iframe><p>And now, the reverse-engineered methods are very similar to the originals.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2b282a5a2da2973a2dae03e719456e5/href">https://medium.com/media/a2b282a5a2da2973a2dae03e719456e5/href</a></iframe><h4>Deobfuscation, Web Scraping, &amp; ASTs</h4><p>Among the <a href="http://www.kahusecurity.com/2014/javascript-deobfuscation-tools-redux/">various deobfuscation tools available</a>, a simple script that reads in a JavaScript file and parses it, like displayed above, is plenty for an infiltrator to analyze how these manipulation methods modify the XHR response. Alarmingly so, one may potentially even utilize the original source code to write a <a href="https://blog.hartleybrody.com/web-scraping/">web scraping script</a> to automate the processing of this sensitive data.</p><p>For more complex reverse obfuscation techniques, one could parse their script into an <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">abstract syntax tree</a> (AST) and more efficiently convert these using <a href="http://babeljs.io/docs/core-packages/babylon/">Babel’s Babylon</a> or with <a href="https://astexplorer.net/">one of the many other JavaScript AST parsers</a>.</p><h3>Thinking Pragmatically</h3><p>As with any application, it is up to the developers to decide which practices they feel have technical worth and whether they will limit a hacker’s ability to manipulate the state of their application. But certain mechanisms like authorization frameworks, such as <a href="https://oauth.net/2/">OAuth</a> with <a href="https://jwt.io/">JSON Web Tokens</a> (JWT), are truly essential. <a href="https://www.owasp.org/index.php/REST_Security_Cheat_Sheet">Proper endpoint security solutions</a> to ensure that sensitive resources are guarded safely and requests are coming from authenticated sources are imperative requisites for secure web applications.</p><p>Recognizing when a security implementation does more harm than added value to the application is the hardest part about deciding whether to use some of the tactics discussed. Take into consideration the above notions, perform research of your own, and make informed decisions.</p><p><em>This article was originally posted on my own site, </em><a href="https://patmigliaccio.com/"><em>patmigliaccio.com</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dddfdafbc60e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rate Limiting: Throttling Consecutive Function Calls with Queues]]></title>
            <link>https://medium.com/@pat_migliaccio/rate-limiting-throttling-consecutive-function-calls-with-queues-4c9de7106acc?source=rss-209d6628cb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c9de7106acc</guid>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[throttling]]></category>
            <dc:creator><![CDATA[Pat Migliaccio]]></dc:creator>
            <pubDate>Tue, 21 Mar 2017 00:12:23 GMT</pubDate>
            <atom:updated>2017-09-02T01:32:30.610Z</atom:updated>
            <content:encoded><![CDATA[<p>An implementation for limiting the amount of time between function calls in JavaScript may be necessary when any sort of process requires regulation like generating network requests. Whether it’s an API call, data store, or even a DOM event, multiple consecutive firings of a function may not give enough time for the intended result before the next invocation occurs.</p><h3>Rate Limiters</h3><p>A common solution for controlling consecutive function calls would be to create a <strong>rate limiter</strong> or <strong>throttled</strong> wrapper function. A method that, when given a function and a time delay, will return a new function that when executed consecutively, will invoke the given function parameter with a wait time before it can be executed again.</p><p>Below is a working example of an initial design.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1056d74a1f60e16afc1d3f5ac96b94a8/href">https://medium.com/media/1056d74a1f60e16afc1d3f5ac96b94a8/href</a></iframe><p>As you can see, the boolean value of isCalled determines whether or not the function has already been called. Now after the given wait in milliseconds, the setTimeout callback reverts the limiter back to a <strong>“non-called” state</strong>, making the method available for continued execution.</p><h3>Dropped Calls</h3><p>A rather significant limitation to this rate limiter is the other function calls are dropped. For example, if one were to loop over this method, they would lose certain iterations of the call if they occurred within the specified wait period. If the desired intention is for <em>every</em> call to be actualized, this method will not suffice.</p><p>A way to improve this would be to queue up each of the calls, so that every iteration will still occur but with the rate limiting timeout between each invocation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b2457e23d774fc950e08e3d96f66d87/href">https://medium.com/media/7b2457e23d774fc950e08e3d96f66d87/href</a></iframe><p>Each execution of the returned function, will push the call to the array variable calls, which will act as the queue. While the queue contains values, a check will see if the function is already in a called state. If not, it will shift off the top or first method in the queue and call it. Then as with the earlier implementation, the isCalled boolean value will revert after the allotted timeout.</p><h3>This does not work.</h3><p>Why? Because the while loop blocks the event-driven nature of JavaScript and its <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop">event loop</a>. Our callback in the setTimeout will never execute until after the while loop finishes its iterating, which won’t occur until the callback happens, causing an <strong>infinite loop</strong>.</p><h3>Recursive Design</h3><p>With this realization or understanding in mind, our real solution lies with good old <strong>recursion</strong>. By abstracting a caller method containing the fn parameter invocation which could be called recursively from within the setTimeout, it would allow the function execution to occur after the callback and would prevent the unintended continuous iteration.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aff613b2982695d372f7bb860b0798ce/href">https://medium.com/media/aff613b2982695d372f7bb860b0798ce/href</a></iframe><h3>Passing Arguments</h3><p>Now how could the fn function be supplied with arguments of its own?</p><p>Looking into examples of other solutions, one implementation I have found, created by <a href="https://github.com/mattheworiordan/">Matthew O’Riordan</a>, contains some insight into ways in which this method could be optimized. His <a href="https://gist.github.com/mattheworiordan/1084831#file-rate_limit-js-L24">_.rateLimit()</a> method, written as an extension of <a href="http://underscorejs.org/">Underscore.js</a>, adds additional functionality for parameters to be provided at the time of execution.</p><p>Continuing with the usage of idiomatic JavaScript and not requiring any dependencies or additional libraries is the most pragmatic approach for this implementation. Now making sure that arguments may be passed to the rate limited function requires their inclusion within the limiter method’s return function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7ed9994dfc3a0068668586f55d2c82c/href">https://medium.com/media/c7ed9994dfc3a0068668586f55d2c82c/href</a></iframe><p>For simplicity the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">Spread operator</a> is used on the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments">Arguments object</a> to bind the arguments to our fn parameter.</p><p>Alternatively, if ES6 compatibility is a concern, <em>among the other changes necessary (i.e. ‘let’)</em>, an alteration must be made to provide the arguments to the rate limited function. As shown in the comments, the arguments object must first be converted into a <strong>true array</strong> which will enable it to be properly passed into the apply() method. This method is necessary to allow for the passing of an array of parameters to the bind() method as opposed to its de facto spread sequence.</p><h3>Brief Implementation</h3><p>Implementing the limiter function is now a rather straightforward task.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d6542f7552d6c4eea2c45d8b3a9a723/href">https://medium.com/media/6d6542f7552d6c4eea2c45d8b3a9a723/href</a></iframe><p>Each message will log in the console at an interval of 500ms. This may easily be replicated with setInterval(), but its usage gives a rather clear picture of a rate limited function declaration.</p><p>Rate limiters and throttling functions exist through a plethora of JavaScript libraries; I’m now looking forward to comparing their methodologies with the one above.</p><p><em>This article was originally posted on my own site, </em><a href="https://patmigliaccio.com/"><em>patmigliaccio.com</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c9de7106acc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fishing for the Multi-Promise Resolution]]></title>
            <link>https://medium.com/@pat_migliaccio/fishing-for-the-multi-promise-resolution-f10dcfd85cdf?source=rss-209d6628cb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f10dcfd85cdf</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Pat Migliaccio]]></dc:creator>
            <pubDate>Thu, 29 Dec 2016 23:01:01 GMT</pubDate>
            <atom:updated>2017-09-02T01:21:17.187Z</atom:updated>
            <content:encoded><![CDATA[<p>Fishing was an analogy that came to mind when I tried to explain a problem I was having to a non-developer. The notion that an HTTP request is like casting a line out and waiting for a bite or response was what I came up with. If I caught a fish, it was a 200 status code, otherwise a missed attempt errored out. Finding an analogous event comparable to an error or bad response was slightly more difficult and not worth the additional effort for the explanation. You understand the point. Now to the problem I was having and my “deep dive” in understanding various solutions.</p><h3>The 404 — Fish Not Found.</h3><p>A project I was working on required that I process several files requested from the server, but I couldn’t run this process until I received them <strong>all</strong>. As if I was out at sea on a fishing vessel and had several lines casted out and I couldn’t pack up and head home until I reeled them all in.</p><p>With ES6 Promises, there is the Promise.all method that takes an Array (or any iterable) of Promises as a parameter and returns a Promise that resolves with an array of all the individual Promises’ resolutions. But from my understanding, if one of the Promises were to be rejected, the entire resulting Promise would be rejected. I like to call this “total rejection.” This was not going to work for me. If I didn’t get a snag on one of the lines, I still wanted the fish from my other lines. Naturally, I decided to do some digging and construct my own way of doing a multi-promise resolution.</p><h3>The Other Fish In The Sea</h3><p>Working extensively with AngularJS (1.x), I decided to see how the $q.all()function <a href="https://github.com/angular/angular.js/blob/v1.4.9/src/ng/q.js#L540">handles it</a>. In their <a href="https://code.angularjs.org/1.4.9/docs/api/ng/service/$q">documentation</a>, they mention that $q “is a Promises/A+-compliant implementation of promises/deferred objects inspired by <a href="https://github.com/kriskowal/q">Kris Kowal’s Q</a>.” Now knowing this, I was compelled to find one of the earliest constructions of this method that I could. Within the <a href="https://github.com/kriskowal/q/blob/v1/q.js#L1578">source</a> of Kowal’s Q, he mentions that the Q.all() method was created by Mark Miller. So once more down the rabbit hole, I found a seemingly <a href="http://wiki.ecmascript.org/doku.php?id=strawman:concurrency&amp;rev=1308776521#allfulfilled">original implementation</a> of the multi-promise resolution.</p><p>Let’s take a look at how Miller implemented this technique:</p><p><em>Compared with </em><em>$q.all() and </em><em>Q.all() this </em><em>allFulfilled method is theoretically the same concept.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29905032e51c46d201606d2c2351368b/href">https://medium.com/media/29905032e51c46d201606d2c2351368b/href</a></iframe><p>Not to my surprise, you can see how he went with the notion of <em>total rejection </em>within his for loop. If the Q(answerP).when() executes the error callback, the whole deferredResult promise resolves as rejected. I needed to find another way, so I kept diving.</p><h3>The Graceful Catch</h3><p>Although I felt it could be more expressive, I found this <a href="http://stackoverflow.com/a/36115549/5199198">StackOverflow inspiration</a> to be a good thrust in the right direction towards a solution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b5ed9d873b6ff39496ac7af6521c11b/href">https://medium.com/media/5b5ed9d873b6ff39496ac7af6521c11b/href</a></iframe><p>I figured that catching these errors and returning them within the array of results was the best course for me to take. Some library implementations have this already. For example, <a href="https://github.com/cujojs/when/blob/3.7.7/docs/api.md#whensettle">when.js</a> has a settle method that returns an array of objects that contain a pass-fail-like state property. But I felt I could come up with something a bit more intuitive.</p><h3>The Easy Cast</h3><p>For my implementation, I needed to return an ArrayBuffer of each file requested. A basic XMLHttpRequest can be mocked up in many different libraries but I decided I wanted to use strictly plain JavaScript. With the help of ES6’s destructured parameters, I was able to create a generic request with some default parameters that will return a Promise for me. Also, MDN always contains excellent <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest">documentation</a> in <a href="http://vanilla-js.com/">Vanilla JS</a> to build off when I’m in need of a reference.</p><p><em>NOTE: They’re still trying to make </em><a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch"><em>Fetch</em></a><em> happen. — </em><a href="http://www.imdb.com/title/tt0377092/quotes?item=qt0395835"><em>Mean Girls</em></a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f006fa60ded1ab449474635c8324603c/href">https://medium.com/media/f006fa60ded1ab449474635c8324603c/href</a></iframe><h3>The Multi-Promise Method</h3><p>Once I had that simple request method setup, I decided to create my multi-promise all method. Now I would like to add this to the Promise class simply for ease of use, but I prefer to <a href="https://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/">never overwrite an object I don’t own</a>. For sake of this example I am just going to leave it as a general method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7521d3d6ae7d664b8dcf798834aeda0/href">https://medium.com/media/a7521d3d6ae7d664b8dcf798834aeda0/href</a></iframe><p>As you can see, like Mark Miller’s allFulfilled implementation, I loop through the Promises and add the response to an array. But if I receive an error, my catchcallback will continue to add it to the array but instantiating a new Error to maintain the stack trace as well as easily identifying that as an error later on.</p><p>Since the asynchronous requests can return a response at varying times, the total Promise may resolve in a different order than the array of Promises that was passed in. To maintain the integrity of my output array, I add my responses in the index position of the promises array or the order corresponding with which they were requested and not simply pushed onto the end of the responses array.</p><h3>The Main Call</h3><p>Wrapping it all together is now as simple as creating request Promises, adding them to an array, and making my all method call on them. This total Promise will resolve with my array of responses, which can each be handled as needed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9bb09783d1aba649f511551b0ee68ad/href">https://medium.com/media/f9bb09783d1aba649f511551b0ee68ad/href</a></iframe><p>Detecting if any errors occurred only requires a simple check to see if the value is the instanceOf the Error class. Now if I wanted <em>total rejection</em> to occur, I can throwmy Error within the success function of the total Promise but I’m not forced to like with the other all methods.</p><p>Now depending on the circumstances of the initial Promises array, there may be alterations necessary to fit the specific use case. I found this solution to work well in my situation but it may not be <em>all</em> encompassing. I’m curious to see other ways in which this problem has been solved.</p><p><em>This article was originally posted on my own site, </em><a href="https://patmigliaccio.com/"><em>patmigliaccio.com</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f10dcfd85cdf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>