<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Trademo Engineering on Medium]]></title>
        <description><![CDATA[Stories by Trademo Engineering on Medium]]></description>
        <link>https://medium.com/@trademo-engineering?source=rss-13328dc729f2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Gi_WTzVUYj5ZT3zZBQ3H7A.png</url>
            <title>Stories by Trademo Engineering on Medium</title>
            <link>https://medium.com/@trademo-engineering?source=rss-13328dc729f2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 17:03:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@trademo-engineering/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating robust International Trade data pipelines using Apache Airflow]]></title>
            <link>https://trademo-engineering.medium.com/creating-robust-international-trade-data-pipelines-using-apache-airflow-d09e0a12352a?source=rss-13328dc729f2------2</link>
            <guid isPermaLink="false">https://medium.com/p/d09e0a12352a</guid>
            <category><![CDATA[global-trade]]></category>
            <category><![CDATA[import-export]]></category>
            <category><![CDATA[trademo]]></category>
            <category><![CDATA[apache-airflow]]></category>
            <category><![CDATA[data-pipeline]]></category>
            <dc:creator><![CDATA[Trademo Engineering]]></dc:creator>
            <pubDate>Tue, 29 Jun 2021 13:14:07 GMT</pubDate>
            <atom:updated>2021-06-29T13:14:07.673Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/184/1*lrM5R48lo7OTqrEeEfzSvQ.png" /></figure><p><strong>Trademo</strong> is on a mission to solve the problem of trust and information asymmetry in Global Trade. To achieve this objective, Trademo is organizing all information around global trade.</p><p>At Trademo, we leverage the power of Apache Airflow to create batch ETL (Extract Transform Load) jobs that are dependent on each other and are fault tolerant. In this article, I would be discussing the process of choosing the best task orchestration tool as per your use case, why Airflow works out to be the perfect fit for our use case and the terminologies related to Apache Airflow. So, lets get started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/47/1*Mk7rvdsP-QBpnqcKBAOI8w.gif" /><figcaption>Source: <a href="https://airflow.apache.org/">https://airflow.apache.org/</a></figcaption></figure><p><strong>Background of data pipelines at Trademo:</strong></p><p>Businesses in Global Trade use Trademo to find reliable buyers and suppliers, monitor competition and get actionable product insights. We at Trademo have created multiple data pipelines corresponding to each data source as the data refresh intervals, schema and transformation rules associated with each data source are different. The source data can be in the form of daily trade shipments, weekly or monthly aggregated trade data, public APIs that provide insights related to international trade, information related to companies etc.</p><p>We started with executing the data fetching and processing jobs manually and eventually ending up setting multiple cron jobs that were independent of each other with majority of the code written in Python. But, in a real world scenario, the batch cron jobs can be dependent on each other. For example: Company profiles can be generated from the Trade shipments. If the job that fetches the trade shipments fails on a certain day, the company profiles should not be generated because the source data could not be fetched and stored subsequently.</p><p>So, there was a need to create dependency cron jobs that are fault tolerant, and adopt a task orchestration tool that provides an intuitive way to track all the jobs and tasks, has efficient alerting and retry mechanisms and treats ETL jobs as DAGs (Directed Acyclic Graphs) with lots of dependent tasks. The following workflow automation tools were evaluated before finalizing with Apache Airflow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*8Yg-9mS3vFaEnqsqdyHIJg.png" /><figcaption>Sample DAG of tasks (Source: <a href="http://airflow.apache.org/docs/apache-airflow/1.10.12/_images/branch_note.png">http://airflow.apache.org/docs/apache-airflow/1.10.12/_images/branch_note.png</a>)</figcaption></figure><p><strong>Comparison of multiple data worflow automation tools:</strong></p><p>We looked into four open source projects that we thought were useful for workflow management and long dependency chains:</p><h3>Luigi</h3><p>Luigi is a fairly popular open source project created by <a href="https://www.spotify.com/">Spotify</a>. It has a lot of great reviews online and the user interface for creating job flows is very easy to use. However, Luigi does not have a trigger mechanism, we needed a scheduler that was capable of finding and triggering tasks that we create. Additionally, Luigi does not assign tasks to workers and isn’t highly capable of monitoring schedules. Since, It doesn’t have a scheduler and users have to rely on cron for scheduling of jobs, it was kind of a deal breaker for us. We continued our job to find an efficient workflow scheduler tool.</p><h3>Argo</h3><p><a href="https://argoproj.github.io/projects/argo">Argo</a> is implemented as a <a href="https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/">Kubernetes CRD</a> and allows you to run pipelines natively on your cluster. With Argo, each task executes in a pod and you can easily execute multiple tasks as a DAG. It contains many important features such as passing artifacts between tasks, parameterization and scheduling. The way you run Argo Workflows is by using a YAML configuration file. YAML has its limitations, especially when you want to run pipelines with many tasks and do fast iterations.</p><p>For this reason, various Argo SDKs are currently being built that will grant you the ability to programmatically define Argo Workflows in Python and translate your code to the Argo YAML specification. We explored <a href="https://www.kubeflow.org/docs/pipelines/overview/pipelines-overview/">Kubeflow Pipelines</a> (KFP) python SDK to define Argo Workflows in Python, but since we at Trademo are not using Kubernetes as of now to deploy our applications, it would have uneccessarily added complexities in creating workflow pipelines.</p><h3>Airflow</h3><p><a href="https://github.com/apache/airflow">Airflow</a> is an open source project developed by <a href="http://airbnb.com/">Airbnb</a>. It is supported by a large community of software engineers and can be utilized with a lot of different frameworks. The maturity level of this project is high, yet it’s currently in the process of stabilization as it is being incubated by<a href="https://www.apache.org/"> Apache</a>.</p><p>Airflow has multiple modules which can be configured in different ways. Airflow has a larger community and some extra features, but a much steeper learning curve. Specifically, Airflow is far more powerful when it comes to scheduling, and it provides a calendar UI to help you set up when your tasks should run.</p><p>You should probably use:</p><ul><li><a href="https://airflow.apache.org/"><strong>Apache Airflow</strong></a><strong> </strong>if you want the most full-featured, mature tool and you can dedicate time to learning how it works, setting it up, and maintaining it.</li><li><a href="https://github.com/spotify/luigi"><strong>Luigi</strong></a><strong> </strong>if you need something with an easier learning curve than Airflow. It has fewer features, but it’s easier to get off the ground.</li><li><a href="https://argoproj.github.io/projects/argo/"><strong>Argo</strong></a><strong> +</strong><a href="https://www.kubeflow.org/"><strong>KubeFlow</strong></a><strong> </strong>if you want use Kubernetes, don’t have a hesitation working with YAML specification and want to deal with a huge scale of data, and upscaling is not a concern.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/917/1*Jr1o5KEnY4xbSzXBmXxYqA.png" /><figcaption>Chart representing the number of stars on each repo on github (Source: <a href="https://star-history.t9t.io/">https://star-history.t9t.io/</a>)</figcaption></figure><p>After analysis of the tools mentioned above, Apache Airflow was a decisive winner and we went ahead with installing Airflow and migrating all our existing cron jobs to Airflow as dependency DAGs.</p><p><strong>Creating a Trade data pipeline to fetch, transform and ingest data</strong></p><p>We at Trademo get <a href="https://www.trademo.com/?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=apache-content"><strong>trade data</strong></a> files on a daily, weekly and monthly basis depending upon the source of data, so we create different DAGs for each source, and for each DAG, we create tasks that deal with fetching the data, transforming it, calculating certain derived attributes using various NLP techniques and then finally ingest the data to Elasticsearch, MongoDB and PostgreSQL.</p><p>In Airflow, DAGs are composed of multiple tasks. We use Operators and sensors (which is also a type of operator) to define tasks. Once an operator is instantiated within a given DAG, it is referred to as a task of the DAG.</p><p>An Operator usually provides integration to some other service like PythonOperator, SlackOperator etc which provides a way to access these services from Airflow.</p><p>Some common operators available in Airflow that we use here at Trademo are:</p><ol><li>BashOperator — used to execute bash commands on the machine it runs on</li><li>PythonOperator — takes any python function as an input and calls the same (this means the function should have a specific signature as well)</li><li>EmailOperator — sends emails using SMTP server configured</li></ol><p>We won’t be able to disclose the names of our data sources, but a typical data pipeline looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6aHgC4kNicl6KDo2BazJmQ.png" /></figure><p>Sample code to get started using BashOperator:</p><pre>from airflow import DAG<br>from airflow.operators.bash_operator import BashOperator<br>from datetime import datetime, timedelta</pre><pre>default_args = {<br>    &#39;owner&#39;: &#39;airflow&#39;,<br>    &#39;depends_on_past&#39;: False,<br>    &#39;start_date&#39;: datetime(2021, 6, 12),<br>    &#39;email&#39;: [&#39;<a href="/service/mailto:prabhjot.singh@trademo.com">ps@trademo.com</a>&#39;],<br>    &#39;email_on_failure&#39;: True,<br>    &#39;email_on_retry&#39;: False,<br>    &#39;retries&#39;: 2,<br>    &#39;retry_delay&#39;: timedelta(minutes=5),<br>}</pre><pre>dag = DAG(&#39;usa_trade_shipments_dag&#39;, default_args=default_args, schedule_interval=&#39;33 10 * * *&#39;)</pre><pre>t1 = BashOperator(<br>    task_id=&#39;fetch_new_trade_data_from_source&#39;,<br>    bash_command=&#39;your-command-to-execute-code&#39;,<br>    dag=dag)</pre><pre>t2 = BashOperator(<br>    task_id=&#39;copy_trade_data_to_s3&#39;,<br>    bash_command=&#39;your-command-to-execute-code&#39;,<br>    dag=dag)</pre><pre>t2.set_upstream(t1) #Define dependency tasks</pre><p>The following DAG would get trigerred at 10:33am every day and execute all the defined tasks considering the order in which they have been defined to execute.</p><p>You would need to setup a SMTP server to ensure that Airflow can send alerts via email, in case of a task failure.</p><p>We have setup our alerting mechanisms using Gmail SMTP server, but you can use any SMTP server of your choice.</p><p><strong>Setting up SMTP Server for Airflow Email alerts using Gmail</strong>:</p><p>Create an email id from which you want to send alerts about DAG failure or if you want to use <strong>EmailOperator</strong>. Edit airflow.cfg file to edit the smtp details for the mail server.</p><p>Create a google App Password for your gmail account. This is done so that you don’t use your original password or two factor authentication.</p><ol><li>Visit your <a href="https://security.google.com/settings/security/apppasswords">App passwords</a> page. You may be asked to sign in to your Google Account.</li><li>At the bottom, click <strong>Select app</strong> and choose the app you’re using.</li><li>Click <strong>Select device</strong> and choose the device you’re using.</li><li>Select <strong>Generate</strong>.</li><li>Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.</li><li>Select <strong>Done</strong>.</li></ol><p>Once you are finished, you won’t see that App password code again. However, you will see a list of apps and devices you’ve created App passwords for.</p><p>Edit airflow.cfg and edit the [smtp] section as shown below:</p><pre>[smtp]<br>smtp_host = smtp.gmail.com<br>smtp_starttls = True<br>smtp_ssl = False<br>smtp_user = YOUR_EMAIL_ADDRESS<br>smtp_password = 16_DIGIT_APP_PASSWORD<br>smtp_port = 587<br>smtp_mail_from = YOUR_EMAIL_ADDRESS</pre><p>Edit the below parameters to the corresponding values:</p><p>YOUR_EMAIL_ADDRESS = Your Gmail address<br>16_DIGIT_APP_PASSWORD = The App password generated above</p><p><strong>Making sure that Airflow process is always up and running</strong></p><p>To run Airflow in a robust way and to ensure auto-recovery with systemd<br>so you can do:<br>- to start systemctl start airflow<br>- to stop systemctl stop airflow<br>- to restart systemctl restart airflow</p><p>For this you’ll need a systemd ‘unit’ file. As a (working) example you can use the following:<br>put it in /lib/systemd/system/airflow.service</p><pre>[Unit]<br>Description=Airflow webserver daemon<br>After=network.target postgresql.service mysql.service redis.service rabbitmq-server.service<br>Wants=postgresql.service mysql.service redis.service rabbitmq-server.service<br>[Service]<br>PIDFile=/run/airflow/webserver.pid<br>EnvironmentFile=/home/airflow/airflow.env<br>User=airflow<br>Group=airflow<br>Type=simple<br>ExecStart=/bin/bash -c &#39;export AIRFLOW_HOME=/home/airflow ; airflow webserver --pid /run/airflow/webserver.pid&#39;<br>ExecReload=/bin/kill -s HUP $MAINPID<br>ExecStop=/bin/kill -s TERM $MAINPID<br>Restart=on-failure<br>RestartSec=42s<br>PrivateTmp=true<br>[Install]<br>WantedBy=multi-user.target</pre><p>P.S: change AIRFLOW_HOME to your airflow folder directory.</p><p>This is how we at <a href="https://www.trademo.com/?utm_source=medium&amp;utm_medium=blog&amp;utm_campaign=apache-content">Trademo</a> ensure that all the data pipelines are <strong>robust, fault tolerant and are able to auto recover</strong> in case of any unforeseen circumstances.</p><p>We look forward to your comments on your experience working with the discussed open-source projects.</p><p><a href="https://www.facebook.com/Trademo20">Facebook</a></p><p><a href="https://twitter.com/Trademo1">Twitter</a></p><p><a href="https://www.linkedin.com/company/trademo-inc/">LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d09e0a12352a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>