<?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[Crux Intelligence - Medium]]></title>
        <description><![CDATA[Building the future of Business Intelligence - Medium]]></description>
        <link>https://medium.com/cuddle-ai?source=rss----27e35c10d45d---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Crux Intelligence - Medium</title>
            <link>https://medium.com/cuddle-ai?source=rss----27e35c10d45d---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 19:02:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/cuddle-ai" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Auto Scaling Microservices with Kubernetes Event-Driven Autoscaler (KEDA)]]></title>
            <link>https://medium.com/cuddle-ai/auto-scaling-microservices-with-kubernetes-event-driven-autoscaler-keda-8db6c301b18?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/8db6c301b18</guid>
            <category><![CDATA[autoscaling]]></category>
            <category><![CDATA[cuddlepe]]></category>
            <category><![CDATA[cuddleengineering]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[Suman Das]]></dc:creator>
            <pubDate>Mon, 27 Feb 2023 05:29:13 GMT</pubDate>
            <atom:updated>2023-02-27T05:29:13.157Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jVHESsjaA45xZf8IJKITVA.png" /></figure><p><strong>Autoscaling</strong> (sometimes spelled as auto scaling or auto-scaling) is the process of automatically increasing or decreasing the usage of computational resources required for a cloud workload based on the application&#39;s need. Scalability is one of the most important aspects to consider for modern container based application deployments. Autoscaling has become an integral component in almost all the cloud platforms, microservices <em>aka</em> containers/pods are not an exemption to this. Microservices in fact are known for flexible and decoupled design, which can best fit in for auto-scaling as they are much easier to create on-demand than virtual machines.</p><p>The primary benefit of autoscaling, when configured and managed properly, is that the workload gets exactly the cloud computational resources it requires at any given point in time. We pay only for the server resources we need at that point in time.</p><p>Since Kubernetes has become the de facto standard for container orchestration and has been adopted very well across all industries, we need to think about how we can scale these applications deployed on Kubernetes on demand and also to scale it down when not in use. Since most of the cloud providers support <a href="https://learn.microsoft.com/en-us/azure/aks/cluster-autoscaler"><strong>Cluster Autoscaler</strong></a>, cluster autoscaler along with container autoscaling enabled becomes a powerful combination to scale any applications with minimal cloud cost.</p><h4>Cluster Autoscaler</h4><p>To adjust to changing application demands, such as between the workday and evening or on a weekend, clusters often need a way to automatically scale. Most of the Cloud providers like Azure, AWS, and GCP support the given Kubernetes Autoscalers. Cluster AutoScaler is nothing but the capability provided by Cloud providers to add/remove computing resources (machines -&gt; VM) at runtime. In general Kubernetes clusters scale in and scale out in one of two ways:</p><ul><li>The Cluster Autoscaler watches for pods that can’t be scheduled on nodes because of resource constraints. The cluster then automatically increases the number of nodes. In case there are no pods running on any nodes then it will remove that node automatically.</li><li>The Horizontal Pod Autoscaler uses the Metrics Server in a Kubernetes cluster to monitor the resource demand of pods. If an application needs more resources, the number of pods is automatically increased to meet the demand.</li></ul><p>Here, we will not demonstrate the use of the Cluster Autoscaler feature for Node AutoScaling. But in real-world scenarios, we can enable Cluster AutoScaler along with Pod AutoScaling to save on cloud costs. Using <strong>KEDA</strong>, we can remove the pods when not required, and then the Cluster Autoscaler will remove the node on which no pods are scheduled. This will help us to reduce cloud costs.</p><h4>What is KEDA?</h4><p><strong>KEDA</strong> is a <a href="https://kubernetes.io/">Kubernetes</a>-based Event Driven Autoscaler. With KEDA, we can drive the scaling of any container in Kubernetes based on the number of events needing to be processed. KEDA handles the triggers to respond to events that occur in other services and scales workloads as needed.</p><p><strong>KEDA</strong> is a single-purpose and lightweight component that can be added to any Kubernetes cluster. KEDA works alongside standard Kubernetes components like the <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/">Horizontal Pod Autoscaler</a> and can extend functionality without overwriting or duplication. With KEDA we can explicitly map the apps we want to scale, with other apps continuing to function. This makes KEDA a flexible and safe option to run alongside any Kubernetes applications or frameworks.</p><h4>Scaler</h4><p><strong>KEDA</strong> has a wide range of <a href="https://keda.sh/scalers"><strong>scalers</strong></a> that can detect if a deployment should be activated or deactivated, and feed custom metrics for a specific event source.</p><h4>ScaledObject</h4><p>ScaledObject is deployed as a Kubernetes CRD (Custom Resource Definition) which is used to define how <strong>KEDA</strong> should scale our application and what triggers to use.</p><p>In this tutorial, we will see how we can leverage <strong>KEDA</strong> to autoscale Microservices deployed as containers or pods in the Kubernetes cluster, based on the below events.</p><ol><li>External event or trigger.</li><li>HTTP event</li></ol><p>Although the example that we will use here is very trivial, it is just for pure demonstration purposes only. Here, we will explore some of the <a href="https://keda.sh/docs/2.9/scalers/"><strong>scalers</strong></a> supported by KEDA.</p><p>We will follow the below steps to illustrate, how we can leverage KEDA for auto-scaling pods deployed in the Kubernetes cluster.</p><ol><li>Installing KEDA</li><li>Deploying sample application</li><li>Deploying KEDA Event Scaler</li><li>Testing Auto-Scaling with Event Scaler</li><li>Deploying HTTP Scaler</li><li>Testing Auto-Scaling with HTTP Scaler</li></ol><p><strong>Prerequisites</strong></p><ul><li><strong>Kubernetes cluster:</strong> We will need a running Kubernetes cluster. In case Kubernetes Cluster is not available then we can follow the instructions to <a href="https://kubernetes.io/docs/setup/"><em>Set Up a Kubernetes Cluster</em></a><em>.</em></li><li><strong>Docker Hub Account:</strong> An account at Docker Hub for storing Docker images that we will create during this tutorial. Refer to the <a href="https://hub.docker.com/">Docker Hub</a> page for details about creating a new account.</li><li><strong>kubectl:</strong> Refer to <a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/">Install and Set Up kubectl</a> page for details about installing kubectl.</li><li><strong>Helm</strong>: Here we will use Helm (<a href="https://helm.sh/docs/intro/install/">Helm installation guide</a>) to deploy KEDA.</li></ul><p>Let’s begin.</p><h4>1. Installing KEDA</h4><p>Below are the various options which can be used to install KEDA on Kubernetes Cluster.</p><ul><li><a href="https://keda.sh/docs/2.9/deploy/#helm">Helm charts</a></li><li><a href="https://keda.sh/docs/2.9/deploy/#operatorhub">Operator Hub</a></li><li><a href="https://keda.sh/docs/2.9/deploy/#yaml">YAML declarations</a></li></ul><p>Here, we will use Helm Chart to deploy KEDA.</p><p><strong>1.1 Add Keda Core Repo to Helm:</strong> First, we will add the kedacore repository to our Helm instance by executing the below command.</p><pre>helm repo add kedacore https://kedacore.github.io/charts</pre><p><strong>1.2 Update Helm repo:</strong> Then, let’s fetch the information on the new kedacore repo that we added above:</p><pre>helm repo update</pre><p><strong>1.3 Install </strong><strong>keda Helm chart: </strong>Finally, we need to create a namespace for KEDA and install kedacore using Helm chart:</p><pre>kubectl create namespace keda<br>helm install keda kedacore/keda --namespace keda</pre><p>Once the installation is successful, let’s verify whether the <strong>keda</strong> pods are up and running by executing the below command.</p><pre>kubectl get deploy,crd -n keda</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dFaXEEYfre5XVfpJkjE0Cg.png" /><figcaption>keda pods status</figcaption></figure><p>As we can see above in the image the keda pods are up and running.</p><h4>2. Deploying Sample Application</h4><p>It’s time to deploy a demo application. Here, we will use the official latest nginx image at Kubernetes. To deploy the sample application, we will execute the below commands. It creates a ReplicaSet to bring up nginx Pods.</p><p><strong>2.1 Creating a Deployment</strong></p><p>Deployment provides declarative updates for <a href="https://kubernetes.io/docs/concepts/workloads/pods/">Pods</a> and <a href="https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/">ReplicaSets</a>. In Kubernetes, a deployment is a method of launching a pod with containerized applications and ensuring that the necessary number of replicas is always running on the cluster. Let’s create a file nginx-deployment.yaml with the below content for our sample application. The file contains the deployment details for our sample application.</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: nginx<br>  labels:<br>    app: nginx<br>spec:<br>  replicas: 1<br>  selector:<br>    matchLabels:<br>      app: nginx<br>  template:<br>    metadata:<br>      labels:<br>        app: nginx<br>    spec:<br>      containers:<br>      - name: nginx<br>        image: nginx:1.14.2<br>        ports:<br>        - containerPort: 80 </pre><p>Now we will create the Deployment using the above file.</p><pre>kubectl apply -f nginx-deployment.yaml</pre><p>Let’s check if the deployment was created successfully by executing the below command.</p><pre>kubectl get deployments</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*qHb8eO5U3OW6iUKIGtbk-A.png" /><figcaption>deployment status</figcaption></figure><p>As we can see above in the image our deployment is deployed successfully with one ReplicaSet.</p><p>Let’s also check the status of the pod using the below command. Its status should be Running.</p><pre>kubectl get pods</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PqcHT8N9VhOkNIi5kRBHJA.png" /><figcaption>pod status</figcaption></figure><p>We can see that 1 instance of our pod is running that we have specified in our deployment configuration.</p><p><strong>2.4 Exposing the pods</strong></p><p>Now we will be exposing our pods so that they can be accessed on the browser. A NodePort service is the most primitive way to get external traffic directly to our service. NodePort, as the name implies, opens a specific port on all the Nodes (the VMs), and any traffic that is sent to this port is forwarded to the service. We will use the below command to create the NodePort service.</p><pre>kubectl expose deployment nginx --port=80 --type=NodePort</pre><p>Once the above command runs successfully, we should be able to list out the services.</p><pre>kubectl get svc</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W2m-yZlBEJtCYYVCvtrYrQ.png" /><figcaption>node port service</figcaption></figure><p>Now we should be able to access the sample application page using the below command:</p><pre>curl http://localhost:32511</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GkifUy9wEB1Wypa5o7GF4Q.png" /><figcaption>sample application output</figcaption></figure><p>As we can see in the above image we are getting a valid response from our deployed application.</p><p>We now have our Kubernetes Cluster up and running with our sample application deployed. We have also deployed <strong>KEDA</strong> related configurations. Let’s now see how we can scale our application using KEDA.</p><h4>3. Deploying KEDA Event Scaler</h4><p>KEDA integrates with multiple <a href="https://keda.sh/docs/2.9/scalers/"><strong>Scalers</strong></a> (event sources) and uses Custom Resources (CRDs) to define the required/desired scaling behavior and parameters. Deployments and StatefulSets are the most common way to scale workloads with KEDA. It allows us to define the Kubernetes Deployment or StatefulSet that we want KEDA to scale based on a scale trigger. KEDA will monitor that service and based on the events that occur it will automatically scale our resource out/in accordingly. Behind the scenes, KEDA monitors the event source and feeds that data to Kubernetes and the HPA (Horizontal Pod Autoscaler) to drive the rapid scale of a resource.</p><p><strong>3.1 Scaler</strong></p><p>KEDA uses a <strong>Scaler</strong> to detect if a deployment should be activated or deactivated (scaling) which in turn is fed into a specific event source. Here we will use <a href="https://keda.sh/docs/2.9/scalers/cron/"><strong>Cron</strong></a> event scaler to demonstrate Auto Scaling. We will scale out our application for some duration and then later scale it back to normal count. Cron scaler is especially useful when there is a known pattern in the application usage like it’s used during weekdays and not used during weekends.</p><p><strong>3.2 ScaledObject</strong></p><p>ScaledObject is deployed as a Kubernetes CRD (Custom Resource Definition) which defines the relationship between an event source to a specific workload (i.e. Deployment, StatefulSet) for scaling.</p><p>Let’s create a file with the name scaledobjects.yaml with the below content. The file contains the event details that KEDA will monitor to scale our sample application.</p><pre>apiVersion: keda.sh/v1alpha1<br>kind: ScaledObject<br>metadata:<br>  name: nginx-deployment<br>  namespace: default<br>spec:<br>  scaleTargetRef:<br>    apiVersion:    apps/v1        # Optional. Default: apps/v1<br>    kind:          Deployment     # Optional. Default: Deployment<br>    name:          nginx      # Mandatory. Must be in the same namespace as the ScaledObject<br>  pollingInterval:  5                    # Optional. Default: 5 seconds<br>  cooldownPeriod:   300                  # Optional. Default: 300 seconds<br>  minReplicaCount:  0                   # Optional. Default: 0<br>  maxReplicaCount:  5                    # Optional. Default: 100<br>  fallback:                                        # Optional. Section to specify fallback options<br>    failureThreshold: 3                            # Mandatory if fallback section is included<br>    replicas: 1                                    # Mandatory if fallback section is included<br>  advanced:                                        # Optional. Section to specify advanced options<br>    restoreToOriginalReplicaCount: true     # Optional. Default: false<br>    horizontalPodAutoscalerConfig:          # Optional. Section to specify HPA related options<br>      name: keda-hpa-nginx       # Optional. Default: keda-hpa-{scaled-object-name}<br>      behavior:                             # Optional. Use to modify HPA&#39;s scaling behavior<br>        scaleDown:<br>          stabilizationWindowSeconds: 600<br>          policies:<br>            - type: Percent<br>              value: 100<br>              periodSeconds: 15<br>  triggers:<br>  - type: cron<br>    metadata:<br>      # Required<br>      timezone: Asia/Kolkata  # The acceptable values would be a value from the IANA Time Zone Database.<br>      start: 30 * * * *       # Every hour on the 30th minute<br>      end: 45 * * * *         # Every hour on the 45th minute<br>      desiredReplicas: &quot;5&quot;</pre><p>Here we have used Cron scaler as a trigger event. The above ScaledObject Custom Resource is used to define how KEDA will scale our application and what the triggers are. The minimum replica count is set to 0 and the maximum replica count is 5.</p><p>Let’s create the ScaledObject using the below command.</p><pre>kubectl apply -f scaledobjects.yaml</pre><p>As we can see, it’s all pretty straightforward. The most important thing is the triggers definition, which will configure what kind of event our application will be scaled in response to.</p><p>Cron trigger contains the below parameters.</p><ul><li>timezone - One of the acceptable values from the IANA Time Zone Database. The list of timezones can be found <a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones">here</a>.</li><li>start - Cron expression indicating the start of the Cron schedule.</li><li>end - Cron expression indicating the end of the Cron schedule.</li><li>desiredReplicas - Number of replicas to which the resource has to be scaled <strong>between the start and end</strong> of the Cron schedule.</li></ul><p>Once the ScaledObject is created, the KEDA controller automatically syncs the configuration and starts watching the deployment nginx created above. KEDA seamlessly creates a HPA (Horizontal Pod Autoscaler) object with the required configuration and scales out the replicas based on the trigger-rule provided through ScaledObject (in this case it is Cron expression).</p><h4>4. Testing Auto-Scaling with Event Scaler</h4><p>Once the ScaledObject is created for an application with a relevant trigger, KEDA will start monitoring our application. We can check the status of ScaledObjects using the below command.</p><pre>kubectl get scaledobjects</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VZdz8AAZ84YH_WuJHFxUbw.png" /><figcaption>scaledObject</figcaption></figure><p>As we can see above in the image our ScaledObject is deployed successfully with a minimum replica count of 0 and a maximum replica count of 5 which we have specified in our ScaledObject definition. Once the event is triggered then KEDA will scale out our application automatically. Let’s check the KEDA logs to monitor the events.</p><p>We can use the below command to list the KEDA pods.</p><pre>kubectl get pods -n keda</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lDgVX_iOUMPUtYYe0dCOuQ.png" /><figcaption>keda pods</figcaption></figure><p>We can then monitor the logs of keda-operator pod for events.</p><pre>kubectl logs -f keda-operator-6c99649b58-r5qtl -n keda --tail=100</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oaUmh6TjGsWnQE7GjXGlAQ.png" /><figcaption>keda-operator logs</figcaption></figure><p>The above image shows the scale-out event was triggered. Once the scale-out event is triggered, our application will be scaled to run 5 instances and once the scale-in event is triggered it will be scaled back to 0.</p><p>Let’s check the count of our application instances after the scale-out event is triggered:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l26Qw4Tx9ClsilfpppidDg.png" /><figcaption>after scale-out is triggered</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*mp8mtMdVRVknec2F5crhEg.png" /><figcaption>after scale-out is triggered</figcaption></figure><p>The below image displays our application deployment status after the scale-in event is triggered:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*OxCAVrNS4MqLSEZRs798IQ.png" /><figcaption>after scale-in is triggered</figcaption></figure><h4>5. Deploying HTTP Scaler</h4><p>We just saw above, how we can scale our application based on events. Now let’s say we have some application where we cannot generate events, it supports only HTTP traffic. Then how do we scale those applications?</p><p>Let’s see that now.</p><p>Currently, there are 2 ways by which we can scale our application based on HTTP Traffic using KEDA.</p><ol><li>Using <a href="https://keda.sh/scalers/prometheus/">Prometheus scaler</a> to create scale rules based on metrics around HTTP events.</li><li>Using KEDA HTTP Add-on.</li></ol><p>Here we will explore <strong>KEDA HTTP Add-on.</strong></p><p><strong>5.1 Install HTTP add-on</strong></p><p>The KEDA HTTP Add-on allows Kubernetes users to automatically scale their application up and down (including to/from zero) based on incoming HTTP traffic.</p><p>KEDA doesn’t come with an HTTP scaler by default, so we will have to install it separately. We need to execute the below command to install HTTP Add-on on our Kubernetes Cluster.</p><pre>helm install http-add-on kedacore/keda-add-ons-http --namespace keda</pre><p><strong>5.2 Defining an Autoscaling Strategy for HTTP Traffic</strong></p><p>Now we have KEDA installed along with HTTP Add-on. To keep things simple we will delete the above deployed ScaledObject. We don’t have to do this in production since we will be using different scaling strategies for different applications. Since we are using the same application for demonstration purposes, so we are deleting the above deployed ScaledObject.</p><p>To delete the ScaledObject deployed earlier :</p><p>Get the deployed ScaledObject name using the below command.</p><pre>kubectl get scaledobject</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J5GERdG-DvJlLrEurRJ9Jg.png" /><figcaption>scaledObject list</figcaption></figure><p>Then delete the ScaledObject using the below command.</p><pre>kubectl delete scaledobject nginx-deployment</pre><p>Let’s now start defining the new ScaledObject for HTTP Trigger. The KEDA HTTP add-on exposes a CRD where we can describe how our application should be scaled. Let’s create a new file with the name “scaledobjects-http.yaml” with the below contents.</p><pre>kind: HTTPScaledObject<br>apiVersion: http.keda.sh/v1alpha1<br>metadata:<br>    name: nginx-http-scaledobject<br>spec:<br>    host: myhost.com<br>    targetPendingRequests: 1<br>    scaleTargetRef:<br>        deployment: nginx<br>        service: nginx<br>        port: 80<br>    replicas:<br>        min: 0<br>        max: 3</pre><p>The above code defines a ScaledObject of type HTTPScaledObject. It will listen to the requests which come from <strong>myhost.com</strong>. The minimum replica count is set to 0 and the maximum replica count is 3.</p><p>Let’s now deploy the above ScaledObject using the below command.</p><pre>kubectl apply -f scaledobjects-http.yaml</pre><p>When we deploy the above ScaledObject, our application will be scaled down to 0. This is because we have set the minimum replica count to 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*qc55_Nmdx2DBpTHN2-uW-w.png" /><figcaption>deployment status</figcaption></figure><p>So now, our application is not running. This is to simulate the scenario when our application is not in use. So what happens when someone makes a request to our application? Let’s see that next.</p><h4>6. Testing Auto-Scaling with HTTP Scaler</h4><p>A Kubernetes Service called keda-add-ons-http-interceptor-proxy was created automatically when we installed the Helm chart for HTTP-add-on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gPKBEKKJjgpvMOPACbH0iQ.png" /><figcaption>keda services</figcaption></figure><p>For autoscaling to work appropriately, the HTTP traffic must route through the above service first. We can use kubectl port-forward to test this quickly in our local setup.</p><pre>kubectl port-forward svc/keda-add-ons-http-interceptor-proxy -n keda 32511:8080</pre><p>Here, connections made to local port 32511 are forwarded to port 8080 of the keda-add-ons-http-interceptor-proxy service that is running in Kubernetes Cluster.</p><p>Let’s now make some requests to our application pretending the request comes from <strong>myhost.com</strong>.</p><pre>curl localhost:32511 -H &#39;Host: myhost.com&#39;</pre><p>If we inspect the pods, we will notice that the deployment was scaled to a single replica. So when we routed the traffic to the KEDA’s service, the interceptor keeps track of the number of pending HTTP requests that haven’t had a reply yet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XnTergctVQCoaJLdTunyTw.png" /><figcaption>pod status</figcaption></figure><p>The KEDA scaler periodically checks the size of the queue of the interceptor and stores the metrics. The KEDA controller monitors the metrics and increases or decreases the number of replicas as needed. In this case, a single request was pending so the KEDA controller scaled the deployment to a single replica.</p><p>As mentioned above, the Service that the add-on creates will be inaccessible over the network from outside of the Kubernetes cluster.</p><p>While we can access it via the kubectl port-forward command above, it is not the recommended way of using it in a production setting. Instead, we should use an <a href="https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/"><strong>ingress controller</strong></a> to route traffic to the Keda interceptor service.</p><h4>Conclusion</h4><p>In this article, we learned about KEDA and its concepts. We also saw it in action through scaling a sample NGINX application in our Kubernetes cluster. As we can see if we combine Kubernetes pod auto-scaling along with Cluster Autoscaler, then not only can we scale our application on demand but also with minimal cloud cost.</p><h4>References</h4><p><a href="https://keda.sh/">KEDA</a></p><p><a href="https://cloud.google.com/kubernetes-engine/docs/concepts/cluster-autoscaler">About cluster autoscaling | Google Kubernetes Engine (GKE) | Google Cloud</a></p><ul><li><a href="https://kubernetes.io/docs/concepts/overview/">Overview</a></li><li><a href="https://learn.microsoft.com/en-us/azure/aks/cluster-autoscaler">Use the cluster autoscaler in Azure Kubernetes Service (AKS) - Azure Kubernetes Service</a></li><li><a href="https://keda.sh/docs/2.9/concepts/#architecture">KEDA | KEDA Concepts</a></li><li><a href="https://keda.sh/docs/2.9/deploy/">KEDA | Deploying KEDA</a></li><li><a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/">Deployments</a></li><li><a href="https://keda.sh/blog/2021-06-24-announcing-http-add-on/">KEDA | Announcing KEDA HTTP Add-on v0.1.0</a></li><li><a href="https://keda.sh/docs/2.9/scalers/prometheus/">KEDA | Prometheus</a></li><li><a href="https://github.com/kedacore/http-add-on/blob/main/docs/walkthrough.md">http-add-on/walkthrough.md at main · kedacore/http-add-on</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8db6c301b18" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/auto-scaling-microservices-with-kubernetes-event-driven-autoscaler-keda-8db6c301b18">Auto Scaling Microservices with Kubernetes Event-Driven Autoscaler (KEDA)</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DuckDB for Data Science]]></title>
            <link>https://medium.com/cuddle-ai/duckdb-for-data-science-274100d4202a?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/274100d4202a</guid>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[duckdb]]></category>
            <dc:creator><![CDATA[Ritik Jain]]></dc:creator>
            <pubDate>Mon, 27 Feb 2023 05:28:43 GMT</pubDate>
            <atom:updated>2023-02-27T05:28:43.107Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cLydGAfGIuV1xvCq" /><figcaption>Photo by <a href="https://unsplash.com/@museumsvictoria?utm_source=medium&amp;utm_medium=referral">Museums Victoria</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote>DuckDB is an open-source relational database management system (DBMS) tool that supports structural query language (SQL).</blockquote><p>In the industry, there is a wide range of database management systems (DBMS). But all have different trade-offs for specific use-cases, there is no one-size-fits-all database system. DuckDB is no different. It is an in-process SQL-based OLAP database management system that provides a simple interface for in-process databases.</p><h3>Why DuckDB?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-CoioEomCE_kHUX4.png" /></figure><h4>Fast Analytical Operation</h4><p>DuckDB is designed to support <strong>analytical query workloads </strong>(a.k.a. OLAP). The queries are complex, long-running, and take significant portions of the stored dataset. For example, aggregation over entire tables or join operation on several large tables.</p><p>To efficiently operate these operations, duckdb contains a <a href="https://www.infoq.com/articles/columnar-databases-and-vectorization/"><strong>columnar-vectorized query execution engine</strong></a><strong> </strong>where queries are interpreted, but instead of performing the row-level operation, it processes a large batch of values (a vector) in one operation.</p><h4>Simple Operation</h4><p>DuckDB adopts the ideas of simplicity and embedded operation from the <a href="https://www.sqlite.org/mostdeployed.html">world’s most widely deployed DBMS</a> (SQLite).</p><ul><li>DuckDB is a serverless database with no external dependencies.</li><li>Embedded completely within a host process.</li><li>High-speed data transfer to and from the database.</li><li>It can process datasets without copying.</li></ul><h4>Free and Open-source</h4><p>DuckDB’s development started while the main developers were public servants in The Netherlands. DuckDB is free to use and the entire code is available on <a href="https://github.com/duckdb/duckdb">GitHub</a>. It comes with an <a href="https://opensource.org/licenses/MIT">MIT license</a> which means you can use it for commercial purposes.</p><h4>Feature Rich</h4><ul><li>It provides serious data management features. Provide extensive support for complex SQL queries with a large function library etc.</li><li>It provides transactional guarantees (ACID properties) through <a href="https://en.wikipedia.org/wiki/Multiversion_concurrency_control">MVCC</a> and bulk optimization.</li><li>It supports secondary indexes for efficient query execution.</li><li>It is deeply integrated into Python and R for efficient data analysis. It also provides APIs for JAVA, C, C++, and others.</li></ul><h3>Implementation</h3><h4>System Setup</h4><p>As DuckDB provides deep integrations with Python and R language. So it is pretty easy to set up in the local system.</p><p>In Python, you can use the pip package for <a href="https://duckdb.org/docs/installation/">DuckDB installation</a></p><pre>pip install duckdb</pre><p>Similarly, you can use different languages for installation.</p><h4>Data Import</h4><p>In Python, DuckDB supports multiple ways to connect to datasets in the process like</p><p><strong>Connect to the in-memory temporary dataset</strong></p><p>Creating a temporary database instance is straightforward with DuckDB</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0828f16ae9119230776ee8ce5dba12cb/href">https://medium.com/media/0828f16ae9119230776ee8ce5dba12cb/href</a></iframe><p><strong>Connect to dataset files</strong></p><p>With DuckDB, it is really easy to connect to datasets in files.</p><p>It provides support file formats like <a href="https://duckdb.org/docs/data/csv">CSV files</a> and <a href="https://duckdb.org/docs/data/parquet">Parquet Files</a>. You can use advanced parameters to support different versions of CSV and Parquet files.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/029f9dbaaabfe6e06eafd917b087d4b6/href">https://medium.com/media/029f9dbaaabfe6e06eafd917b087d4b6/href</a></iframe><p>Output for importing dataset directly to Python environment</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AD8iKXNhldQO-4gYmVqlHQ.png" /></figure><p><strong>Connect to Pandas Dataframe</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4a1e37b680fa5f1867fcf602f90f74d/href">https://medium.com/media/f4a1e37b680fa5f1867fcf602f90f74d/href</a></iframe><p>Importing data through Dataframe</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QPxXzELa-zugF0znjDHXBg.png" /><figcaption>Output for the above snippet</figcaption></figure><h4>Python Like Function</h4><p>For all operations, DuckDB provides python equivalent functions like</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f074e603ca5ad9198593a9d81e8e570/href">https://medium.com/media/5f074e603ca5ad9198593a9d81e8e570/href</a></iframe><h4>Aggregation Operation</h4><p>DuckDB provides a wide range of aggregation operations. In the notebook, sharing a few. You can find the exhaustive list of the aggregation operations <a href="https://duckdb.org/docs/sql/aggregates">here</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/578d3b2f4b2a3d0b02465ff202d6b617/href">https://medium.com/media/578d3b2f4b2a3d0b02465ff202d6b617/href</a></iframe><h4>Other Operation</h4><p>DuckDB provides an extensive list of functions for OLAP operations on datasets. Operations like</p><ul><li><a href="https://duckdb.org/docs/extensions/full_text_search">Text Analytical functions</a></li><li><a href="https://duckdb.org/docs/extensions/json">Operations on JSON Data</a></li><li><a href="https://duckdb.org/docs/sql/window_functions">Window Functions</a></li></ul><h3>Conclusion</h3><p>DuckDB is an open-source, in-process database management system that does not require any external dependencies, neither for compilation nor for execution.</p><ul><li>It follows the same SQL structure as PostgreSQL.</li><li>It provides support to run complex SQL queries on a temporary dataset without copying the dataset.</li><li>It is deeply integrated with Python and R language and also provides APIs for JAVA, C++, and C.</li><li>Instead of performing row-level operations, DuckDB leverages a columnar-vectorized execution engine to process a large batch at a time.</li></ul><p>If you are interesting to see more like these article <a href="https://www.buymeacoffee.com/rjain">buy me a coffee</a></p><p><a href="https://www.buymeacoffee.com/rjain">Ritik Jain is Data Scientist, Innovator, Engineer</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=274100d4202a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/duckdb-for-data-science-274100d4202a">DuckDB for Data Science</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A guide to successfully navigating through all the challenges of building a design system]]></title>
            <link>https://medium.com/cuddle-ai/a-guide-to-successfully-navigating-through-all-the-challenges-of-building-a-design-system-37dba987d229?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/37dba987d229</guid>
            <category><![CDATA[product-design]]></category>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[design]]></category>
            <dc:creator><![CDATA[Dhruvibetai]]></dc:creator>
            <pubDate>Mon, 05 Sep 2022 05:41:17 GMT</pubDate>
            <atom:updated>2022-10-06T15:28:56.783Z</atom:updated>
            <content:encoded><![CDATA[<h4>Building a mobile design system in tandem with a design project</h4><h4><strong>Background</strong></h4><p>Back in January 2022 I was hired as a Design System, Research and Product Intern at Crux Intelligence. At the time when I joined I truly wasn’t aware of what it takes to build a design system for a company. But I had a basic understanding of how systems work and was eager to learn more. Fast forward six months we were successful in creating a design system which powers all devices and is intuitive for both developers and designers.</p><h4>Process to follow:</h4><h4><strong>Start with an app audit</strong></h4><p>To build a design system, you must be familiar with all the different features that make your app what it is today. That is when an app audit comes into play.</p><p>Start by taking screenshots of each screen in your app and critically examine them. One must click on all possible CTAs and explore all that can be done inside the app, capture it and arrange it in multiple flows.</p><p>Personally, this helped me understand all the different types of buttons, cards and other components that are used in the app. Additionally, it is a good opportunity to become acquainted with the already existing design system.</p><p>At crux, we did have one which had limited tokens and components but still, it was important to understand it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QzZcotogyf11772oVIlDcw.png" /><figcaption>Audit File in Figma</figcaption></figure><h4><strong>Create a basic mind node</strong></h4><p>Because of the audit, you now know all the components that will be included in your design system.</p><p>To begin with, consider the first level of your mindnode and add branches that you believe will transform into individual pages in your DS project file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o6TSH9B9BxbsiT1y_q0Uxw.png" /><figcaption>Level 1 of the Components Structure</figcaption></figure><h4><strong>Finalise the design system architecture</strong></h4><p>The design system should be tailored to your product, thus the architecture must be tailored to your needs and the number of products sourced from your DS. At crux, we had evaluated four key factors:</p><p><strong>i. Blocks:</strong></p><ul><li>This file serves as the foundation for all of the text, colour, and style tokens. It already existed, and we attempted to generate all component or context level tokens using only these selected styles.</li><li>This guaranteed that we did not introduce a new colour for each new token, and that our library was constrained yet easily reusable in varied circumstances.</li></ul><p><strong>ii. Tokens:</strong></p><ul><li>At crux, we used four types of tokens: text, colour, effects and grid, which were further divided depending on the context in which they were used.</li><li>Apart from this, we built a mind node to assist us with token nomenclature.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wc5mEat_3U4xR-w5bjH2kQ.png" /><figcaption>Token Structure</figcaption></figure><p><strong>iii. Components:</strong> Following that, we separated our mobile and web components into two independent files.</p><p><strong>iv. Icons:</strong> Finally, we had a single file that had all of the icons in the appropriate sizes for both the web and app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9ceFA9HbjkuamDpl5k-1Kw.png" /><figcaption>Overall Design System Architecture</figcaption></figure><h4><strong>Create tokens and styles</strong></h4><p>If your token nomenclature mind node is correctly made, then this should be simple. Now you need to follow the mind node and name tokens accordingly. All the web token names can have the prefix $web added to them, whereas mobile ones can have $m. Remember that you do not need a new token for each component, if a component is likely to move in tandem with any UI element then you could very well use the $ui token.</p><p>At crux, there were specific components which were white labelled. Honestly, it requires a blog to explain this concept in depth, but to summarise, our clients can add primary colours of their product and specific components like headers, buttons etc in the crux app would change in colour. So for us, these components needed a distinct token which was called $web-app-header or whatever the component was.</p><p>Each token was now mapped to a style from the blocks file, thus we were using limited colours or styles just in a different context. And tomorrow if our product gets a revamp and we change the primary brand colour then we just need to update our blocks file and all the tokens, components and screens would be updated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vqi9_RmJro7e1-wyYlD5KQ.png" /><figcaption>Tokens with its Values in Figma</figcaption></figure><h4><strong>Pick up a product design project and make the newly designed screens your starting point</strong></h4><p>Design systems can be very complex and time-consuming to build if you don’t have a specific starting point. I was really glad that my lead designer <a href="https://medium.com/u/1598bbecc412">Avi Agarwal</a> told me to start working on the components of my own designs that I had created for a new feature that we were about to release. That became my starting point, I started with one screen at a time and covered all the components on that screen from top to bottom, transferring them to the DS while also replacing their instances in my design. This also ensured that the new project was sourcing components from the new System. Make sure to use the tokens you generated in the previous step when creating these new components.</p><blockquote><em>A component is considered to be ready when it is perfectly resizable and has all the colour and text tokens in place.</em></blockquote><p>As soon as you add a component to the file, create a branch for that component in the mind node. This allows the mind node to keep growing and you can also keep a track of all the components in the DS and where they are located. This will make sure that there is no node that does not exist in the DS. In the end, my mind node looked something like this 👇</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f2pIQa7BykEaM9aR30bm1Q.png" /><figcaption>Complete Mobile Components Architecture</figcaption></figure><h4><strong>Categorising components and creating variants</strong></h4><p>Let’s assume that the screen you have selected has a search field component. When you add it to the fields page in the DS add all the variants of that component and remember to complete it. This includes adding states that aren’t used in the project you’ve selected. Another point that <a href="https://medium.com/u/1598bbecc412">Avi Agarwal</a> often reminded me of was that variants can be created in multiple ways using different logics, we need to keep in mind that it should be intuitive to the designer who will be searching for it in their respective projects design file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G7c0wWuai8MwptZeyIv_SQ.png" /><figcaption>Variants of a component</figcaption></figure><h4><strong>Documenting the Design System</strong></h4><p>Lastly, keep in mind that you will not be around forever and that someone will most likely replace you, as a result you need to document all the decisions you have taken. Also, mention how to use the component correctly. When I started using the design system while working on projects I realised a lot of things that needed to be communicated to anyone else who selects a component. For example, instructions on how to set the width of buttons in the resizing panel. Apart from these general rules, I also documented all the variants and expected scenarios where that component will be used. And when they are used in these scenarios other details like what spacing should be maintained by the developer or a designer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wDpZGG-tsVZyaFnkgeyzCg.png" /><figcaption>Component Guidelines in Figma</figcaption></figure><h4>Key learnings</h4><p>If you manage to sail through all this then I can guarantee you will have no problem doing all the things mentioned below 👇</p><ul><li>Understanding and building a mindnode that servers <strong>your</strong> product.</li><li>Creating resizable and reusable components and applying the correct logic while creating variants.</li><li>Understanding how a designer will look for that component in the future and also the way developers will work with them.</li></ul><p>That was it for this blog. I want to thank my mentor <a href="https://medium.com/u/1598bbecc412">Avi Agarwal</a> for teaching me all that I know today and for making me fall in love with design systems. And for always believing in me.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37dba987d229" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/a-guide-to-successfully-navigating-through-all-the-challenges-of-building-a-design-system-37dba987d229">A guide to successfully navigating through all the challenges of building a design system</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing Spring Boot Application using WireMock and JUnit 5]]></title>
            <link>https://medium.com/cuddle-ai/testing-spring-boot-application-using-wiremock-and-junit-5-d514a47ab931?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/d514a47ab931</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[cuddleengineering]]></category>
            <category><![CDATA[cuddlepe]]></category>
            <category><![CDATA[integration-testing]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Suman Das]]></dc:creator>
            <pubDate>Thu, 28 Jul 2022 13:18:01 GMT</pubDate>
            <atom:updated>2022-07-28T13:18:01.354Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8AvtHu8XsoRHv9a7-3j1tA.png" /><figcaption>Spring Boot with WireMock</figcaption></figure><p>In this world of Microservices, we want to build and ship things faster. When we ship our code, we want to make sure we ship well tested quality code. Hence, testing our code becomes very important. Since testing our code is so important, today we have many tools to help us write efficient tests.</p><p>In this article, we will discuss one such tool named <a href="https://wiremock.org/docs/"><strong>Wiremock</strong></a><strong> </strong>along with<strong> </strong><a href="https://junit.org/junit5/docs/current/user-guide/"><strong>Junit 5</strong></a>,<strong> </strong>that we can use to simulate other services on which our app depends during integration testing of the <strong>Spring Boot</strong> application.</p><p>Let us start with WireMock.</p><h4>What is WireMock?</h4><p><strong>WireMock</strong> is a library for stubbing and mocking web services. It constructs an HTTP server that acts as an actual web service. In simple terms, it is a mock server that is highly configurable and has the ability to return recorded HTTP responses for requests matching criteria.</p><p><strong>WireMock</strong> has a feature called Record and Playback. It can create stub mappings automatically from requests it has received. Combined with its proxying feature this allows us to “record” stub mappings from interaction with existing APIs. When in action, we have the option to either call the external API and capture the response or use one of the earlier recorded stubs to reply to the caller without calling the external API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*HnWxZqBy1Dvi-_LYZvBdMA.png" /><figcaption>WireMock Flow</figcaption></figure><h4><strong>Why do we need WireMock?</strong></h4><p>Suppose our SpringBoot Application calls external applications and this call to external services bears some cost. However, we can’t test the application without calling the external services. In this case, we can use WireMock to mock the external applications while testing the REST API that we are developing in SpringBoot.</p><p><strong>WireMock</strong> can be used as a library or as a standalone process. In this tutorial, we will use it as a library. Later, we will see how we can use <strong>WireMock</strong> to mock the external applications while testing the REST API that we are developing using SpringBoot.</p><h4>What is JUnit 5?</h4><p><strong>JUnit</strong> is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development and is one of a family of unit testing frameworks, which is collectively known as xUnit that originated with SUnit.</p><p>Unlike previous versions of JUnit, JUnit 5 is composed of several different modules from three different sub-projects.</p><p><strong>JUnit 5 = <em>JUnit Platform</em> + <em>JUnit Jupiter</em> + <em>JUnit Vintage</em></strong></p><p>The <strong>JUnit Platform</strong> serves as a foundation for <a href="https://junit.org/junit5/docs/current/user-guide/#launcher-api">launching testing frameworks</a> on the JVM. It also defines the <a href="https://junit.org/junit5/docs/current/api/org.junit.platform.engine/org/junit/platform/engine/TestEngine.html">TestEngine</a> API for developing a testing framework that runs on the platform. Furthermore, the platform provides a <a href="https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher">Console Launcher</a> to launch the platform from the command line and the <a href="https://junit.org/junit5/docs/current/user-guide/#junit-platform-suite-engine">JUnit Platform Suite Engine</a> for running a custom test suite using one or more test engines on the platform.</p><p><strong>JUnit Jupiter</strong> is the combination of the new <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests">programming model</a> and <a href="https://junit.org/junit5/docs/current/user-guide/#extensions">extension model</a> for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform.</p><p><strong>JUnit Vintage</strong> provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform. It requires JUnit 4.12 or later to be present on the classpath or module path.</p><p>Let’s create a simple REST API using Spring Boot and JAVA Config. Then we will perform an integration test of our application using the <strong>JUnit 5</strong> and <strong>WireMock</strong>.</p><p><strong>Prerequisites</strong></p><p>Before we begin, we must ensure that the following pre-requisites are installed on the local machine:</p><ul><li><a href="https://maven.apache.org/install.html">Maven</a></li><li>Java 17</li></ul><h3>Steps to Build</h3><ol><li>Set up the Spring Boot Project</li><li>Include Additional Maven Dependencies</li><li>Furnish the Configurations</li><li>Define a DTO Class</li><li>Define the Rest Controller Endpoints</li><li>Construct the Service Layer</li><li>Start the Application</li><li>Test the Application with Swagger-UI</li><li>Add Integration Test</li><li>Conclusion</li></ol><h4>1. Set up the Spring Boot Project</h4><p>For this tutorial, we are using JDK 17 and Spring Boot 2.7.1 project<em>. </em>Use <a href="https://start.spring.io/">start.spring.io</a> and create a sample project using the below artifacts:</p><ul><li><strong>Spring Web: </strong>Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qim0E5ic4vfe6SQXoFrmEw.png" /><figcaption>Creating Sample Project</figcaption></figure><h4>2. Include Additional Maven Dependencies</h4><p>Add the additional dependencies, shown below, to our project to enable Swagger, Lombok, WireMock, and JUnit for our application.</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springdoc&lt;/groupId&gt;<br>    &lt;artifactId&gt;springdoc-openapi-ui&lt;/artifactId&gt;<br>    &lt;version&gt;1.6.9&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;<br>    &lt;artifactId&gt;lombok&lt;/artifactId&gt;<br>    &lt;version&gt;1.18.24&lt;/version&gt;<br>    &lt;scope&gt;provided&lt;/scope&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;com.github.tomakehurst&lt;/groupId&gt;<br>    &lt;artifactId&gt;wiremock-standalone&lt;/artifactId&gt;<br>    &lt;version&gt;2.27.2&lt;/version&gt;<br>    &lt;scope&gt;test&lt;/scope&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;<br>    &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;<br>    &lt;version&gt;5.8.2&lt;/version&gt;<br>    &lt;scope&gt;test&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><p><strong>Swagger</strong>: Swagger is an open-source tool to develop APIs, interact with APIs, and mainly generate documentation for APIs.</p><p><strong>Lombok: </strong>Java annotation library which helps to reduce boilerplate code.</p><p><strong>WireMock</strong> and <strong>JUnit: </strong>for Integration testing.</p><h4>3. Furnish the Configurations</h4><p><strong>3.1 Configure RestTemplate</strong></p><p>To call an external API we need to create a RestTemplate instance. Let’s start with that. <strong><em>Spring Boot &gt;= 1.4 </em></strong>no longer automatically defines a RestTemplate but instead defines a RestTemplateBuilder allowing us more control over the RestTemplate that gets created. We can inject the RestTemplateBuilder as an argument in our @Bean method to create a RestTemplate:</p><pre>@Configuration<br>public class ApplicationConfig {<br>    @Bean<br>    public RestTemplate restTemplate(RestTemplateBuilder builder) {<br>        // Do any additional configuration here<br>        return builder.build();<br>    }<br>}</pre><p><strong>3.2 Configure application.yml</strong></p><p>application.yml file contains all the configuration required by our application during runtime. It also contains externalized configurations. Here, we will just specify our application name and externalize the URL for the dependent service. We will use this URL later to connect to the external service.</p><pre>spring:<br>  application:<br>    name: &quot;Sample Spring Boot Application&quot;<br><br>universitiesBaseURL: http://universities.hipolabs.com</pre><p><strong>3.3 Configuration to read external properties</strong></p><p>We can bind the properties defined in the application.yml file to a Java class using @ConfigurationProperties. This annotation allows to map the entire properties and yml files into an object easily. The @ConfigurationProperties annotation takes a prefix parameter, and binds all the properties with the specified prefix to the Java class.</p><pre>@Configuration<br>@ConfigurationProperties(prefix = &quot;wiremock-config&quot;)<br>@Data<br>@Profile(&quot;integration&quot;)<br>public class WireMockConfig {<br>    private List&lt;WireMockProxy&gt; proxies;<br>}</pre><p>In this article, we will use the WireMockConfig class to read all the configurations related to WireMock defined in application-integration.yml file. We will be defining this application-integration.yml file when we write the integration tests in Step 9. We will need these configurations during the Integration test.</p><h4>4. Define a DTO Class</h4><p>In this application, we will call some external API and then map the JSON response of the API to this DTO UniversityDTO.</p><pre>@Data<br>@JsonIgnoreProperties(ignoreUnknown = true)<br>@JsonInclude(JsonInclude.Include.<em>NON_NULL</em>)<br>public class UniversityDTO {<br>    private List&lt;String&gt; domains;<br>    private List&lt;String&gt; web_pages;<br>    private String name;<br>    private String country;<br>    private String alpha_two_code;<br>}</pre><h4>5. Define the Rest Controller Endpoints</h4><p>Finally, let us write some APIs that can be exposed to the clients. This API will call some external APIs and return the results to the client. Here, we are using one of the free API listed in <a href="https://apipheny.io/free-api/">Apipheny</a> for demonstration purposes. We will use the <a href="https://github.com/Hipo/university-domains-list">Universities List API</a> to get a list of universities in a specified country. We will not be validating the response, as we are just simulating the scenario where in our application we may need to call some APIs, do certain processing on top of that and then return the results.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fdd9f7b8711f2ea5d59cf68ce6e9aaa9/href">https://medium.com/media/fdd9f7b8711f2ea5d59cf68ce6e9aaa9/href</a></iframe><p>In the above code from line 22to 23 we are calling the method getUniversitiesForCountry of the service class, for fetching the list of all universities for the given input. Then we return the result to the caller.</p><p>Next, let’s see how we are getting the data from the external API.</p><h4>6. Construct the Service Layer</h4><p>Let us construct the Service Layer for our application. The service layer is required for encapsulating the business logic and also for centralizing the data access.</p><p>We will add a Service class named UniversityService inside the sub-package service of the root package. This class will contain the method getUniversitiesForCountry that will be used to fetch the universities for the given country.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8df2fb39d1bb091f72b32cd37a20f453/href">https://medium.com/media/8df2fb39d1bb091f72b32cd37a20f453/href</a></iframe><p>As we can see above, UniversityService does not have much business logic other than calling some external service, and map the response to UniversityDTO and then return the response to the caller.</p><h4>7. Start the Application</h4><p>Till now we have written all the code required for our application to run. We will execute the below maven command from the root directory of the project to compile and build the jar.</p><pre>mvn clean package</pre><p>Once the build is complete then we can start the application. Here, we will use IntelliJ to run the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zqZJqQpQ-G5UX2jPRGnHug.png" /><figcaption>Starting the application</figcaption></figure><p>As we can see above, the application started successfully on port 8080.</p><h4>8. Test the Application with Swagger-UI</h4><p>Let’s test our universities endpoint using Swagger-UI. Once the application started successfully, we can see all the endpoints for our application using the Swagger-UI. Open the swagger UI and check the endpoint. The Swagger will open at the below URL.</p><pre><a href="http://localhost:8080/swagger-ui/index.html">http://localhost:8080/swagger-ui/index.html</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b2gN-7WGrW7VupzTd5p95g.png" /><figcaption>Swagger-UI</figcaption></figure><p>Let’s now test the endpoint for some sample countries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f3yUIMYir6rqZqQyx5vdKQ.png" /><figcaption>Sample Test using Swagger</figcaption></figure><p>As we can see above, we got the response for the country India. So our application is up and running. Similarly, it can also be tested for other countries.</p><h4>9. Add Integration Test</h4><p>Let’s start writing some Integration tests for this application. During Integration tests, we don’t want to call the actual HTTP endpoint. This is less critical for this example, but imagine a scenario where we manipulate the data within the remote application using HTTP POST or PUT. We don’t want this to happen during our tests and instead talk to a stub.</p><p>Also, if we connect to the actual remote system for our integration test, the test outcome depends on the availability of the remote service and produces additional dependency. At the same time, we may also want to test different responses (e.g., for different scenarios and status codes).</p><p>We can resolve the above challenges of connecting to the external API during integration testing using <strong>WireMock</strong>. With WireMock, we can capture the actual response for a given URL and set the body and status code to whatever we need. Internally, WireMock spawns a local Jetty server that handles the remote calls during our integration tests.</p><p><strong>9.1 Add the Configuration File</strong></p><p>First, we need to add a configuration file name : application-integration.yml inside the “src/main/resources” folder. This file will contain all the configurations, which we need during the Integration test or we want to override during the Integration tests. Here, the term integration in the file name refers to the <strong>profile</strong> name which we will use during the tests. We have already defined the Java class WireMockConfig in step 3.3 above to read these configurations during runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*uQICH2wAJ7VxwauSySdl6g.png" /><figcaption>application-integration.yml</figcaption></figure><p>The file will contain the below configurations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lz8HiqUA9eZB2WeRmc6-fA.png" /><figcaption>Integration configurations</figcaption></figure><p>As we have seen above in step 5 we are calling the <a href="https://github.com/Hipo/university-domains-list">Universities List API</a> to get a list of universities for a given country. So during our Integration tests, we want WireMock to listen to this URL and return responses from the stubs if present. Here, we are specifying the Mock Server to run at port : 9081 for the base URL : http://universities.hipolabs.com. Any request made to this URL will be intercepted by this Mock Server and will return a response from the source or stubs depending upon the value of the property : recording .</p><p>If recording is true then it will call the source and capture the response and return the response to the caller. If the recording is false then it will look into the mappings folder for the captured stubs. If there is a stub matching the required criteria, then it will return a response from the stub to the caller.</p><p><strong>Note</strong>: We can see above that we have overridden the universitiesBaseURL to the proxy URL.</p><p>Here, we have taken proxies in a list format and not as a single entry, although we are calling only one API in this case. This is because if we have to call multiple external APIs, then we can just append the configuration below the current one and it will work.</p><p><strong>9.2 Add Test Directories</strong></p><p>We need to create some directories inside the “src/test/resources” folder for WireMock to record files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*lOZvTY3zHVMhE134DTAmFA.png" /><figcaption>test directories</figcaption></figure><p>As we can see above, we need to add the following directories for the integration test:</p><ul><li><strong>__file </strong>(contains response errors and messages JSON)</li><li><strong>mappings </strong>(contains request and response JSON) directories that are needed by WireMock for recording purposes</li><li><strong>recorded</strong> directory is used to capture the actual response of the REST API which we will use to compare against our Integration test results</li></ul><p>Let’s now start writing the Integration test for the REST API which we exposed earlier.</p><p><strong>9.3 Add the Test Case</strong></p><p>Let’s start with adding a new Integration Test class with the file name : SpringBootWiremockApplicationTests inside “src/test/java” folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*p4j3PdN3fTrTF855z2IF8A.png" /><figcaption>Integration Test file</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/084c3de9052269b6092f38f910c1fb50/href">https://medium.com/media/084c3de9052269b6092f38f910c1fb50/href</a></iframe><p>In the above code:</p><ul><li><strong>Set the active profile: </strong>In line 1 we are directing our tests to use the profile: integration by annotating the test class with the @ActiveProfiles annotation. By setting integration profile as the active profile, the tests will read the configurations from the file application-integration.yml which we defined in step 9.1.</li><li><strong>Look up main configuration class:</strong> In line 2 @SpringBootTest annotation tells Spring Boot to look for the main configuration class (one with @SpringBootApplication, for instance) and use that to start a Spring application context.</li><li><strong>Configure auto-configuration of MockMVC:</strong> We also have added the annotation @AutoConfigureMockMvc in line 4 because we’ll use <strong>MockMvc</strong> to call the “/api/university “ endpoint for the test. We could have called the service method directly depending on what kind of test we wanted to implement. In this article, we are trying to demonstrate an example of an integration test by calling the REST endpoints directly with MockMvc.</li><li><strong>Start WireMock server and record:</strong> From line 24 to 36 we defined a method called startRecording, which will be executed before every test case. It reads the configuration file defined in step 9.1. If recording flag is turned on then it will start the <strong>WireMock</strong> server in recording mode, else it will start in regular mode.</li><li><strong>Shut down WireMock server and stop recording:</strong> From line 39 to 48 we defined a method called stopRecording which will be executed after every test case. If recording flag is turned on then it will stop the recording before shutting down the <strong>WireMock</strong> server in recording mode or else it will just stop the server in regular mode.</li><li><strong>Provide WireMock server configuration:</strong> From line 124 to 133 we have provided some configuration for the WireMock server. It will start intercepting all the requests specified at the wiremock-config.proxies.url. In this tutorial we have specified only one URL : <a href="http://universities.hipolabs.com/">http://universities.hipolabs.com</a>. If the recording is enabled then it will persist the stubs in the mappings folder. While matching against the stubs it will ignore the arrayOrder and extraElements in the response JSON.</li><li><strong>Define integration test case:</strong> From line 51 to 63 we defined the integration test case for the REST API “/api/university”. Here we have used the parameterized test case of JUnit 5. <strong>MockMvc</strong> is used to call the “/api/university “ endpoint. It then saves the response temporarily in JSON format to compare against the expected response. Here, we are using <a href="https://github.com/skyscreamer/JSONassert">JsonAssert</a> to compare the expected and actual JSON responses. Once the test case is successful then the current file is cleaned up automatically.</li></ul><h4>9.4 Record the Test Case</h4><p>Let’s now see how easily we can record the response of external API and use it during the Integration test. To turn on the recording mode we just need to set recording : true in the configuration defined in step 9.1 above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hhP8OEFug4fwQWIyeT5cSQ.png" /><figcaption>recording enabled</figcaption></figure><p>Now run the test case. When we run the test case, the response of the external API will be captured inside the mappings folder. The expected response of our API will be captured inside the recorded folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IkpC_2wGwk_9cQ3pN4OZIw.png" /><figcaption>recording the test case</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4B6vMnQx4ikOj774JQ5chA.png" /><figcaption>running test case in recording mode</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fCaGUkuK5Up1NsrHY-mdeA.png" /><figcaption>recorded files captured</figcaption></figure><h4>9.5 Replay the Test Case</h4><p>Now everything is in place. We have the Integration Test case ready to be executed. We have all the recorded files in place. Let’s now run the Test case with recording: false .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lz8HiqUA9eZB2WeRmc6-fA.png" /><figcaption>recording disabled</figcaption></figure><p>With recording off, <strong>WireMock</strong> will try to search the mappings folder for one of the matched responses for the given request. In case it is able to find the response then will return that, else, it will throw an exception saying it’s not able to get any stubbed response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8jrOjjzhdKbECsR37M67Ug.png" /><figcaption>replay the test case</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t_90eyD_HXLTFhyoCi0_IA.png" /><figcaption>Running the test case with recording off</figcaption></figure><p>Let’s modify the recorded response manually to see what happens when WireMock is not able to find the matched response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jczsA2HUm2fWuVZwUh8CRw.png" /><figcaption>update the captured response</figcaption></figure><p>Let’s say we update the country name in the response to india1 from india . to simulate the scenario when we don’t have the recorded file for a given request.</p><p>Now let’s run the test case again with recording: false</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S28vVfSrOh3TLTHjS8uEiQ.png" /><figcaption>no recording found</figcaption></figure><p>As we can see above, when we run our test case after modifying the response, we got the error <strong>Request was not matched</strong>. This time WireMock is not able to find any recordings. It also displayed the closest match it can find.</p><h4>Conclusion</h4><p>In this article, we saw how we can leverage WireMock in Spring Boot applications to record the requests and responses for external API for various scenarios, and then we can use that recorded files during Integration Test.</p><p>If you would like to refer to the full code, do check:</p><p><a href="https://github.com/sumanentc/spring-boot-with-wiremock">GitHub - sumanentc/spring-boot-with-wiremock: The purpose of this project is to demonstrate how we can use Spring Boot along with WireMock and JUnit 5 to perform Integration Testing with mocking external REST API calls.</a></p><h4>References &amp; Useful Readings</h4><ul><li><a href="https://junit.org/junit5/docs/current/user-guide/">JUnit 5 User Guide</a></li><li><a href="https://wiremock.org/docs/">WireMock User Documentation</a></li><li><a href="https://wiremock.org/docs/record-playback/">Record and Playback</a></li><li><a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests">JUnit 5 User Guide</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d514a47ab931" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/testing-spring-boot-application-using-wiremock-and-junit-5-d514a47ab931">Testing Spring Boot Application using WireMock and JUnit 5</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Evolution of decision making]]></title>
            <link>https://medium.com/cuddle-ai/evolution-of-decision-making-26c1b259926b?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/26c1b259926b</guid>
            <category><![CDATA[decisions]]></category>
            <category><![CDATA[decision-making]]></category>
            <dc:creator><![CDATA[Santrupti P]]></dc:creator>
            <pubDate>Thu, 21 Jul 2022 10:52:25 GMT</pubDate>
            <atom:updated>2022-07-21T10:52:25.799Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2m1Ah1pX7jWeaGIVmCj7_w.png" /></figure><p>Hello hi, congratulations, you’ve successfully made a decision i.e, to click on this article. You should be proud of yourself and pat yourself on the back. Even though it took a fraction of a second, it probably took you years of practice to take decisions well., it’s no easy feat ya know? You’re constantly bombarded with an overwhelming amount of information but you’re still here, making one successful decision after another. Although some of them at your detriment, you’ve successfully made it this far.</p><p>Now that you’ve hopefully pat your back, let’s get to business. You’ve probably always wondered about all the questionable decisions people around you constantly make. Sometimes they’re so incomprehensible you wonder if they even thought about what they’re doing. I might not be able to completely help you decipher the reasoning behind their actions, but I can definitely show you through experience how my own decision making tools have evolved and how I now make decisions. These are some layers that got added into my toolbox to make better decisions.</p><h3>Layer #1 of decision making — Narcissism and happiness</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WHRQ7xT_LHGDQd-Ai_ZiUw.png" /></figure><p>Think about this for a moment, the first time you ever made a conscious decision, you were probably an infant. I remember mine clearly, it was…..Sike, I’m kidding I have a pretty good long term memory and even mine doesn’t go that far behind. It was probably whether or not I wanted to give my chocolate away when my friend asked for it. I didn’t like chocolates but owning something felt nice, kind of nicer than watching my friend eat an extra birthday chocolate looking happy. I was going to put myself first all the way. I based my decisions off of short term happiness. For example — I derived some sort of sadistic pleasure from watching people get in trouble. I was the pet child, be it at home or at school. So, you bet I wrote up every kid talking in between classes. As a child, these small pockets of joy is what I lived for and made decisions to get them. However this lifestyle came to a crashing hault.</p><h3>Layer #2 of decision making — Pleasing authority figures</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*neZh6Tk51YWoRkxojm2-7Q.png" /></figure><p>My reign as the devil sadistic king came to a sudden hault when I fell in trouble with my parents. Although minor, getting into trouble with parents hurt me. How could I, the perfect evil-king-overlord-child do anything wrong? It felt shitty. It hurt enough to start optimising for avoiding the pain I felt. So I avoided everything under the sun that would get my parents/ authority figures to see me in the bad light. You might think, oh that’s not too bad. But my evil-king-overlord now made sure all my shenanigans fit in that little box my authority figures had created for me.The thing about trying to fit mouldable minds into boxes is that, once the melty young brain moulds into that box, it’s cemented. Good or not, that’s now your package to work with. Even in situations that box doesn’t make sense, it was the only tool I had to work with.</p><p>This is one of the examples of how I sabotaged my social life because my tool box hadn’t grown beyond “authority figures are always right”. Growing up in india means that you’re not “allowed” to date. (But obviously people still did) So when one of the nights my bestfriend gets careless and gets caught talking to her boyfriend by her mom. And I happen to visit her the next morning and her mom firmly tells me “come in, I have something to talk to you about” and she asks me if I knew anything about my bestfriend’s shenanigans. And I, having never lied to an authority figure in my life blurt everything I knew. The details don’t matter, what matters is I remember my bestfriend’s face dropping and being hurt by my disloyalty. That was the day I realized maybe doing everything an authority figure considered “good” wasn’t the best idea.</p><p>So my next layer for decision making was — maybe don’t make decisions that make you a social outcast</p><h3>Layer #3 of decision making — Your peers are dumb but so are you</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2ltpQdlE1yXlOsv3AEoMFA.png" /></figure><p>After me being a rat, it spread like wildfire at school and people didn’t actually want to be around me after this. Being alone in high school wasn’t fun. It made me understand the value of belonging. Then I went on the search to finding a group I belonged in and man, did I find one.</p><p>The only ones that wanted to be around me after that rat incident were people that thought that dating before marriage was a sin. Super fun-</p><p>Yeah, don’t worry mom, I didn’t do alcohol and drugs because of peer pressure. I did something way worse. I joined the bandwagon of the “no dating before marriage” cult and made sure every person that hung out with a person of the opposite gender was weirded out in my presence. Lol, there went my popularity.</p><p>And finally, in college I shed my “no dating before marriage” skin and dated people. My first boyfriend was a douche. And given my people pleasing nature from the pains of being a social outcast,I was a very “I will service my existence to my boyfriend” sorta girlfriend. Soon it came to burn my relationship and self-esteem. Talk about losing both kids to divorce. I realized two things after this relationship.</p><ol><li>You can’t please a douchebag.</li><li>People can see right through you when you do everything in your power to please everyone around you. Here’s when I decided to give selfish-ness a try again after a long time.</li></ol><h3>Layer #4 Self serving might not be that bad</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cP9RknN6X7XByLGuFrmX3g.png" /></figure><p>Navigating the world became more complex and easier at the same time after this. It became easier because I understood why people do the things they do — because they gave a shit about themselves. It became complex because do I optimize for my own happiness so I can live with myself? or do I optimize for someone else’s happiness so they’d want to be around me. This is where my black and white brain got super-fucked. Neurodivergent brains are just not made for the grey world. And oh man, was the world a million shades of grey. There were infinite choices that were so grey that you couldn’t even make out if they were more black or more white. Every situation and context was different from each other. If you wanted to effectively make decisions, you’d want to understand the context as much as possible. But even so, sometimes you make mistakes. And knowing it’s okay was one way to deal with. There’s also this inexplainable thing called instinct that guides you in your decision making. Tim Urban put instinct in a way that my science brain digs “It’s made from your ancestors making decisions, making mistakes and passing it onto you through dna and we call it — instinct”.</p><p>There’s always pro’s and con’s lists you can make. I have a friend that makes decisions using a a very detailed spreadsheet-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xpBReThKyQa_Do82WWA3Fw.jpeg" /></figure><p>Is it overkill ? Maybe but atleast you’ll feel confident about the decisions you’ve made.</p><p>I finally reached a middle ground. This time I was not going to fully go ham and optimize my own happiness but also keep in mind that I matter too. Gradually learning to give and care whenever I can, because people are nicer to you that way and therefore, the world gets a bit easier to live in for you. But not so much that I’m in misery. Because people go and come but I do have to live with myself for the rest of my life.</p><h3>Layer #5 of decision making — The third perspective</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8aSI9RgizZquKiViUBQmVg.png" /></figure><p>This will probably not be the end of layers to my decision making because there will be more as I go about my life. But here goes the ultimate layer :</p><p>We’ve all made a lot of decisions at the height of your emotions and regretted them. Anger, resentment and despair are extreme emotions that drive our decisions in ways that might not be good for us (it’s not all bad though, some of these are sometimes valid and are meant to be expressed) But something that has helped me not make drastic decisions when I’m not prepared to make them, when my tools are not functioning is “the third perspective”. It’s when you try to detach yourself as much as possible from the situation and look at it from the outside. And look at it as objectively as possible. I’m sure you’ve already heard of it but it is a hard muscle to master. And practice helps you get there. But something people don’t take into account sometimes is when your feelings are actually valid. Yes, objectivity helps but sometimes feelings help more in understanding what you’re feeling</p><p>Now that you’ve seen my life history, and in detail have understood how a person’s tools to make decisions have evolved (although I’m sure the way you or someone else’s way of gaining tools for better decision making might’ve been different). Why does it matter? What does this tell us? To put it in simple words, people make the decisions that they are equipped to make and the rest is a gamble.</p><p>For example : Let’s say you have to pick between ruacalyptius tea and strang coffee? And you’re not allowed to use google. You’ll probably take a blow in the dark and pick whatever. That’s exactly what a person is doing when they’re not equipped with the tools they need to make the best decision for themselves. They’re taking a blow in the dark.</p><p>Hopefully this helped you understand people that make absurd decisions all the time. And another thing I was hoping to do with this blog was to also make you question your own decision making frameworks, do they make sense or were they shaped by absurd experiences that you either steer towards (like peer acceptance) or steer away from (like pain of rebuked by your authorities).</p><p>P.S : ruacalyptius and strang are made up names. That kinda tea/coffee doesn’t actually exist :D I made up ruacalyptius and Glibert strang is a professor of linear algebra at MIT (cool guy, look him up)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26c1b259926b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/evolution-of-decision-making-26c1b259926b">Evolution of decision making</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Async Architecture with FastAPI, Celery, and RabbitMQ]]></title>
            <link>https://medium.com/cuddle-ai/async-architecture-with-fastapi-celery-and-rabbitmq-c7d029030377?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/c7d029030377</guid>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[cuddleengineering]]></category>
            <category><![CDATA[cuddlepe]]></category>
            <category><![CDATA[python-web-developer]]></category>
            <dc:creator><![CDATA[Suman Das]]></dc:creator>
            <pubDate>Sat, 14 May 2022 16:21:38 GMT</pubDate>
            <atom:updated>2022-05-14T16:21:38.648Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zK1KQN6k_fy4xvSB6mo9cA.png" /><figcaption>FastAPI With RabbitMQ and Celery</figcaption></figure><p>In one of my earlier tutorials, we have seen how we can optimize the performance of a <a href="https://fastapi.tiangolo.com/"><strong>FastAPI</strong></a><strong> </strong>application using<strong> Async IO</strong>. To know more you can refer to <a href="https://medium.com/cuddle-ai/concurrency-with-fastapi-1bd809916130">Concurrency with FastAPI</a>.</p><p>Async IO is suitable when we want to perform small BackgroundTasks or to perform tasks using concurrency and parallelism. However, when it comes to performing heavy background computations or complex tasks, we would ideally not like to run them as a part of the same process. As such, to perform these complex tasks as a separate process, we need specialized tools such as <a href="https://docs.celeryq.dev/en/latest/getting-started/introduction.html">Celery</a>.</p><p><a href="https://docs.celeryq.dev/en/latest/getting-started/introduction.html"><strong>Celery</strong></a> is a distributed task queue that simplifies the management of task distribution and processing. Task queues are used as a mechanism to distribute work across threads or machines. A task queue’s input is a unit of work called a task. Dedicated worker processes constantly monitor task queues for new work to perform.</p><p>As a task-queueing system, Celery works well with long running processes or small repeatable tasks working in batches. The types of problems Celery handles are common asynchronous tasks.</p><p><strong>Celery</strong> communicates via messages, usually using a broker to mediate between clients and workers. To initiate a task the client adds a message to the queue, and the broker then delivers that message to a worker. <strong>RabbitMQ</strong> and <strong>Redis</strong> are the brokers transports completely supported by Celery. In our example, we will use RabbitMQ as broker transport.</p><p><a href="https://www.rabbitmq.com/"><strong>RabbitMQ</strong></a> is the most widely deployed open-source message broker. RabbitMQ is lightweight and easy to deploy on-premises and in the cloud. It supports multiple messaging protocols. RabbitMQ can be deployed in distributed and federated configurations to meet high-scale, high-availability requirements. RabbitMQ is the default broker for Celery so it doesn’t require any additional dependencies or initial configuration, other than the URL location of the broker instance we want to use.</p><p>There are several tools available to monitor and inspect Celery clusters. <a href="https://flower.readthedocs.io/"><strong>Flower</strong></a> is a real-time web application monitoring and administration tool for Celery.</p><p>The following figure shows a simplified diagram of how various components interact. Here, we are using FastAPI as our Celery Client and RabbitMQ as a message broker and Result Backend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GzqPxgh43BMtJZnhOmrjnw.png" /><figcaption>FastAPI with Celery Flow</figcaption></figure><ol><li>The client sends a request to our FastAPI application.</li><li>FastAPI app sends the task message to the message broker.</li><li>Celery workers consume the messages from the message broker. After the task finishes, it saves the result to the Result Backend and updates the task status.</li><li>After sending the task to the message broker, the FastAPI app can also monitor the status of the task from the Result Backend.</li><li>Flower can also monitor the celery app tasks by processing messages on the message broker.</li></ol><p>In this tutorial, we will see how we can integrate <strong>Celery</strong> into <a href="https://fastapi.tiangolo.com/"><strong>FastAPI</strong></a> application to perform asynchronous tasks without blocking user’s requests. Although the example which we will use here is very trivial, it is just for pure demonstration purposes only.</p><p>Some of the use cases where the application of Celery with FastAPI can be useful:</p><ol><li>Sending out Emails as Background tasks in an app.</li><li>Processing the uploaded images in the background.</li><li>Offline training of ML models.</li><li>Periodic tasks like report generation or web scrapping.</li></ol><p>The following steps illustrate, how we can use Celery along with FastAPI to perform asynchronous tasks:</p><ol><li>Setup and Installation</li><li>Set up Message Broker</li><li>Add Celery</li><li>Add Celery Tasks</li><li>Add APIRouter</li><li>Start the Application and Celery Worker Server</li><li>Test the Application</li><li>Monitor the Tasks</li></ol><p><strong>Prerequisites</strong></p><p>We require Python 3.9.2 with <strong>Pipenv</strong> and <a href="https://git-scm.com/">Git</a> installed. <a href="https://pipenv.readthedocs.io/en/latest/"><strong>Pipenv</strong></a> is a package and a virtual environment manager, which uses PIP under the hood. It provides more advanced features like version locking and dependency isolation between projects.</p><h3>1. Setup and Installation</h3><p>Once the prerequisites are in place we can begin creating our application.</p><p>To begin with our application, create a folder called fastapi-celery-rabbitmq-application in any directory on the disk for our project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PWQKUAJOiACpIeKgDNpHNA.png" /><figcaption>create project folder</figcaption></figure><p>Navigate to the project folder.</p><p><strong>Activate Virtual Environment</strong></p><p>Once we are inside the project folder, execute the following commands to activate the VirtualEnv.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YXdqOnIEDleJ6nTMlLKYaw.png" /><figcaption>Start pipenv shell</figcaption></figure><p>The virtual environment will now be activated, which will provide the required project isolation and version locking.</p><p><strong>Install Dependencies</strong></p><p>Next, we will install all the required dependencies using Pipenv present in requirements.txt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u1f-KdYYRZ8Axw9-gP2gyg.png" /><figcaption>all the dependencies used in our application</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QCr26kt49lkVd29_XwWn3w.png" /><figcaption>install all the dependencies</figcaption></figure><p>After we execute the above commands, the required dependencies will be installed.</p><p>We can see now two files, which have been created inside our project folder, namely, Pipfile and Pipfile.lock.</p><ul><li>Pipfile contains all the names of the dependencies we just installed.</li><li>Pipfile.lock is intended to specify, based on the dependencies present in Pipfile, which specific version of those should be used, avoiding the risks of automatically upgrading dependencies that depend upon each other and breaking your project dependency tree.</li></ul><p><strong><em>Note</em></strong>: Here, we have installed all the dependencies with specific versions, which worked on my machine while writing this tutorial. If we don’t specify any version then the latest version of that dependency will be installed, which might not be compatible with other dependencies.</p><p>The next step is to set up a Message Broker.</p><h4>2. Set up Message Broker</h4><p>Celery requires a solution to send and receive messages; usually, this comes in the form of a separate service called a <em>message broker</em>. There are several choices available, <strong>RabbitMQ</strong> is one of them.</p><p>One of the easiest ways to run <strong>RabbitMQ</strong> on our local machine is by using <a href="https://brew.sh/">Homebrew</a>. <a href="https://brew.sh/">Homebrew</a> is a popular package manager for <strong>MacOS</strong>. <a href="https://github.com/Homebrew/homebrew-core/blob/master/Formula/rabbitmq.rb">RabbitMQ formula</a> is available from <a href="https://brew.sh/">Homebrew</a>’s core tap (out of the box).</p><ol><li>To install the RabbitMQ server using Homebrew just execute the below command:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*FHUzBaXmKPg7eUjGmx1jeg.png" /><figcaption>Brew install RabbitMQ</figcaption></figure><p>2. The server can then be started with <strong>rabbitmq-server</strong> in the foreground or with brew services start rabbitmq to have it run under launched in the background.</p><p>3. Once we start the server then we can log in to the server with default credentials(Username: guest, Password: guest) at http://localhost:15672.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*fCnp4arc1mq1hSFjG95RIA.png" /><figcaption>RabbitMQ Server</figcaption></figure><p>For other installation options for RabbitMQ, check the below link:</p><p><a href="https://www.rabbitmq.com/documentation.html">RabbitMQ Documentation | RabbitMQ</a></p><p>Our message broker is up and running. Let’s configure Celery now.</p><h4>3. Add Celery</h4><p>We have already installed all the dependencies required for Celery to work. Let’s now configure it. In the root directory of the project, let’s create a file named main.py .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7758a2744d039968bb5445c753ec977e/href">https://medium.com/media/7758a2744d039968bb5445c753ec977e/href</a></iframe><ul><li>In the above code from line 8 to 16 we have created a factory function called create_app(), which can be called any time, and returns a FastAPI app instance for us to use. In this function, we are calling a function called create_celery()to create a Celery app instance. Also, we have integrated the universities routing module, which will be explained further in this tutorial as we go ahead.</li><li>The code from line 23 to 24 is required to run the FastAPI application using <strong>Uvicorn</strong>. FastAPI is the framework that we have used to build our API, and <strong>Uvicorn</strong> is the server that we will use to serve the requests.</li></ul><p><strong>Celery Configuration</strong></p><p>Now start with the celery configuration by adding a file config/celery_config.py.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ed82baa3418caeede251bca4b3f3117/href">https://medium.com/media/7ed82baa3418caeede251bca4b3f3117/href</a></iframe><ul><li>The above file contains all the configurations required by Celery to run. From line 14 to 15 we have defined the Broker URL and Result backend. Here, we are using RabbitMQ, which we installed in step 2, as a broker as well as Result Backend.</li><li>From line 17 to 23 we have defined all the Queues, which we will use in our application along with the default celery Queue. Instead of manually configuring routing rules at each task level, here we will use CELERY_TASK_ROUTES to configure the routing rules dynamically.</li><li>We have added a helper method from line 6 to 10 fetch the routes per task at runtime.</li><li>From line 32 to 39 we defined a function to load the celery configuration during startup.</li></ul><p><strong>Celery App Instance</strong></p><p>Let’s now add one more file config/celery_utils.py that will contain the below code to create the celery app instance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a309ace9a24dc1efd3a57f557569c83a/href">https://medium.com/media/a309ace9a24dc1efd3a57f557569c83a/href</a></iframe><p>The above file defines a create_celery factory function that configures and then returns a Celery app instance. It will read all the celery related configurations defined earlier. We also updated some of the default celery configurations. This function is called from main.py while creating the FastAPI app instance.</p><p>From line 22 to 32 we defined one more helper function get_task_info to get the status and result of the asynchronous tasks submitted via Celery.</p><h4>4. Add Celery Tasks</h4><p>We have now all the Celery-related configurations in place. Let us now add some tasks which we want to execute asynchronously. We have to add a file celery_tasks/tasks.py which will contain all the tasks which we want to execute asynchronously.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88bf7bdc1e862ce7812fc2ed9f667238/href">https://medium.com/media/88bf7bdc1e862ce7812fc2ed9f667238/href</a></iframe><p>In the above file we have defined two tasks get_all_universities_task and get_university_task . get_all_universities_task is used to get the all the universities for the list of countries provided as input. get_university_task is used to get the universities for a given country.</p><p>Here, we have used shared_task to make our code reusable, which requires current_app instance inside create_celery method instead of creating a new Celery instance. The @shared_task decorator returns a proxy that always uses the task instance in the current_app. It lets us create tasks that can be used by any app(s). Since Celery has built-in support for retrying the failed tasks, we have enabled it for our tasks. With retry option, we can now let the exception bubble up and let the decorator handle it.</p><h4>5. Add APIRouter</h4><p>Now, let’s add some entry points to test our application. We will start with a simple Sync entrypoint. This entry point will call some external APIs and return the results to the client. Here, we are using the same free API which I have used in one of my earlier tutorial. It’s listed in <a href="https://apipheny.io/free-api/">Apipheny</a>. We will use the <a href="https://github.com/Hipo/university-domains-list">Universities List API</a> to get a list of universities for a specified country. For this tutorial, we are fetching the list of universities provided as input to the API. We will not be validating the response, as we are just simulating the scenario where in our application we are performing some long running tasks to complete the request like we need to call multiple APIs, do certain processing on top of that and then return the results.</p><p>Let’s add a dedicated file for handling just universities requests as a module at /routers/universities.py.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d7b5cdf1a304118d3812d137a66939c/href">https://medium.com/media/4d7b5cdf1a304118d3812d137a66939c/href</a></iframe><p>We wanted to have the <em>path operations</em> related to universities separated from the rest of the code, to keep the code organized. But it’s still part of the same <strong>FastAPI</strong> application. We can create the <em>path operations</em> for any module using APIRouter. We have already included this router in our application in step 3 . Here we have added 4 different endpoints to test our application.</p><ul><li>get_universites : From line 14 to 22 we have defined a simple API which takes a list of countries as an input, and then it calls some external API to get the result. The result of each country is combined and then returned to the caller. This API is added just to check the API integration. It has nothing to do with Celery.</li><li>get_universities_async: This API is used to demonstrate how we can use Celery to perform the long running tasks asynchronously. When we call this API, it submits a task message to the broker and returns the taskID for the same. Consumers then can use the other API to check the status of the submitted task or to get the task result. As we can see here, we just need to call the get_all_universities_task method defined earlier in step 4 . Celery task is called just by adding the .apply_async() after the function name. This will tell Celery to add new task to the queue. It internally submits the task to universities Queue. There will be a worker process which will listen to this Queue . The worker process will pickup the message, process the request and save the results in Result Backend.</li><li>get_universities_parallel: This API is used to demonstrate how we can use Celery to split a large task into smaller subtasks and execute in parallel. When we call this API, we create a one task for each country provided as input. Then we group these tasks using Celery <a href="https://docs.celeryq.dev/en/stable/reference/celery.html#celery.group"><strong>Group</strong></a><strong>. </strong>Celery<strong> </strong>group is lazy so we must call it to take action and evaluate the group. Then we execute the group and wait for it to complete. Once all the tasks are completed then the group will be completed and we will get the response. After that we collect the response of the group and return it to the caller. This is a synchronous flow which uses Celery for parallel execution of subtasks.</li><li>get_task_status: This API is used to get the status and result of the asynchronous tasks submitted using one of the API which we described earlier.</li></ul><p>Let us have a look at the code which fetches the universities:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43597d4e1c113b5c234dadfb322d78c3/href">https://medium.com/media/43597d4e1c113b5c234dadfb322d78c3/href</a></iframe><p>In line 5 we are using the HTTP client named <a href="https://www.python-httpx.org/"><strong>httpx</strong></a><strong> </strong>to send an HTTP request. HTTPX is a fully-featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2.</p><p>Here, we are using the sync API to get the universities data. In the above code in line 6 we are calling the universities API to get the results. In line 7 we convert the response to JSON format. Then from line 9 to 11 , we map the JSON object to the <strong>Pydantic</strong> model University. And finally, we return the dictionary, which contains the country name as key and the list of universities as value.</p><p>Now our code is complete to run the FastAPI with Celery.</p><h4>6. Start the Application and Celery Worker Server</h4><p>We have already added the code in step 3 required to start the application.</p><p>FastAPI is the framework that we have used to build our API, and <strong>Uvicorn</strong> is the server that we will use to serve the requests. We have already installed <a href="https://www.uvicorn.org/">Uvicorn</a>. That will be our server. We can start the application by executing the below command:</p><pre><strong>python main.py</strong></pre><p>Once the application starts successfully, we can navigate to http://localhost:9000/docs. The system will bring up a page that looks something like this. We can see the endpoint in the Swagger-UI which was defined earlier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qZuebxYa0Yz9K-adcOTfYw.png" /><figcaption>Swagger-ui</figcaption></figure><p>Let’s now start the Celery worker server. We need to open a new terminal and go the project root directory. Then we need to install all the dependencies using the below command:</p><pre><strong>Pipenv install -r requirements.txt</strong></pre><p>Once all the dependencies are installed then we can execute the below command:</p><pre><strong>celery -A main.celery worker --loglevel=info -Q universities,university</strong></pre><p>The Celery worker will listen to the custom Queue : universities, university used in our application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*91bnwwNT4tNTHcNdBLesew.png" /><figcaption>Celery-worker</figcaption></figure><p>Everything is up and running now! Let’s test our application.</p><h4>7. Test the Application</h4><p>Let us test our universities endpoint with some sample countries to validate the external API integration. Here, we are getting all the universities for “Turkey” and “India”. We are not validating the response or data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AU94msS5ICztmZBAFdSwVg.png" /><figcaption>get-universities-request</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5uc60h4EtRcIJ0oSgbo2FA.png" /><figcaption>get-universities-response</figcaption></figure><p>As we can see above, we got the universities list for the requested countries. Here, we are using the <a href="https://github.com/Hipo/university-domains-list">Universities List API</a> to get a list of universities for the specified country.</p><p>Once we have tested that the external API interaction is working fine, let’s test the asynchronous task execution using Celery. Here, we will try to get the list of universities for the same set of countries provided as input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f0BHYXAzdYZMq1nQI4v-ig.png" /><figcaption>async-request</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g2A1wWF8ybvLa0a5AZoaxQ.png" /><figcaption>async-response</figcaption></figure><p>Once we submit the request, we just get the task_id as an acknowledgment response. The request is getting processed asynchronously using Celery as a separate process. We can check the status of the task using the other API. With this architecture, we can handle a large number of requests. We can Enqueue the user requests and process them as per resource availability for our application.</p><p>Let’s check the status of the asynchronous task submitted above. We can use the below API with task_id as input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pQiMZ-pOWqB9ESoR5YJsJA.png" /><figcaption>get task status request</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k-8MCi8U6on_khX55zLE0Q.png" /><figcaption>get task status response</figcaption></figure><p>Let’s now test the scenario where we want to split the user request into multiple subtasks, process them in parallel using Celery and then return the results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ljSbNV0y3i3U77WfOJd8eQ.png" /><figcaption>parallel-request</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r4vWrnJhT59vfKIdAwkRsA.png" /><figcaption>parallel-response</figcaption></figure><p>This has the same request and response as the first API which we tested earlier. However, this will execute faster because we are getting the universities of each country in parallel using Celery. Here, we got the response in 0.58 sec as compared to 1.67 sec as per the first API.</p><h4>8. Monitor the tasks</h4><p>Till now we can see that using Celery we are submitting some tasks to be executed in the background. It would be good if we can see the progress of these using GUI. The easiest way to monitor the Celery app is via Flower. Using it, we can easily check the progress of all the tasks. We already have all the dependencies installed. We just need to start flower. To run the flower server on our local machine execute the below command from the project root directory:</p><pre><strong>celery -A main.celery flower --port=5555</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YtGNf-OsJKXElqMoQrE98w.png" /><figcaption>flower</figcaption></figure><p>We can now see our flower monitoring tool running on our local server at <a href="http://localhost:5555/">http://localhost:5555/</a> .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTpBVoBbvGbOxmzz4foSBA.png" /><figcaption>Flower-dashboard</figcaption></figure><p>In the Dashboard, we can see the summary of all the tasks executed via Celery.</p><p>Once the tasks are submitted, we can also see the progress of each task by clicking on the tasks tab on the Flower navigation bar.</p><p>For example, we can see the state of each task as it goes from In Progress to Success or Failed. We can also see when the task started, its runtime and its response, and so on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lJpIs0nfRlpWrI6OSmCd5w.png" /><figcaption>Celery-tasks</figcaption></figure><h4>Conclusion</h4><p>In this tutorial, we saw how we can integrate Celery with FastAPI application. Using Celery along with FastAPI application will not only improve the overall performance and efficiency but will help in better handling of background jobs for long-running tasks. We also saw if we want to monitor the tasks submitted via Celery then how we can do the same using Flower.</p><p>If you would like to refer to the full code, do check:</p><p><a href="https://github.com/sumanentc/fastapi-celery-rabbitmq-application">GitHub - sumanentc/fastapi-celery-rabbitmq-application: Sample FastAPI Application to demonstrate Async architecture with Celery, RabbitMQ and Flower</a></p><h4>References &amp; Useful Readings</h4><ul><li><a href="https://docs.celeryq.dev/en/stable/index.html">Celery - Distributed Task Queue - Celery 5.2.6 documentation</a></li><li><a href="http://ask.github.io/celery/userguide/groups.html#groups">Groups, Chords, Chains and Callbacks - Celery 2.6.0rc4 documentation</a></li><li><a href="https://flower.readthedocs.io/en/latest/">Flower - Celery monitoring tool - Flower 1.0.1 documentation</a></li><li><a href="https://stackoverflow.com/questions/21233089/how-to-use-the-shared-task-decorator-for-class-based-tasks">How to use the @shared_task decorator for class based tasks</a></li><li><a href="https://www.rabbitmq.com/documentation.html">RabbitMQ Documentation | RabbitMQ</a></li><li><a href="https://www.rabbitmq.com/documentation.html">RabbitMQ Documentation | RabbitMQ</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7d029030377" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/async-architecture-with-fastapi-celery-and-rabbitmq-c7d029030377">Async Architecture with FastAPI, Celery, and RabbitMQ</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ML Lifecycle with MLFlow — MLDevOps]]></title>
            <link>https://medium.com/cuddle-ai/ml-lifecycle-with-mlflow-mldevops-4209294a4ff0?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/4209294a4ff0</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[tracking]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[scikit-learn]]></category>
            <category><![CDATA[mlflow]]></category>
            <dc:creator><![CDATA[Ritik Jain]]></dc:creator>
            <pubDate>Sat, 14 May 2022 16:21:26 GMT</pubDate>
            <atom:updated>2022-12-23T05:14:10.839Z</atom:updated>
            <content:encoded><![CDATA[<h3>ML Lifecycle with MLFlow — MLDevOps</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*f6TIyf3mCSrSf6e0" /><figcaption>Photo by <a href="https://unsplash.com/@martinadams?utm_source=medium&amp;utm_medium=referral">Martin Adams</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>ML modelling requires a lot of deep knowledge of ML and its components before it can be built. But executing entire lifecycle with keeping track of all experiments from experimenting to deploying models to production environment is harder.</p><p>The ML engineer develops the model, evaluates it, retrains it, and releases it for testing. After all experimentation, comparing and evaluating different models and choosing the best model is unachievable without an explicit automated manager to keeps the track.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*geJwZ8EabVFnqaA5ObBYgA.png" /><figcaption>Traditional ML Lifecycle</figcaption></figure><h3>MLFLow</h3><p>MLflow is an open source platform for managing the end-to-end machine learning lifecycle. MLFlow provides simple API to perform tasks like reproducing the model, logging and tracking hyper-parameters, monitoring model performance and deployment in production.</p><p>Core of MLFlow have 4 major components:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NR94JZHcwCtiRsOKBp5fcg.png" /><figcaption>The components of MLflow — taming end-to-end ML lifecycle management.</figcaption></figure><ul><li><strong>MLFlow Tracking <br></strong>This component provides API and UI interface. API interface enables MLFlow to log parameters, metrics, code versions and artifacts while running your machine learning code. UI interface provides the various visualisation of result, logged parameters, other metrics, and model comparisons. ML Flow tracking can run in any environment like a standalone script or a notebook and log results in local directory or centralised databases. It also provides a centralized access point for teams to compare results for different users.</li><li><strong>MLFlow Projects<br></strong>It provides a standard packaging mechanism for storing ML environment setup and code base. In MLFlow, each project is a simple directory with code or git repository and a descriptor file to specify its dependencies and how to run the code. When you use the MLFlow tracking API in a project, MLFlow automatically remembers the project version. In MLFlow, projects can be chained into multi-step workflows easily.</li><li><strong>MLFlow Models</strong><br>It offers a convention for packaging machine learning models in various flavors (different formats). Each model saves a directory containing the model binaries and a descriptor file listing down different flavors in which the model can be used. For example, a PyTorch model can be loaded as a PyTorch binary file, or as a python, function to apply input data. MLFLow provide various tools to deploy many common model types to diverse platform: Any model supporting “python function” can be deployed to a Docker-based REST server, to cloud platforms like AWS Sagemaker and Azure ML.</li><li><strong>MLFlow Registry<br></strong>It offers a centralized model store, set of APIs, and UI to collaboratively manage the entire model lifecycle of an MLFlow model. MLFlow Registry provides different features like model stage transition, model versioning, model lineage, and model annotations.</li></ul><h3><strong>Installing MLFLow</strong></h3><ol><li>Setup the anaconda environment (Follow the <a href="https://docs.anaconda.com/anaconda/install/windows/">documentation</a>)</li><li>Use conda install -c conda-forge mlflow for installing mlflow</li></ol><p>If installation works fine,</p><pre>mlflow --version</pre><p>the output will be,</p><pre>mlflow, version 1.24.0</pre><p>Depending on the time and version you use, the output may differ.</p><p><strong>Quickstart with MLFlow</strong></p><p>Let&#39;s perform an experiment and train a classification model on the IRIS dataset. I am going to use the s<a href="https://scikit-learn.org/">cikit-learn</a> library for dataset loading, transforming, AllaNumPynd model training.</p><p>Let’s get started.</p><p><strong>Code without MLFlow APIs</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23f1ff4cf8241183c2873286f30a1e4c/href">https://medium.com/media/23f1ff4cf8241183c2873286f30a1e4c/href</a></iframe><p><strong>Simple MLFLow Workflow for Scikit-Learn</strong></p><ol><li>Start an experiment using <em>mlflow.start_run() </em>which switches the context of your existing model code to enable mlflow tracking.</li><li>Detect all the important parameters for logging into MLFlow Tracking</li><li>After model training and evaluation, I have logged the model using <em>mlflow.sklearn.log_model().</em> As I’m using scikit-learn for training the model, So using the sklearn package of mlflow to log the model. Similarly to other frameworks, MLFlow has a wide range of packages. (find more <a href="https://mlflow.org/docs/latest/models.html#built-in-model-flavors">here</a>)</li></ol><p><strong>Code with MLFlow API integrated</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4c1c80daf1e60bb04fc59c572a68614/href">https://medium.com/media/e4c1c80daf1e60bb04fc59c572a68614/href</a></iframe><p>I have added a few self-explanatory lines for starting the experiment, logging parameters, and model details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/0*oNDorBi-BCg_aZam.gif" /></figure><p><strong>MLFlow UI</strong></p><p>After completing the model training and logging, we can track the model progress using MLFlow UI.</p><p>To enable the tracking, Navigate to the curselfrent project in Terminal and use the command below</p><pre>mlflow ui</pre><p>Output,</p><pre>[2022-03-14 13:04:07 +0530] [65940] [INFO] Starting gunicorn 20.1.0<br>[2022-03-14 13:04:07 +0530] [65940] [INFO] Listening at: <a href="http://127.0.0.1:5000/">http://127.0.0.1:5000</a> (65940)<br>[2022-03-14 13:04:07 +0530] [65940] [INFO] Using worker: sync<br>[2022-03-14 13:04:07 +0530] [65942] [INFO] Booting worker with pid: 65942</pre><p>It will host the UI on localhost network and port 5000.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a4QyU2Zdx8NQ9z4-dJNjbQ.png" /><figcaption>MLFlow Tracking Board</figcaption></figure><p>As shown in UI, it is sectioned based on Experiments. In each experiment you’ll have all passed or failed iteration of model training with the parameters used and their metrics. Clicking on a row in Table, a redirect will be made to the selected run, showing all the details shown below.</p><p>It will redirect to selected run and show all the details shown below. For reusability, the model will have all parameters with auto-generated code that enables predictions to be made.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eORILuNnTmaPCgcGJRfHfw.png" /><figcaption>MLFlow Run Details</figcaption></figure><h3>Conclusion</h3><ul><li>MLFlow is an open-source platform for managing the machine learning lifecycle</li><li>It has 4 major components that work synchronously to provide pipeline experience.</li><li>MLFlow provides a simple user-interface to navigate and understanding and tracking model progress</li><li>Support of over 10 popular machine learning frameworks and different deployment plugins</li><li>Provide modern API interfaces like REST API, gRPC</li><li>Ease of model registry and production deployment.</li></ul><h3>References</h3><ul><li><a href="https://mlflow.org/docs/latest/concepts.html">MLFlow Official Document</a></li><li><a href="https://towardsdatascience.com/manage-your-machine-learning-lifecycle-with-mlflow-part-1-a7252c859f72">Manage your machine learning using MLFlow</a></li><li><a href="https://medium.com/pytorch/mlflow-and-pytorch-where-cutting-edge-ai-meets-mlops-1985cf8aa789">MLFlow and PyTorch</a></li></ul><p>Interested to see more like these.<a href="https://www.buymeacoffee.com/rjain"> Buy me a coffee</a></p><p><a href="https://www.buymeacoffee.com/rjain">Ritik Jain is Data Scientist, Innovator, Engineer</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4209294a4ff0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/ml-lifecycle-with-mlflow-mldevops-4209294a4ff0">ML Lifecycle with MLFlow — MLDevOps</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrency with FastAPI]]></title>
            <link>https://medium.com/cuddle-ai/concurrency-with-fastapi-1bd809916130?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/1bd809916130</guid>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[cuddleengineering]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[cuddlepe]]></category>
            <dc:creator><![CDATA[Suman Das]]></dc:creator>
            <pubDate>Thu, 20 Jan 2022 05:50:48 GMT</pubDate>
            <atom:updated>2022-01-20T05:50:48.321Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*N-jySKpYBpiJPAaWW8PWfg.png" /><figcaption>Concurrency with FastAPI</figcaption></figure><p>In one of my earlier tutorials, we have seen how we can create a Python application using <a href="https://fastapi.tiangolo.com/"><strong>FastAPI</strong></a>. To know more you can refer to <a href="https://dassum.medium.com/building-rest-apis-using-fastapi-sqlalchemy-uvicorn-8a163ccf3aa1">Building REST APIs using FastAPI</a>. One of the main reasons to choose FastAPI over other frameworks is due to its <a href="https://fastapi.tiangolo.com/#performance">very high performance</a>, which is on par with <strong>NodeJS</strong> and <strong>Go.</strong></p><p>In this tutorial, we will see how we can optimize the performance of our FastAPI application using concurrency and parallelism.</p><h3>Understanding Concurrency and Parallelism</h3><h4>Concurrency</h4><p>Concurrency is when two or more tasks can start, run, and complete in overlapping time periods. It doesn’t necessarily mean they’ll be running at the same instant, such as multitasking on a single-core machine.</p><p><em>Threading</em> is a concurrent execution model whereby multiple threads take turns executing tasks. One process can contain multiple threads. It uses pre-emptive multitasking.</p><p><em>Async IO</em> is a style of concurrent programming that is single-threaded, single-process design. It uses co-operative multitasking. We will be covering Async IO in Python in detail as we go ahead.</p><h4>Parallelism</h4><p>Parallelism is when tasks literally run at the same time, wherein we can split tasks up into smaller subtasks, which can be processed in parallel using multiple CPUs at the exact same time, such as, on a multicore processor.</p><p><em>Multiprocessing</em> is a means to effect parallelism, and it entails spreading tasks over a computer’s central processing units (CPUs, or cores). The processes all run at the same time, essentially multiprocessing on different processors.</p><p><strong>Concurrency</strong> and <strong>parallelism</strong> both relate to “different things happening more or less at the same time”.</p><h3>Introduction to Async IO</h3><p><strong>Async IO</strong> is a style of concurrent programming introduced in <strong>Python 3.4</strong>. Asynchronous I/O is a form of input/output processing. While Synchronous I/O waits for an operation to complete before moving on to the next one, Asynchronous I/O permits other processing to continue before the transmission of one operation has finished.</p><p>Synchronous I/O is easier to code as it follows all the steps in a program to complete a task before moving on to the next. To simplify asynchronous code and make it as simple as synchronous code, Async IO uses coroutines and futures as there are no callbacks. It uses <strong>async </strong>and<strong> await</strong> keywords to manage and execute asynchronous code.</p><p><a href="https://docs.python.org/3.5/library/asyncio-task.html#coroutines"><strong>Coroutines</strong></a> are special functions that work similar to Python generators. It is a function that can suspend its execution before reaching return, and it can indirectly pass control to another coroutine for some time. On <strong><em>await,</em></strong> they release the flow of control back to the event loop. A coroutine needs to be scheduled to run on the event loop, once scheduled coroutines are wrapped in <em>Tasks </em>which is a type of <em>Future</em>.</p><p><a href="https://docs.python.org/3.5/library/asyncio-task.html#future"><strong>Futures</strong></a> represent the result of a task that may or may not have been executed. This result may be an exception.</p><p>With <strong>FastAPI</strong> we can take the advantage of concurrency that is very commonly used for web development. We don’t have to worry about the Event loop or <strong>async/await </strong>management. What we need is to just do the following :</p><ul><li>Declare the first path functions as coroutines via async def wherever appropriate. If we do this wrong, FastAPI will still be able to handle it, we just won’t get the performance benefits.</li><li>Declare particular points as awaitable via the await keyword within the coroutines.</li></ul><p>We will see this in practice below when we come to the coding part.</p><p>In most of the applications, we perform some Input-output (I/O) operations which are slow in nature. To demonstrate the slow nature of Input-output (I/O) operations, in this tutorial we will create a FastAPI application that will internally call some external APIs to get some data. When we call external API we are performing some network (I/O). Then we will see how to utilize Async IO to improve the performance of our application.</p><p>The following steps illustrate, how we can use Async IO in FastAPI to improve performance:</p><ol><li>Setup and Installation</li><li>Add a Sync EntryPoint</li><li>Add Middleware</li><li>Start the Application</li><li>Test the Application</li><li>Add an Async EntryPoint</li><li>Compare the Performance</li></ol><p><strong>Prerequisites</strong></p><p>We require Python 3.9 with <strong>Pipenv</strong> and <a href="https://git-scm.com/">Git</a> installed. <a href="https://pipenv.readthedocs.io/en/latest/"><strong>Pipenv</strong></a> is a package and a virtual environment manager, which uses PIP under the hood. It provides more advanced features like version locking and dependency isolation between projects.</p><h4>1. Setup and Installation</h4><p>Once the prerequisites are in place we can begin creating our application.</p><p>To begin with, our application, create a folder called python-sample-fastapi-application in any directory on the disk for our project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w-SJeMzI2p6gfeb6j0mLdQ.png" /><figcaption>create-project-folder</figcaption></figure><p>Navigate to the project folder.</p><p><strong>b) Activate Virtual Environment</strong></p><p>Once we are inside the project folder, execute the following commands to activate the VirtualEnv.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kKaQPhVqTdu_omZ57CCJ8g.png" /><figcaption>start pipenv shell</figcaption></figure><p>The virtual environment will now be activated, which will provide the required project isolation and version locking.</p><p><strong>c) Install Dependencies</strong></p><p>Next, install all the required dependencies using Pipenv as shown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O75Byb4TkcYTixU_2fRIOQ.png" /><figcaption>install-dependencies</figcaption></figure><p>After we execute the above commands, the required dependencies will be installed.</p><p>We can see now two files, which have been created inside our project folder, namely, Pipfile and Pipfile.lock.</p><ul><li>Pipfile contains all the names of the dependencies we just installed.</li><li>Pipfile.lock is intended to specify, based on the dependencies present in Pipfile, which specific version of those should be used, avoiding the risks of automatically upgrading dependencies that depend upon each other and breaking your project dependency tree.</li></ul><p><strong><em>Note</em></strong>: Here, we have installed all the dependencies with specific versions, which worked on my machine while writing this tutorial. If we don’t specify any version then the latest version of that dependency will be installed, which might not be compatible with other dependencies.</p><p>The next step is to add the Entrypoint for our application.</p><h4>2. Add a Sync EntryPoint</h4><p>In the root directory of the project, let’s create a file named main.py . Now, let’s add an entry point. The entry point will call some external APIs and return the results to the client. Here, we are using one of the free API listed in <a href="https://apipheny.io/free-api/">Apipheny</a> for demonstration purposes. We will use the <a href="https://github.com/Hipo/university-domains-list">Universities List API</a> to get a list of universities in a specified country. For this tutorial, we are fetching the list of universities in turkey, india and australia. We will not be validating the response, as we are just simulating the scenario where in our application we may need to call multiple APIs, do certain processing on top of that and then return the results.</p><p>Let us have a look at the code for adding the non-async endpoint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37e273b36358bf4149d8898919e8cf60/href">https://medium.com/media/37e273b36358bf4149d8898919e8cf60/href</a></iframe><p>In the above code from line 7 to 9 we are calling the method get_all_universities_for_country for fetching the list of all universities from three different countries and storing the result in the dictionary. After collecting the results for all the countries we are returning the results to the caller.</p><p>Next, let’s see how we are getting the data. We will add a file named universities.py in the root directory. This file will contain the function get_all_universities_for_country that will be used to fetch the universities for the given country.</p><p>Let us have a look at the code which fetches the universities:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43597d4e1c113b5c234dadfb322d78c3/href">https://medium.com/media/43597d4e1c113b5c234dadfb322d78c3/href</a></iframe><p>In line 5 we are using the HTTP client named <a href="https://www.python-httpx.org/"><strong>httpx</strong></a><strong> </strong>to send an HTTP request. HTTPX is a fully-featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2.</p><p>Here, we are using the sync API to get the universities data. Later on, we will use the async API.</p><p>In the above code in line 6 we are calling the universities API to get the results. In line 7 we convert the response to JSON format. Then from line 9 to 11 , we map the JSON object to the <strong>Pydantic</strong> model University.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4xnfdGXwlr5Gkg10ynxVjw.png" /><figcaption>University Model</figcaption></figure><p>And finally, we return the dictionary, which contains the country name as key and the list of universities as value.</p><p>Now our sync endpoint is ready. To check the response time of our API let’s add a middleware.</p><h4>3. Add middleware</h4><p>A <a href="https://fastapi.tiangolo.com/tutorial/middleware/"><strong>middleware</strong></a> is a function that works with every <strong>request</strong> before it is processed by any specific <em>path operation </em>and also with every <strong>response</strong> before returning it.</p><p>Add the below middleware code in main.py</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0acce06ba8d4eab9ba5b6ca33dfb871b/href">https://medium.com/media/0acce06ba8d4eab9ba5b6ca33dfb871b/href</a></iframe><p>As we can see in the above code, the middleware function uses the decorator @app.middleware(&quot;http&quot;) at the top. It will intercept our entrypoint request and then forward the request to the corresponding path operation. We will use this middleware to calculate the time taken by our entrypoint and return the response time to the caller. Here, we are adding a response header X-Process-Time in line 6 which will contain the response time.</p><h4>4. Start the Application</h4><p>Till now we have written all the code required for our application to run. We just need to add the below code to main.py file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m7inf4rrH1zNHqet2xSolQ.png" /><figcaption>main.py</figcaption></figure><p>FastAPI is the framework that we have used to build our API, and <strong>Uvicorn</strong> is the server that we will use to serve the requests. We have already installed <a href="https://www.uvicorn.org/">Uvicorn</a>. That will be our server. Our application is ready now. We can start the application by executing the below command:</p><pre>python main.py</pre><p>Once the application starts successfully, we can navigate to http://localhost:9000/docs. The system will bring up a page that looks something like this. We can see the endpoint in the Swagger-UI which was defined earlier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*khVBDr4h_mEHeeaFKSpH6w.png" /><figcaption>swagger-with-sync-API</figcaption></figure><h4>5. Test the application</h4><p>Let us test our universities endpoint and see, how much time does it take to complete the request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IQb26Jz8RvanKv7qyPvLoA.png" /><figcaption>sync-response</figcaption></figure><p>As we can see in the response that the sync endpoint took around 2.0514 sec to get the list of universities for three countries.</p><h4>6. Add an Async EntryPoint</h4><p>Let’s now see how we can improve the performance of the REST API using Async IO.</p><p>We will add an async equivalent of the above endpoint. There is no functional difference between these two endpoints. It is just a different way of handling the request.</p><p>Let us have a look at the code for the async endpoint:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09320d7d3d2500c52181396f4aff1320/href">https://medium.com/media/09320d7d3d2500c52181396f4aff1320/href</a></iframe><p>When we define a function with the async keyword, it is a special function called a <a href="https://docs.python.org/3/glossary.html#term-coroutine">coroutine</a>. The reason why coroutines are special is that they can be paused internally, allowing the program to execute them in increments via multiple entry points for suspending and resuming execution. This is in contrast to normal functions which only have one entry point for execution.</p><p>When we use the await keyword, inside async function, it tells the program that this is a “suspendable point” in the coroutine. It will suspend execution at this point and can perform some other tasks. It will come back later once the results are available.</p><p>In the above code in line 7 we can see that, we are using <a href="https://docs.python.org/3/library/asyncio-task.html#asyncio.gather">asyncio.gather</a> to run a sequence of awaitable objects (i.e. our coroutines) concurrently. Once we gather the response of all the coroutines, we are returning the response to the caller. The main difference here is that we are not waiting for the response of the first API to finish before calling the next API. It’s like we are calling all the APIs concurrently and waiting for the results.</p><p>Next, we will add one more function in universities.py called get_all_universities_for_country_async to get the universities in async mode.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a97de5f7580c2d4ad0a2cf4c3d1311bc/href">https://medium.com/media/a97de5f7580c2d4ad0a2cf4c3d1311bc/href</a></iframe><p>The above function is declared with the async keyword, defining it as a coroutine. In the above code in line 3 we can see that here we are using the async API of httpx . async with httpx.AsyncClient() is the httpx context managed client for making async HTTP calls. Here, GET request is made with the await keyword, telling Python that this is a point where it can suspend execution and do something else. Once we receive the response then the code remains the same as sync call.</p><h4>7. Compare the Performance</h4><p>Now our async API is ready to be tested. Let us re-run the application. We can now see two endpoints for our application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SEAt8xVUM0OZwuPXGNM5Ug.png" /><figcaption>sample-FastAPI-application</figcaption></figure><p>Let us run the async endpoint and compare the results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_SHmnTVSa5SEm5PR5RgPlA.png" /><figcaption>async-API</figcaption></figure><p>As we can see that using Async IO we are able to get the response in 0.58 sec which earlier took around 2 sec</p><h4><strong>Conclusion</strong></h4><p>In this tutorial, we saw how we can take advantage of the Async IO style of programming in FastAPI to improve the performance of our REST APIs. If we have a scope in our application to use Async IO, then we should give it a try.</p><p>If you would like to refer to the full code, do check:</p><p><a href="https://github.com/sumanentc/python-sample-FastAPI-application">GitHub - sumanentc/python-sample-FastAPI-application: Sample Python application using FastAPI , Uvicorn and SQLAlchemy</a></p><h4>References &amp; Useful Readings</h4><ul><li><a href="https://www.python-httpx.org/">HTTPX</a></li><li><a href="https://fastapi.tiangolo.com/async/#asynchronous-code">Concurrency and async / await - FastAPI</a></li><li><a href="https://stackoverflow.com/questions/1050222/what-is-the-difference-between-concurrency-and-parallelism">What is the difference between concurrency and parallelism?</a></li><li><a href="https://docs.python.org/3/library/asyncio.html">asyncio - Asynchronous I/O</a></li><li><a href="https://www.python.org/dev/peps/pep-0492/">PEP 492 -- Coroutines with async and await syntax</a></li><li><a href="https://github.com/Hipo/university-domains-list">GitHub - Hipo/university-domains-list: University Domains and Names Data List &amp; API</a></li><li><a href="https://apipheny.io/free-api/">Free API - Huge List of Public APIs For Testing [No Key] - Apipheny</a></li><li><a href="https://realpython.com/python-concurrency/">Speed Up Your Python Program With Concurrency - Real Python</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1bd809916130" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/concurrency-with-fastapi-1bd809916130">Concurrency with FastAPI</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Querying Cache in Apache Ignite]]></title>
            <link>https://medium.com/cuddle-ai/querying-cache-in-apache-ignite-ecd6d350c52a?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/ecd6d350c52a</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[cuddleengineering]]></category>
            <category><![CDATA[apache-ignite]]></category>
            <category><![CDATA[search-engines]]></category>
            <category><![CDATA[cuddlepe]]></category>
            <dc:creator><![CDATA[Suman Das]]></dc:creator>
            <pubDate>Mon, 13 Dec 2021 13:12:25 GMT</pubDate>
            <atom:updated>2021-12-13T13:12:25.121Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*mfXP4xJ5150c7SCrR7nAIA.jpeg" /><figcaption>Cache Queries</figcaption></figure><p><strong>Apache Ignite </strong>is a memory-centric distributed database, caching, and processing platform for transactional, analytical, and streaming workloads delivering in-memory speeds at the petabyte scale.</p><p><strong>Ignite</strong> provides an in-memory data store where each node in an <strong>Ignite</strong> cluster by default stores data in RAM. The data is kept in off-heap storage to ensure low latency and consistent access times. The system is multi-model, with the ability to support structured, semi-structured, and unstructured data.</p><p>Apache Ignite offers an elegant query API with the following components:</p><ul><li><strong>Predicate-based Scan Query<br></strong>A scan query is a simple search query used to retrieve data from a cache in a distributed manner. When executed without parameters, a scan query returns all entries from the cache. Scan queries return entries that match a predicate, if specified. The predicate is applied to the remote nodes.</li><li><strong>ANSI 99-compliant SQL Query<br></strong>Apache Ignite SQL Grid is a distributed data grid where you can execute ANSI SQL-99-compliant SQLs (SELECT, UPDATE, INSERT, MERGE, and DELETE queries) to manipulate a cache.</li><li><strong>Lucene Index-based Text Query<br></strong>Lucene is a full-text search library in Java which makes it easy to add search functionality to an application or website. Ignite supports full-text queries based on the Apache Lucene engine.</li><li><strong>Continuous query <br></strong>Continuous queries allow registering a remote filter and a local listener for cache updates. Once the cache is updated and passes the filter criteria, then the cache update events will be sent to the node that executed the query, and the local listener will be notified. This helps us to take action in case the cache is updated on Ignite Cluster.</li></ul><p>For SQL queries, Ignite supports in-memory indexing, so all the data lookups are extremely fast. If we are caching our data in off-heap memory, then query indexes will also be cached in off-heap memory as well.</p><p>In this tutorial, we will learn how to query the data stored in the <strong>Cache</strong> of <a href="https://ignite.apache.org/docs/latest/"><strong>Apache Ignite</strong></a><strong> </strong>using query API. We will not cover Continuous Query in this tutorial.</p><p>We will use the following steps to explore Query-based search in Apache Ignite Cache:</p><ol><li>Integrating Apache Ignite Server Node With Spring Boot</li><li>Enabling Caching</li><li>Query and QueryCursor</li><li>Scan Queries</li><li>SQL Queries</li><li>Text Queries</li><li>Testing the Application</li></ol><h4>Prerequisites</h4><p>For this tutorial, we are using JDK 1.8 and Spring Boot 2.5.6 project with the following dependencies:</p><pre>spring-boot-starter<br>spring-boot-starter-web<br>spring-boot-starter-test</pre><p>Here, we will use <a href="https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html"><strong>Maven</strong></a> for dependency management. Along with the above dependencies, we will also use <a href="https://github.com/springdoc/springdoc-openapi"><strong>springdoc-openapi</strong></a> to automatically generate the OpenAPI 3 specification docs for our API. For that, we simply need to add the springdoc-openapi-ui dependency to our <a href="https://maven.apache.org/guides/introduction/introduction-to-the-pom.html"><strong>pom.xml</strong></a><strong> :</strong></p><pre>&lt;dependency&gt;     <br>  &lt;groupId&gt;org.springdoc&lt;/groupId&gt;<br>  &lt;artifactId&gt;springdoc-openapi-ui&lt;/artifactId&gt;<br>   &lt;version&gt;1.5.2&lt;/version&gt;<br>&lt;/dependency&gt;</pre><h4>1. Integrating Apache Ignite Server Node With Spring Boot</h4><p>The Apache Ignite server nodes act as containers for data and computations. Once interconnected, the server nodes will represent a distributed database (or data grid) that stores the data, participates in queries processing, compute execution, stream processing, and so on.</p><p><strong>Ignite</strong> provides an implementation of JCache (JSR 107) specification. <strong>JCache</strong> provides a very simple and powerful API for data access. However, the specification omits any details about data distribution and consistency to allow vendors enough freedom for their own implementations. Here, we will use <strong>org.apache.ignite.cache.CacheManager</strong> to interact with the Cache.</p><p>We will use Spring XML-based configuration for Ignite.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/140f476a569ed5d231cf5e834d0c8141/href">https://medium.com/media/140f476a569ed5d231cf5e834d0c8141/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a904719984d15ab1157b99c7beaf49a/href">https://medium.com/media/9a904719984d15ab1157b99c7beaf49a/href</a></iframe><p>The above codes create a CacheManager instance using the Ignite configuration XML file path specified in igniteConfigPath variable.</p><p>We also need to add the following Maven dependencies in our Spring Boot app to enable Ignite-based caching.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d610ff3abd0772251ed6b1e4596f655a/href">https://medium.com/media/d610ff3abd0772251ed6b1e4596f655a/href</a></iframe><p>Here, we are using GridGain community edition version: <strong>8.8.10. </strong>Once we are done with the above configuration, then we can start the Ignite node in embedded mode, that is, in the same JVM where the application is running.</p><h4>2. Enabling Caching</h4><p>After we have the Spring Boot app with embedded Ignite node configured, then we need to define our Cache. Here, we will define a sample <strong>Player</strong> cache, which we will use for querying purposes. We are using XML-based configuration to define our cache.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e574343d13541d0d0dee00370619f331/href">https://medium.com/media/e574343d13541d0d0dee00370619f331/href</a></iframe><p>Once we declare our Player cache, then we have to define it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5118001e1e677a8557903df8d9971c03/href">https://medium.com/media/5118001e1e677a8557903df8d9971c03/href</a></iframe><p>The above code does the following:</p><p>We started off by defining the Player class with some attributes, which we have declared in our cache configuration. We have used the annotation <a href="https://ignite.apache.org/releases/latest/javadoc/org/apache/ignite/cache/query/annotations/QuerySqlField.html"><strong>@QuerySqlField</strong></a><strong> </strong>on fields, which we will use for SQL queries. All fields which need to be involved in SQL clauses must have this annotation. We have also used the annotation <a href="https://ignite.apache.org/releases/latest/javadoc/org/apache/ignite/cache/query/annotations/QueryTextField.html"><strong>@QueryTextField </strong></a>on fields that need to be indexed for full-text search using Lucene.</p><h4>3. Query and QueryCursor</h4><p>IgniteCache has several query methods, all of which require some subclass of the Query class. When we query IgniteCache it returns the QueryCursor.Query abstract class that represents an abstract paginated query to be executed on the distributed cache. We can also set the page size for the returned cursor via Query.setPageSize(...) method (default is 1024).</p><p>QueryCursor class represents the query result set and allows for transparent page-by-page iteration. Whenever we start iterating over the last page, it will automatically request the next page in the background. For cases when pagination is not needed, we can use the QueryCursor.getAll() method, which will fetch the whole query result and store it in a collection. If we use QueryCursor.getAll() method, it will also automatically close the cursor in the background.</p><p>To explore the search queries we will create a class called PlayerService. This will contain various methods required for the search operation.</p><h4>4. Scan Queries</h4><p>Apache Ignite’s key-value pair API is used to store objects in a cache and retrieve values using keys. It is a query API that lets us query objects using expressions. ScanQuery provides one of the implementations of Query API, which allows us to query the cache in distributed form using some user-defined predicate.</p><p>Let us explore the ScanQuery API now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4871efd30fb1b90041726c15f877a02/href">https://medium.com/media/e4871efd30fb1b90041726c15f877a02/href</a></iframe><p>The above code does the following:</p><ul><li>We started off by creating a public method called scanQuerySearch in line 1 with search text input, which we will use to search in Player cache.</li><li>In line 3, we retrieved an instance of the Player cache from Ignite node.</li><li>Then we created an instance of ScanQuery in line 4, which takes IgniteBiPredicate as an argument. IgniteBiPredicate is a functional interface that accepts two parameters and returns a boolean. It is usually used to filter a collection of objects and can be used in lambda expression also. Here, we are going to use a Java 8 lambda expression to represent IgniteBiPredicate. The k represents the key of the cache and v is the value of Player. Our IgniteBiPredicate returns True only if any player stored in the cache qualifies the expression player.getTeam() EQ input text. The result is returned as a QueryCursor. It stores all qualified entries (key-value pairs).</li><li>From line 7 to 14 we iterated through the results to locate if such an entry exists in the cache and then populated a local List of filtered Players.</li><li>In line 15 we are returning the filtered list of Players to the caller.</li></ul><p>The ScanQuery goes over each cache entry and applies the predicate, which may not always be the most efficient way to query objects, especially if the data size is large.</p><h4>5. SQL Queries</h4><p>The SQLQuery API supports ANSI-99 SQL queries against caches. This API allows SQL joins against collocated entries on the same node as well as non-collocated distributed nodes.</p><p>To tell Apache Ignite which fields are accessible for SQL queries, we need to define the metadata. Apache Ignite’s <strong>@</strong><a href="https://ignite.apache.org/releases/latest/javadoc/org/apache/ignite/cache/query/annotations/QuerySqlField.html"><strong>QuerySqlField</strong></a> annotation does the trick. We can even index the fields for a faster query by setting the annotation value: <strong>@</strong><a href="https://ignite.apache.org/releases/latest/javadoc/org/apache/ignite/cache/query/annotations/QuerySqlField.html"><strong>QuerySqlField</strong></a><strong>(index=true)</strong>. We have used this annotation in Step 2 for our Player cache to enable SQL queries.</p><p>Let us explore the SQLQuery API now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb3f98e270dff8f5470848fc6481a539/href">https://medium.com/media/cb3f98e270dff8f5470848fc6481a539/href</a></iframe><p>The above code does the following:</p><ul><li>We started off by creating a public method called sqlQuerySearch in line 1 with search text input, which we will use to search in Player cache.</li><li>In line 3, we retrieved an instance of the Player cache from Ignite node.</li><li>Next, we extracted the table name from the cache in line 4 to 5. Once we have the table name, then we can form a SQL Query using the table name to search the cache.</li><li>We created an instance of SqlFieldsQuery in line 6.<br>The SqlFieldsQuery class provides another implementation of Query API. It is used for executing SQL statements and navigating through the results. It accepts a standard SQL query as its constructor​ parameter and executes it. SqlFieldsQuery is executed through the IgniteCache.query(SqlFieldsQuery) method, which returns a QueryCursor.</li><li>From line 8 to 9 we iterated and extracted the results using getAll() method. If an entry exists in the cache, then we collect the results and return the response to the caller.</li></ul><p>In replicated mode, SQL joins execute fast as all entries are replicated in all cluster nodes. However, in partitioned mode, a few nodes may not contain the primary backup, hence, distributed joins become complex and expensive. If we are planning to use distributed joins on partitioned tables then we should use <a href="https://ignite.apache.org/docs/latest/SQL/distributed-joins">Colocated joins</a> or <a href="https://ignite.apache.org/docs/latest/SQL/distributed-joins#hash-joins">Hash joins</a>.</p><h4>6. Text Queries</h4><p>The TextQuery API allows us to run a full-text search on stored objects in the cache. The TextQuery works on Lucene indexes. Elasticsearch and Apache Solr also use Lucene for indexing text.</p><p>Here too, we need to define the metadata to tell Apache Ignite which fields are to be enabled for Text search. The <a href="http://twitter.com/QueryTextField"><strong>@QueryTextField</strong></a> annotation enables indexing along with Text Search. Also, our cache configuration needs to enable indexing by setting the <strong>setIndexedTypes</strong>. We have already set this in Step 2 during our cache configuration.</p><p>Let us explore the TextQuery API now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43dc5b3d5760033fb656b294cc27ffec/href">https://medium.com/media/43dc5b3d5760033fb656b294cc27ffec/href</a></iframe><p>The above code does the following:</p><ul><li>In line 1 we created a method called textSearch, which takes a text as an input and then searches that text in Player cache. It searches the fields in Player cache annotated with <strong>QueryTextField</strong>.</li><li>Then in line 3, we retrieved an instance of the Player cache from Ignite node.</li><li>We created an instance of TextQuery in line 4. Once we have an instance of TextQuery and IgniteCache we called the method searchPlayers to search the text in the cache.</li><li>From line 25 to 29 we iterated through the results to locate if such an entry exists in the cache and then populated a local List of filtered Players.</li><li>In line 30 we are returning the filtered list of Players to the caller.</li><li>Similar to textSearch we also created a method called fuzzySearch from line 8 to 13 to perform Fuzzy search on Player cache. Fuzzy search determines whether there is any similarity between elements of the data. Let’s say we want to fetch players where the team name is ‘Barcelona’ or ‘Barcenola’. Then we just need to add a ‘~’ at the end of our search string and pass it to the TextQuery constructor. We are going to find the players where the text ‘Barcenola’ is present, and the fuzzy search will find a match between <strong>nola</strong> and <strong>lona</strong> and return all ‘Barcelona’ players.</li><li>We can also perform Fuzzy search on specific fields. For this reason we added another method called fuzzySearchOnSpecificField from line 15 to 20 which takes fieldName and text as an input.</li><li>In the previous methods <strong>textSearch</strong> and <strong>fuzzySearch</strong>, during search operation <strong>Ignite</strong> looked at all the indexes, name and team, to find data. However, we can also ask Ignite to look into specific index. We can see that in line 18 . In this method we ask <strong>Ignite</strong> to look at a specific index. It will search the input text only in the field provided by user. For example, if we are interested in name = ‘Neymar’, then the TextQuery can be configured as name:”Neymar”.</li></ul><p>It is advisable to use indexes for querying entries. However, one drawback is that an index itself takes up space and may slow down the data modification as every time we modify an entry, the index needs to be rebuilt.</p><h4>7. Testing the Application</h4><p>Till now we have written most of the code required to perform various kinds of searches in Ignite Cache. Now let us write some code to populate the cache at startup . The PlayerService class contains various methods required to explore Query API in Ignite. Let us also add the below method to populate the cache.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04f9cb7633e0a6438c356e40b30d0bac/href">https://medium.com/media/04f9cb7633e0a6438c356e40b30d0bac/href</a></iframe><p>The above code populates the Player Cache with Player name and Team. We can use these values for testing purposes.</p><p>After we have populated our cache with some sample values, then we added a Controller to enable users to perform various search operations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a130de30adc6513d97c8631103802a9/href">https://medium.com/media/8a130de30adc6513d97c8631103802a9/href</a></iframe><p>The above code does the following:</p><p>It exposes API which can be used to test the various features of Ignite Query API that we discussed so far. It uses the PlayerService class to perform the search operations.</p><p>Once we build the project using mvn clean package then we can start the application using java -jar target/ignite-poc*.jar command.</p><p>After the application is started successfully, then we can use the Swagger-UI to test the application. The Swagger-UI comes up at <strong>http://localhost:9051/swagger-ui.html</strong> in local.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cSgOvjKx_UbTKkmCk07zQg.png" /><figcaption>Swagger-UI</figcaption></figure><p><strong>7.1 Search Player Cache with team name as input using SQLQuery API</strong></p><p>Here, we are using SQLQuery to search all the Players whose team contains the given text : “Barcelona”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w1bMc3ODOh8WQdR2UzjVvA.png" /><figcaption>SQLQuery</figcaption></figure><p><strong>7.2 Search Player Cache with team name or player name that matches the text input using TextQuery API</strong></p><p>Here, we are using TextQuery to search all the Players where either name or team contains the given text: “United”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mtz7gKjB9uRMzVknWLzVng.png" /><figcaption>TextQuery</figcaption></figure><p><strong>7.3 Search Player Cache with team name that matches the text input using ScanQuery API</strong></p><p>Here, we are using ScanQuery to search all the Players whose team contains the given text: “Barcelona”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*75gAoIwbliEsvA0Z2kFR6A.png" /><figcaption>SsanQuery</figcaption></figure><p><strong>7.4 Search Player Cache with team name or player name that matches the text input using TextQuery with Fuzzy search enabled</strong></p><p>Here, we are using using Fuzzy TextQuery to search all the Players where either name or team contains the given text: ”Barcenona”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X1JzBW_eOFRGYjytZ6B2Vg.png" /><figcaption>Fuzzy-Search</figcaption></figure><p><strong>7.5 Search Player Cache with input field name and text using TextQuery API</strong></p><p>Here, we are using Fuzzy TextQuery to search all Players with fieldName where fieldName contains the given text: “Messi”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8la48Y3LP5mu1u8_nThemQ.png" /><figcaption>TextQuery-with-field</figcaption></figure><p>If you would like to refer to the full code, do check:</p><p><a href="https://github.com/sumanentc/Ignite-Querying-Cache">GitHub - sumanentc/Ignite-Querying-Cache: Sample Spring Boot application to query the data stored in the Cache of Apache Ignite using query API.</a></p><h4>Conclusion</h4><p>To perform search operation on Key-Value pair, Cache is a great feature provided by Ignite. If Query API is used properly then the performance is also at par with key-value pair search. If there is a need for key-value search along with SQL Query support then Apache Ignite is one of the best choices currently available.</p><h4>References &amp; Useful Readings</h4><ul><li><a href="https://ignite.apache.org/use-cases/caching/jcache-provider.html">JCache Provider - Apache Ignite</a></li><li><a href="https://ignite.apache.org/docs/latest/extensions-and-integrations/spring/spring-boot">Apache Ignite With Spring Boot</a></li><li><a href="https://www.gridgain.com/docs/latest/getting-started/what-is-gridgain">What Is GridGain?</a></li><li><a href="https://ignite.apache.org/docs/latest/index">Apache Ignite Documentation</a></li><li><a href="https://apacheignite.readme.io/docs/cache-queries">Cache Queries</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecd6d350c52a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/querying-cache-in-apache-ignite-ecd6d350c52a">Querying Cache in Apache Ignite</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MicroService Design — Part 1]]></title>
            <link>https://medium.com/cuddle-ai/microservice-design-part-1-418c224e2de7?source=rss----27e35c10d45d---4</link>
            <guid isPermaLink="false">https://medium.com/p/418c224e2de7</guid>
            <category><![CDATA[mvc]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[microservice-architecture]]></category>
            <category><![CDATA[microservices]]></category>
            <dc:creator><![CDATA[Ritik Jain]]></dc:creator>
            <pubDate>Tue, 23 Nov 2021 09:56:34 GMT</pubDate>
            <atom:updated>2021-11-23T09:56:34.337Z</atom:updated>
            <content:encoded><![CDATA[<h3>MicroService Design — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uTFvDZowUp3xDbzQ" /><figcaption>Photo by <a href="https://unsplash.com/@kaleidico?utm_source=medium&amp;utm_medium=referral">Kaleidico</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Recently microservices gain insane popularity in latest technology stacks. <em>A microservice is an architectural structure that allows rapid, frequent, and reliable delivery of complex applications.</em> It provides high maintainability and testability, loose component coupling (low interdependence), independent deployments and can be managed by small teams.</p><p>In this article, I will discuss the best practices for designing microservices. Before designing a particular microservice, we should answer some questions like:</p><ul><li>What design patterns can be used in the service?</li><li>What should be the structure of a service?</li><li>What technology stack should be used?</li></ul><p>I will address these questions in this article. So let’s get started.</p><p><strong>What should be the service structure?</strong><br>The service structure refers to the project structure. Industry uses a diverse range of project structures, i’ll discuss some of the most common project structures.</p><ul><li><strong>Simple Structure (MVC Structure)</strong></li></ul><p>Simple structure also known as <strong>Model-View-Controller</strong> Framework, where the project structure divided into Models, Views and controllers. <strong>Model</strong> refers to the Database interface (CRUD Application). <strong>View</strong> refers to application interfaces (API Endpoints), <strong>Controller</strong> refers to application business logic. <br>This structure is well suited for simple application without includes multiple functionalities.</p><p>Advantages:</p><ul><li>Segregation at functionality level</li><li>High Code reusability and readability</li></ul><p>Disadvantages:</p><ul><li>Not scalable</li><li>No testability and validation</li><li>Complex maintainability and navigation for large application</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/0*wpEsGghf1WB-ih-_.png" /></figure><ul><li><strong>Divisional Structure</strong></li></ul><p>Divisional structures provide the application segregation at business objectives, whereas the simple structure lacks with maintainability of complex application. The divisional structure performs well in terms of navigation and maintenance of applications.</p><p>Advantages:</p><ul><li>Segregation at objective level</li><li>High code readability and reusability</li><li>Centralisation and Decentralisation of codebase</li><li>Individual contribution for individual modules</li></ul><p>Disadvantages:</p><ul><li>Lack of Testability</li><li>Complex code validation and review</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*lTWgsk8N7w3jIAySMj1b5w.png" /></figure><p><strong>Test driven Division Structure</strong></p><p>This structure is similar to Divisional Structure. It is being added with the Testability and code validation. <br>We can use test-driven divisional structure for building and maintaining complex application with an ease of continuous validation of the modules.</p><p>Advantages</p><ul><li>Similar to Divisional Structure</li><li>Added Code validation and review</li></ul><p>Disadvantages</p><ul><li>High dependency on Test Coverage</li><li>Difficult learning curve</li></ul><p><strong>Answer</strong> — Service Structure can be defined based on application complexity and ease of team. For simple application we can directly use MVC structure, whereas complex application requires a divisional structure.</p><p><strong>What design patterns can be used in the service?</strong></p><blockquote>A <strong>design pattern</strong> is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn’t a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.<br>By — <a href="https://sourcemaking.com/design_patterns">Source Maker</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/0*QQ-vdToKOfT9j_0X.png" /><figcaption>Source: <a href="https://www.jobsity.com/blog/design-pattern-software">Jobsity</a></figcaption></figure><p>The patterns describe the solution to the problem, when and where to apply the solution and the consequences of the implementation. Broadly design patterns categorised into</p><p><strong>Creational Patterns</strong><br>Creational design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case. These patterns helps with hiding the main business logic under the abstract umbrella. Creational Patterns like</p><ul><li>Factory Pattern</li><li>Builder Pattern</li><li>Prototype</li><li>Singleton Pattern</li><li>Object Pool Pattern</li></ul><p>Creational patterns provide a common interface for different objects. As shown below, Based on different parameters, the pattern define the functionality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/0*LBEX2MBAWKgnFZ-W" /><figcaption>Source: Refactoring Guru</figcaption></figure><p><strong>Structural Patterns<br></strong>Structural patterns are considering composition of classes and objects in order to achieve new functionality. These patterns uses the concept of Inheritance for composition. Structural Patterns like</p><ul><li>Composition</li><li>Adapter</li><li>Decorator</li><li>Bridge</li><li>Facade</li><li>Proxy</li></ul><p>Structural patterns generally divides the whole complex task into small and simple tasks which later compound and solve the main objective. As shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/0*Wd17e7m_AZ1XjQgD" /><figcaption>Composite Pattern by <a href="https://refactoring.guru/design-patterns/composite">Refactoring Guru</a></figcaption></figure><p><strong>Behavioural Patterns</strong></p><p>Behavioural design patterns are mainly concerned about the communication between different objects. These patterns can be used where the behaviour or state is known and recurring. Patterns like</p><ul><li>Template</li><li>Chain of responsibility</li><li>Observer</li><li>Visitors</li><li>Null Object</li></ul><p>Here is a mediator pattern, where multiple objects can communicate with each other. Sharing of different data can be possible using simple classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/0*fQAvaPqbuUIF1PsU.png" /><figcaption>Source: Refactoring Guru</figcaption></figure><p><strong>Answer —</strong> Choosing the right design pattern for a module is essential. We can have various modules within one application, all with different functionality, so we must choose the right pattern.</p><p><strong>What technology stack need to be used for microservice?</strong></p><p>Microservice architecture is technology stack agnostic, where we can use different technology based on service requirement. Technology stack should align with business objective.</p><p>If business objective requires ML capabilities then we can create the service using Python Stack which includes</p><p>API Endpoint:- FastAPI, Flask<br>Framework:- Tensorflow, PyTorch, Scikit-Learn</p><p>For business processes involving a lot of event-driven processes, like in Blockchain, <strong>Node.JS is a great choice</strong>.</p><p><strong>Answer</strong> — Technology Stack will always be defined based on the business objective. Before selecting the technology stack, we need to understand the business requirement.</p><h3><strong>Conclusion</strong></h3><p>The article have addressed three major questions generally asked before developing any service in microservice environment.</p><ul><li>Service structure can be defined based on application complexity and ease of team</li><li>Choosing the right design pattern for a module is essential</li><li>Understanding the business requirements is essential before answering the technology stack.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=418c224e2de7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cuddle-ai/microservice-design-part-1-418c224e2de7">MicroService Design — Part 1</a> was originally published in <a href="https://medium.com/cuddle-ai">Crux Intelligence</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>