<?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[Backstage With Architects - Medium]]></title>
        <description><![CDATA[Tutorials and opinions on Kubernetes and Microservices! - Medium]]></description>
        <link>https://medium.com/backstagewitharchitects?source=rss----903c183e8eba---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Backstage With Architects - Medium</title>
            <link>https://medium.com/backstagewitharchitects?source=rss----903c183e8eba---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 06:39:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/backstagewitharchitects" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Autoscaling Works in Kubernetes? Why You Need To Start Using KEDA?]]></title>
            <link>https://medium.com/backstagewitharchitects/how-autoscaling-works-in-kubernetes-why-you-need-to-start-using-keda-b601b483d355?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/b601b483d355</guid>
            <category><![CDATA[autoscaling]]></category>
            <category><![CDATA[keda]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Atharva Ajgaonkar]]></dc:creator>
            <pubDate>Tue, 01 Mar 2022 12:18:36 GMT</pubDate>
            <atom:updated>2022-03-01T12:18:36.854Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CgsOEatptboVCVqEVpZ3-Q.jpeg" /></figure><p>Kubernetes is a very vast topic. The most common question asked when building cloud native apps is on <strong>Scaling </strong>⬆️.</p><p>What is scaling? What should we do to implement an efficient scaling practice? And does Kubernetes help us in this regard?</p><p>Well well, no need to get worried because I will be sharing some insights on application autoscaling and touch upon some <strong>real-life challenges faced </strong>when using the K8s autoscaler (last point of this article). So tighten up your seatbelts and get ready for the ride!!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FDqJTzjtQpOM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DDqJTzjtQpOM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FDqJTzjtQpOM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/81dfbd824fc90ec05eda9986674da90c/href">https://medium.com/media/81dfbd824fc90ec05eda9986674da90c/href</a></iframe><p>Scaling is the process of provisioning your application in such a way that it can handle changes in the incoming load like a charm. There are two types of scaling viz. cluster &amp; application level.</p><p>I’m sure you’ve heard of the <strong>Horizontal Pod Autoscaler</strong> or <strong>HPA</strong>. Its the first thing that shows up once you dive into the realm of autoscaling in Kubernetes. HPA performs autoscaling based on CPU, memory, or any external metric source.</p><p>While on the surface HPA seems to be perfect, there are a few challenges which limit it’s use for modern application.</p><h3><strong>Let’s understand where K8s HPA lacks?</strong></h3><h4><strong>HPA offers only CPU and Memory as sources out-of-the-box!</strong></h4><p>This can be a huge problem for most modern applications. It very likely that your microservice would be communicating via. one the following ways:</p><ol><li>Directly via RESTFul or gRPC APIs.</li><li>Indirectly via a messaging broker like RabbitMQ.</li></ol><p>To maintain a good QoS and prevent swamping your service when load spikes, you implement some kind of rate limiting functionality. In HTTP based APIs we use API rate limiters. For messaging brokers, we limit the number of events our service can process simultaneously.</p><p>In either case, these mechanisms <strong>prevent CPU / Memory consumption from skyrocketing</strong> making those metrics <strong>incorrect for scaling purposes</strong>.</p><h4>Adding new metrics to HPA is hard.</h4><p>The solution seems simple right? Simply add new metric sources like the pending message queue size to the HPA?</p><p>Not really 😔!</p><p>Adding new metrics sources is hard. And honestly, it just seems like too much effort to put it for such a tiny problem.</p><h4>HPA cannot scale down to zero!</h4><p>Most of you probably don’t need this. But I am a heavy user of event driven architectures. Quite a few of my pipelines are asynchronous. This means that when the load on my system is zero, I can scale down my background tasks to zero to save on costs.</p><p>Do you find this feature necessary? Let me know in the comments below!</p><p>Due to the way HPA’s scaling algorithm works, it isn’t possible to scale your applications back up from zero.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*p_vGgO41Km_HLVyBJEGjSQ.png" /><figcaption>HPA scaling algorithm</figcaption></figure><p>If your currentReplicasbecome zero, which it would when you scale to zero, your multiplier will become zero as well. This means no matter how high your load is, your desiredReplicas will always be zero.</p><p>Okay okay! Enough backstabbing HPA for now. Don’t worry, we have got an amazing Open source project to help us out!</p><p>We have got KEDA!</p><h3><strong>What is KEDA? How will it make our life easy </strong>😊 <strong>?</strong></h3><ol><li>KEDA is a Kubernetes-based Event-Driven Autoscaler.</li><li>It provides <strong>30+ built-in scalers </strong>for Kubernetes resources so that we don’t have to worry about writing custom adapters for the various metric sources we need.</li><li>KEDA gives you the awesome feature of scaling your resources to zero!! Yes, I am not kidding. KEDA can scale your resources from <strong>0 to1</strong> or<strong> 1 to 0. </strong>Scaling from <strong>1 to n and back</strong> is taken care of by HPA.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZAaq5ZHvJRVQ2YsDvRBnBA.png" /><figcaption>Architecture using KEDA as a scaler</figcaption></figure><h4><strong>Let’s talk about KEDA’s architecture!</strong></h4><p>The architecture of KEDA is simple and pretty easy to understand. KEDA has 3 important components i.e. <strong>Metric adapter</strong>, <strong>Controller </strong>&amp; <strong>Scaler</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*0jzYfsyYvXpFd9WkBirHKw.png" /><figcaption>Keda’s Architecture</figcaption></figure><ul><li>Scaler: Connects to an external component (e.g., RabbitMQ) and fetches metrics (e.g., Pending Message Queue Size).</li><li>Operator (Agent): Responsible for “activating” a Deployment and creating a Horizontal Pod Autoscaler object.</li><li>Metrics Adapter: Presents metrics from external sources to the Horizontal Pod Autoscaler.</li></ul><p><strong>How KEDA achieves “0 to 1&quot; and “1 to 0&quot; scaling </strong>🧐 <strong>?</strong></p><ul><li>Whenever KEDA’s metric adapter senses that no load exists, it scales down the Deployment to zero.</li><li>When there is no application running and the Metric adapter senses load, it scales the Deployment from 0 to 1.</li></ul><h4><strong>How can I use KEDA? Do I have to write a lot of configuration for it to work?</strong></h4><p>The answer is - not really.</p><p>KEDA provides a <strong>single CRD</strong> for all types of mapping between scaler metric and resource autoscaling logic. Let’s take the example of using Prometheus as a metric source for scaling our deployment.</p><p>Prometheus has become a standard place for storing Kubernetes metrics. So in the ScaledObject we have to add a PromQL query for autoscaling.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5d6dbf78b835fabdb1f49aa5dde3ef8/href">https://medium.com/media/a5d6dbf78b835fabdb1f49aa5dde3ef8/href</a></iframe><p>Just by adding a single line of PromQL query, we can now autoscale our resources based on the value the query returns.</p><p>Let’s now dive into some of the configs of ScaledObject</p><ul><li>pollingInterval: This is the interval to pull metrics on. KEDA will check each trigger source on that ScaledObject and scale the deployment up or down accordingly.</li><li>coolDownPeriod : It the period to wait before scaling down to zero occurs.</li><li>minReplicaCount: Minimum replicas your application demands. By default, it is 0.</li></ul><p>Well, the Prometheus autoscaler was really cool!. But now things will become more interesting when we see the next scaler in action.</p><p>At the time of sales, Amazon and other e-commerce platforms face a lot of traffic. So with the help of KEDA, we can autoscale based on a <strong>cron schedule</strong>. We can basically preemptively scale up the replica count on a fixed schedule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbbcbe818824f3890fbae56926499094/href">https://medium.com/media/cbbcbe818824f3890fbae56926499094/href</a></iframe><p>In the above config, we have to specify the timezone and time span during which KEDA will scale your resource to the desired replicas.</p><p>With the help of the cron scaler, you can prepare to manage traffic in advance.</p><h4><strong>Let’s now talk about some REAL LIFE CHALLENGES one might face while using KEDA and how to overcome them?</strong></h4><p>Let’s say your application is running a video transcoding job &amp; every workload is transcoding a video which takes around 8 hours &amp; your queue contains 1000&#39;s of such events. The following image demonstrates the progress bar for each video.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*NGs8dHDxRlaKQA_CtMGCyw.png" /></figure><p>Now the problem will start when Queue events start slowing down, HPA starts to scale down your resource</p><p>The problem is that <strong>HPA doesn&#39;t know the progress of transcoding each video, </strong>so it’s just going to snap its fingers like Thanos and will kill a random worker.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/515/1*qzJ8Jkrjjr-7a_-8QSqwhw.png" /></figure><p>To avoid this problem you can have 2 solutions :</p><ol><li>Use Kubernetes lifecycle management hooks SIGTERM , which you can leverage to delay termination.</li><li>Instead of creating deployment with respect to the event, we can create JOB and scale that using KEDA’s ScaledJobobject. This way, it can control the parallelism &amp; those jobs can run till completion.</li></ol><h3><strong>Conclusion</strong>:</h3><ul><li>KEDA is a lightweight component that can be added to any Kubernetes cluster to extend its functionality. It solves the modern world application autoscaling problems that K8s HPA is unable to solve.</li><li>It provides extensibility by giving the ability to write custom event sources.</li><li>KEDA can scale from “0 to 1” and back depending on workloads, optimizing the cost of infrastructure.</li></ul><p><strong>References</strong>:</p><ul><li><a href="https://keda.sh/">KEDA</a></li><li><a href="https://cloudblogs.microsoft.com/opensource/2020/05/12/scaling-kubernetes-keda-intro-kubernetes-based-event-driven-autoscaling/">Scaling Kubernetes: Intro to Kubernetes-based event-driven autoscaling (KEDA)</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b601b483d355" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/how-autoscaling-works-in-kubernetes-why-you-need-to-start-using-keda-b601b483d355">How Autoscaling Works in Kubernetes? Why You Need To Start Using KEDA?</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to setup Monitoring using Prometheus and Grafana]]></title>
            <link>https://medium.com/backstagewitharchitects/how-to-setup-monitoring-using-prometheus-and-grafana-9ba7bf8cbda9?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/9ba7bf8cbda9</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[grafana]]></category>
            <category><![CDATA[monitoring]]></category>
            <category><![CDATA[prometheus]]></category>
            <dc:creator><![CDATA[Noorain - YourTechBud]]></dc:creator>
            <pubDate>Tue, 23 Feb 2021 08:11:21 GMT</pubDate>
            <atom:updated>2021-02-23T08:11:20.907Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0J6gmeEh4oqlq-B_9qYVHg.png" /></figure><p>So you have finally written and deployed your first microservice? Or maybe you have decided to embark on the microservices adventure to future proof yourself? Either way, Congratulations!!!</p><p>It’s time to take the next step. Its time to set up monitoring!</p><h3>Why setting up monitoring is important?</h3><p>Monitoring is super important. It is that part of your system that lets you know what’s going on in your app.</p><p>And it isn’t just a dashboard with fancy charts.</p><blockquote>Monitoring is the systematic process of aggregating actionable metrics and logs.</blockquote><p>The keyword here is <strong>actionable</strong>.</p><p>You are collecting all these metrics and logs to <strong>take decisions based on it</strong>.</p><p>For example, you would want to collect the health of your VMs &amp; microservices to ensure you have enough healthy capacity to service user requests. You’d also like to trigger emails &amp; notifications in case failures go below a certain threshold.</p><p>This is what monitoring helps you achieve. This is why you need to set up monitoring.</p><p>As its clearly evident, <strong>your monitoring stack is a source from which several processes can be automated.</strong> So its really important to make sure your monitoring stack is reliable and that it can scale with your application.</p><p><a href="https://prometheus.io/">Prometheus</a> has become the go-to monitoring stack in recent times.</p><p>It’s novel pull-based architecture, along with <a href="https://prometheus.io/docs/alerting/latest/overview/">in-built support for alerting</a>, makes it an ideal choice for a wide variety of workloads.</p><p>In this article, we’ll use Prometheus to setup up monitoring. For visualisations, we’ll use <a href="https://grafana.com/oss/grafana/">Grafana</a>.</p><p>This guide is available in video format as well. Feel free to refer to it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FjUhacrwEwDk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DjUhacrwEwDk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FjUhacrwEwDk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/58d6015aad86ea93e9b070e34009ad77/href">https://medium.com/media/58d6015aad86ea93e9b070e34009ad77/href</a></iframe><p>I’ve set up a <a href="https://github.com/YourTechBud/devops-guide/tree/master/monitoring-microservices-docker">GitHub repository</a> for you guys as well. Use it to reproduce everything we’ll be doing today.</p><h3>What is Prometheus and Grafana?</h3><p>Prometheus is a metrics aggregator.</p><blockquote>Instead of your services pushing metrics to it, like in the case of a database, Prometheus pulls metrics from your services.</blockquote><p>It expects services to make an endpoint exposing all the metrics in a particular format. All we need to do now is tell Prometheus the address of such services, and it will begin scraping them periodically.</p><p>Prometheus then stores these metrics for a configured duration and makes them <a href="https://prometheus.io/docs/prometheus/latest/querying/examples/">available for querying</a>. It pretty much acts like a time-series database in that regard.</p><p>Along with that, Prometheus has got first-class support for alerting using <a href="https://prometheus.io/docs/alerting/latest/alertmanager/">AlertManager</a>. With AlertManager, you can send notifications via Slack, email, PagerDuty and tons of other mediums when certain triggers go off.</p><p>All of this makes it super easy to set up monitoring using Prometheus.</p><p>While Prometheus and AlertManager provided a well-rounded monitoring solution, we need dashboards to visualise what’s going on within our cluster. That’s where Grafana comes in.</p><p>Grafana is a fantastic tool that can create outstanding visualisation.</p><p>Grafana itself can’t store data. But you can <a href="https://grafana.com/docs/grafana/latest/datasources/">hook it up to various sources</a> to pull metrics from including Prometheus.</p><p>So the way it works is, we use an aggregator like Prometheus to scrap all the metrics. Then we link Grafana to it to visualise the data in the form of dashboards.</p><p>Easy!</p><h3>What we’ll be monitoring?</h3><h4>Microservices</h4><p>We’ll be monitoring two microservices today.</p><p>The first microservices is written by a math genius to perform insane calculations. By insane calculations, I mean adding two numbers.</p><p>It’s a simple HTTP endpoint expecting two numbers in the request and responds back with the addition of those two numbers.</p><p>The second one is a polite greeter service that takes a name as a URL parameter and responds with a greeting.</p><p>Here’s what the endpoints look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8c2cab6ec0a3dc41be2165d66161744/href">https://medium.com/media/a8c2cab6ec0a3dc41be2165d66161744/href</a></iframe><p>And here’s the link to the code:</p><ul><li><a href="https://github.com/spaceuptech/space-cloud-samples/tree/master/basic-service">Math Service</a>: Written in Node.js</li><li><a href="https://github.com/spaceuptech/space-cloud-samples/tree/master/greeter">Greeter Service</a>: Written in Golang</li></ul><p>Both these services will be running inside <a href="https://www.docker.com/">Docker</a>. Neither one of them have any code related to monitoring whatsoever. In fact, they don’t even know that they are being monitored.</p><p>Wait that doesn’t sound right.</p><h4>Metrics</h4><p>We will be monitoring a couple of metrics. This is what the final dashboard will look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XL2GbXgSHenodp-2.png" /></figure><p>First is the <strong>CPU &amp; memory utilisation of our services</strong>. Since all our services will be running in Docker, we’ll use <a href="https://github.com/google/cadvisor">cAdvisor</a> to collect these metrics.</p><p>cAdvisor is a really neat tool. It collects container metrics directly from the host and makes it available for Prometheus to scrape. <em>You don’t really need to configure anything for it to work.</em></p><p>We’ll also be <strong>collecting HTTP metrics</strong>. These are also called l7 metrics.</p><p>What we are interested in is the <em>requests coming in per second grouped by response status code</em>. Using this metric alone, we can infer the <strong>error rates</strong> and <strong>total throughput</strong> of each service.</p><p>Now we could modify our services to collect these metrics and make them available for Prometheus to scrape. But that sounds like a lot of work. Instead, we will use a reverse proxy like <a href="https://haproxy.com/">HAProxy</a> in front of our services which can collect metrics on our behalf.</p><p>The reverse proxy plays two roles, it exposes our apps to the outside world, and it collects metrics while it’s at it.</p><p>Unfortunately, HAProxy doesn’t export metrics in a Prometheus compatible format. Luckily the community has built an <a href="https://github.com/prometheus/haproxy_exporter">exporter</a> which can do this for us.</p><p>We have a small problem with this setup.</p><p>HAproxy won’t capture metrics for <a href="https://medium.com/spaceuptech/how-to-make-microservices-communicate-84b09d077c96">direct service to service communication</a> since that will bypass it altogether.</p><p>We can solve this loophole entirely by using something we call a service mesh. You can read more about it in <a href="https://blog.spaceuptech.com/posts/why-your-microservices-need-a-service-mesh/">this article</a>.</p><h3>Our Final Monitoring Setup</h3><p>Our final monitoring setup using Prometheus and Grafana will look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yAI9hOXbtQoz31yY.png" /></figure><h3>Deploying our Monitoring Stack</h3><p>Finally, it’s time to get our hands dirty.</p><p>To get our services up, we’ll write a docker-compose file. Docker-compose is an awesome way to describe all the containers we need in a single YAML file. Again, all the resources can be found in <a href="https://github.com/YourTechBud/devops-guide/tree/master/monitoring-microservices-docker">this GitHub repo</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b27972c39e0362554cc8763690193e1/href">https://medium.com/media/6b27972c39e0362554cc8763690193e1/href</a></iframe><p>I have also gone ahead to make custom config files for <a href="https://github.com/YourTechBud/devops-guide/blob/master/monitoring-microservices-docker/prometheus.yml">Prometheus</a> and <a href="https://github.com/YourTechBud/devops-guide/blob/master/monitoring-microservices-docker/haproxy.cfg">HAProxy</a>.</p><p>For Prometheus, we are setting up scrapping jobs for cAdvisor and HAProxy exporter. All we need to do is to give Prometheus the host and port of the targets. If not provided explicitly, Prometheus fires HTTP requests on the /metrics endpoint to retreive metrics.</p><p>For HAProxy, we configure one backend for each service. We will be splitting traffic between the two microservices based on the <em>incoming request&#39;s url</em>. We&#39;ll forward the request to the greeter service if the url begins with /greeting. Otherwise, we forward the request to the math service.</p><h4>Bringing our services online</h4><p>Now that we have described all our services in a single docker-compose file, we can copy all of this on our VM, ssh into it and then run docker-compose -p monitoring up -d.</p><p>That’s it.</p><p>Docker will get everything up and running.</p><p>These are the ports all the exposed services will be listening on. Make sure these ports are accessible.</p><ul><li>Prometheus: 9090</li><li>Grafana: 3000</li><li>HAProxy: 11000</li></ul><p>We can check if Prometheus was configured correctly by visiting http://YOUR_IP:9090/targets. Both the targets (cAdvisor and HAProxy exported) should be healthy.</p><p>We can check if our proxy is configured properly by opening the following urls:</p><ul><li>Math Service: http://YOUR_IP:11000/add/1/2</li><li>Greeter Service: http://YOUR_IP:11000/greeting/YourTechBud</li></ul><h3>Setting up our Monitoring Dashboard</h3><p>Then the last step remaining would be to configure Grafana.</p><p>Visit http://YOUR_IP:3000 to open up Grafana. The default username and password will be admin.</p><h4>Add Prometheus as a data source</h4><p>All our monitoring metrics are being scrapped and stored in Prometheus. Hence, the <em>first step is to add Prometheus as a datastore</em>.</p><p>To do that, Hit Add a datasource &gt; Select Prometheus from the dropdown &gt; Enter http://prometheus:9090 as the Prometheus url &gt; Hit Save &amp; Test.</p><p>That’s all we need to do to link Grafana with Prometheus.</p><h4>Create our dashboard.</h4><p>Creating a dashboard from scratch can take time. You can <a href="https://github.com/YourTechBud/devops-guide/blob/master/monitoring-microservices-docker/grafana-dashboard.json">import the dashboard</a> I’ve already made to speed things up.</p><p>After hitting the Import Dashboard button in the Manage Dashboards section, simply copy and paste the JSON in the text area and select Prometheus as the data source.</p><p>That’s it.</p><p>Feel free to refer to this <a href="https://youtu.be/jUhacrwEwDk">10 minute video</a> if you get lost.</p><h3>Wrapping Up</h3><p>You’ve just setup monitoring using Prometheus and Grafana.</p><p>You can edit a chart to see what I’ve done. You’ll see that all it takes to populate a chart is a Prometheus query.</p><p>Here’s one query as an example: sum(container_memory_usage_bytes{name=~\&quot;monitoring_svc.*\&quot;} / 1024 / 1024) by (name). This query calculates the total memory being consumed by our two microservices.</p><p>I agree that the Prometheus queries can be a bit overwhelming. But that doesn’t really matter. You can simply import this dashboard and expect it to just work.</p><p>Use it as a boilerplate. Mess around with it. Enjoy!</p><p><em>Did this article help you? How do you make sure your apps are cloud-native? Share your experiences below</em>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ba7bf8cbda9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/how-to-setup-monitoring-using-prometheus-and-grafana-9ba7bf8cbda9">How to setup Monitoring using Prometheus and Grafana</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is DevOps? A DevOps Tutorial in Plain English]]></title>
            <link>https://medium.com/backstagewitharchitects/what-is-devops-a-devops-tutorial-in-plain-english-f17b9ead2bdd?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/f17b9ead2bdd</guid>
            <category><![CDATA[continous-deployment]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[devops-approach]]></category>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Noorain - YourTechBud]]></dc:creator>
            <pubDate>Sun, 24 Jan 2021 16:13:53 GMT</pubDate>
            <atom:updated>2021-01-24T16:18:13.923Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bz51EfXP33PsgvrT6Re_pw.jpeg" /></figure><p>DevOps… CI/CD… Docker… Kubernetes… I’m sure you’ve been bombarded with these words a lot the past year. Seems like the entire world is talking about it. The rate at which this segment is progressing, it won’t be long before we reach the stage of NoOps.</p><p>Don’t worry. It’s okay to feel lost in the giant sea of tools and practices. It’s about time we break down what DevOps really is.</p><p>The objective of this article is to set a solid foundation for you to build on top of. So let’s start with the obvious question.</p><h3>What is DevOps?</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FyczwWzkbFAQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DyczwWzkbFAQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyczwWzkbFAQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f48f5a737ada77b74cd3d877d78665a/href">https://medium.com/media/9f48f5a737ada77b74cd3d877d78665a/href</a></iframe><blockquote>DevOps is the simplification or automation of <strong>established IT processes.</strong></blockquote><p>You can probably see where I’m going from here.</p><p>I’ve seen so many people start this journey to adopt DevOps to only find themselves lost. There seems to be a pattern to this.</p><p>It usually starts with a video on how a fancy tech startup has automated its release cycle. Deployments happen automatically once all the tests pass. Rollbacks, in case of failures, is automatic. Thousands of simultaneous A/B test is driving up customer engagement.</p><p>Let’s be honest. We all want to achieve DevOps “Moksha”.</p><p>We all are tired of releasing a new version like its a rollercoaster ride.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uYeB0MQ3Yyr1R6oa.png" /></figure><p>Unfortunately, DevOps doesn’t work that way. DevOps isn’t a magic wand which can solve all your problems in an instant.</p><p>Instead, it is a systematic process of choosing the right tools and technology to get the job done.</p><p><strong>All of this starts with a process.</strong></p><p>It doesn’t matter what the process is. It could be simplifying the deployment of your app or automate testing. Whatever your process is, the act of making your life easier is what DevOps is all about.</p><p>But you always need to start with a process.</p><p>In fact, <em>if your process cannot be done manually (on a smaller scale), you should probably re-examine your process.</em></p><p>I mean it.</p><p>Enough talking. Let’s take a real world example to understand things better.</p><h3>Let’s take a Real DevOps Example</h3><p>Let’s take a simple example of <em>making a </em><a href="https://nodejs.org/en/"><em>Nodejs</em></a><em> app live on a VM in the cloud</em>.</p><h4>The Process</h4><p>Here’s what our process looks like:</p><ul><li><strong>Start with the source code:</strong> This is our source of truth. We can run our process from anywhere as long as we have access to the source code.</li><li><strong>Build an Artifact:</strong> We then package our source code to build an <a href="https://en.wikipedia.org/wiki/Artifact_(software_development)">Artifact</a>. In case of a compiled language, the compiled output (JAR file in case of JAVA) would be our artifact. In our case, our source code itself is the artifact to be released.</li><li><strong>Publish to an Artifact Repository:</strong> Next, we push our artifact to a repository. This is a location from where our target environment can pull the artifact from. We could stick with something like <a href="https://github.com/">Github</a> since we are working with source code here.</li><li><strong>Pull and run your app:</strong> Finally, we pull the artifact onto our VM and schedule a Nodejs process by running npm start.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DifRZJzeqoA3frZ6.png" /></figure><p>It’s okay if you do things a slightly different way. We are here to focus on the journey and not the destination.</p><h4>Our first DevOps Project</h4><p>Let’s not do anything fancy here.</p><p>The easiest way to automate this process would be to write a simple <a href="https://en.wikipedia.org/wiki/Shell_script">shell script</a> to run all the commands in sequence.</p><p>Congratulations!!! That’s our first DevOps project!!! 🥳🎉</p><p>I know shell scripts sounds too simple to be taken seriously. I suspect you already have such scripts in place. But believe me, that’s DevOps!</p><p>Don’t worry, we will get to the fancy stuff in a minute. But it’s important to understand that this is how DevOps works.</p><h3>Importance of Repeatability</h3><p>Let me ask you one question. Which one of these would you prefer?</p><ul><li>An automated deployments pipeline which works 60% of the time or</li><li>A boring shell script which gets the job done every time its executed</li></ul><p>If you have dealt with production failures in the middle of the night, you’ll choose the shell script.</p><p>The reason is simple.</p><p><strong>Reliability is far more critical than degree of automation.</strong></p><p>In other words,</p><blockquote>A DevOps process must be able to produce consistent results every time it’s run.</blockquote><h4>Making our process repeatable</h4><p>Let’s take the example of our shell script.</p><p>Currently, our shell script depends on Nodejs to be installed on the VM we want to deploy the app to.</p><p>What would happen if the Nodejs runtime was missing? These days, an incorrect version of the runtime is enough to break our application.</p><p>This problem only gets worse in <a href="https://en.wikipedia.org/wiki/Polyglot_(computing)">polyglot environments</a> where we deal with multiple programming languages.</p><p>A simple solution would be to <em>archive the Nodejs runtime along with our source code</em> in a zip file. The zip file can then be sent to the VM. This way, the VM can use the local Nodejs runtime present in the archive to run our app.</p><p>Luckily, there is a tool to make our lives easier.</p><h4>In comes Docker and Containers</h4><p>If you are new to this, think of <a href="https://www.docker.com/">Docker</a> as a way to package your artifact along with all its OS dependencies, including Nodejs, into a container image.</p><p>Using containers, we can deploy any application on a VM which has Docker installed.</p><p>With Docker, our flow will look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LMn_LBm6WntKWyrU.png" /></figure><p>There is a lot more to containers than just this. However, this was one of the reasons why containers got so popular.</p><h4>Docker Vs Containers</h4><p>Let me clarify this. Docker and containers are not the same things anymore.</p><blockquote>Docker is a set of utility tools to build and ship container images which container runtimes like <a href="https://containerd.io/">containerd</a> use to make and run containers.</blockquote><p>Many are concerned about the future of Docker given the <a href="https://kubernetes.io/blog/2020/12/02/dont-panic-kubernetes-and-docker/">recent events</a> which have taken place.</p><p>It is important to understand that Docker is not going anywhere anytime soon. It <em>provides the best DX</em> and will <em>continue to play a major role in building and shipping container images</em>.</p><h3>Getting Serious with DevOps</h3><p>We have made some serious progress already. Hopefully, we understand how Docker fits into the DevOps process.</p><p>Its time to take things to the next level.</p><h4>Triggering deployment based on events</h4><p>Our script looks pretty solid but it’s still triggered manually.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EUpLfoFiqVjpuEtX.png" /></figure><p>Wouldn’t it be great if we could trigger this script automatically whenever someone pushes code on GitHub? In other words, we want to trigger this script on an event.</p><p>GitHub can <a href="https://docs.github.com/en/developers/webhooks-and-events">invoke webhooks</a> on a certain <a href="https://docs.github.com/en/developers/webhooks-and-events/events">set of events</a>.</p><p>To achieve this, we need to make a simple HTTP server that executes our shell script whenever its endpoint is hit. We can configure GitHub to hit our endpoint on the <a href="https://docs.github.com/en/developers/webhooks-and-events/github-event-types#pushevent">Push Event</a>.</p><p>Let’s call this server <em>Colorful Daemons</em> or <em>CD</em>.</p><p>Our new flow will look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*toNMvDuJcOul9Zcc.png" /></figure><p>Congratulations! You just set up what we call a <em>CD pipeline</em>.</p><p>And no… I don’t mean Colorful Daemons. I’m talking about <a href="https://en.wikipedia.org/wiki/Continuous_deployment">Continous Deployments</a>.</p><blockquote>Continuous Deployments is a piece of software responsible for taking your app from something like GitHub all the way to your target environment where it finally gets deployed.</blockquote><p>This is basically the CI/CD stuff you keep hearing about. When people talk about tools like <a href="https://www.jenkins.io/">Jenkins</a> and <a href="https://circleci.com/">CircleCI</a>, they are usually referring to CI/CD.</p><p>What we just made with Colorful Daemons was a continuous deployments pipeline. Don’t confuse it with continuous integration or delivery. We’ll get to those some other day.</p><h3>The DevOps pattern</h3><p>I guess you’ve already found a pattern here. We start with a process, find a section we aren’t happy with and then introduce some software component to simplify or automate it.</p><p>That’s getting the <em>dev in ops</em>. And that’s all that there is to it.</p><p>This is the real answer to the question, <em>“What is DevOps?”</em>.</p><h3>Introducing Container Orchestration</h3><p>Let’s finish up by making one small improvement.</p><p>Till now, we have been dealing with deploying our app to a single VM or a single node. What if we wanted to <em>deploy our app to multiple nodes?</em></p><p>The easiest way to achieve this would be to modify our CD server to ssh into all the VMs and deploy our container to each one of them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*95t-85Kz-18YKxDa.png" /></figure><p>While this method works, <strong>we’ll need to change our script every time our infrastructure changes</strong>. In a world where <em>applications are always autoscaling</em> and <em>VMs are considered disposable</em>, this is unacceptable.</p><p>A better way would be to make another HTTP server to track infrastructure changes. We can call this server, <em>“Pilot”</em>.</p><p>This server will be responsible for performing health checks on the various VMs in our cluster to maintain a list of active VMs. It could even communicate with the cloud vendor to make things more robust.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ikdE5KTs-VXxJbOZ.png" /></figure><p>Pilot will expose an endpoint as well, to accept the details of the container to spawn. It can then talk to the various VMs to get the job done.</p><p>Now, our CD server can simply request Pilot instead of talking to each VM individually.</p><p>Our new flow will look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WlhwL9eOL6U1naue.png" /></figure><p>The second server, Pilot, is called a container orchestrator. That’s what <a href="https://kubernetes.io/">Kubernetes</a> is!</p><p>You just designed a mini version of Kubernetes!</p><p>Also, Kubernetes is greek for Pilot. Isn’t that a pleasant co-incidence?</p><h3>Where to Start?</h3><p>We covered quite a few tools together. This brings me to my last point. Ever wondered why the DevOps space is so fragmented?</p><p>If you think about it. There are so many tools out there making it hard to decide what’s the right choice or where you should even start?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Yp3Z9gmWBTXFpkX2.png" /></figure><p>Every organisation has their own way, their own process to do things, And since their paths are different, the tools they need to use are different.</p><p>Your job is not to find which tool is the best. <em>Your job is to find what process works for you best.</em> Once you have that figured out, the tools are just a google search away.</p><p>So now you know where to start. It’s not with the tools out there.</p><blockquote>Start by understanding how your company &amp; teams does things.</blockquote><p>I’m literally asking you to open up a Word document, and copy-paste the commands you need to run to do stuff.</p><h3>Wrapping up</h3><p>I hope this post has been helpful in understanding how the DevOps field is arranged and how different tools depend and coexist with each other.</p><p>I’d like to add:</p><blockquote>Your DevOps process is only as strong as its foundation.</blockquote><p>So work on the underlying process. It’s okay if you need to tweak your current process a bit.</p><p>An excellent foundation to build upon could be using tools like <a href="https://spaceuptech.com/">SpaceCloud</a>. Space Cloud is a Kubernetes based serverless platform which helps you develop, deploy and secure cloud-native applications.</p><p>In a nutshell, SpaceCloud gives you <em>an excellent starting point to build your DevOps practices on top of</em>. It makes performing rolling upgrades, canary deployments and autoscaling your applications easy. You can configure everything using the space-cli or REST APIs.</p><p>Enough of me bragging, <a href="https://docs.spaceuptech.com/microservices/deployments/">check out what Space Cloud is capable of</a> for yourself!</p><p>You can also support us in our mission by <a href="https://github.com/spaceuptech/space-cloud">giving us a star on Github</a>.</p><p><em>Did this article help you? How do you make sure your apps are cloud-native? Share your experiences below</em>.</p><p><a href="https://storyset.com/"><em>Illustrations by Freepik Storyset</em></a></p><p><em>Originally published at </em><a href="https://blog.spaceuptech.com/posts/what-is-devops/"><em>https://blog.spaceuptech.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f17b9ead2bdd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/what-is-devops-a-devops-tutorial-in-plain-english-f17b9ead2bdd">What is DevOps? A DevOps Tutorial in Plain English</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How We Moved To Kubernetes From VMs]]></title>
            <link>https://medium.com/backstagewitharchitects/how-we-moved-to-kubernetes-from-vms-c45dfbc0ff5e?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/c45dfbc0ff5e</guid>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Noorain - YourTechBud]]></dc:creator>
            <pubDate>Sat, 13 Jun 2020 12:31:01 GMT</pubDate>
            <atom:updated>2020-06-13T12:31:01.158Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gt8Zwzr5pn8jbfh31cQ43g.jpeg" /></figure><p>We are living in the containers era right now. Everyone seems to be using them. Those who aren’t already are on the journey to adopt it.</p><p>The thing is, containers alone don’t solve all your problems. You need a way to run them.</p><p>And what’s the best way to run containers?</p><p>It’s Kubernetes!!!</p><p>Kubernetes has clearly won the orchestration wars. All major cloud providers like <a href="https://cloud.google.com/kubernetes-engine">Google</a>, <a href="https://aws.amazon.com/eks/">AWS</a>, <a href="https://azure.microsoft.com/en-in/services/kubernetes-service/">Azure</a> and even <a href="https://www.digitalocean.com/products/kubernetes/">DigitalOcean</a> offer a managed Kubernetes solution.</p><p>We recently moved to Kubernetes from VMs at <a href="https://spaceuptech.com/">SpaceUpTech</a>. In this article, I’ll be talking about how we achieved precisely that.</p><p>I must add that this isn’t a getting started with Kubernetes guide. This is more of a moving to Kubernetes guide. However, if you are new, you can use it to note the steps you’ll need to follow.</p><h3>Always Start with Preparations</h3><p>Let’s start with this.</p><p>Moving to Kubernetes is not a one step process.</p><p>You can’t move to Kubernetes overnight. That’s just the way it is.</p><p>This means that this isn’t the usual X mins to achieve Y guide. You will have to invest some time in carefully planning on how to go about it.</p><p><strong>Moving to Kubernetes is like moving to another apartment.</strong></p><p>You need to start by packaging all your furniture and belongings. Then you move them to the new apartment without breaking anything. And trust me, things break!</p><p>But you can’t just unpack everything yet.</p><p>You might want to make some adjustments to the new apartment like re-painting everything or what not.</p><p>And let’s not forget the most difficult part. You need to decide what goes where!!</p><p>If you are lucky and have more or less the same layout, you will be able to move things as is. If you aren’t that lucky, you might have to re-architect things here and there.</p><p>Those who have the luxury of time and money choose to use this opportunity to buy everything and start afresh.</p><p>The point is, you’ll have to plan this out to get things done.</p><p>You might be thinking that given my deep knowledge in setting up apartments, I might be some real-estate guy. You are partially correct. I am an Architect. A Cloud Architect! 😜</p><p>Keeping my lame jokes aside you probably know what I’m talking about. I bet you have already associated each activity I’ve mentioned with a task you’ll need to do.</p><p>So let’s get to it! Let’s move our application to Kubernetes.</p><h3>Package Everything As Containers</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-D60ae7PpxBYVMVJ.png" /></figure><p>You need to have a count of what services you are running. You also need to know what are their dependencies.</p><p>Whether you choose to build everything from scratch or not, you cannot avoid this step.</p><p>Kubernetes runs containers not code. In short, you’ll need to package your code/artifacts to containers as the first step.</p><h3>Know your dependencies</h3><p>I think it’s important to cover this topic. This is where we had made a major mistake.</p><p>While building a Docker image for each one of your services, you need to install the dependencies required by that service.</p><p>For example, your Spring app needs the JVM to run. Similarly, Nodejs would require… well… Nodejs! 😅</p><p>You might also need to install OS level packages in some cases.</p><p>But dependencies are not just limited to things you need to include in your docker image. There is a little more to it.</p><p>We learned it the hard way.</p><p>One of the services we ran was a gateway which exposed our Rest APIs and database via. GraphQL. You might already be aware of this as we have released it as an open source project called <a href="https://github.com/spaceuptech/space-cloud">Space Cloud</a>.</p><p>Our gateway relied on a config file. Initially, we packaged this config file as a part of the docker image.</p><p>This was a terrible decision.</p><p>We had to redeploy the image every time we made a change in the config file. It used to get super annoying to wait for the docker image to get built and uploaded.</p><p>Another one of our services needs to speak to all of its replicas to keep certain data in sync. In essence, we had a dependency on the IP of each instance of that service. We needed the IP addresses for each replica to be constant.</p><p><strong>Things like storage and network dependencies need to be identified upfront.</strong></p><p>It might not influence the way you build your containers. But it does influence how you run them. Documenting these little things will help you out in the next step.</p><p>To sum things up.</p><p>List down the environment, storage and network dependencies for each one of your containers.</p><p>So go ahead and do this for any one service. I’ll wait!</p><p>.</p><p>.</p><p>.</p><p>Don’t cheat.</p><p>.</p><p>.</p><p>.</p><p>Seriously!?</p><p>Go do it for any one service.</p><p>.</p><p>.</p><p>.</p><p>Hmmmm…</p><p>.</p><p>.</p><p>.</p><p>So you finally decided to not do it.</p><p>For those who did, you deserve a pat on the back. And those who didn’t, you’ve hurt my feelings! 😢</p><p>Just building docker images isn’t enough. You need to push them to a docker registry.</p><p>This is important.</p><p>We will have to provide the url of the docker image to Kubernetes so it knows what image to pull from where.</p><p>I usually recommend using the Docker registry provided by the cloud vendors. They usually have direct integration with Kubernetes so you can pull private images without having to provide credentials.</p><p>You can use Docker Hub for all public images.</p><h3>Mapping Your Requirements To Kubernetes</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6J4BAH0Dn5K3FKir.png" /></figure><p>Okay, its time to get serious now. This is by far the most intensive step.</p><p>Now is a good time to brush up your Kubernetes skills before we move forward. If you are new to the Kubernetes world, don’t worry. I’ll try to make this guide as complete as possible.</p><p>I will be linking out to specific pages in the Kubernetes docs for more information.</p><p>Luckily, we don’t need to worry about setting up Kubernetes. As I mentioned above, most cloud vendors provide a managed Kubernetes solution. Gone are the days of doing <a href="https://github.com/kelseyhightower/kubernetes-the-hard-way">Kubernetes the hard way</a>. 😜</p><blockquote><strong><em>I would encourage you to check out that guide if you are interested in learning the nuts and bolts of Kubernetes.</em></strong></blockquote><p>We chose GCP primarily because Google was the one who came up with Kubernetes. No other reasons.</p><p>So lets get to mapping your services to Kubernetes.</p><h3>Stateful or Stateless?</h3><p>This is the burning question.</p><p>All the hard work you’ve put in listing down your dependencies is gonna pay off now.</p><p>Oh wait! I remember… You didn’t. You can still go back and do it now! 🙃</p><p>Answering the stateful vs stateless question is important. Kubernetes has different operators to manage both of them. So how can you decide whether your application is stateful or stateless?</p><p>Its easy.</p><p>Just have a look at your dependencies!</p><p>Okay Okay. Let me elaborate a bit.</p><p>If your service depends on a resource beyond its own lifecycle, your service is stateful.</p><p>Let’s take a few examples.</p><ul><li>A database depends on the files it created in the local filesystem. These files are required even after the database is restarted. The database will use these files to resume from where it left off.</li><li>An in-memory pub-sub needs to know the existence of its peer nodes. To enable auto-discovery, its important for the seed nodes to have static IP addresses or DNS names. These addresses need to be static even after the peers restart.</li></ul><p>The examples above have state in the form of storage and network dependencies respectively. More importantly, their dependency exists beyond their own lifecycle.</p><p>You must always default to stateless services unless they have dependencies which exist beyond their own lifecycle</p><p>The reason is simple.</p><p><strong>Stateless services are easier to scale and manage.</strong></p><p>At SpaceUpTech, our initial inclination was towards deploying each service as a stateful one. We did this in order to be <em>future ready</em>.</p><p>If it isn’t already clear, this wasn’t the right attitude.</p><p>There are several ways you can convert seemingly stateful services to stateless.</p><ul><li><strong>Configuration metadata is not state.</strong> Many people mistake configuration metadata as state. That’s not true. <em>Store configuration metadata as </em><a href="https://kubernetes.io/docs/concepts/configuration/configmap/"><em>ConfigMaps</em></a><em> or </em><a href="https://kubernetes.io/docs/concepts/configuration/secret/#use-cases"><em>Secrets</em></a><em>.</em> Kubernetes can mount these as files or environment variables at runtime.</li><li><strong>Move sessional data to an external store.</strong> It’s not a good practice to store sessional information like session ids within your service. It’s wise to move this to redis or memcache. This will make scaling the service a lot easier as well.</li><li><strong>Don’t consider cache as state.</strong> We cache certain objects aggressively for performance. Initially, we would consider the services which cached data as stateful. Practically speaking, this is not required.</li></ul><h3>Working with stateless services</h3><p>In Kubernetes, each instance of your service runs as a <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/">Pod</a>. To be accurate, a pod consists of one or more containers.</p><p>You might be thinking why do we need that?</p><p>In many cases, we require running worker containers adjacent to our container for secondary tasks. I’m talking about tasks like exporting logs, monitoring health, etc.</p><p>You don’t need to worry about all of this now.</p><p>But the thing is, <strong>there is no mechanism in the pod itself for scaling and self healing</strong>. This does makes sense. Both of these elements are outside the scope of a single pod’s lifecycle.</p><p>But we do want all the awesomeness of defining the replicas we want for our services right? And why are we even moving to Kubernetes if services can’t be restarted on infrastructure failures?</p><p>Worry not. Lord Kubernetes has us covered!</p><p>Kubernetes provides a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/">Deployments controller</a>. To be more specific, <strong>a Deployment provides declarative updates for Pods</strong>.</p><p>This means you describe the desired state for a specific deployment, like the env variables to apply, the number of replicas required and the docker image to use. Now it’s the Deployments Controller’s responsibility to achieve the desired state.</p><p>So if a VM fails for any reason, the Deployments controller will automatically start new pods to maintain the required number of replicas in the cluster.</p><p>Pretty cool right?</p><p>A deployments object looks something like this.</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: my-service<br>  labels:<br>    app: my-service<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      app: my-service<br>  template:<br>    metadata:<br>      labels:<br>        app: my-service<br>    spec:<br>      containers:<br>      - name: my-service<br>        image: my-org/my-service-image:0.1.0<br>        env:<br>          # Define the environment variable<br>          - name: SOME_VAR<br>            valueFrom:<br>              configMapKeyRef:<br>                # The ConfigMap containing the value you want to assign to SOME_VAR<br>                name: my-config<br>                # Specify the key associated with the value<br>                key: some-key<br>        ports:<br>        - containerPort: 8080</pre><p>As you can see, in this example I’ve created a deployment object named my-service.</p><p>This deployment will deploy 3 replicas of single container pods. The image to be used is my-org/my-service-image:0.1.0. I&#39;m also mounting an environment variable from a configMap named my-config.</p><p>Kubernetes config is generally stored as YAML files and can be applied using the kubectl apply -f &lt;filename&gt;.yaml command.</p><p>We need to keep in mind that this only deploys a service. It doesn’t provide our pods a dns name which can be used to access our pods.</p><p>For that, we need to create a <a href="https://kubernetes.io/docs/concepts/services-networking/service/">Service</a>.</p><p>Here’s how the service for our deployment would look like:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: my-service<br>spec:<br>  selector:<br>    app:  my-service<br>  ports:<br>    - protocol: TCP<br>      port: 8080</pre><p>This would make our service accessible as my-service and automatically set up load balancing between all the pods. A request to this service would look something like this: <a href="http://my-service:8080/some/path.">http://my-service:8080/some/path.</a></p><p>If you have been following me till now, Deployments and Services together take care of configuration management and service discovery requirements for our services as well.</p><p>We often define Services and deployments in the same file. Like this:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: my-service<br>spec:<br>  selector:<br>    app:  my-service<br>  ports:<br>    - protocol: TCP<br>      port: 8080<br>---<br>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: my-service<br>  labels:<br>    app: my-service<br>spec:<br>  replicas: 3<br>  selector:<br>    matchLabels:<br>      app: my-service<br>  template:<br>    metadata:<br>      labels:<br>        app: my-service<br>    spec:<br>      containers:<br>      - name: my-service<br>        image: my-org/my-service-image:0.1.0<br>        env:<br>          # Define the environment variable<br>          - name: SOME_VAR<br>            valueFrom:<br>              configMapKeyRef:<br>                # The ConfigMap containing the value you want to assign to SOME_VAR<br>                name: my-config<br>                # Specify the key associated with the value<br>                key: some-key<br>        ports:<br>        - containerPort: 8080</pre><p>The order doesn’t really matter here.</p><h3>Can’t all of this be a bit easier?</h3><p>That’s the unanimous question our entire team asked. I know you won’t believe this, but they all asked this in chorus. I still remember their faces. It was hilarious! Memories! ❤️</p><p>But let’s be practical here. This entire exercise doesn’t make a lot of sense if the team wasn’t with us.</p><p>We had to take a call.</p><p><strong>Make Kubernetes easier or scrap the entire migration.</strong></p><p>We are a dev team, and not all of us had experience dealing with DevOps and Kubernetes. A tool which nobody wants to use is useless.</p><p>We build an automation tool named runner, which auto-generated these Kubernetes config files. We slowly added features based on our requirements.</p><p>I’m talking about things like managing secrets and an easy to use admin UI.</p><p>Meanwhile. some of us were also having a look at Services Meshes. <a href="https://istio.io/">Istio</a> stood out. So we also added native Istio support to runner.</p><p>In my previous articles, I’ve spoken about <a href="https://blog.spaceuptech.com/posts/why-your-microservices-need-a-service-mesh/">why service meshes are important for your microservices</a> and <a href="https://blog.spaceuptech.com/posts/service-meshes-compared-istio-vs-linkerd-vs-consul/">compared the popular services meshes</a> out there.</p><p>Today runner is capable of deploying stateless services to Kubernetes within Istio. It takes care of setting up <strong>mtls</strong> and encryption for inter-service communication. It also has a traffic splitting mechanism to carry out canary deployments and A/B tests.</p><p>We were so happy with the results that <a href="https://blog.spaceuptech.com/posts/open-source-firebase-heroku/">we have open sourced <em>runner</em> and released it as a part Space Cloud</a>.</p><p>You can check out this <a href="https://learn.spaceuptech.com/space-cloud/basics/deploy-a-service/">step-by-step guide</a> to deploy your applications on Kubernetes with Space Cloud without having to go through the learning curve.</p><h3>Working with stateful services</h3><p>Okay. Time to move on now.</p><p>At SpaceUpTech, we tend to avoid using stateful services altogether. The only stateful set we have is the gateway component of SpaceCloud.</p><p>We did not even add stateful service support to runner to discourage the use of stateful services.</p><p>However, I do understand that you may have some stateful services that you need to run. So let’s quickly go through how you can do that.</p><p>Kubernetes provides another controller named <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/">StatefulSets</a> to work with Stateful services.</p><p>Fortunately, the configuration of a StatefulSet is similar to a Deployment. StatefulSets, too, require a special type of Service, called a <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services">Headless Service</a>, to accompany it.</p><p>Here’s a sample:</p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: nginx<br>  labels:<br>    app: nginx<br>spec:<br>  ports:<br>  - port: 80<br>    name: web<br>  clusterIP: None<br>  selector:<br>    app: nginx<br>---<br>apiVersion: apps/v1<br>kind: StatefulSet<br>metadata:<br>  name: web<br>spec:<br>  selector:<br>    matchLabels:<br>      app: nginx # Label selector that determines which Pods belong to the StatefulSet<br>                 # Must match spec: template: metadata: labels<br>  serviceName: &quot;nginx&quot;<br>  replicas: 3<br>  template:<br>    metadata:<br>      labels:<br>        app: nginx # Pod template&#39;s label selector<br>    spec:<br>      terminationGracePeriodSeconds: 10<br>      containers:<br>      - name: nginx<br>        image: gcr.io/google_containers/nginx-slim:0.8<br>        ports:<br>        - containerPort: 80<br>          name: web<br>        volumeMounts:<br>        - name: www<br>          mountPath: /usr/share/nginx/html<br>  volumeClaimTemplates:<br>  - metadata:<br>      name: www<br>    spec:<br>      accessModes: [ &quot;ReadWriteOnce&quot; ]<br>      resources:<br>        requests:<br>          storage: 1Gi</pre><p>You may have noticed that the service config has an additional clusterIP: None field. To put things simply, this creates a unique domain name for each replica in that service.</p><p>You can access individual replicas in the above example as nginx-0, nginx-1 and so on.</p><p>Another cool aspect is the volumeClaimTemplates.</p><p>This essentially attaches a block storage device to the container. In our example, a block storage device of size 1Gi will be created and attached to each replica.</p><p>The same block storage device is reattached to the container in case it is restarted due to any reason.</p><p>I know all this sounds super awesome. But you cannot use this as an excuse to exploit StatefulSets. All this functionality comes at a cost.</p><h3>Setting up Ingress</h3><p>You will have to publish a service using a service type in order to make it accessible from the external world. I won’t be diving into this much.</p><p>Normally services are accessible from within the cluster only. By setting the service type to <a href="https://kubernetes.io/docs/concepts/services-networking/service/#nodeport">NodePort</a> or <a href="https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer">LoadBalancer</a> you are able to make it accessible from outside.</p><p>Service type <em>NodePort</em> exposes the service on a specified port of each Kubernetes node while type <em>LoadBalancer</em> provisions a loadbalancer to handle incoming traffic. The target of the load balancer will be your service.</p><p><strong>You must almost always use type LoadBalancer in production.</strong></p><p>You can refer to the <a href="https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types">documentation</a> for more info.</p><h3>Making the Switch</h3><p>By now you should have the Kubernetes YAML files we will use to deploy your services stored in a directory. If the steps till here have been tricky or vague, feel free to ask questions in the comments or <a href="https://twitter.com/YourTechBud">ping me on twitter</a>.</p><p>The next step is deploying our services. All you need to do is run a simple command: kubectl apply -f &lt;config-dir&gt;.</p><p>This will apply all the config to your Kubernetes cluster. From that configuration, Kubernetes will know which services to deploy. It will also setup the necessary volumes and environment variables that you may have listed down.</p><p>I usually suggest trying out Kubernetes locally with <a href="https://kubernetes.io/docs/tasks/tools/install-minikube/">Minikube</a> or <a href="https://microk8s.io/">Microk8s</a>. If you are interested in using Kubernetes with Space Cloud to simply things, you can refer to this <a href="https://docs.spaceuptech.com/install/kubernetes/">guide</a>.</p><p>Once everything works as expected in a local environment, you can deploy your services in production.</p><h3>Transitioning traffic from VMs to Kubernetes</h3><p>My journey had one small hiccup.</p><p>Our VM based infra was already serving live traffic. We wanted to make the switch without disrupting existing traffic.</p><p>On top of that, we still had to test our Kubernetes environment against production traffic. It would be a disaster if things would crash after we made the switch.</p><p><strong>We needed a way to migrate live traffic incrementally to our Kubernetes cluster.</strong></p><p>This is where the decision to use Istio paid off.</p><p>We used <a href="https://istio.io/docs/tasks/traffic-management/traffic-shifting/">Istio’s traffic splitting functionality</a> to direct all ingress traffic (coming to Kubernetes) to our VM infrastructure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OpU0SV8MO9n51UOx.png" /></figure><p>In short, we were using our Kubernetes infrastructure as a mere API gateway to begin with.</p><p>Once we have made this change, we switched our DNS systems to point to our Kubernetes cluster. As expected, Istio was routing all incoming traffic to VM infrastructure where it was getting processed.</p><p>Once we had this setup in place, we went for the next step. We split a small fraction of our traffic (10%) to the Kubernetes services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CysAOyKK0MzmXa_x.png" /></figure><p>We let this setup be for a few days to verify if the services in Kubernetes were processing it’s share of requests reliably.</p><p>We moved all incoming traffic to Kubernetes once we were confident with the setup.</p><p>I highly recommend this approach whenever moving to Kubernetes. In fact, we still use this method to deploy new versions of our services. We test the new version with a subset of production traffic before making the switch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ni8D6eqUcdcUlgNY.png" /></figure><p>To simplify this process, we added support for <a href="https://docs.spaceuptech.com/microservices/deployments/traffic-splitting/">traffic splitting in Space Cloud</a> which lets us do all of this simply via a UI. This way, our team need not worry about learning Istio.</p><h3>Post Migration Steps</h3><p>Our journey did not end after moving to Kubernetes. We faced some problems even after the migration was successful.</p><p>Many guides leave out the post-migration scenario. That’s why I’ve made an attempt to describe the changes we had to make in our development practices.</p><h3>Strategy for local development</h3><p>This was by far the most affected domain. Local development and testing had become increasingly hard by adding Kubernetes to the mix.</p><p>It is quite demanding to run a Kubernetes cluster on your laptop. Not all of our devs had a high-end laptop.</p><p>We do have a dev environment, but local testing has always been a part of our practices. We just count not ignore this aspect of development.</p><p>Initially, we tried to develop and test locally without kubernetes. This worked great. But we faced a lot of issues with the Kubernetes configuration when it came to the dev environment.</p><p><strong>We decided that we want a consistent experience on the local, dev and production environments.</strong></p><p>One way to achieve this was by using Docker for local development. The challenge was the inconsistency of the Docker and Kubernetes cli tools.</p><p>We realized that we were already using Space Cloud for handling all deployments. What if Space Cloud could provide us with the same features for Docker?</p><p>After some tinkering, we added Docker support to Space Cloud. Today, Space Cloud supports Kubernetes and Docker as target environments for deploying services. It also provides features like traffic splitting so we can rest assured that config that works locally will work in production as well.</p><h3>Developer experience.</h3><p>Working with Docker and Kubernetes added a bit of a learning curve to our developers. Even with Space Cloud, they still had to write Dockerfiles, config files, build and push docker images.</p><p>This slowed down development speed.</p><p>To tackle this, we added a space-cli deploy command to our cli. This tool was a tiny wrapper on the Docker and Space Cloud commands one might need to deploy a service.</p><p>We also added a space-cli deploy --prepare command to generate a Dockerfile and config file with smart defaults.</p><p><strong>So do you have to use Space Cloud to do anything meaningful with Kubernetes?</strong></p><p>No. Not at all.</p><p>I would wish so, but you honestly don’t.</p><p>I still find myself writing Kubernetes and Istio config files for certain niche tasks. No one can beat the flexibility the raw API provides.</p><p>There are several other tools you can use which achieves similar results. Here are a few we tried out before building runner.</p><h3>Concluding</h3><p>Like all great journeys, this one had to come to an end as well.</p><p>All I wish for is you have gained a little by hearing me out. Do let me know on <a href="https://twitter.com/YourTechBud">twitter</a> or in the comments in case you wish to talk to me regarding moving to Kubernetes.</p><p>If you like this article, do share it with those it may help as well.</p><p>Don’t forget to <a href="https://github.com/spaceuptech/space-cloud">star Space Cloud on GitHub</a>.</p><p><em>Originally published at </em><a href="https://blog.spaceuptech.com/posts/how-we-moved-to-kubernetes/"><em>https://blog.spaceuptech.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c45dfbc0ff5e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/how-we-moved-to-kubernetes-from-vms-c45dfbc0ff5e">How We Moved To Kubernetes From VMs</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Adding Realtime Functionality to Your Apps]]></title>
            <link>https://medium.com/backstagewitharchitects/adding-realtime-functionality-to-your-apps-7a99928dd349?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/7a99928dd349</guid>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[realtime]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Jayesh Choudhary]]></dc:creator>
            <pubDate>Tue, 19 May 2020 12:22:36 GMT</pubDate>
            <atom:updated>2020-05-19T12:22:36.693Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*flj8ZSj4de7taZBn5ThC9A.jpeg" /></figure><p>Adding real time functionality to your app is an excellent way of keeping your users engaged.</p><p>In this post, we are going to see how you can add realtime functionality to our app and the challenges that one encounters while adding realtime functionality.</p><h3>What is Realtime?</h3><p><strong><em>I want patience, and I want it now</em></strong></p><p>This statement kind of summarizes the craving of the 21st-century users to be updated with the latest information.</p><blockquote>Realtime is nothing but getting informed about the latest events or changes in data as and when they happen.</blockquote><p>Remember the time when the whole world communicated via SMS? A few lines of text and an infinite waiting period until your friend finally replied.</p><p>No delivery status. No typing indicators 🙁. Just you, silence and a little excitement.</p><p>While this sounds quite romantic, it was very inconvenient 😅.</p><p>Soon Whatsapp came with realtime messaging and took the world by storm. After all, the nervousness and excitement when you see your crush typing… It just can’t be described in words 😍!</p><p>Sooner or later all major players followed the suite and today we can’t imagine our life without these realtime features.</p><blockquote><strong><em>Realtime features provide an engaging and interactive UX😇</em></strong></blockquote><p>Wait…but how were those numbers increasing in discrete values rather than continuous? That’s an exercise for you! Nice opportunity for you to show off in the comments if you can figure this out!😛</p><h3>Use Cases of Realtime</h3><p>Realtime extends way beyond instant messaging to almost every other field or app that we can think of:</p><ul><li><strong>Live location tracking:</strong> Cab booking apps like Uber. Logistics and food delivery apps.</li><li><strong>Multi-user collaboration:</strong> From project management tools like Trello and Jira to Google Docs to multiplayer online games.</li><li><strong>Live order status:</strong> eCommerce apps like Amazon and Flipkart.</li><li><strong>Live score and commentary:</strong> Sports apps like Cricbuzz.</li><li><strong>Realtime feed:</strong> Social media apps like Instagram, Facebook and Twitter.</li><li><strong>Realtime data analytics:</strong> Google Analytics.</li><li><strong>Live data streaming:</strong> Instagram and Facebook Live.</li><li><strong>Push notifications:</strong> Almost every other app to bring users back. For example, blogging platforms.</li></ul><p>I am sure you have already started thinking about what features in your app can be realtime😛. Great! Now let’s see how you can add those features to your app!</p><h3>Adding Realtime Functionalities</h3><p>Let’s take the example of a realtime messaging app. We just want these three features to be realtime:</p><ul><li>Messages (push notifications)</li><li>Online indicator</li><li>Is typing indicator</li></ul><p>Building the frontend interface for the online or is typing indicator is reasonably straight forward if you have some experience building frontend interfaces before. The real challenge though in adding these realtime features to your app is…</p><blockquote><em>Building a realtime backend that is </em><strong><em>performant, reliable and economical at scale</em></strong><em> while keeping your sanity.</em></blockquote><p>And that’s what we are going to explore for the rest of this article in detail.</p><p>There are two ways of adding realtime data to your app:</p><p><strong>Long polling:</strong> (Pull based)</p><p>The client keeps polling the server for data every few seconds. This is not just a naive way of solving our realtime needs, but it also turns out to be expensive at scale as each additional request to your server costs CPU and memory.</p><p><strong>Realtime subscriptions:</strong> (Push based)</p><p>Server pushes the data to the client <em>in realtime</em> (i.e. on any relevant event or change in data).</p><p>This server-side push is the kind of realtime mechanism we want to use for our app as it is very scalable and cost-effective.</p><h3>Implementing Realtime Subscriptions</h3><p>The first thing while implementing realtime subscriptions is choosing a communication protocol for it.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API">Websockets</a> seems to be the most popular protocol when it comes to handling realtime data since its bidirectional (i.e. it can send messages in both directions) and supported by almost all browsers out there.</p><p>After all, implementing a basic chat app with WebSockets in NodeJS is as simple as this:</p><pre>const WebSocket = require(&#39;ws&#39;);</pre><pre>const webSocketServer = new WebSocket.Server({ port: 8080 });</pre><pre>webSocketServer.on(&#39;connection&#39;, (webSocket) =&gt; {<br>  webSocket.on(&#39;message&#39;, (message) =&gt; {<br>    console.log(&#39;Received:&#39;, message);<br>    broadcast(message);<br>  });<br>});</pre><pre>function broadcast(data) {<br>  webSocketServer.clients.forEach((client) =&gt; {<br>    if (client.readyState === WebSocket.OPEN) {<br>      client.send(data);<br>    }<br>  });<br>}</pre><p>The above code simply emits (broadcasts) any messages that it receives to all the connected users in realtime. But you can easily adapt this code to implement one-to-one and group messaging.</p><p>While such realtime implementations are good enough on a single server, they don’t work at scale.</p><h3>Going Distributed</h3><blockquote>Even simple things become complicated at scale — YourTechBud</blockquote><p>I couldn’t agree more on this for realtime functionality.</p><p>Let’s imagine the realtime messaging app we just made starts scaling, and we add more servers to balance the load.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1rLpL_TTril_-KF7.png" /></figure><p>As you would imagine, all our users are not connected to a single server any more. So how does a message travels from one client to the other in realtime if the <strong>clients are on two different servers</strong>?</p><p>This type of realtime backend is what I call distributed realtime backend — The one required at scale and the one that we are interested in😎.</p><h3>Broadcasting</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8hKeO2oitnNaX4uq.png" /></figure><p>One easy hack to implement distributed realtime is to broadcast any received messages to all the servers. These servers then notify the message to their connected clients as per the application logic in realtime.</p><p>However, at scale, there can be thousands of servers each broadcasting every message to thousands of other servers. If just one server was interested in the message, its simply hogging up all your network and resources.</p><p>Thus, broadcasting isn’t performant and hence not a scalable solution at all for our realtime backend. We need something better than this!</p><p>Ideally, we want servers to receive only those messages from other servers that they are interested in (i.e. the messages for the clients connected to them.</p><p>Let’s say after a lot of research; we figured out a way on how to implement this. So should we call it a day?</p><p>No, not yet 😛.</p><h3>Filtering Our Results</h3><p>Even after optimizing the communication between the servers, we still need to write the application layer logic to filter the realtime messages that each client gets.</p><p>No one wants his secret messages to be leaked to the public right? We need to make sure that only the right client gets the right data.</p><p>Moreover, this filtering logic can be different for each realtime feature in our app.</p><p>For example, the group chat demands that a user receives a message if he is a member of that group. On the other hand, the online indicator feature demands the online status of a user be forwarded to a client if that user is in his friends’ list.</p><p>Note not just how both this logic are completely different but also that they require you to query the database state in many cases.</p><p>This means we need to rewrite the filtering logic every time for each realtime feature in our app. The only way we can get rid of this is by using some kind of a <strong>general-purpose rules engine</strong>. It would then validate/filter messages before sending them to clients.</p><h3>Reliability</h3><p>Remember, when everyone in your peer group knew a secret except you. Even your best friend did not tell it to you. How did you felt at that time? Cheated right? 😢</p><p>Now image if a user doesn’t receive a message due to a backend failure. He will feel cheated too! 😢</p><p>But wait, why will our beloved server cheat with us in the first place?😦 Trust me it doesn’t want to! It’s just that it isn’t aware of the failures happening in the system.</p><blockquote>At scale, failure becomes a norm rather than an exception.</blockquote><p>Messages getting lost between servers due to network errors is of daily occurrence. Even if you manage to mitigate these errors with retry mechanisms, don’t forget that your servers can crash too.</p><p>Imagine what happens if a server dies before broadcasting to any of the other servers just after writing the chat message to a database? Even though we have persisted the data, it wouldn’t be delivered to our clients in realtime.</p><p>And these are just a few scenarios that I am listing. If you go on to solve any of these challenges, you would soon realize that new problems keep spawning up!</p><h3>What If Our Database Itself is Realtime?</h3><p>Well, that’s an interesting question. It would be great if our database itself can push data in realtime to our clients.</p><p>Let’s see if this is a viable solution keeping aside the security aspects of such a solution for now.</p><p>If you go on to research, you would be surprised by the fact that even after decades of maturity in database management systems, how hard it is to find a database that can send realtime messages to millions if not billions of clients.</p><h3>RethinkDB</h3><p><a href="https://rethinkdb.com/">RethinkDB</a> was the closest we have gotten so far to a general-purpose realtime storage solution, but it seen some <a href="https://rethinkdb.com/blog/rethinkdb-shutdown/">difficult times</a>.</p><h3>Cloud Firestore</h3><p><a href="https://firebase.google.com/products/firestore">Cloud Firestore</a> is another such realtime database which is famous for its ease of use and zero maintenance as its a hosted solution. However, it’s a proprietary technology by Google and becomes costly at scale. And if you were thinking of any aggregations on top of it then seriously, forget it!</p><h3>CDC Mechanisms</h3><p><a href="https://en.wikipedia.org/wiki/Change_data_capture">CDC (Change Data Capture)</a> is a set of software design patterns used to determine and track the data that has changed so that you can take action on the changed data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pmjQoQ5OzbuQR69T.png" /></figure><p>These changes if pumped into a broker or a reliable message queue like <a href="https://kafka.apache.org/">Kafka</a> or <a href="https://www.rabbitmq.com/">RabbitMQ</a>, can solve most of our reliability and performance problems.</p><p>Though CDC is a very robust way of adding realtime features to your app, there are many downsides to it as well.</p><p>The biggest drawback is it adds more moving pieces to our architecture that we need to deploy and maintain. And if you don’t believe me on how painful is maintaining Kafka in production, there’s a company named <a href="https://confluent.io/">Confluent</a> whose prime business revolves around helping people do that!</p><h3>But I Want Realtime Functionality in My App!!</h3><p>We do! We don’t want the tech giants of the world like Google and Facebook to win our users just because they have a big tech team!😈</p><p>I mean we also want to provide the same interactive and engaging realtime experiences to our users at scale.</p><p>After all we love our users so much!</p><p>So is there an easy way out to implement realtime functionalities? Something that:</p><ul><li>Provides distributed realtime over any database without impacting the database performance.</li><li>Offers <strong>strong reliability guarantees</strong> without introducing heavy components.</li><li>Scales horizontally.</li><li>Provides a rules engine so that we don’t need to implement filtering logic for each feature again and again.</li></ul><p>It looks like there is one — <a href="https://github.com/spaceuptech/space-cloud">Space Cloud</a>. Obviously, I didn’t write such a long blog post just to disappoint you at the end by saying there are no hopes! Even I care about your experience a lot 😛.</p><h3>Using Space Cloud as a Realtime Engine</h3><p>Space Cloud is a self-hosted, GraphQL Backend as a Service that provides <em>instant realtime APIs on top of any database</em>.</p><p>Yes, you heard that right — <strong><em>on top of any database</em></strong>.</p><p>Space Cloud supports all popular databases like Postgres, MongoDB, MySQL, etc. out of the box.</p><p>Already in love with Space Cloud? Go ahead and <a href="https://github.com/spaceuptech/space-cloud">star us on Github</a>!.</p><p>Moreover, it provides us with a rules engine ( <strong>authorization module</strong>) that we spoke about earlier.</p><p>Thanks to it, the client can securely specify what data it is interested in and receive only that data. No more rewriting realtime functionalities in our app!</p><p>Performance and reliability? We got them all! Thanks to the in-built <a href="https://docs.spaceuptech.com/microservices/eventing/">eventing module</a> of Space Cloud.</p><p>And guess what? All of these realtime goodies is <a href="https://github.com/spaceuptech/space-cloud">opensource</a>!</p><p>No more vendor lock-ins!</p><p>Too excited to see things in action? 😛 Don’t worry; we have already built a <a href="https://github.com/spaceuptech/space-cloud-samples/tree/master/react-todo-app-graphql">sample realtime todo app</a> in ReactJS that uses Space Cloud.</p><p>You can also follow this <a href="https://learn.spaceuptech.com/space-cloud/basics/">step-by-step guide</a> as well.</p><p><em>Originally published at </em><a href="https://blog.spaceuptech.com/posts/adding-realtime-functionality/"><em>https://blog.spaceuptech.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a99928dd349" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/adding-realtime-functionality-to-your-apps-7a99928dd349">Adding Realtime Functionality to Your Apps</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why Your Microservices Need GraphQL?]]></title>
            <link>https://medium.com/backstagewitharchitects/why-your-microservices-need-graphql-aa2605f9f950?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/aa2605f9f950</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Noorain - YourTechBud]]></dc:creator>
            <pubDate>Tue, 12 May 2020 12:09:31 GMT</pubDate>
            <atom:updated>2021-04-24T15:43:07.735Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7WF3ePiP5Ptxukvn4xmFaA.jpeg" /></figure><p>While microservices offer tremendous benefits and help you stay agile, it’s important to get them right.</p><p>In this post, we are going to see how the nature of microservices makes choosing the right API layer even more important and how <a href="https://graphql.org/">GraphQL</a> perfectly fits in it.</p><p>At the end, I have also listed some key points that you need to keep in mind while adopting GraphQL in your microservices.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FJ5KVaAf3pVU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DJ5KVaAf3pVU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJ5KVaAf3pVU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5e0513f70e2019830328b5aea256994/href">https://medium.com/media/a5e0513f70e2019830328b5aea256994/href</a></iframe><h3>What are Microservices?</h3><blockquote>Microservices based architecture is a design pattern and not a framework.</blockquote><p>While there is no precise definition of what microservices are, it loosely corresponds to architecting your single application as a suite of smaller services that are:</p><ul><li>Organized around business capabilities.</li><li>Have a single responsibility.</li><li>Owned by a small team.</li><li>Loosely coupled.</li><li>Independently deployable.</li><li>Highly maintainable and testable.</li></ul><p>Before diving deep into microservices and why they need GraphQL, let’s compare it with monoliths to get a clearer global picture.</p><h3>Microservices vs monoliths</h3><p>Unlike monoliths, in microservices architecture, we have different services, each taking ownership of a particular part of data. Let’s take an example to understand this.</p><p>Consider the feeds page of Instagram. A simple page like this has various features in it:</p><ul><li>Showing a personalized list of feeds/posts.</li><li>Showing details of the post author (name, image).</li><li>Showing the likes and comments of a post.</li></ul><p>Unlike monoliths, in a microservice architecture, there are different services for each of the above feature.</p><p>If you are thinking what’s so cool about splitting our app in such a way, it’s useful to consider the following benefits of doing so:</p><ul><li><strong>Independently deployable:</strong> Changes in one part of our application doesn’t mean deploying the whole application again, leading to shorter QA and release cycles.</li><li><strong>Increased productivity and confidence:</strong> Each team owns a small codebase which is easier to understand and test.</li><li><strong>Easy to migrate and optimize:</strong> You can build different services with different technologies (languages, frameworks, databases) as per the task and team resources.</li></ul><p>Already excited and want to get started with microservices?😛 Won’t stop you from doing so, however, there are certain things that you need to be careful of before adopting microservices. One such thing is choosing the right API layer for your microservices.</p><h3>Why your microservices shouldn’t REST</h3><p>Although REST has some inherent shortcomings, it is still the most popular choice of API for many. However, the nature of microservices tends to make the problems with REST even more severe.</p><p>Let’s look at some key points on how REST degrades our microservice architecture.</p><h3>Multiple requests</h3><p>With REST, the simple feeds page that we saw above has to make requests to multiple services (feeds service, user details service, comments service)</p><p>The problems become even worse when these requests need to be joint. For example, in our feeds page, we can request the user details and comments microservices only after we have the results from the feeds list service.</p><p>This requirement of joining requests due to the relational nature of data leads to increased latency and poor UX. Remember that <strong>a few seconds of delay is all it takes for the user to close the tab</strong> and move to some other alternative. No! We don’t want that to happen with us 😭. Who would!?</p><h3>N+1 problem</h3><p>If you have been consuming the GraphQL diet consistently, you would have surely been served the N+1 dish by the GraphQL advocates till now! 😁 If not, then don’t worry, here&#39;s a teaser.</p><p>If you remember, our app had a service to fetch the details of a user?</p><p>Typically we would design this service to take a single user id and return the details of the corresponding user. However, each post in our feeds page can potentially belong to a different user.</p><p>This means we would have to make one request to fetch all the posts and then N requests to fetch the details of each post author where N is the number of posts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sYgqhJQM4dqZSVpd.png" /></figure><p>And this is just a simple example. You can imagine <strong>how easily this situation can cascade to a networking nightmare in a real world scenario</strong>.</p><p>If you are anything like me, you would have already started thinking of solutions in your head 😇.</p><p>Let’s think of some right now 😋!</p><p>An aggregator service in front of feeds and user details service can reduce the latency by enforcing the joining of requests on the backend rather than frontend!</p><p>This surely helps but its yet another service that you need to deploy and maintain 😅. And if you think monitoring a monolith was hard, monitoring microservices is 10x harder.</p><p>You could also tweak your user details microservices to take an array of users ids and return details of all those users at once.</p><p>Wait… you already thought of that? If yes, then I’d love to meet you for a cup of coffee 😛.</p><p>Nevertheless, the problems don’t end here.</p><h3>Bandwidth</h3><p>In most cases, our user details service would return a whole lot of other information as well apart from the name and profile picture.</p><p>Information like email, bio, address, date of birth and phone number could be required by other pages of our app.</p><p>You might think that these extra fields add barely a few KBs to the payload. Multiply this number by the number of posts present in the feed and by how many times the feed is viewed in a day.</p><p>This problem is typically referred to as <strong>over fetching</strong> in the API world.</p><h3>So should we ban microservices?</h3><p>No. Not yet.</p><p>Remember why we use them in the first place. It&#39;s for agility, right?</p><p>That’s got nothing to do with the above problems.</p><p>Microservices is a brilliant concept in itself when used correctly.</p><p><strong>The problem lies in the multi-facet demands of the business.</strong></p><p>On the agility side, it requires us to architect our app into smaller services, while on the UX side it demands us to join data from various sources to provide a unified and enriching experience to our users.</p><p>Its almost like we need to build a monolith and a microservices-based architecture at the same time.</p><p>To bridge this gap, we need a <strong>unified API layer in front of our microservices</strong> that is smart enough to <strong>fetch and combine data dynamically</strong> from various sources.</p><p>I know you have already guessed it. Its GraphQL.</p><h3>What is GraphQL?</h3><p>GraphQL is a query language for your APIs. As compared to REST, the GraphQL server has a single endpoint.</p><p>Instead of making multiple requests to different endpoints, we send a single GraphQL query string describing our data needs to the GraphQL server.</p><p>Think of it as SQL for your API server.</p><p>The GraphQL server is smart enough to interpret this query and request the concerned data-sources (microservices in our cases) to return only the data required by the client.</p><p>Let’s take our Instagram example once again. Here’s how the client can describe its data requirements in a single GraphQL query for it:</p><pre>query {<br>  feeds {<br>    id<br>    content<br>    author {<br>      id<br>      name<br>    }<br>    comments {<br>      comment<br>      created_at<br>      user_id<br>    }<br>  }<br>}</pre><p>Notice how the above nested GraphQL query also clearly indicates the request chaining pattern required to be performed by the GraphQL server.</p><p>This is how the GraphQL server makes requests to our microservices as per the GraphQL query:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qVAtznM_NwRq27PK.png" /></figure><p>The graphql server resolves the request from left to right. That’s why it first requests the feeds microservices since it comes first from the left.</p><p>After getting the response from the feeds service, it then makes the request to the user details and comments service parallelly as they are at the same level inside the feeds request in the GraphQL query string.</p><p>Note that the N+1 requests happening here are between the GraphQL server and the microservices. These N+1 requests are of very low latency because they are part of the same network.</p><p>Apart from shifting the N+1 request from frontend to backend and making a single request, <strong>GraphQL sends the values of only the requested fields back to the client</strong>. Thus we have ticked all the problems that we had using REST with microservices.</p><p>That’s the reason why I firmly believe that any microservice architecture should have GraphQL as its API layer.</p><p>Excited for GraphQL as you were for microservices? 😛 Let’s take a step back and tackle some of the stepping stones that you may face while adopting GraphQL.</p><h3>Adopting GraphQL</h3><p>While GraphQL is amazing, its relatively new and the world is still catching up with it. Here are some gotchas that you need to be aware of before using GraphQL in your microservices architecture:</p><p><strong>It has a fixed schema:</strong> While this is one of the strongest selling points of GraphQL, one may hate writing a schema if he/she prefers the schemaless way of doing things. This can be especially true if you love Javascript more than Typescript or MongoDB more than SQL. Did I just start a war here?</p><p><strong>Implementing resolvers:</strong> In GraphQL, you need to write resolvers on the backend that resolve data for a particular field/type as opposed to writing request handlers for endpoints. This is a new way of writing API servers, and you might have a hard time initially understanding how resolvers work.</p><p><strong>Optimizations:</strong> As we saw earlier in our Instagram example, the GraphQL server itself has to make N+1 requests to fulfil relational/nested requests. You can use the Dataloader pattern to reduce the number of requests that the GraphQL server makes. No need for premature optimizations early on, but this is something you should surely keep in your mind as your applications start scaling.</p><p><strong>Security:</strong> In GraphQL you have to secure fields as opposed to endpoints in REST. Securing endpoints is way easier than securing fields. Implementing field level authorization can be tricky to get right at first.</p><p><strong>Migrating existing REST APIs:</strong> What if you already have tons of microservices in REST already deployed in production? Migrating existing REST APIs can be a very daunting task. This guide on <a href="https://learn.spaceuptech.com/space-cloud/basics/rest-to-graphql/">migrating existing REST APIs to GraphQL</a> can help you incrementally do that.</p><p>Husshhh😅! That was a lot. If you are overwhelmed by today’s GraphQL dose and want an easier way to adopt GraphQL for your microservices, don’t worry, I have a bonus for you.</p><h3>Bonus: Space Cloud as a GraphQL layer</h3><p>Implementing GraphQL from scratch can be overwhelming at first, especially if you aren’t used to it. But you also don’t want to miss on the amazing benefits that GraphQL has to offer. That’s why we have made <a href="https://github.com/spaceuptech/space-cloud">Space Cloud: An open-source GraphQL layer</a>.</p><p>It connects to your existing databases and REST services and provides a <strong>unified GraphQL API</strong> on top of them which you can use securely from the frontend.</p><p>The best part?</p><p><strong>You get all the GraphQL goodness without making any changes to your existing REST APIs.</strong></p><p>Sounds exciting?</p><p>Check out the <a href="https://learn.spaceuptech.com/space-cloud/basics/setup/">step by step guide</a> to get started with Space Cloud. And yes, don’t forget to <a href="https://github.com/spaceuptech/space-cloud">star us on Github</a> if you love Space Cloud and GraphQL♥️.</p><p>Also, feel free to reach out to us on our <a href="https://discord.gg/RkGjW93">discord server</a> or directly put a comment below.</p><p><em>Originally published at </em><a href="https://blog.spaceuptech.com/posts/why-your-microservices-need-graphql/"><em>https://blog.spaceuptech.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa2605f9f950" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/why-your-microservices-need-graphql-aa2605f9f950">Why Your Microservices Need GraphQL?</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Right Way Of Making Microservices Communicate]]></title>
            <link>https://medium.com/backstagewitharchitects/the-right-way-of-making-microservices-communicate-b6cd517ae702?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/b6cd517ae702</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Noorain - YourTechBud]]></dc:creator>
            <pubDate>Mon, 02 Mar 2020 07:09:03 GMT</pubDate>
            <atom:updated>2021-04-24T15:46:48.416Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building Event-Driven Microservices That Scale</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ZuVfc1yjdsxnIw1Ou4YKQ.jpeg" /></figure><p>Welcome to 2020, the year which will mark more serious adoption of <a href="https://kubernetes.io/">Kubernetes</a>, DevOps practises and with that, Microservices!</p><p>Earlier, I spoke about how one can <a href="https://blog.spaceuptech.com/posts/how-to-make-microservices-communicate/">make microservices communicate</a>. In this post, I want to dive a bit deeper into building scalable microservices-based architectures. Skipping all the pep talk, I’ll get right to it.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FX-JWP7E1xPc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DX-JWP7E1xPc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FX-JWP7E1xPc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9083ff18faa60a491e91c02bf364affe/href">https://medium.com/media/9083ff18faa60a491e91c02bf364affe/href</a></iframe><h3>What really goes into building microservices?</h3><p>For me, microservices is just a fancy name for a bunch of web servers having cross dependencies on one another. However, there is one restriction; each web server or service we run must manage its own state.</p><p>While we can add more rules to the mix, the goal is to consume more and more APIs rather than rely on the state stored in databases.</p><p>Let’s be honest with ourselves. We are pretty good at writing simple HTTP servers. Bragging a little about how beautiful your API or project structure is wouldn’t be wrong at all. We have been doing it for years after all.</p><p>The tricky part, however, would be <strong>building APIs which do not break often</strong>. In other words, we need some form of versioning for our APIs, which can preserve backward compatibility. I have full faith in you for this too. I’m pretty sure you’ve nailed this aspect already. No need for me to go deeper into this.</p><p>What’s left is the deployment of these microservices (a topic for another day) and making these microservices communicate. I’m going to be talking about communications in this post.</p><h3>Breaking down microservice communications</h3><p>There are two fundamental ways of making two microservices talk:</p><ul><li><strong>Synchronous</strong>, also known as <a href="https://blog.spaceuptech.com/posts/how-to-make-microservices-communicate/#the-brokerless-design">a brokerless design</a>.</li><li><strong>Asynchronous</strong>, also known as <a href="https://blog.spaceuptech.com/posts/how-to-make-microservices-communicate/#the-messaging-bus-broker-design">a broker based designed</a>.</li></ul><p>Let’s dissect them one by one.</p><h4>Synchronous communication</h4><p>These are <em>request-response type of communication</em> which usually use HTTP as the underlying protocol. The main point to note here is that the services talk to each other directly. Requests passing through a proxy, like an API gateway, are also classified as synchronous.</p><p><strong>The core idea is that the client gets an absolute response</strong>.</p><p>Now, this might sound strange initially. All requests return a response right? 🙄</p><p>Let’s take an example to understand this. Imagine if you send a command to insert a document in a database and the database returns an acknowledgement. Ever wondered what that acknowledgement means?</p><p>It’s intuitive to think that the ack means the database has <strong>successfully written your data to disk</strong>, and hence, your data is persisted. This does sound logical. Everything would become way more reliable if things worked this way.</p><p>This is a classic example of synchronous mode of communications. Even if you had a load balancer like pgbouncer in front of your database which could retry requests on your behalf, the architecture still remains the same.</p><h4>Asynchronous communications</h4><p>What if I told you, that the ack meant the database has accepted your command and will <strong>perform the actual write to disk later</strong> 😈. I&#39;m not talking about the concept of a WAL here. What I&#39;m implying is that if the database failed, your document would be lost. Forever!</p><p>Imagine working in such an environment. It’s complete madness.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*73WzogpEUYxnwF1I.jpg" /></figure><p>You would have no idea if the write has actually gone through or not. It seems there is no possible way to retry as well. This seems like the worst possible way to do things.</p><h3>The perils of synchronous microservices</h3><p>You’re probably right to have formed a negative opinion on async communications. And what has that got to do we event driven microservices anyways?</p><p>I know it’s intuitive for us to think in terms of synchronous microservices. Its way easier. Faster to implement.</p><p>Now listen to what I’m about to tell you.</p><p><strong>Synchrounous microservices are slowing you down!</strong></p><p>That’s right. I said it. You can curse me if you want, but this is a fact written in all codebases throughout the globe.</p><blockquote>Tightly coupled microservices are nothing but distrubuted monoliths which are way harder to build, debug and maintain.</blockquote><p>Let’s see why synchronous microservices aren’t always the best choice.</p><h4>The Inefficiencies</h4><p>First things first, it is very inefficient. Certain I/O bound operations take time. A lot of time.</p><p>Let’s take the example of sending an email whenever a new user signs up. In this case, you would hit your mail server (to send an email) right after your database insert.</p><p>But what if the mail server is unavailable? Would you retry? For how long? What if the mail server is out for hours 😥?</p><p>In the synchronous world of microservices, your sign-up service would simply stop working if something like that was to happen.</p><p>Wouldn’t it be better we could queue this operation somehow and try this later?</p><h4>Your microservices would really remain that micro</h4><p>The beauty of a microservice architecture is that it lets teams work on just their logic without having to worry about anything else. That’s where the agility comes from.</p><p>But look at the previous example again. The sign-up service is talking to the email service!</p><p>WHY?????</p><p>What if there were more operations you wanted to do whenever the user signs up? Everyone would have to <strong>modify the sign-up service</strong>! And what happens if any one of the services is down? Things won’t look good.</p><p>The poor microservice simply wanted to sign up new users. It was never meant to do so many things!</p><p>And this is just a single example. There could be several such instances.</p><p>Apparently, the only way out is asynchronous microservices.</p><h3>Taming asynchronous microservices</h3><p>So we need to improve our architecture such that the sign-up service can happily do its job without having to worry about anything else.</p><p>How do we do it?</p><p>Wouldn’t it be great if we could broadcast or <em>publish</em> a message whenever the user signs up? Anyone interested could keep an ear out and <em>subscribe</em> for that message? This is the pub-sub broker patten.</p><p>What we do here is that the sign-up service would publish an event in the broker (let&#39;s say <a href="https://kafka.apache.org/">Kafka</a> or <a href="https://www.rabbitmq.com/">RabbitMQ</a>). The email microservices interested in this event, would subscribe for it.</p><p>What’s important to note here is, the sign-up service will talk to the broker and <strong>the broker simply acknowledges the receipt of the message</strong>. There are no guarantees that the event has been processed or not.</p><p>I think that’s fair. It shouldn’t be the sign-up service’s problem anyway.</p><p>But someone needs to take the responsibility of delivering those events reliably! Luckily, for us, the message brokers do it.</p><h3>Going event driven</h3><p>I still have a few problems with this setup. But you can probably see the benefits already. Let’s take a look at a few areas where we can still improve?</p><h4>My simple microservices needs to learn the existence of a broker</h4><p>This might not be that big a deal for you. But why should the sign-up microservice publish events?</p><p>If you think about it, it all boils down to a database write operation. The sign-in event is triggered whenever a new document is inserted in the database. If somehow we can capture these events (using CDC or similar), our sign up microservice doesn’t really need to publish events.</p><p>Unnecessarily adding new components adds points of coupling. Also, in my experience:</p><blockquote>Most events are triggered via. the data layer.</blockquote><p>So making our data layer smarter can help us go a long way.</p><h4>Maintaining a messaging broker is an add-on cost</h4><p>Have you ever tried setting up Kafka? It’s difficult.</p><p>What about maintaining it in production? Total nightmare.</p><p>Initially, you don’t need to get into such heavy tools. We need to be careful not to add too much complexity early on without compromising on scale and flexibility.</p><h4>How long will a broker last?</h4><p>This is an inherent problem of most of the pub-sub based messaging queues. What happens if the mail server is out for a few days straight? The message will lie in the broker and eat up space. To make matters worse, if a huge surge of new users arrives at your doorstep, you are most likely to exhaust your memory.</p><p>Most brokers will probably give up at this point or suffer a major performance hit.</p><p>Mature ones like Kafka might be able to take the load, but it too will end up losing events at some point or the other.</p><p>To be fair, message brokers are designed to handle transient failures, not week-long outages.</p><h3>What does the final event-driven microservices architecture look like?</h3><p>Let’s say somehow we figure out how to solve the problems I’ve mentioned above. So now we would have an architecture where each microservice is going on minding their own business. Each is talking to their share of databases and other microservices.</p><p>Events would automatically get generated as a side effect based on these interactions (e.g. writes to a database).</p><p>Interested microservices can express interest in these events and can consume them.</p><p>At the core of this lies the eventing system which is capturing events from various sources (databases, HTTP, etc.) and triggering the HTTP endpoints of the microservices which have shown interest in those events.</p><p>Let’s say you have another microservices interested in an event? Just add a webhook for that event in the eventing system , and there you go.</p><p>In a nutshell the eventing system is responsible for:</p><ul><li>Capturing events from various sources (database, object stores, HTTP, etc.)</li><li>Maintaining a configurable mapping on the webhooks to call for each event.</li><li>Hitting the endpoints with the respective payloads.</li><li>Handling automatic retries, timeouts and even rescheduling of the same event in case of long-lived outages</li></ul><p>One added benefit of such architecture is that it’s massively scalable. As long as your eventing system can take the load, you can process millions of queries per second. I’ll leave it up to you to figure out why is this the case : P.</p><p>Having said that it’s important to note <strong>when not to use an event-driven microservices architecture</strong>.</p><blockquote>You should avoid using event-driven microservices architecture for transactional workloads requiring an absolute response.</blockquote><p>Requests like sign in/up, fetching of billing history, etc.need to be synchronous cause the data needs to be forwarded to the user. As a rule of thumb, you can default to using event stores for everything. It would be evident what requests need to be synchronous.</p><p>This approach will make sure you don’t end up making all kinds of requests synchronous and, as I mentioned before, ending up with a distributed monolith.</p><h3>What’s Next</h3><p>We have built <a href="https://github.com/spaceuptech/space-cloud">Space Cloud</a> keeping all this in mind and built into it an <a href="https://docs.spaceuptech.com/microservices/eventing/">eventing system</a> which does precisely what I described above.</p><p>Just a quick intro of what Space Cloud is:</p><p><em>Space Cloud is an open source Firebase + Heroku to develop, scale and secure your serverless applications.</em></p><p>Do checkout <a href="https://docs.spaceuptech.com/microservices/eventing/">Space Cloud’s eventing system</a> and let me know what you think about it.</p><p>As promised, we’ll be jumping into building an event-driven microservice-based architecture using Space Cloud’s event store in the next blog post.</p><p>I hope this article helped share some light on the need to adopt event-driven architectures.</p><p>Leave a comment below if I’ve missed something important or have got anything wrong. I would love to hear from you!</p><p><em>Originally published at </em><a href="https://blog.spaceuptech.com/posts/building-event-driven-microservices-that-scale/"><em>https://blog.spaceuptech.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6cd517ae702" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/the-right-way-of-making-microservices-communicate-b6cd517ae702">The Right Way Of Making Microservices Communicate</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to make microservices communicate?]]></title>
            <link>https://medium.com/backstagewitharchitects/how-to-make-microservices-communicate-84b09d077c96?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/84b09d077c96</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Noorain - YourTechBud]]></dc:creator>
            <pubDate>Thu, 06 Feb 2020 14:55:08 GMT</pubDate>
            <atom:updated>2021-01-12T05:33:58.492Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m sure you want to <a href="https://blog.spaceuptech.com/posts/how-to-build-scalable-apps">build scalable apps</a>, right? Who doesn’t? If so, you must have come across the words “Cloud Native.” This approach is like an angel which can resolve most of your scaling challenges. So, what exactly is cloud native?</p><p><strong>Cloud native is an approach used to build apps which can harness all the capabilities of the cloud.</strong></p><p>Yup, you got that right. It’s an approach. Not a framework. Not a bunch of steps to follow. And due to this, there are a million different approaches to go cloud native and achieve cloud computing “Moksha.”</p><p>One key principle of cloud native is microservices. <strong>Microservices are tiny (sometimes not so tiny) modules which can work independently of each other</strong>. They could have dependencies on other microservices or even a data persistence layer like a database. But the key is to use <a href="https://en.wikipedia.org/wiki/Loose_coupling">loose-coupling</a>. Microservices coordinate by means of “communication.”</p><p>This means each microservice sits in a different repository and is being deployed independently. For the DevOps folks out there, you have an independent continuous delivery pipeline dedicated to each microservice.</p><p>But that brings me to the most important question.</p><p><strong>How can we make microservices communicate?</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F9QsDN6-M3ec%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D9QsDN6-M3ec&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F9QsDN6-M3ec%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4da3806e7275f0a9fe7ab0789155cc43/href">https://medium.com/media/4da3806e7275f0a9fe7ab0789155cc43/href</a></iframe><p>Keeping aside the difficulties in deciding on a “<a href="https://en.wikipedia.org/wiki/Forward_compatibility">forward compatible</a>” API for microservices, just making them talk isn’t as simple as it looks. There are multiple parameters you need to consider. These are <strong>throughput</strong>, <strong>latency</strong>, and <strong>scalability</strong>.</p><p>Now there are many ways to classify the different modes of microservice communication. Synchronous (blocking) and asynchronous (non-blocking) are used quite often, but I feel these are mostly the characteristics of a programing language. I’m also going to disregard half vs. full duplex modes since these days it’s very easy to use either (or even both) in most cloud architectures.</p><p>So let’s dive in.</p><h3>The Brokerless Design</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9EQAUWH7lRELcckBXAH1pg.jpeg" /></figure><p><strong>What it is</strong>: In here we make our microservices <strong>talk to each other directly</strong>. You could use HTTP for traditional request-response or use websockets (or HTTP2) for streaming.</p><p>There are absolutely no intermediary nodes (except routers and load balancers) between two or more microservices. You can connect to any service directly, <strong>provided you know their service address and the API</strong> used by them.</p><p>Sounds pretty basic right? It pretty much is. There are wonderful protocols like <a href="https://grpc.io/">GRPC</a> to make life much easier.</p><h4>The Pros:</h4><ul><li>Low Latency: This method has the lowest latency possible. There is no middle man here. It’s fast. The limitations are imposed mostly due to poor API implementations. But again, tools like GRPC make sure you get maximum performance at the API layer.</li><li>Easy to Implement: A brokerless design is easy to visualise and implement. This makes life much easier and the world a happier place to live in.</li><li>Easy Debugging: This method is fairly easy to debug, especially from the next one I’m going to talk about. Debugging or tracing where the errors are is a super important topic in distributed systems. This becomes even more important when you are releasing new updates multiple times a day.</li><li>High Throughput: In this mechanism, more CPU cycles are actually spent on doing work rather than routing. It may not be that apparent now, but a broker design would make this a bit more clear. It’s not a surprise that most of the database APIs actually use a brokerless design.</li></ul><h4>The Cons:</h4><ul><li>Service Discovery: In such a design, service discovery has utmost importance. The service discovery mechanism needs to be responsive and scalable enough to reflect the latest state of the cluster.</li><li>Connection Nightmare: Imagine if all the microservices need to connect to each other. That would be a lot of connections. Most of these connections are fairly idle. As a result, a lot of resources are wasted due to this.</li><li>Tightly Coupled: By nature, brokerless designs are tightly coupled. Imagine you have a microservice to process online payments. Now you want another microservice to give you a real-time update of number of payments happening per minute. This will require you to make modifications in multiple microservices which is undesirable.</li></ul><p>In many cases, a brokerless design just doesn’t work. You often have requirements to simply publish the message once and have multiple subscribers consume it. This is where a broker design comes into the picture.</p><h3>The Messaging Bus (Broker) Design</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fKnahmNZdg8TnW9T0-o8wQ.jpeg" /></figure><p><strong>What it is</strong>: In this architecture, all communication is routed via a group of brokers. Brokers are server programs running some advanced routing algorithms.</p><p>Each microservice connects to a broker. The microservice can send and receive messages via the same connection. The service sending messages is called a <strong>publisher</strong> and receiver is called a <strong>subscriber</strong>. Messages are published to a particular “topic.” A subscriber receives those messages for topics to which it has subscribed.</p><h4>The Pros:</h4><ul><li>Load Balancing: Most messaging brokers support load balancing out of the box. This makes the overall architecture much simpler and highly scalable. Some brokers (like <a href="https://www.rabbitmq.com/">RabbitMQ</a>) have built-in retries and more for making the communication channel more reliable.</li><li>Service Discovery: Service discovery is not required when using a messaging backend. All microservices act as clients. The only service which needs to be discoverable is the message broker.</li><li>Fan In and Fan Out: A messaging backend makes it easier to distribute workload and aggregate the results. The best part is that adding worker microservices can be done transparently without having to update the other microservices.</li></ul><p>Stream Based Design: Such an approach also gives birth to a concept of streams. Each topic is essentially a stream of messages. Any subscriber can tap into these streams as and when required. The possibilities of modelling a system design using streams are endless.</p><h4>The Cons:</h4><ul><li>Scaling the brokers: While the advantages are amazing, scaling the brokers themselves becomes a challenge for highly distributed systems. It’s just another piece to maintain alongside your microservices.</li><li>Higher Latency: The number of hops in a message bus increases the overall latency. This is especially true for an RPC-like use case. In mission-critical apps, this might not be a feasible solution.</li><li>Higher Resource Utilization: The brokers need CPU, memory, and storage resources to run. These resources could otherwise be utilized for running other microservices. The overheads associated with a broker design could be too much for a small cluster.</li></ul><p>Just knowing the advantage and disadvantage of various architectures isn’t enough. It is important to know when to use what.</p><p><strong>You must always default to a brokerless design</strong>. Make the switch if you need the flexibility of streams or need to leverage the pub-sub semantics of a message bus. If you’re starting off fresh, it would make sense to start with a brokerless design and then switch once the need arises.</p><p>It’s not necessary to choose just one. You can use both. For our <a href="https://spaceuptech.com/">tool</a>, we are using a broker design to implement the RPC calls. The communication with our database layer is brokerless to provide lower latencies.</p><p><strong>If you are opting for a microservices based architecture, I always recommend going event driven. </strong><a href="https://blog.spaceuptech.com/posts/building-event-driven-microservices-that-scale/"><strong>Event driven architectures</strong></a><strong> can be seen as having an advanced broker at its core which has a a ton of capabilities (like task scheduling) built in.</strong></p><h3>Wrapping Up</h3><p>Using the right approach for the job is important. Choosing the mode of communication is a fundamental decision which needs to be taken with great care.</p><p>There are multiple options for both. Sticking to a well-established framework almost always makes more sense than making something from scratch. There are so many options out there. For message brokers, you’ve got RabbitMQ, <a href="https://nats.io/">Nats</a>, <a href="https://kafka.apache.org/">Kafka</a>, etc., and each one is built for particular messaging semantics.</p><p>Another awesome way is to use a Backend as a Service like <a href="https://github.com/spaceuptech/space-cloud">Space Cloud</a>. Space Cloud will automate the entire backend so you can focus on the business logic rather than cloud architecture.</p><p><em>Did this article help you? How do you make sure your apps are cloud native? Share your experiences below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84b09d077c96" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/how-to-make-microservices-communicate-84b09d077c96">How to make microservices communicate?</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[REST to GraphQL in minutes]]></title>
            <link>https://medium.com/backstagewitharchitects/rest-to-graphql-in-minutes-44f3b86cf79f?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/44f3b86cf79f</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[graphql-vs-rest]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Noorain - YourTechBud]]></dc:creator>
            <pubDate>Sat, 07 Dec 2019 16:05:22 GMT</pubDate>
            <atom:updated>2020-06-30T07:36:15.813Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6x5qCaI2GljsZSXr_fj6Cg.jpeg" /></figure><p>With all the madness currently going in the API world with <a href="https://graphql.org/">GraphQL</a>, you might be wondering how to migrate your existing REST APIs to GraphQL without breaking anything. This guide will help you accomplish your REST to GraphQL mission without changing your codebase so that you can REST with GraphQL! (pun intended 🤭)</p><h3><strong>REST to GraphQL</strong></h3><p>Well, GraphQL advocates have done a great job in <a href="https://www.howtographql.com/basics/1-graphql-is-the-better-rest/">marketing GraphQL</a> already. Respecting their efforts, I won’t be going into details, but provide a summary:</p><ul><li>GraphQL allows you to fetch multiple resources in a single request.</li><li>GraphQL solves the over fetching problem of REST by letting you describe your exact data needs.</li><li>GraphQL helps you solve the N+1 query problem on frontend by fetching related data in a single query.</li></ul><p>What I will be touching upon in this guide instead is the aspect that most people miss while advocating GraphQL, which is, “<strong>We have already invested heavily in REST”</strong>. This means:</p><ul><li>Most of our existing services are in REST.</li><li>We are more comfortable writing REST services.</li><li>We want to support existing clients using REST APIs.</li></ul><p>While many articles help you migrate from REST to GraphQL, they all force you to change your existing codebase or write a new one in front of your REST services.</p><h3><strong>But wait…</strong></h3><blockquote>If it works, then don’t touch it.</blockquote><p>Wasn’t this the first rule of programming?</p><p>Migrations can be painful, especially the magnitude of a huge codebase can be daunting. There is always a chance of breaking something.</p><h3><strong>Why can’t we just REST?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zu0gEzZsgO-sKFwhGCbPNg.jpeg" /></figure><p>Let’s face the fact — <strong>We all are lazy by nature</strong>. We love simple hacks and easy solutions.</p><p>What if there was a way wherein you could <strong>keep your REST services as is and still get a GraphQL layer</strong> on top of it without writing any code whatsoever? Sounds like a magic right? Well, <a href="https://github.com/spaceuptech/space-cloud">Space Cloud</a> helps make this possible.</p><h3><strong>What is Space Cloud?</strong></h3><p>To keep things simple,</p><blockquote>Space Cloud is an open-source platform to build, secure and scale your apps. It provides instant GraphQL and REST APIs on your database and microservices.</blockquote><p>The coolest part about Space Cloud is that all the APIs are realtime. You can optionally <em>subscribe</em> to changes in the database. This functionality comes in handy while <a href="https://blog.spaceuptech.com/posts/adding-realtime-functionality/">making real-time apps</a>.</p><p>It also has a deployments module that deploys and autoscales your services on Docker/Kubernetes.</p><p>However, in this guide, we will stick to Space Cloud’s remote service module to migrate your REST services to GraphQL.</p><p>Like it already? Show your support ❤️ by g<a href="https://github.com/spaceuptech/space-cloud">iving us a star on Github</a>.</p><h3><strong>The architecture</strong></h3><p>This is what the final architecture of GraphQL over REST looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zwsKPnb5_N64qI5JUIYvjA.png" /></figure><p>Your apps make a GraphQL query to Space Cloud, which in turn hits a REST endpoint on your server. Space Cloud acts as a GraphQL proxy or API gateway in this case.</p><p>As you would have noticed, Space Cloud is a separate GraphQL layer that sits on top of your REST services. The beauty of this fact is that your <strong>REST services are still intact</strong>, and you can use them directly in your existing clients. This fact lets you ship your REST service to GraphQL without breaking your older clients.</p><p>Great! We have understood what Space Cloud is and how it sits with our application. Let’s get straight to some action!</p><h3><strong>What we will be building</strong></h3><p>In this guide, we will be building a simple arithmetic service which has the following endpoints:</p><ul><li><strong>Adder endpoint:</strong> POST /adder</li><li><strong>Doubler endpoint:</strong> GET /doubler/:num</li></ul><p>The adder endpoint will return the sum of two numbers that it gets from the request body. The doubler endpoint, on the other hand, will return double of the number that it receives as the URL path parameter.</p><p>Great! Let’s start building things now!</p><blockquote><strong>Note:</strong> Don’t worry if you get stuck somewhere. You can always ping me on this <a href="https://discordapp.com/invite/ypXEEBr">Discord server</a>, and I will help you out personally.</blockquote><h3><strong>Step 1: Writing a service</strong></h3><blockquote><strong>Note:</strong> Even if you are reading this from your mobile right now, you can still follow this guide along just by reading.</blockquote><p>Let’s start by writing our REST service. We are going to write our REST service in NodeJS using Express.</p><blockquote><strong>Note:</strong> You can write your service in any language or framework as long as it can speak HTTP since that’s the protocol Space Cloud is going to use to communicate with your REST service.</blockquote><p>First, create a folder that serves as our working directory.</p><p><strong>Create NPM project:</strong></p><pre>npm init -y</pre><p><strong>Install Express:</strong></p><pre>npm install - save express</pre><p><strong>Write express server</strong></p><p>Create a file index.js and copy-paste the following code:</p><pre>var express = require(&quot;express&quot;);<br>var app = express();</pre><pre>app.use(express.json());</pre><pre>app.post(&quot;/adder&quot;, function(req, res) {<br>  const num1 = req.body.num1;<br>  const num2 = req.body.num2;<br>  const response = { value: num1 + num2 };<br>  res.status(200).send(JSON.stringify(response));<br>});</pre><pre>app.get(&quot;/doubler/:num&quot;, function(req, res) {<br>  const num = Number(req.params.num);<br>  const response = { value: num * 2 };<br>  res.status(200).send(JSON.stringify(response));<br>});</pre><pre>var server = app.listen(8080, function () {<br>    console.log(&quot;app running on port:&quot;, server.address().port);<br>});</pre><p>As you can see, the code is pretty straight forward. We have just created an HTTP server using ExpressJS that is listening on port 5000.</p><p>The server has two endpoints, as we discussed earlier:</p><ul><li><strong>Adder endpoint:</strong> We are expecting to receive two numbers — num1 and num2 from the POST body. All we are doing is returning the sum of these two numbers.</li><li><strong>Doubler endpoint:</strong> We are simply returning the number that we are getting from the URL path parameter.</li></ul><p>That’s all we need to write for this service.</p><blockquote><strong>Note:</strong> For migrating your existing REST services, you don’t need to write/modify any code. And if your services are already running, you can skip the 4th step.</blockquote><h3><strong>Step 2: Install Space Cloud</strong></h3><p>We are using space-cli to run Space Cloud for us.</p><h4><strong>Download space-cli</strong></h4><p>space-cli is the cli tool for Space Cloud. You can download a version for your particular platform:</p><ul><li><a href="https://storage.googleapis.com/space-cloud/linux/space-cli.zip">Linux</a></li><li><a href="https://storage.googleapis.com/space-cloud/darwin/space-cli.zip">MacOS</a></li><li><a href="https://storage.googleapis.com/space-cloud/windows/space-cli.zip">Windows</a></li></ul><p>Unzip the compressed archive.</p><p><strong>For Linux / Mac:</strong> unzip space-cli.zip &amp;&amp; chmod +x space-cli</p><p><strong>For Windows:</strong> Right-click on the archive and select extract here.</p><p>Copy the space-cli binary to your environment path variable for global usage.</p><p><strong>For Linux / Mac:</strong> Copy the space-cli to /usr/local/bin. You may have to use sudo depending on the permissions of your /usr/local/bin.</p><p><strong>For Windows:</strong> Add the path of the space-cli.exe to the environment variable PATH for making space-cli accessible globally.</p><p>To make sure if the space-cli binary is installed correctly, type the following command:</p><p><strong>For Linux / Mac:</strong> space-cli --version</p><p><strong>For Windows:</strong> space-cli.exe --version</p><p>This prints the space-cli version.</p><h4><strong>Run Space Cloud</strong></h4><p>We can set up all Space Cloud components (gateway and runner) via space-cli using a single command.</p><pre>space-cli setup --dev</pre><p>The setup command selects Docker as a target by default and runs all the containers required to setup Space Cloud. On successful installation it generates an output similar to this one:</p><pre>INFO[0000] Setting up Space Cloud on docker.            <br>INFO[0000] Fetching latest Space Cloud Version         <br>INFO[0000] Starting container space-cloud-gateway...    <br>INFO[0000] Image spaceuptech/gateway:latest already exists. No need to pull it again <br>INFO[0000] Starting container space-cloud-runner...     <br>INFO[0000] Image spaceuptech/runner:latest already exists. No need to pull it again</pre><pre>INFO[0001] Space Cloud (id: &quot;local-admin&quot;) has been successfully setup! 👍 <br>INFO[0001] You can visit mission control at <a href="http://localhost:4122/mission-control">http://localhost:4122/mission-control</a> 💻 <br>INFO[0001] Your login credentials: [username: &quot;local-admin&quot;; key: &quot;KkYr6FvgYsvr&quot;] 🤫</pre><p><strong>Verify Space Cloud installation</strong></p><p>Verify the installation by running the following docker command:</p><pre>docker ps</pre><p>You should see an output similar to this:</p><pre>CONTAINER ID        IMAGE                        COMMAND             CREATED              STATUS              PORTS                                            NAMES<br>507ce4042486        spaceuptech/runner:latest    &quot;./app start&quot;       About a minute ago   Up About a minute                                                    space-cloud-runner<br>33a5a7a9be3a        spaceuptech/gateway:latest   &quot;./app run&quot;         About a minute ago   Up About a minute   0.0.0.0:4122-&gt;4122/tcp, 0.0.0.0:4126-&gt;4126/tcp   space-cloud-gateway</pre><h3><strong>Step 3: Configure Space Cloud</strong></h3><p>If you exec into docker container of Space Cloud’s gateway, you can see a config.yaml file generated by the gateway in the /app directory.</p><p>Space Cloud needs this config file to function. The config file is used to load various information like which databases to use, their connection strings and security rules.</p><p>Space Cloud has it’s own Mission Control (admin UI) to configure all of this quickly.</p><p><strong>Open Mission Control:</strong></p><p>Head over to <a href="http://localhost:4122/mission-control">http://localhost:4122/mission-control</a> to open Mission Control.</p><blockquote><strong>Note:</strong> Replace localhost with the address of your Space Cloud if you are not running it locally.</blockquote><p><strong>Create a project:</strong></p><p>Click on Create a Project button to open the following screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KW12xfsblTYzlJuZWRKztw.png" /></figure><p>Name your project as MyProject.</p><p>Hit Create Project button to create the project.</p><p>After creating the project, the next step would be adding a database to your project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23n8jlmXquDZ1fmPIqbiHA.png" /></figure><p>However, we can simply <strong>skip this step</strong> as we don’t need a database for this project.</p><p>After that you would be directed to the overview page of your project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xkQWjeXsje0thmf0f4RJlQ.png" /></figure><p>That’s all we need to configure as of now.</p><h3><strong>Step 4: Deploying the REST service</strong></h3><blockquote><strong>You can skip this step if you already have a REST service deployed.</strong></blockquote><p>Space Cloud can deploy only docker containers as of now. So we need to dockerize our app. We are going to take the help of space-cli to do that.</p><p>First of all, we need a docker registry that can host the docker images of our service. Run this command to spin up a docker registry locally:</p><pre>space-cli --project myproject add registry</pre><blockquote><strong>In production, it is recommended to </strong><a href="https://docs.spaceuptech.com/microservices/deployments/using-custom-container-registry"><strong>use a managed container registry</strong></a></blockquote><p>Now we need yo generate two files:</p><p>- Dockerfile — To build the docker image.</p><p>- service.yaml — The service configuration (example: resources, auto-scaling, ports) to deploy this service via Space Cloud.</p><p>space-cli has a built-in command to generate both of these automatically for us. Just run the following command:</p><pre>space-cli deploy --prepare</pre><p>It is going to ask you a bunch of questions. Answer them with the following required values and leave the rest to default:</p><ul><li><strong>Project Id: </strong>myproject</li><li><strong>Service Id: </strong>myapp</li></ul><p>Great! We now have a Dockerfile and a service.yaml. Feel free to explore and change both these files before finally deploying the service. The service.yaml file looks something like this:</p><pre>api: /v1/runner/{project}/services/{id}/{version}<br>type: service<br>meta:<br>  id: myapp<br>  project: myproject<br>  version: v1<br>spec:<br>  scale:<br>    replicas: 1<br>    minReplicas: 1<br>    maxReplicas: 100<br>    concurrency: 50<br>    mode: parallel<br>  labels: {}<br>  tasks:<br>  - id: myapp<br>    ports:<br>    - name: http<br>      protocol: http<br>      port: 8080<br>    resources:<br>      cpu: 250<br>      memory: 512<br>    docker:<br>      image: localhost:5000/myproject-myapp:v1<br>      cmd: []<br>      secret: &quot;&quot;<br>      imagePullPolicy: &quot;pull-if-not-exists&quot;<br>    env: {}<br>    secrets: []<br>    runtime: image<br>  affinity: []<br>  whitelists:<br>  - projectId: myproject<br>    service: &#39;*&#39;<br>  upstreams:<br>  - projectId: myproject<br>    service: &#39;*&#39;</pre><p>The only step left now is building the docker image and deploying it via Space Cloud.</p><p>We are going to use the deploy command of space-cli for that. It first builds a docker image for us using the Dockerfile (generated in the above step) and then publishes it to the docker registry. Once it’s done publishing, it uses the config in service.yaml file to deploy the service via Space Cloud.</p><p>Enough of talking. Let’s hit the magical command now:</p><pre>space-cli deploy</pre><blockquote><strong>You may have to run the above command with sudo privileges if your </strong><strong>docker is not in the sudoer group.</strong></blockquote><h4><strong>Verify the deployment</strong></h4><p>Checkout to the Overview tab of Deployments section in Mission Control and <strong>hit refresh</strong>. You should be able to see the service we just deployed like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HSlxQjf0QaD4lTw34axk3Q.png" /></figure><h4><strong>Expose your service</strong></h4><p>Currently, the REST service we deployed is accessible from within the cluster only. We need to add Space Cloud Routes to expose our service to the outside world.</p><p>Let’s head over to the Ingress Routing section in the Microservices tab in Mission Control.</p><p>Hit Create your first route to open the following form:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gAiGDNnrT52FKZA2Lzwl5Q.png" /></figure><p>We’ll simply redirect all non Space Cloud traffic to our service for now.</p><p>You’ll need to provide the following details in the form:</p><ul><li><strong>Route Matching Type:</strong> Prefix Match</li><li><strong>Prefix: </strong><strong>/</strong></li></ul><p>A target:</p><ul><li><strong>Scheme:</strong> HTTP</li><li><strong>Service Host: </strong>myapp.myproject.svc.cluster.local</li><li><strong>Port: </strong>8080</li><li><strong>Weight: </strong>100</li></ul><p>Once you are done, hit Add.</p><p>To verify that our REST service is exposed, simply open another tab in your browser and enter:</p><pre><a href="http://localhost:4122/doubler/3">http://localhost:4122/doubler/3</a></pre><p>You should be able to see the following response on your screen:</p><pre>{<br>  &quot;value&quot;: 6<br>}</pre><p>Great! We have our REST service up and running. The REST endpoints can be consumed by the outside world as well. Time to expose these REST endpoints to GraphQL. (Without writing any extra code whatsoever 😛).</p><h3>Step 5: Adding GraphQL to your REST endpoints</h3><p>Head over to the GraphQL APIsection under Microservices in Mission Control.</p><p>Click on the Add first remote service button.</p><p>In the form, enter the following details:</p><ul><li><strong>Service Name: </strong>myapp</li><li><strong>URL: </strong><a href="http://myapp.myproject.svc.cluster.local:8080`">http://myapp.myproject.svc.cluster.local:8080</a></li></ul><blockquote><strong>Note:</strong> myapp.myproject.svc.cluster.local is the domain created by space cloud for internal use. The domain format looks like this &lt;service_id&gt;.&lt;project_id&gt;.svc.cluster.local. If you have an existing REST service, provide it’s hostname here.</blockquote><p>Hit Add. This will add a GraphQL API over the service.</p><p>Unfortunately, Space Cloud cannot automatically discover our APIs. This is because of the way HTTP works in general. There is no introspection feature available for Space Cloud to follow the APIs.</p><p>To overcome this, we need to add endpoints.</p><p>Click the View action against our newly added myapp service.</p><p>This is where you add endpoints. We have two endpoints. Let’s go and add them now!</p><p>Hit the Add your first endpoint button.</p><p>Fill up the form as shown below:</p><ul><li><strong>Endpoint name: </strong>adder</li><li><strong>Endpoint type: </strong>Internal</li><li><strong>Method: </strong>POST</li><li><strong>Path: </strong>/adder</li></ul><p>Now let’s create the next endpoint for doubling the result. Enter the following in the form:</p><ul><li><strong>Endpoint name: </strong>doubler</li><li><strong>Endpoint type: </strong>Internal</li><li><strong>Method: </strong>GET</li><li><strong>Path: </strong><strong>/</strong>doubler/{args.num}</li></ul><p>Notice the {args.num} in the path? This is actually referring to the parameters being passed to the GraphQL query. Don’t worry much about it for now. It will get clearer as we move forward.</p><p>Cool! That’s about it! All that left’s to do is playing around with the API we just created.</p><h3><strong>Step 6: Query your REST service via GraphQL</strong></h3><p>We have added our REST service and the two endpoints as well to Space Cloud. Time to query it using our unified GraphQL API.</p><p>Head to the Explorer section:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OpFb2QlkBn5sCuhbF68POw.png" /></figure><p>Try running the following GraphQL query in the GraphiQL explorer:</p><pre>{<br>  adder(num1: 10, num2: 20) <a href="http://twitter.com/arithmetic">@m</a>yapp {<br>    value<br>  }<br>}</pre><p>You should be able to see a response like this:</p><pre>{<br>  &quot;adder&quot;: {<br>    &quot;value&quot;: 30<br>  }<br>}</pre><p>On getting the above GraphQL query, Space Cloud had made the following request to your REST service:</p><ul><li><strong>Method:</strong> POST</li><li><strong>Path:</strong> /adder</li><li><strong>Request Body:</strong></li></ul><pre>{<br>  &quot;num1&quot;: 10,<br>  &quot;num2&quot;: 20<br>}</pre><p>This means the arguments that you pass in the GraphQL query are send as request body to your REST service.</p><p>Let’s try querying the doubler endpoint with the following GraphQL query:</p><pre>{<br>  doubler(num: 50) <a href="http://twitter.com/arithmetic">@m</a>yapp {<br>    value<br>  }<br>}</pre><p>This GraphQL query is translated by SC to a REST call as follow:</p><pre>GET /doubler/50</pre><p>If you remember the endpoint which we added to Space Cloud for doubler was:</p><pre>/doubler/{args.num}</pre><p>Based on this endpoint, Space Cloud understands that it has to pick an argument num from the GraphQL query and use it as a variable to form the path /doubler/50.</p><p>On successful invocation, you should be able to see a response like this:</p><pre>{<br>  &quot;doubler&quot;: {<br>    &quot;value&quot;: 100<br>  }<br>}</pre><h3><strong>Bonus — Service Chaining</strong></h3><p>Having followed this guide till here successfully, we all deserve a well-earned bonus! Let’s see how this REST to GraphQL transition unlocks a super awesome power, Service Chaining, for us.</p><p>Let’s take a scenario where:</p><ul><li>We want to sum two numbers using the adder service.</li><li>Double the result we got from the adder service.</li></ul><h4><strong>The REST way</strong></h4><p>If we were using REST in our client-side code, the above task would have looked like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WXnD7Fd26m-iNVV2czzdxg.png" /></figure><p>Notice, how we are making two requests from the frontend which means <strong>twice the roundtrip time</strong>. It leads to a slow response time and poor user experience.</p><h4><strong>The GraphQL way</strong></h4><p>Now if we switched the client from REST to GraphQL using Space Cloud, our requests will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K2qvEld2st4RmMwbN5XXwA.png" /></figure><p>Notice, here we are making only one GraphQL query from the frontend to the backend (Space Cloud). Space Cloud, in turn, is making two requests to your REST server to fulfil this request. However, the round trips of these requests (from Space Cloud to your server) are negligible as they are in the same network.</p><p>The GraphQL query to Space Cloud to achieve the above task will be:</p><pre>{<br>  adder(num1: 10, num2: 20) <a href="http://twitter.com/arithmetic">@m</a>yapp {<br>    doubler(num: &quot;adder.value&quot;) <a href="http://twitter.com/arithmetic">@m</a>yapp {<br>      value<br>    }<br>  }<br>}</pre><p>Notice, how we are calling the doubler service after the response of adder service and passing the result of adder service to the doubler as an argument.</p><p>The response for this query will look like this:</p><pre>{<br>  &quot;adder&quot;: {<br>    &quot;doubler&quot;: {<br>      &quot;value&quot;: 60<br>    }<br>  }<br>}</pre><p>As you would have guessed we got 60 as a result ((10 + 20) * 2).</p><p><strong>Additional tip:</strong> If you wanted to query two unrelated REST services parallelly, you could have done that too in a single request like this:</p><pre>{<br>  adder(num1: 10, num2: 20) <a href="http://twitter.com/arithmetic">@m</a>yapp {<br>    value<br>  }<br>  doubler(num: 50) <a href="http://twitter.com/arithmetic">@m</a>yapp {<br>    value<br>  }<br>}</pre><p>I will leave the response part of this query to you as an assignment😛.</p><h3><strong>Conclusion</strong></h3><p>First of all, give yourself a pat on the back for having followed this guide to the end.</p><p>We learned that:</p><ul><li>Migrating from REST to GraphQL doesn’t require code changes.</li><li>We don’t need to choose between REST and GraphQL. We can support both REST and GraphQL together in the same application.</li><li>Using GraphQL with Space Cloud provides us with some neat networking benefits and helps you reduce your round trips.</li></ul><p>Well, there’s a lot more you can do with Space Cloud other than migrating from REST to GraphQL (eg: cross-database joins). <a href="https://github.com/spaceuptech/space-cloud">Give Space Cloud a star on Github</a> if you like it ❤️. <a href="https://discordapp.com/invite/ypXEEBr">Join our Discord server</a> if you wanna bounce off some ideas or have any questions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44f3b86cf79f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/rest-to-graphql-in-minutes-44f3b86cf79f">REST to GraphQL in minutes</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Instant GraphQL APIs on MySQL]]></title>
            <link>https://medium.com/backstagewitharchitects/instant-graphql-apis-on-mysql-d2457e1c177d?source=rss----903c183e8eba---4</link>
            <guid isPermaLink="false">https://medium.com/p/d2457e1c177d</guid>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[graphql]]></category>
            <dc:creator><![CDATA[Noorain - YourTechBud]]></dc:creator>
            <pubDate>Sat, 23 Nov 2019 12:33:03 GMT</pubDate>
            <atom:updated>2020-06-12T13:16:17.101Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RQQLJy_N0XD2qs5V2ncw6w.jpeg" /></figure><p>GraphQL and MySQL are two popular technologies that developers love!</p><p>In this guide, I am going to show how you can easily set up a GraphQL API layer for your MySQL using <a href="https://github.com/spaceuptech/space-cloud">Space Cloud</a> in a couple of minutes.</p><p>The keyword to note here is <strong>instant</strong>. Which means you won’t have to write a GraphQL backend yourself. Sounds like a magic right? Let me explain it to you in a minute.</p><h3><strong>Table of contents</strong></h3><ul><li><a href="#2655">MySQL and GraphQL intro</a></li><li><a href="#8c67">What is Space Cloud?</a></li><li><a href="#d1bb">What if I don’t use MySQL?</a></li><li><a href="#5dc3">Getting started with GraphQL and MySQL</a></li><li><a href="#8f4a">Setup</a></li><li><a href="#e45a">Creating MySQL schema</a></li><li><a href="#c835">Running GraphQL queries on Space Cloud</a></li><li><a href="#eee9">Conclusion</a></li></ul><h3><strong>MySQL and GraphQL Intro</strong></h3><p><a href="https://www.mysql.com/">MySQL</a> is one of the most popular SQL databases out there, <a href="https://db-engines.com/en/ranking">next only to Oracle</a>. The ease of setup and learning MySQL makes it a popular choice for most web projects. If you are an organization that needs a robust database management tool but are on a budget, then MySQL might be ideal for you.</p><p>GraphQL, on the other hand, is a query language for your APIs, gradually replacing REST. It allows you to describe your data needs precisely and provides you with the exact data you had asked for, solving age-old under fetching and over fetching problems. If you are new to GraphQL and want to learn more about it, check out its official <a href="https://graphql.org/">site</a>.</p><p>However, to avail these excellent benefits, you need to set up a GraphQL API on the backend by <a href="https://graphql.org/learn/execution/">writing resolvers</a>. Having to write a GraphQL server adds friction to its adoption. While many tools help you auto-generate GraphQL APIs for Postgres, there aren’t many that work with MySQL. That’s where Space Cloud comes into the picture by <strong>providing GraphQL APIs for your MySQL without writing a backend</strong>. So, let’s see what Space Cloud is all about.</p><h3><strong>What is Space Cloud?</strong></h3><p>To keep things simple,</p><blockquote>Space Cloud is an open-source, serverless platform to develop, secure and scale your applications. It provides instant GraphQL and REST APIs on top of any database and microservice.</blockquote><p>The coolest part about Space Cloud is that the database APIs are realtime. i.e. You can optionally <em>_subscribe_</em> to changes in the database. This functionality comes in handy while <a href="https://blog.spaceuptech.com/posts/adding-realtime-functionality">adding realtime functionalities to your app</a>.</p><p>Since Space Cloud also has a robust security module built into it, you can consume these APIs directly from the frontend. So in most use cases, just making an Angular or React app using the Space Cloud APIs should be the only code you write!</p><p>What’s more? You can deploy your existing HTTP workloads to Kubernetes/Docker using Space Cloud via a single command. Space Cloud will automatically scale these workloads including <strong>scale down to zero</strong>. It will also take care of end to end encryption, traffic splitting and much more! Check out all the <a href="https://docs.spaceuptech.com/introduction/features/">features of Space Cloud</a></p><p>Already in love with Space Cloud? Show your support ❤ by <a href="https://github.com/spaceuptech/space-cloud">giving us a star on Github</a>.</p><h3><strong>What if I don’t use MySQL?</strong></h3><p>Space Cloud provides GraphQL APIs for MongoDB, Postgres, SQL Server and MySQL (and their compatible databases) out of the box. If you use any other database apart from MySQL, then these guides can help you get started:</p><ul><li><a href="https://blog.spaceuptech.com/posts/getting-started-with-graphql-and-postgres/">Getting started with Postgres</a></li><li><a href="https://blog.spaceuptech.com/posts/getting-started-with-graphql-and-mongo/">Getting started with MongoDB</a></li></ul><p>Cool, that’s enough of history and features. Let’s dive straight into it!</p><h3><strong>Getting started with GraphQL and MySQL</strong></h3><p>In this guide, we are going to building a simple blogging application on top of MySQL, which has authors and posts.</p><p>Each author can have multiple posts, whereas a post can only belong to a single author. Note that this is a <strong>one-to-many relation</strong>.</p><blockquote><strong>Note:</strong> Don’t worry if you get stuck somewhere. You can always ping me on our <a href="https://discordapp.com/invite/ypXEEBr">Discord channel</a>.</blockquote><h3><strong>Setup</strong></h3><p>We are using space-cli to run both Space Cloud and MySQL for us.</p><h4>Step 1: Install the space-cli</h4><p>space-cli is the cli tool for Space Cloud. You can download a version for your particular platform:</p><ul><li><a href="https://storage.googleapis.com/space-cloud/linux/space-cli.zip">Linux</a></li><li><a href="https://storage.googleapis.com/space-cloud/darwin/space-cli.zip">MacOS</a></li><li><a href="https://storage.googleapis.com/space-cloud/windows/space-cli.zip">Windows</a></li></ul><p>Unzip the compressed archive.</p><p><strong>For Linux / Mac:</strong> unzip space-cli.zip &amp;&amp; chmod +x space-cli</p><p><strong>For Windows:</strong> Right-click on the archive and select extract here.</p><p>Copy the space-cli binary to your environment path variable for global usage.</p><p><strong>For Linux / Mac:</strong> Copy the space-cli to /usr/local/bin. You may have to use sudo depending on the permissions of your /usr/local/bin.</p><p><strong>For Windows: </strong>Add the path of the space-cli.exe to the environment variable PATH for making space-cli accessible globally.</p><p>To make sure if the space-cli binary is installed correctly, type the following command:</p><p><strong>For Linux / Mac: </strong>space-cli --version</p><p><strong>For Windows:</strong> space-cli.exe --version</p><p>This prints the space-cli version.</p><h4>Step 2: Run Space Cloud &amp; MySQL</h4><p>We can set up all Space Cloud components (gateway and runner) via space-cli using a single command.</p><pre>space-cli setup --dev</pre><p>The setup command selects Docker as a target by default and runs all the containers required to setup Space Cloud. On successful installation it generates an output similar to this one:</p><pre>INFO[0000] Setting up Space Cloud on docker. <br>INFO[0000] Fetching latest Space Cloud Version <br>INFO[0000] Starting container space-cloud-gateway… <br>INFO[0000] Image spaceuptech/gateway:latest already exists. No need to pull it again <br>INFO[0000] Starting container space-cloud-runner… <br>INFO[0000] Image spaceuptech/runner:latest already exists. No need to pull it again<br>INFO[0001] Space Cloud (id: &quot;local-admin&quot;) has been successfully setup! 👍 <br>INFO[0001] You can visit mission control at <a href="http://localhost:4122/mission-control">http://localhost:4122/mission-control</a> 💻 <br>INFO[0001] Your login credentials: [username: &quot;local-admin&quot;; key: &quot;KkYr6FvgYsvr&quot;] 🤫</pre><p><strong>Verify Space Cloud installation</strong></p><p>Verify the installation by running the following docker command:</p><pre>docker ps</pre><p>You should see an output similar to this:</p><pre>CONTAINER ID        IMAGE                        COMMAND             CREATED              STATUS              PORTS                                            NAMES<br>507ce4042486        spaceuptech/runner:latest    &quot;./app start&quot;       About a minute ago   Up About a minute                                                    space-cloud-runner<br>33a5a7a9be3a        spaceuptech/gateway:latest   &quot;./app run&quot;         About a minute ago   Up About a minute   0.0.0.0:4122-&gt;4122/tcp, 0.0.0.0:4126-&gt;4126/tcp   space-cloud-gateway</pre><p><strong>Run MySQL</strong></p><p>We will be running a fresh MySQL database in this guide. However, you can even <a href="https://docs.spaceuptech.com/storage/database/add-existing-database">add your existing databases to Space Cloud</a>.</p><p>We can run a MySQL database inside a docker container via space-cli using the following command:</p><pre>space-cli add database mysql --alias mysql</pre><p>It might take some time if you did not have the mysql image cached locally.</p><p>Once we have started MySQL, we need to know its IP address. Luckily, the space-cli add database command also creates a domain name for our database, which is of the following format:</p><pre>&lt;alias-name&gt;.db.svc.cluster.local</pre><p>Since we had set the — alias to mysql in the add database command, the domain name for our MySQL translates to:</p><pre>mysql.db.svc.cluster.local</pre><h4><strong>Step 3: Configure Space Cloud</strong></h4><p>If you exec into docker container of Space Cloud’s gateway, you can see a config.yaml file generated by Space Cloud in the /app directory.</p><p>Space Cloud needs this config file to function. The config file is used to load various information like which databases to use, their connection strings and security rules.</p><p>Space Cloud has it’s own Mission Control (admin UI) to configure all of this quickly.</p><p><strong>Open Mission Control:</strong></p><p>Head over to <a href="http://localhost:4122/mission-control">http://localhost:4122/mission-control</a> to open Mission Control.</p><blockquote><strong>Note:</strong> Replace localhost with the address of your Space Cloud if you are not running it locally.</blockquote><p><strong>Create a project:</strong></p><p>Click on Create a Project button to open the following screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KW12xfsblTYzlJuZWRKztw.png" /></figure><p>Give a name to your project.</p><p>Hit Create Project button to create the project.</p><p><strong>Add database to your project</strong></p><p>After creating the project, the next step is to add a database to your project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23n8jlmXquDZ1fmPIqbiHA.png" /></figure><p>Select MySQL as your database.</p><p>Replace localhost with the domain name we noted earlier to get the final connection string as:</p><pre>root:my-secret-pw@tcp(mysql.db.svc.cluster.local:3306)/</pre><p>Hit Add database button. If the connection was successful, you would get a notification for a successful connection. After that you would be directed to the overview page of your project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xkQWjeXsje0thmf0f4RJlQ.png" /></figure><h3><strong>Creating MySQL schema</strong></h3><p>Space Cloud provides you with a neat <strong>GraphQL SDL</strong> to describe the schema for your MySQL tables which looks something like this:</p><pre>type author {<br>  id: ID! <a href="http://twitter.com/primary">@primary</a><br>  name: String!<br>}</pre><p>Space Cloud automatically creates a table named author in MySQL based on the schema. Thus, there is <strong>no need to deal with SQL statements</strong> to create tables.</p><p>The best part is that the schema SDL provided by Space Cloud is <strong>declarative</strong> in nature.</p><p>What this means is that Space Cloud always tries to accomplish the schema you have provided by only making those changes which are required. For example, let’s say you added one more field to your schema like this:</p><pre>type author {<br>  id: ID! <a href="http://twitter.com/primary">@primary</a><br>  name: String!<br>  description: String<br>}</pre><blockquote><strong>Note:</strong> Space Cloud won’t create the entire table again since it’s already there. Instead, it just uses the ALTER TABLE statement to add a column. This ability in Space Cloud allows us to focus on our application rather than the database.</blockquote><h4><strong>Creating author table</strong></h4><p>Head over to the Database section.</p><p>Click on Add a table button in the Overview tab to open the following form:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J1AXfQCXbMXaoyOZ6a3U1g.png" /></figure><p>Give your table name as author.</p><p><strong>Copy-paste the following schema and hit save</strong>:</p><pre>type author {<br>  id: ID! <a href="http://twitter.com/primary">@primary</a><br>  name: String!  <br>  posts: [post] @link(table: &quot;post&quot;, from: &quot;id&quot;, to: &quot;author_id&quot;)<br>}</pre><p>This schema creates a table author with id and name fields. The type ID is nothing but an auto-generated unique id whereas @primary directive tells Space Cloud to make the id field as a primary key of the table. Checkout <a href="https://docs.spaceuptech.com/essentials/data-modelling/">data modelling in Space Cloud</a> to learn more.</p><blockquote><strong>Note:</strong> The posts field is not a physical field in the author table. It’s just a virtual field that links to the post table to make GraphQL queries and mutations simpler from the frontend. We are going to cover it in more details below.</blockquote><h4><strong>Creating post table</strong></h4><p>Once again click on the Add button in the Database section.</p><p>Give your table name as post.</p><p>Copy-paste the following schema and hit save:</p><pre>type post {<br>  id: ID! <a href="http://twitter.com/primary">@primary</a><br>  title: String!<br>  author_id: ID! @foreign(table: &quot;author&quot;, field: &quot;id&quot;)<br>}</pre><p>Notice the @foreign directive. It creates a foreign key on the author table to maintain the integrity of the relation. Read more about <a href="https://docs.spaceuptech.com/essentials/data-modelling/relations/">modelling relations in Space Cloud</a>.</p><p>Great! We have created all the tables required for this guide. Its time to play around with MySQL using the auto-generated GraphQL APIs.</p><h3><strong>Running GraphQL queries on Space Cloud</strong></h3><p>Mission Control has an embedded GraphiQL IDE which lets us fire queries without having to build a frontend.</p><p>Head over to the Explorer section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OpFb2QlkBn5sCuhbF68POw.png" /></figure><p>Let’s start by creating some authors and posts, which translates to inserting a few records in your MySQL.</p><h4><strong>Inserting data</strong></h4><p>Inserting data into our MySQL is as simple as firing a GraphQL query for us. So let’s try inserting a few authors along with their posts. Below is a GraphQL query that helps us do that. Try running this GraphQL query in the GraphiQL editor:</p><pre>mutation {<br> insert_author(<br>   docs: [<br>    { <br>      id: &quot;1&quot;, <br>      name: &quot;Dan&quot;,<br>      posts: [<br>        { id: &quot;1&quot;, title: &quot;Introducing Hooks&quot; },<br>        { id: &quot;2&quot;, title: &quot;React vs Vue&quot; }<br>      ]<br>    },<br>    { <br>      id: &quot;2&quot;, <br>      name: &quot;Ryan&quot;,<br>      posts: [<br>        { id: &quot;3&quot;, title: &quot;Context API&quot; },<br>        { id: &quot;4&quot;, title: &quot;React + Redux&quot; }<br>      ]<br>    }<br>  ]<br> ) <a href="http://twitter.com/mysql">@mysql</a> {<br>   status<br>   error<br> }<br>}</pre><p>On successful insert, you should be able to see the status as 200 which means you have successfully inserted the documents in your MySQL.</p><p>The docs argument is nothing but the array of records that you want to insert in your table.</p><p>If you remember, we mentioned a @link directive on the posts field in the author table along with the argument — table: “post”. Space Cloud uses this information to insert the posts array correctly into the post table. It also inserts the post.author_id field automatically based on the foreign key that we mentioned.</p><blockquote><strong>Note:</strong> Even if we leave the id field blank, Space Cloud auto-generates a unique random string as id for us.</blockquote><p>If you have noticed, we have used a @mysql directive in the above query. Replace it with whatever alias name you have kept while adding the database in Mission Control.</p><p><strong>Querying data with filters</strong></p><p>Having inserted authors and their posts, let’s try to query posts of a particular author. It’s nothing but a filtering operation.</p><p>Try running the following query in the GraphiQL editor:</p><pre>query {<br> post (<br>   where: {author_id: &quot;1&quot;}<br> ) <a href="http://twitter.com/mysql">@mysql</a> {<br>   id<br>   title<br> }<br>}</pre><p>As you can see, we only got the posts that belonged to author_id 1. Check out this guide for a <a href="https://docs.spaceuptech.com/essentials/queries/filtering">complete list of filtering options</a>.</p><h4><strong>Updating data</strong></h4><p>Updates require two information: a where clause to filter the documents to be updated and at least one update operator.</p><p>Let’s say you want to update the name of a particular author in your MySQL. Here’s a GraphQL query to do it:</p><pre>mutation {<br> update_author (<br>   where: { id: &quot;2&quot;},<br>   set: {name: &quot;Ryan Florence&quot;}<br> ) <a href="http://twitter.com/mysql">@mysql</a> {<br>   status<br>   error<br> }<br>}</pre><p>You can also perform various operations like incrementing, decrementing, multiplying. Check out this guide for a <a href="https://docs.spaceuptech.com/essentials/mutations/update">complete list of update operations</a>.</p><h4><strong>Performing joins</strong></h4><p>Now comes the fun part, i.e. fetching relational data from your MySQL as a nested JSON on frontend.</p><p>Let’s say we want to show a list of authors along with the title of their posts. You can easily query such relational data from MySQL via a simple GraphQL query of Space Cloud. Try running the following GraphQL query:</p><pre>query {<br> author <a href="http://twitter.com/mysql">@mysql</a> {<br>   id<br>   name<br>   posts {<br>     title<br>   }<br> }<br>}</pre><p>The response should look something like this:</p><pre>{<br>  &quot;author&quot;: [<br>    {<br>      &quot;id&quot;: &quot;1&quot;,<br>      &quot;name&quot;: &quot;Dan&quot;,<br>      &quot;posts&quot;: [<br>        {<br>          &quot;title&quot;: &quot;Introducing Hooks&quot;<br>        },<br>        {<br>          &quot;title&quot;: &quot;React vs Vue&quot;<br>        }<br>      ]<br>    },<br>    {<br>      &quot;id&quot;: &quot;2&quot;,<br>      &quot;name&quot;: &quot;Ryan Florence&quot;,<br>      &quot;posts&quot;: [<br>        {<br>          &quot;title&quot;: &quot;Context API&quot;<br>        },<br>        {<br>          &quot;title&quot;: &quot;React + Redux&quot;<br>        }<br>      ]<br>    }<br>  ]<br>}</pre><p>The query that we used above performs a join operation on the backend between author and post table with the condition — author.id == post.author_id. This condition is derived from the arguments (table, from, to) of the @link directive, which we mentioned earlier. You can read more about @link directive from <a href="https://medium.com/essentials/data-modelling/types-and-directives/#link-directive">here</a>.</p><p>Although Space Cloud supports most of the database operations out of the box, it also provides an escape hatch for advanced use cases in the form of <a href="https://docs.spaceuptech.com/storage/database/prepared-queries">prepared queries</a>.</p><h3><strong>Conclusion</strong></h3><p>Setting up an GraphQL layer on top of MySQL is not that difficult. Just point Space Cloud to a MySQL instance are you are good to consume GraphQL in your applications.</p><p>As an advantage, GraphQL opens up tons of new possibilities like performing joins and making the already popular MySQL even simpler to use. We are very excited about it and are going to double down on making our GraphQL support more robust.</p><p>The declarative schema for MySQL allows us to focus more on our applications rather than the database.</p><p>The future, however, seems to be amazing. With the ability to do <a href="https://docs.spaceuptech.com/essentials/queries/joins/">cross-database joins</a> with MySQL and other NoSQL databases mean we can build enterprise-grade apps that leverage the best of both worlds.</p><p>Show your support❤️ by <a href="https://github.com/spaceuptech/space-cloud">giving it a star on Github</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2457e1c177d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/backstagewitharchitects/instant-graphql-apis-on-mysql-d2457e1c177d">Instant GraphQL APIs on MySQL</a> was originally published in <a href="https://medium.com/backstagewitharchitects">Backstage With Architects</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>