<?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 Javier Buquet on Medium]]></title>
        <description><![CDATA[Stories by Javier Buquet on Medium]]></description>
        <link>https://medium.com/@jbuquet?source=rss-d06be6ffd88e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7EReQ1XQcGNTNvjDzoqknw.jpeg</url>
            <title>Stories by Javier Buquet on Medium</title>
            <link>https://medium.com/@jbuquet?source=rss-d06be6ffd88e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:42:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jbuquet/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[Synchronizing Data Pipelines]]></title>
            <link>https://medium.com/dataxutech/synchronizing-data-pipelines-93443b501a4a?source=rss-d06be6ffd88e------2</link>
            <guid isPermaLink="false">https://medium.com/p/93443b501a4a</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-pipeline]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[big-data]]></category>
            <dc:creator><![CDATA[Javier Buquet]]></dc:creator>
            <pubDate>Wed, 13 Jun 2018 13:37:49 GMT</pubDate>
            <atom:updated>2018-06-13T13:37:49.106Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*NjHaL5EtJDIHY7SF7mewlw.png" /></figure><p>Are you finding it hard to synchronize processes that read and write data on a data lake?</p><p>Are your solutions overly complex?</p><p><strong>If you answered yes to these questions, there is a simple pattern we use at </strong><a href="https://www.dataxu.com/"><strong>dataxu</strong></a><strong> that can help. It gets the work done with minimal overhead.</strong></p><p>The Data Science Engineering team at dataxu spends most of its’ day designing, developing, and maintaining a wide range of data pipelines that support our AI-based bidding system.</p><p><strong>A pipeline is a set of stages (or sub-processes) that process input data producing an output, in which the output of each stage serves as the input for the subsequent stage</strong>; thus creating a chain of stages that modify a given input to produce a desired output. In particular, a data pipeline can be defined as the process or pipeline of processes that move, join, format, and process data in a system. And a good data pipeline should do so in an automated and reliable way, with minimum — or, ideally, no — human intervention.</p><p>In the case of systems with the scale and complexity of dataxu’s platform, TouchPoint, it’s common to have many different data pipelines — each having a particular goal or role in the whole system — that need to interact. Let’s say we have a dataset with all the ads we’ve shown to different users and another dataset with all the clicks the users have made. We want to have two pipelines that “clean” both datasets independently, and then use the output of those pipelines to study which ads were effective.</p><p>Given all these interactions, it’s very important that we have a robust mechanism, or protocol, for synchronizing the data between the different pipelines. I.e. making sure we provide the correct input to each pipeline based on the output of its predecessors.</p><h3>Defining a synchronization protocol</h3><p>An acceptable first approach to this problem could be to run each of the predecessors for a given pipeline on each run keeping the results in the program’s memory, and therefore have all the necessary data available right away. Of course, this method is immediately hindered by a number of drawbacks such as:</p><ul><li>Not being able to reuse an intermediate result in case we need to re-run a pipeline (which usually makes things a lot more difficult to debug too)</li><li>Doing duplicate work when different pipelines have shared predecessors</li><li>Not being able to fully de-couple a given pipeline from its predecessors (for example, implementing them using different languages or technologies).</li></ul><p>Another approach could be to implement every pipeline independently of its predecessors and agree on a location in which each pipeline will drop its final results (so that they can be picked up by any other pipeline).</p><p>For example, let’s say we agree that <em>Pipeline A</em> will drop its results in <em>Location A</em>, so that <em>Pipeline B</em> knows that it should look for its input in such location. Each time <em>Pipeline A</em> runs, it must make sure it cleans its drop location (remove any previous results) and then store the new results in the same place.</p><p>This is, in many ways, better than our previous approach, but still not ideal as we don’t keep a history of the various pipelines’ results (which might be useful in some other moment or for some other pipeline). Nor does a pipeline have a way to differentiate complete results from partial results. Imagine the case in which <em>Pipeline B</em> starts running before <em>Pipeline A </em>has finished storing its results.</p><blockquote>By the way, this is a simple example of what we call <a href="https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-170-laboratory-in-software-engineering-fall-2005/lecture-notes/lec18.pdf">“the blackboard” architectural pattern</a>. The main idea is that<strong> we define “a blackboard” in which each “producer” (in this case a predecessor pipeline) writes down its results so that any “consumer” can pick it up at any given time that it needs to.</strong> In our case, we usually use <a href="https://aws.amazon.com/s3/">Amazon S3</a> as “the blackboard” and each pipeline defines a specific location for its results within the service, which is then shared with pipelines that need its output as input.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HFg_GCVNPTOkbXf5Vw3Duw.png" /></figure><p>So, how can we define a synchronization protocol in which we address or mitigate each of the drawbacks of the two approaches mentioned above?</p><h3>The File Feed</h3><p>We developed our own synchronization protocol for data pipelines, and named it the <strong>“File Feed”.</strong> This protocol is no more than a set of conventions that make it possible for a data pipeline to store its results in a way that any other pipeline can synchronize with it — i.e. use its output (and the correct output at any given moment in time) as input.</p><p>In order to comply with this protocol, a data pipeline must:</p><ul><li>Define a location for the feed</li><li>Upload results following the “new update convention”</li><li>Add an “update manifest” when the uploading of results is complete</li></ul><h4>Feed Location</h4><p><strong>To comply with the File Feed protocol, a data pipeline first declares a location in which it will store its results</strong>. An example of this location when using Amazon S3 as the blackboard could be <em>s3://my-bucket/my-prefix/</em>. A data pipeline also normally declares a current version, which, if available, will be used as the suffix for the location, resulting in something like <em>s3://my-bucket/my-prefix/v2.1/</em> as the full path to a feed. <strong>This is where the data pipeline should store its results, so that another pipeline can retrieve them and use them as input.</strong></p><h4>New Update Convention</h4><p><strong>Each time a data pipeline creates a new update in the feed — i.e. uploads a new set of results — the timestamp of creation of the update is recorded and used as the prefix for the path in which the result files are stored.</strong></p><p>For example, if we were to create a new update on May 16th 2018 at midday in a feed with location <em>s3://my-bucket/my-prefix/v2.1/</em>, then the results should be uploaded to <em>s3://my-bucket/my-prefix/v2.1/20180516.120000/</em>. This way we can keep a history of updates (or data pipeline run outputs) and still have a way to distinguish which is the latest, and to what specific moment in time those results belong to.</p><h4>Update Manifest</h4><p>Lastly, a <strong>data pipeline complying with the File Feed protocol should mark the created updates as complete when the upload of results is done</strong>. This is done by creating a file with name <em>_SUCCESS,</em> which should have as its content the number of files uploaded in the update — e.g. if we create an update and upload three files containing the results to it, then the <em>_SUCCESS</em> file’s content should be just <em>“3”</em> — and helps to distinguish those updates that are complete from those that are still being uploaded.</p><blockquote><strong>Note:</strong></blockquote><blockquote>If our “blackboard” is an <a href="https://en.wikipedia.org/wiki/Eventual_consistency">“eventually consistent”</a> data store — such as Amazon S3 — it is important that we include the number of files in the update as there is a chance that a consumer will only see a subset of the uploaded results even if the update is marked as complete by the producer.</blockquote><blockquote>If you don’t know what eventual consistency means, I would suggest you also take a look to the <a href="https://en.wikipedia.org/wiki/CAP_theorem">CAP theorem</a> to see why a distributed data store would make this tradeoff.</blockquote><p>This mechanism also comes in handy when there is the need to “invalidate” an update. One can either remove the <em>_SUCCESS</em> file or add an empty file to the update, and that would cause the update to fail to be considered as valid when being consumed.</p><h3>How To Use It</h3><p>Let’s say we want to use the File Feed to synchronize the three pipelines in the example described at the beginning of this post, the pipeline that cleans the ads shown to users, the one that cleans the ads clicked by users, and the one that uses the results’ from both to calculate the effectiveness of ads shown.</p><h4>Defining A Pipeline</h4><p>First, we need to define each pipeline and define a location for each pipeline’s result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LCaa90INn82l_s1eYoRWKw.png" /></figure><h4>Storing New Results</h4><p>Each time a pipeline needs to store new results, it should:</p><ol><li><strong>Create a new prefix inside the pipeline’s defined location with the current timestamp as name</strong> — i.e. in the case of the “Clean Ads Shown To Users” pipeline, if we were to store results for the ads shown to users on date <em>2018–05–20</em> and we were creating this update at <em>2018–05–21 16:00:00</em> we should create a new prefix <em>s3://my-bucket/ads-shown-to-users/v1.0/2018–05–20/20180521.160000/</em> in which we will store our results.</li><li><strong>Upload the results to the created prefix — </strong>i.e. If we had our results split into 3 partial csv files, we would upload them to <em>s3://my-bucket/ads-shown-to-users/v1.0/2018–05–20/20180521.160000/part-1.csv, </em><em>s3://my-bucket/ads-shown-to-users/v1.0/2018–05–20/20180521.160000/part-2.csv, </em><em>s3://my-bucket/ads-shown-to-users/v1.0/2018–05–20/20180521.160000/part-3.csv</em></li><li><strong>Upload the update manifest to the created prefix — </strong>i.e. In this case we should upload a new file with name <em>_SUCCESS</em> and content “3” to <em>s3://my-bucket/ads-shown-to-users/v1.0/2018–05–20/20180521.160000/_SUCCESS</em></li></ol><h4>Retrieving Results</h4><p>When a pipeline <em>A</em> needs to retrieve results from some other pipeline <em>B</em>, it should:</p><ol><li><strong>Get all the existing prefixes in pipeline <em>B</em>’s location — i.e.</strong> We should list all the prefixes in <em>s3://my-bucket/ads-shown-to-users/v1.0/2018–05–20/</em></li><li><strong>Sort these prefixes by their name,</strong> which is actually the timestamp in which they were created, in descending order</li><li><strong>Check whether the latest update can be considered valid</strong> by making sure the content of the <em>_SUCCESS</em> file matches the actual number of files in the update</li><li>Download all files in the latest valid update found</li></ol><h3>Simple data pipeline synchronization</h3><p>By implementing this protocol, it makes it very easy to synchronize data pipelines, and has proven to be very effective for our use case.</p><p>The fact that no additional components are required for tracking the outputs, or state, of different pipelines — such as a database containing the path to the latest results produced by a pipeline or a hive metastore — makes this solution a lot simpler than other approaches. And at the same time it is much easier to perform tests or human exploration of the data.</p><p>Versioning the feeds, even though not an enforced item in the protocol declaration (and instead is just part of the feed location definition), has also proven to be very effective in our case by protecting dependent pipelines from changes in the data definition contract of some of its predecessors until the pipeline is appropriately adapted.</p><p><em>Let us know if you found this post interesting and helpful, and if you use a different approach to deal with this same problem!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93443b501a4a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dataxutech/synchronizing-data-pipelines-93443b501a4a">Synchronizing Data Pipelines</a> was originally published in <a href="https://medium.com/dataxutech">dataxu.technology</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we implement Singletons in Scala at dataxu]]></title>
            <link>https://medium.com/dataxutech/how-we-implement-singletons-in-scala-at-dataxu-8ccba9e6dd59?source=rss-d06be6ffd88e------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ccba9e6dd59</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[software-design-patterns]]></category>
            <category><![CDATA[software-design]]></category>
            <category><![CDATA[singleton]]></category>
            <dc:creator><![CDATA[Javier Buquet]]></dc:creator>
            <pubDate>Mon, 15 Jan 2018 14:04:17 GMT</pubDate>
            <atom:updated>2018-01-15T14:04:17.010Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*6m-iOEM0-f6jP-yPsyUmXg.png" /></figure><p>At dataxu, we have been using <a href="https://www.scala-lang.org/"><em>Scala</em></a> for a while now, and in our journey we have perfected our best practices. Among these best practices, we have refined our implementation of the <em>Singleton design pattern</em> in Scala. In implementing this pattern there are some aspects that require significant attention to detail to increase its ease of use, especially when testing.</p><p>The <strong>Singleton design pattern,</strong> according to <a href="https://en.wikipedia.org/wiki/Singleton_pattern">Wikipedia</a>, <em>is a software design pattern that restricts the instantiation of a class to one object</em>. This means that this pattern promises to be handy when dealing with a class that we want to instantiate once, and only once, across a system (or, more generally, a subsystem), and have that same instance shared by every potential user of the class. For example, think about a <em>DatabaseConnection</em> class that handles the connection between the system and its database, it is reasonable to have only one instance of the class and make any database client use that same connection.</p><p>But the question is, <strong>how do we implement this Singleton design pattern in Scala</strong>?</p><h3>Implementing the Singleton design pattern in Scala</h3><p>If you have already given Scala a try, you are probably familiar with Scala’s <a href="https://docs.scala-lang.org/tour/singleton-objects.html"><em>Singleton Objects</em></a>. These are Scala’s approach to the <em>static</em> keyword in <em>Java</em>. Any function or variable defined in a <em>Scala Object</em> can be used statically, as there is one and only one instance of an object, which is not instantiated by any consumer, but just exists.</p><p><strong>Our first hunch when implementing the Singleton design pattern in Scala was to make use of the Singleton Objects.</strong></p><p>Let’s say we actually want to implement the DatabaseConnection class we mentioned above following the Singleton design pattern. This will allow different consumers to share the same DatabaseConnection instance for running commands on our application’s database. Using Scala’s Singleton Objects we can achieve this by doing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75af9d112f93ffe8a7e9dec345c33b83/href">https://medium.com/media/75af9d112f93ffe8a7e9dec345c33b83/href</a></iframe><p>And that’s all. We can now support different consumers running commands on a shared DatabaseConnection instance implementing the Singleton design pattern. For example, we could have a function somewhere else that retrieves all the <em>Users</em> in the database, as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1feb30cc5c7230744038dbed45a4c756/href">https://medium.com/media/1feb30cc5c7230744038dbed45a4c756/href</a></iframe><p>As you can see, there’s no need to instantiate a new DatabaseConnection nor to somehow keep a reference to it — we have direct access to the one and only DatabaseConnection instance just as if it was a static call in Java.</p><p>As we started using this approach in our code, we found it had some limitations that were important to note:</p><ul><li><strong>There’s no way one can easily swap the implementation being used.</strong> The fact that the user does not control the creation of this singleton instance — as it exists naturally in the program — makes it impossible to easily swap the implementation being used. It is common for us at dataxu to need a different implementation of our DatabaseConnection class when running our unit tests so that we don’t connect to the actual database but instead use an in-memory implementation of the class which is faster and cheaper. Using the Singleton Object does not allow us to swap the implementation of DatabaseConnection we want to instantiate as the singleton, but instead we would need to modify each and every consumer of the class to point to a different Object.</li><li><strong>Can’t be instantiated lazily.</strong> It might be the case that we need our singleton instance to be created lazily — only instantiate it once it has been requested for the first time by a consumer, not before. Again, as we don’t control the creation of this singleton instance, there’s no way we can do so lazily — it will be instantiated upon program execution.</li></ul><p>These limitations are a consequence of the fact that these Scala Singleton Objects are not meant to be used this way. These Objects are not a built-in implementation of the Singleton design pattern, but instead are Scala’s approach to Java’s static. Hence, we should not include in these Objects code that is intended to be used as non-static code accessed through a singleton instance, but instead we should only include static code.</p><h4>Perfecting our implementation of the Singleton design pattern</h4><p>Given the limitations of our first approach, we came up with a different strategy that could effectively implement the Singleton design pattern without sacrificing the ease to test and use, while still being clean and elegant.</p><p>We knew that we shouldn’t use a Scala Object, as it proved to not fit our needs. Instead, we should use a regular Scala Class and have some infrastructure for holding a single instance of it and making sure we only allow that one instance to be used.</p><p>By the way, this is similar to how we implement the Singleton design pattern in Java — we define a <em>class</em> and its functionality, and add static infrastructure for handling the singleton instance. In this case, Scala helps to make our code cleaner by more explicitly encapsulating the static code in a Scala Object.</p><p>Our code now looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5099f1b844c12771d254a156c86ca85/href">https://medium.com/media/e5099f1b844c12771d254a156c86ca85/href</a></iframe><p>And we can use the singleton instance this way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb2a43ecd3a769675983c6d3db849abe/href">https://medium.com/media/eb2a43ecd3a769675983c6d3db849abe/href</a></iframe><p>And <em>voilà</em> — we can now easily change the implementation of DatabaseConnection we want to instantiate as the singleton instance by just modifying the instantiation of the <em>val instance</em> in our <em>object </em>DatabaseConnection, as well as instantiating the singleton instance lazily just by adding the <em>lazy</em> keyword before the <em>val</em> for the <em>instance</em>.</p><p>This infrastructure can easily (and we think it should) be extended to allow programmatic swapping of the singleton instance being held. This means adding support for overriding the implementation being used, using a single line of code, which comes in very handy for solving the case mentioned above where we want to use a different implementation of the DatabaseConnection class when testing.</p><p>We decided to add this infrastructure to new functions to make our lives easier when testing:</p><ul><li>A <strong>force(…)</strong> function that receives an instance of DatabaseConnection and forces the singleton instance to be the one received.</li><li>A <strong>reset()</strong> function that puts the singleton instance back to its default (not-forced) one.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d49240afb60e0a311bd5502ddbea4a3b/href">https://medium.com/media/d49240afb60e0a311bd5502ddbea4a3b/href</a></iframe><h4>Generalizing our solution</h4><p>Once we were happy with our solution, and proved that it was an effective implementation of the Singleton design pattern in Scala, we tried to generalize our solution to make it as simple as possible for other classes using this infrastructure. We came up with a simple utility that any developer who needs to implement a Singleton class in Scala can simply extend and start using.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4839c4bbf742896e11cf7b7c8dad226b/href">https://medium.com/media/4839c4bbf742896e11cf7b7c8dad226b/href</a></iframe><p>And can be simply used as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01cd2e0db396144a709c86d0c206ae34/href">https://medium.com/media/01cd2e0db396144a709c86d0c206ae34/href</a></iframe><p>We also created a test base for our solution so that those who are using it can very easily have their <em>Singleton Instance Providers</em> thoroughly tested, just by extending this <em>trait</em> in their test classes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ddc37ffff07959731ad8019e0b36e9d/href">https://medium.com/media/4ddc37ffff07959731ad8019e0b36e9d/href</a></iframe><p><strong><em>What do you think of our solution? </em></strong><em>Do you think others would find it useful if we published this simple utility? How do you handle implementing Singletons in Scala? Please let us know! If you found this post useful, please feel free to “applause” and share!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ccba9e6dd59" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dataxutech/how-we-implement-singletons-in-scala-at-dataxu-8ccba9e6dd59">How we implement Singletons in Scala at dataxu</a> was originally published in <a href="https://medium.com/dataxutech">dataxu.technology</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>