<?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 Bruno Cosso on Medium]]></title>
        <description><![CDATA[Stories by Bruno Cosso on Medium]]></description>
        <link>https://medium.com/@bcosso?source=rss-fb82a32d294a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Uvs8uYlWZi5VSrGC</url>
            <title>Stories by Bruno Cosso on Medium</title>
            <link>https://medium.com/@bcosso?source=rss-fb82a32d294a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 08:08:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bcosso/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[How I created an In Memory SQL database]]></title>
            <link>https://medium.com/@bcosso/how-i-created-an-in-memory-sql-database-eb9974ce98ca?source=rss-fb82a32d294a------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb9974ce98ca</guid>
            <category><![CDATA[in-memory-database]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Bruno Cosso]]></dc:creator>
            <pubDate>Thu, 29 May 2025 16:39:04 GMT</pubDate>
            <atom:updated>2025-05-29T16:39:04.003Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*vQUKiQ8tn16YGdYlO-B5fA.png" /></figure><p>I’ve been working with database systems since 2004 — if you consider SQL Server 2000 a proper database 😉. Over the years, I’ve worked with a wide range of systems: relational OLTPs, OLAP engines, document stores, row-based NoSQL, and even file system-based storage like HDFS.</p><p>At some point, I asked myself a simple but exciting question:</p><p>“What if I could build a low-latency, in-memory, distributed database?”</p><p>That question turned into a personal challenge — and eventually, a working system.</p><h3>🧠 Design Goals</h3><p>I gave myself a few constraints and goals:</p><p>✅ Must support document-style data (JSON)</p><p>✅ Any node should be able to receive and coordinate a request</p><p>✅ Must support replication and/or sharding</p><p>✅ Should be low-latency and in-memory</p><p>✅ Should be fun to build</p><h3>🛠️ Why Go?</h3><p>Initially, I considered both Rust and Golang. Rust is powerful, but at the time, Go had:</p><ul><li>Better documentation</li><li>More mature tooling</li><li>Easier concurrency primitives</li><li>More helpful error messages</li></ul><p>So I chose Go — and I’m glad I did. It allowed me to move fast and focus on architecture.</p><h3>🧩 What I Built</h3><p>Over time, I implemented many features inspired by production-grade databases:</p><h3>🔧 Core Features</h3><ul><li>Write-ahead logging (WAL) — for durability and recovery</li><li>Mutex-based concurrency control — using sync.RWMutex</li><li>RSocket communication — for low-latency, async messaging between nodes</li><li>Table-level sharding — for horizontal scalability</li><li>Replication and fault recovery — including hot node recovery</li><li>Impala-style node coordination — any node can act as a query coordinator</li><li>SQL-like parser — with logical tree evaluation and join support</li><li>Rust-based HTTP load balancer — handles client requests and forwards them via RSocket</li></ul><h3>🧪 What Surprised Me</h3><ul><li>Writing the system was surprisingly fun</li><li>Even more surprising: it worked!</li><li>Not having expectations made it easier to enjoy the process and learn deeply</li></ul><h3>📚 What’s Next?</h3><p>This is the first in a series of articles where I’ll dive deeper into specific parts of the system:</p><ul><li>How I built the SQL parser</li><li>Sharding Implementation</li><li>Write ahead logging</li><li><a href="https://medium.com/@bcosso/shared-memory-and-request-isolation-664b37446f5d">Shared Memory and request isolation</a></li><li>Lessons learned from building a distributed system solo</li></ul><h3>🙌 Final Thoughts</h3><p>This project started as a curiosity and turned into a full-fledged system. It’s not perfect — the code still needs polishing — but it’s real, it runs, and it taught me more than I expected.</p><p>If you’re interested in distributed systems, in-memory databases, or just building something from scratch, I hope this inspires you to start your own “what if” project.</p><p>GitHub link:</p><p><a href="https://github.com/bcosso/nimpha">https://github.com/bcosso/nimpha</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb9974ce98ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Shared memory and request isolation]]></title>
            <link>https://medium.com/@bcosso/shared-memory-and-request-isolation-664b37446f5d?source=rss-fb82a32d294a------2</link>
            <guid isPermaLink="false">https://medium.com/p/664b37446f5d</guid>
            <category><![CDATA[in-memory-database]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Bruno Cosso]]></dc:creator>
            <pubDate>Thu, 29 May 2025 16:37:35 GMT</pubDate>
            <atom:updated>2025-05-29T16:41:33.154Z</atom:updated>
            <content:encoded><![CDATA[<p>In my previous article I introduced my experience with building an In Memory Pet Database from scratch. In this article I will detail a bit my experience with handling user requests per node while maintaining a main table object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*vQUKiQ8tn16YGdYlO-B5fA.png" /></figure><p>Databases have to deal with concurrent requests accessing a single source of truth. At the same time, those requests are unique and have their own context, many times showing completely distinct visions of that source of truth (i.e. one query from one user can show completely different data from a different query from another user).</p><p>For that, we need the data to be shared but the queries to be isolated, like in the drawing below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/0*VoTs4T3fqp4T8J1c" /></figure><p>To accomplish that, the memory table needs to be a singleton instance, while the query table needs to be divided per request, in the case of my pet project, Nimpha, in the stack. Let’s start with the memory table.</p><p>In order to maintain the single source of truth, data needs to be written to and read from an atomic source. That source in Nimpha is a memory object. But multiple users and requests also mean asynchronous concurrency, meaning risks of locking the object while another request is being performed, causing the database to crash. That is a common problem in concurrent systems and most of the programming languages implement a mechanism to deal with multithread shared memory, which is the Mutex. Nimpha is written in Golang, which possesses a nice and relatively simple concurrency architecture, and also interesting features like separate Read/Write locks. This allows to perform multiple simultaneous reads but just one write at a time. If more than one write attempt is being performed, it is queued until the resource is unlocked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*acUXxy0ZM_3ZNx5elQYRNA.png" /></figure><p>In the above example, an insert adds a new row while locking the memory table (mt) object.</p><p>On the query side though, each request needs to carry its own memory. For Nimpha design the best way was via a context variable. That allows us to use the memory for the request with the stack, in an isolated manner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*ypxJT--HurCwKpslA8bZXg.png" /></figure><p>The GitHub link to the project:</p><p><a href="https://github.com/bcosso/nimpha">https://github.com/bcosso/nimpha</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=664b37446f5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RSocket: Webserver-like wrapper in Golang]]></title>
            <link>https://medium.com/@bcosso/rsocket-webserver-like-wrapper-in-golang-8cddf1f99659?source=rss-fb82a32d294a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cddf1f99659</guid>
            <dc:creator><![CDATA[Bruno Cosso]]></dc:creator>
            <pubDate>Mon, 21 Nov 2022 13:47:04 GMT</pubDate>
            <atom:updated>2022-11-21T13:47:04.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uOOhILOf5F5znLwxWegr-Q.jpeg" /></figure><p>Recently I’ve learned about an elegant form of socket implementation, the RSocket. I have always liked lower level development, and I was looking for a low latency networking solution for a pet project. This project requires distributed processing and the communication is currently implemented over HTTP. Sockets run on level 5 (session), on top of TCP or UDP, on level 4 (Transport) of the OSI model. Compared to HTTP, on level 7 (Application), the additional abstraction layers result in overhead: the stack of encapsulated protocols increases the usage of bandwidth and processing. RSocket, as an extension of the traditional sockets, targets distributed architecture and is recommended for Microservices architecture with need for low latency and asynchronous reactive messaging.</p><p>The aforementioned pet project, a simple in-memory database, is developed in Golang, so I naturally looked for a Golang implementation of RSocket (<a href="https://pkg.go.dev/github.com/rsocket/rsocket-go">https://pkg.go.dev/github.com/rsocket/rsocket-go</a>).</p><p>However, I couldn’t find the capability of attaching methods on the server side and calling for routes on the client side, like it’s done in consolidated web servers in Golang (e.g. Gorilla). The solution I found was to create a wrapper to perform those calls:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*IZHQnWOuC56_hZZNHEvUlw.png" /></figure><p>To achieve a generic method signature on the server side, like in Gorilla, I used a generic interface containing JSON as parameter to the method, which returns another generic interface with the same type of content.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*NPSpyJDvc7LgawNJhnAeqA.png" /></figure><p>On the client side, a list object was created to be sent to the server, with the name of the method created on the server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*kZO4dL4VksxLuMqKV1OnPw.png" /></figure><p>This approach allows me to use RSocket in a similar way as Gorilla.</p><p>As to what’s inside the wrapper, it includes the code to perform the RSocket connection and encapsulation of the json payload in another json “envelope”, so we can identify the method it will trigger on the server side:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*a9d0_HVcN2oDlF28fQzKNg.png" /></figure><p>If anyone is interested in making it a more complete project, the git repo is the following:</p><p><a href="https://github.com/bcosso/rsocket_json_requests">https://github.com/bcosso/rsocket_json_requests</a></p><p><em>About the Author</em></p><p><em>Bruno is a Brazilian programmer since 14 years old, worked in companies like Ericsson, Fujitsu and Credit Suisse. Now he works as a Solutions Architect @ Rabobank, in the Netherlands. In the spare time plays videogames and tries not to be so annoying with his friends and family.</em></p><p><a href="https://www.linkedin.com/in/bruno-marcondes-a60a424/"><em>https://www.linkedin.com/in/bruno-marcondes-a60a424/</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cddf1f99659" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Impala Daemon Remote from HDFS]]></title>
            <link>https://medium.com/@bcosso/impala-daemon-remote-from-hdfs-df38494ed8a9?source=rss-fb82a32d294a------2</link>
            <guid isPermaLink="false">https://medium.com/p/df38494ed8a9</guid>
            <category><![CDATA[hdfs]]></category>
            <category><![CDATA[apache-impala]]></category>
            <dc:creator><![CDATA[Bruno Cosso]]></dc:creator>
            <pubDate>Tue, 23 Jun 2020 08:39:26 GMT</pubDate>
            <atom:updated>2020-06-23T08:39:26.841Z</atom:updated>
            <content:encoded><![CDATA[<p>Before we get started there is a good reason to put Impala next to the data and if you are patient enough we will get there ;-). I started writing this at the beginning of the article because we have always in literature about Impala the statement “install Impala Daemon on each data node…”.</p><p>But isn’t impala a direct reader of HDFS? How is that possible to read from HDFS without a local Daemon?</p><p>To explain what have been done, I’d better explain a little more on the HDFS, MapReduce and Impala Architecture.</p><p>I was working for a company until not a long time ago with a deployed cluster of nearly 100 machines and we have faced a need to move Impala. I cannot go into much detail, but the soft limits of resource consumption of Impala were not avoiding conflict with other component’s resource consumption. That caught my attention as the Impala architecture should be able to read the data and process it in a remote node.</p><p>Impala came to life in order to substitute the analytics through MapReduce, one of the most criticized feature of Hadoop. The whole idea is to substitute that batch architecture of MapReduce, by a more sophisticated C++ engine, which will consume data directly from HDFS for analytics.</p><p>But what that means? Well, Impala will consume the HDFS services of DataNode. That means that, if there is no Short-Circuit read (which means to retrieve the data locally, when there is an instance of local HDFS and the referred data is in that node) you can read using impalad remotely, like demonstrated below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/0*UztClLAnF0cx6l3l" /></figure><p><em>Cloudera VM with HDFS</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/0*UOqZSrqmkp9Ulrlz" /></figure><p><em>Ubuntu VM with Impala</em></p><p>For my test I have used the Cloudera VM as the DataNode. I have also created another VM with Ubuntu and installed Impala in it. There are some points to make it feasible:</p><ol><li>hdfs-site.xml, hive-site.xml, core-site.xml and hbase-site.xml must be pointing to the remote machine (in my case, the Cloudera VM)</li><li>References to dfs:// will be changed to hdfs:// so the Impala Daemon knows the data is distributed.</li><li>The reference for short-circuit readings in hdfs-site.xml must be set to false as we are not doing any.</li><li>The ImpalaD of DataNode does not need to be running.</li></ol><p>Why would we put Impala always in the same node as Data? Avoid network latency and enable Short-Circuit Reads makes your data retrieval way faster, for sure!</p><p>When is the “remote impalad” a feasible configuration?</p><p>Well, imagine that you have 20 DataNotes with a replication factor of 3 (standard in Hadoop 2). If every single node has Impala in it, when you run a query there will probably be data off this node. So we will be in a point that Networking will happen anyways if they are in separate bare metal. Now imagine that Impala Daemon is running along with other critical services in the same machine, using their resources. To avoid memory and CPU competition in this case, moving the Impala Cluster outside can be a feasible solution.</p><p>[Edit: Thanks to Eric Leme for the review</p><p><a href="https://www.linkedin.com/in/ericleme/">https://www.linkedin.com/in/ericleme/</a>]</p><p><em>About the Author</em></p><p><em>Bruno is a Brazilian programmer since 14 years old, worked in companies like Ericsson, Fujitsu and Credit Suisse. Now he works as a Big Data Engineer @ Itility, in the Netherlands. In the spare time plays videogames and tries not to be so annoying with his friends and family.</em></p><p><a href="https://www.linkedin.com/in/bruno-marcondes-a60a424/"><em>https://www.linkedin.com/in/bruno-marcondes-a60a424/</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df38494ed8a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Complete Stream Environment (Go + Kafka + Spark + Deltalake)]]></title>
            <link>https://medium.com/@bcosso/complete-stream-environment-go-kafka-spark-deltalake-333bc04f6890?source=rss-fb82a32d294a------2</link>
            <guid isPermaLink="false">https://medium.com/p/333bc04f6890</guid>
            <category><![CDATA[pyspark]]></category>
            <category><![CDATA[delta-lake]]></category>
            <category><![CDATA[dev-environment]]></category>
            <category><![CDATA[kafka-streams]]></category>
            <dc:creator><![CDATA[Bruno Cosso]]></dc:creator>
            <pubDate>Tue, 23 Jun 2020 08:30:26 GMT</pubDate>
            <atom:updated>2020-06-23T08:30:26.342Z</atom:updated>
            <content:encoded><![CDATA[<p>Due to the increasing number of Microservices and IoT scenarios, we often come across the need to have a complete streaming environment to perform analytics.</p><p>The use cases are numerous: from mobile devices and self driving vehicles, to microservices logging and tracing. A simple way of imagining streams is a sequence of tiny messages, usually in order of kilobytes, with no determined end.</p><p>There is very good literature out there explaining what is streaming in deeper details and how Kafka and Spark work and help handling streams. My objective with this article is to show a good architectural design, using them both in what they are best suited for. Also, this is as an environment agnostic architecture as it can run either on the cloud or on-prem, and these building blocks can be easily substituted by PaaS offerings of your favorite cloud service.</p><p>The reference architecture is considering microservices, written in Golang and I am considering that this must be a very performatic service, writing directly into the Kafka stream. For the purpose of this article we are going to skip the the microservices containers orchestration, which could be added on top of this architecture with Kubernetes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*CV5hNC_FNHpWmlrB" /></figure><p>The Go program writes some dummy messages to a Kafka stream:</p><p>for {</p><p>topic := “my-topic”</p><p>partition := 0</p><p>conn, _ := kafka.DialLeader(context.Background(), “tcp”, “localhost:9092”, topic, partition)</p><p>conn.SetWriteDeadline(time.Now().Add(10 * time.Second))</p><p>conn.WriteMessages(</p><p>kafka.Message{Key: []byte(“Key1”), Value: []byte(“One!”)},</p><p>kafka.Message{Key: []byte(“Key2”), Value: []byte(“Two!”)},</p><p>kafka.Message{Key: []byte(“Key3”), Value: []byte(“three!”)},</p><p>kafka.Message{Key: []byte(“Key4”), Value: []byte(time.Now().String())},</p><p>)</p><p>conn.Close()</p><p>fmt.Println(“Infinite Loop”)</p><p>time.Sleep(time.Second*10)</p><p>}</p><p>Kafka is a high performance and fault tolerant platform, which will persist those streams in disk. In case it fails it is possible to continue the execution as the data will not be lost. The retention time can vary according to the configurations, the default is 7 days.</p><p>So Kafka, at this point, centralizes the streams coming from different sources. From that point we will probably want to do something “smart” with these streams. Let’s perform some analytics with it. Here is where Spark comes into the scene.</p><p>In the next code block, we retrieve the Kafka messages and process it to an initial table that can be later explored further. The transformation is done in PySpark and can add intelligence from simple calculations to Machine Learning.</p><p>df = spark \</p><p>.readStream \</p><p>.format(“kafka”) \</p><p>.option(“kafka.bootstrap.servers”, “127.0.0.1:9092”) \</p><p>.option(“subscribe”, “my-topic”) \</p><p>.load()</p><p>df.selectExpr(“CAST(key AS STRING)”, “CAST(value AS STRING)”)</p><p>df.selectExpr(“CAST(key AS STRING)”, “CAST(value AS STRING)”).writeStream.format(“console”).start()</p><p>As the last step, we persist the stream into a Delta Lake as follows.</p><p>The Delta Lake adds a transactional layer on top of our semi-structured data and It adds reliability and “time travel” capabilities to the data (which is a cool name for versioning).</p><p>stream = df.selectExpr(“CAST(key AS STRING)”, “CAST(value AS STRING)”).writeStream.format(“delta”).option(“checkpointLocation”, “/home/bruno/data-files/kafka_topic/checkpoint/”).option(“basePath”, “/home/bruno/data-files/kafka_topic/”).start(“/home/bruno/data-files/kafka_topic/”)</p><p>Once the data is in the Delta Lake, we can transform it accordingly and create value with it.</p><p>Please, notice that I have created it directly in the OS file system for this article. In a Real Life scenario we would use HDFS instead (or a cloud service). But for learning purposes we can use our own file system, without having to install HDFS and spark in a single development machine.</p><p>[Thanks to fellow programmer Lucas Araujo for review!</p><p><a href="https://www.linkedin.com/in/lucaslra/">https://www.linkedin.com/in/lucaslra/</a> ]</p><p><em>About the Author</em></p><p><em>Bruno is a Brazilian programmer since 14 years old, worked in companies like Ericsson, Fujitsu and Credit Suisse. Now he works as a Solution Architect @ Itility, in the Netherlands. In the spare time plays videogames and tries not to be so annoying with his friends and family.</em></p><p><a href="https://www.linkedin.com/in/bruno-marcondes-a60a424/"><em>https://www.linkedin.com/in/bruno-marcondes-a60a424/</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=333bc04f6890" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>