<?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 David Blooman on Medium]]></title>
        <description><![CDATA[Stories by David Blooman on Medium]]></description>
        <link>https://medium.com/@dblooman?source=rss-46fee7f621b3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gT93un8l1pVpxiQ-2eSqVg.jpeg</url>
            <title>Stories by David Blooman on Medium</title>
            <link>https://medium.com/@dblooman?source=rss-46fee7f621b3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 12:20:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dblooman/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[The Golang Zero Value Trap: How pgx v5 Exposed Silent Corruption]]></title>
            <link>https://medium.com/@dblooman/the-golang-zero-value-trap-how-pgx-v5-exposed-silent-corruption-198b8683b6df?source=rss-46fee7f621b3------2</link>
            <guid isPermaLink="false">https://medium.com/p/198b8683b6df</guid>
            <category><![CDATA[pgx]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[David Blooman]]></dc:creator>
            <pubDate>Mon, 29 Dec 2025 12:11:12 GMT</pubDate>
            <atom:updated>2025-12-29T12:11:12.465Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Xik_GOYWLlWt8Ivd9A0Gg.png" /></figure><p><strong>TLDR</strong>: A pgx v5 migration didn’t introduce a bug, it exposed 18 months of silent data corruption caused by a Go map zero-value lookup and time.Time{} being treated as valid.</p><p>Migrations are rarely just about swapping versions. Sometimes, they act as a wake up call, revealing cracks in your system that you didn’t even know were there.</p><p>We recently upgraded our Go PostgreSQL driver from <a href="https://github.com/jackc/pgx"><strong>pgx v4</strong> to <strong>v5</strong></a>. We expected a few compilation errors and maybe some performance tuning. instead, we got a production incident.</p><p>The upgrade didn’t cause a bug, it revealed a logic error that had been silently corrupting our data.</p><h3>The Context: pgx v4 vs. v5</h3><p>To understand the incident, you have to understand what changed in the codebase</p><p>In pgx v4, timestamp handling was largely implicit. Passing a Go time.Time value, including its zero value, would be encoded and sent directly to Postgres. A zero-value time.Time{} serialises to 0001–01–01 00:00:00.</p><p>Postgres considers this a valid timestamp, so the write succeeds without complaint.</p><p>In pgx v5, performance and type safety are handled via the pgtype package. V4 relied on the database/sql package. With the pgtype package, while very similar, it required migration of our types. <a href="https://sqlc.dev/">SQLC</a> made this easier, but we still needed to handle conversions, like using pgtype.Timestamptz.</p><p>To handle this case, I wrote a helper function</p><pre>func TimeToTimestamptz(t time.Time) pgtype.Timestamptz {<br>    if t.IsZero() {<br>        return pgtype.Timestamptz{Valid: false}<br>    }<br>    return pgtype.Timestamptz{Time: t, Valid: true}<br>}</pre><p>If we attempt to insert an invalid timestamp, we are going to get an error, which is what we want. When we run our test suite, it should be obvious if we are doing this.</p><h3>The Incident: “Null Value Violates Constraint”</h3><p>Shortly after releasing to production, we saw errors spiking on the “Save Floorplan” endpoint. At <a href="https://dojo.tech/virtual-queues-bookings/">Dojo, I work on our booking </a>system. This has a floor plan layout for storing the layout of a restaurant. A fixture is a series of X &amp; Y coordinates indicating the floorplan as shown in our web app.</p><pre>ERROR: null value in column &quot;created_at&quot; of relation &quot;fixtures&quot; violates not-null constraint</pre><p>The created_at column is NOT NULL. Our code was designed to preserve the existing timestamp during an edit. We weren&#39;t trying to insert NULLs, or so we thought.</p><h3>The Logic Bug</h3><p>We traced the error to a loop where we update fixtures. Here is the logic</p><ul><li>Load existing fixtures from the DB into a map.</li><li>Iterate through the user’s request.</li><li>If the fixture ID exists, grab its original created_at from the map to preserve it.</li></ul><p>Here is the simplified version of the buggy code:</p><pre>if f.FixtureID == uuid.Nil {<br>    f.FixtureID = r.uuidGen() // It&#39;s new<br>} else {<br>    // BUG: accessing a map without checking for existence<br>    createdAt = currentFixtureIDs[f.FixtureID].CreatedAt.Time<br>}</pre><p>You might ask: <em>if the UUID wasn’t nil, why wasn’t there a </em><em>created_at?</em></p><p>Elsewhere in the code, the UUID was being set before this logic ran. This code assumed that “UUID exists” meant “row exists in the database”. That assumption was wrong. When the fixture wasn’t in the map, the lookup silently failed.</p><h3>The Go Gotcha</h3><p>In Go, accessing a map with a missing key does <strong>not</strong> panic. It returns the zero value for the value type.</p><p>For 18 months the code silently returned time.Time{}</p><ul><li><strong>Before the migration:</strong> pgxv4 took0001-01-01 00:00:00date and wrote it to the database. The database accepted it happily.</li><li><strong>After the migration:</strong> Our new helper saw the zero date, flagged it as Valid: false, and sent NULL to the database. Postgres correctly rejected it.</li></ul><p>The half glass full engineer in me might say, everything in the migration was fine, we just found a bug. Still an incident though isn’t it?</p><h3>Incident: The Data Trap</h3><p>We thought fixing the logic map lookup would solve it. We deployed a fix that ensured we only read from the map if the key actually existed.</p><p>Enter page number 2.</p><p>When we ran our tests, we created a bunch of new fixtures with valid timestamps, then updated them and the new tests passed. At this stage though, we masked the bug, because the difference between our tests and production was that for 18 months we had been inserting zero timestamps.</p><p>Our database was full of rows where created_at was 0001-01-01. This created a loop that the code couldn’t handle.</p><ul><li><strong>Read:</strong> The app reads a fixture from the DB. Postgres returns 0001-01-01.</li><li><strong>Process:</strong> The app tries to preserve this date for the update using our map logic</li><li><strong>Write:</strong> The app passes 0001-01-01 to TimeToTimestamptz.</li><li><strong>The Bug:</strong> The helper sees IsZero() is true. It converts it to NULL.</li><li><strong>The Result:</strong> Postgres rejects the update because created_at cannot be NULL.</li></ul><p>Even though the code logic was fixed, the production data was breaking the process.</p><h3>The Fix</h3><p>We updated our logic to treat invalid historical data as missing. If we encounter a zero value timestamp from the database, we don’t try to preserve it, we overwrite it with Now(). This is a little trust but verify scenario, but in most cases, the data was bad. We don’t mind using the current datetime because the data was already broken, so at the very least, we now had usable data.</p><pre>func resolveFixtureCreatedAt(defaultTime time.Time, existing queries.Fixture) time.Time {<br> <br> // If the existing value is valid, preserve it<br> if existing.CreatedAt.Valid &amp;&amp; !existing.CreatedAt.Time.IsZero() {<br>  return existing.CreatedAt.Time<br> }<br><br> // Fallback: If data is missing OR corrupted, use Now.<br> // This &quot;repairs&quot; the row on the next write.<br> return defaultTime<br>}</pre><h3>Check the Database</h3><p>For 18 months, our system was writing corrupt data, and nobody noticed because nothing failed.</p><p>If a dependency upgrade <em>introduces</em> bugs, don’t rush to patch around it.<br>Ask what the old version was quietly tolerating.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=198b8683b6df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Migration: A Depop story]]></title>
            <link>https://engineering.depop.com/aws-migration-a-depop-story-1444e9aaad31?source=rss-46fee7f621b3------2</link>
            <guid isPermaLink="false">https://medium.com/p/1444e9aaad31</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[containers]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[aws-ecs]]></category>
            <dc:creator><![CDATA[David Blooman]]></dc:creator>
            <pubDate>Mon, 02 Jul 2018 14:03:28 GMT</pubDate>
            <atom:updated>2018-07-02T20:40:48.081Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*BAvnDq9NOYHwKkr4coJITg.jpeg" /></figure><p>Recently at Depop, we completed a 5 months project to create infrastructure and move several key components of our platform from <a href="https://www.heroku.com/">Heroku</a> to <a href="https://aws.amazon.com/">AWS</a>. While I call it a migration, in reality, we went well beyond just moving code and getting the bare minimum of infrastructure in place. We introduced new tools and technologies, wrote lots of code and radically changed the way the code goes from laptop to production. In essence, <strong>we set the foundation for the next generation of our platform</strong>, and with this blog post I want to share some of our main learnings.</p><h3>Overview</h3><p>Prior to this project we were using Heroku to run our largest application, a dozen of asynchronous workers and a bunch of scheduled tasks, all written using Python.</p><p>The plan was to <strong>move this entire ecosystem into AWS</strong> using:</p><ul><li>Docker to package all applications.</li><li><a href="https://aws.amazon.com/ec2/">AWS Elastic Cloud Compute</a> (EC2) to run the main application.</li><li><a href="https://aws.amazon.com/ecs/">AWS Elastic Container Service</a> (ECS) to run workers and tasks.</li><li><a href="https://aws.amazon.com/lambda/">AWS Lamba</a> to execute various tasks.</li><li><a href="https://aws.amazon.com/kinesis/">AWS Kinesis </a>and <a href="https://www.fluentd.org/">Fluentd</a> for logging.</li><li><a href="https://www.datadoghq.com/">Datadog</a> for monitoring.</li><li><a href="https://www.consul.io/">Consul</a> and <a href="https://www.vaultproject.io/">Vault</a> to host configs and secrets.</li></ul><p>All provisioned using <a href="https://www.terraform.io/">Terraform</a>.</p><p>In the following paragraphs I will go through:</p><ul><li>How we overcame some of the limitations in <strong>ECS</strong>.</li><li>How we decided to run the main application on <strong>EC2</strong>.</li><li>How we implemented <strong>load balancing</strong> to distribute the traffic coming from client apps and other services.</li></ul><h3>Elastic Container Service</h3><p>ECS is almost overshadowed by the giant that is Kubernetes, however ECS has a lot of functionality that makes using containers really easy… most of the time.</p><p>In fact, one of the limitations we encountered is that <strong>ECS doesn’t indicate when a container fails</strong>. You may be saying that in the AWS console you can clearly see when a task/service is failing, and that is true. However there are no metrics to alert and this is clearly something we could not afford.</p><p>My former colleague <a href="https://github.com/kenoir">Robert Kenny</a> first indicated this problem to me and he shared a Lambda function that stores events for Welcome Trust deployments. This is triggered by a Cloudwatch event rule, which gives you full access to the ECS data. From here you can see state changes, failed containers, exit codes, error messages etc. (find the source code <a href="https://github.com/wellcometrust/terraform-modules">here</a>).</p><p>This approach inspired our solution and what we built in the end is a Lambda that looks for exit codes from our scheduled tasks to ensure that we get a notification if the exit code is 1. Whenever we see a failure, we send a custom metric to Cloudwatch, which then has an alarm on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y1kTnA9enKThoZuMvhZ8ZA.png" /><figcaption>Slack message for stopped tasks</figcaption></figure><p><strong>The second quirk we found with ECS is scaling, but not container scaling, EC2 scaling</strong>. I will admit, Fargate solves a lot of the issues we faced and perhaps at some point we will move to Fargate, but as a first step we wanted to run as many containers as possible without too much optimisation.</p><p>In order to do that, you need to ensure that you’re not going to run out of compute/memory, or run out of the ability to schedule an container. In ECS you can use different placement strategies, such as binpack, spread or distinct instance. If you use distinct instance and you run out of instances, what happens? Well, nothing, ECS will sit there trying to place your container, but will fail. We needed a better way of handling this, but also tackling the issue when deploying new versions of our containers while scaled up. Lets say we need to place 70 containers, but we are only running 15 instances and we have 100 containers already running, how can we scale correctly? We once again, went to AWS Lambda.</p><p>We will soon publish a new blog post that covers the implementation of this lambda and the scaling, so watch this space!</p><blockquote>AWS recently introduced daemon scheduling, a great way to bootstrap your cluster with all the containers you need, logging, metrics etc. We didn’t have this initially, so we had to come up with workarounds. Thanks to daemon scheduling, we can now reduce a lot of boilerplate that was on our instances. I suggest <a href="https://aws.amazon.com/about-aws/whats-new/2018/06/amazon-ecs-adds-daemon-scheduling/">you take a look</a></blockquote><p>In spite of the issues we encountered we really think we were right to choose ECS for this migration. In fact it was very easy to get going even if we had to write some custom tooling to get the feedback we needed. Compared to say, Kubernetes, the barrier to entry is very low and we were able to carry across much of our AWS knowledge in terms of security groups, IAM, VPC’s, Cloudwatch and EC2.</p><h3>The main application on EC2</h3><p>Our main Python application handles a significant portion of our traffic. As you can see from the chart below it handles between 1500-2000 requests per second depending on time of the day.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c1nSHa2wnebBQ5_AQ4uqyQ.png" /><figcaption>All traffic on the API over one day by minute</figcaption></figure><p>With this in mind the last thing we wanted was to increase the already high level of risk involved with the migration and therefore we chose a simple approach.</p><p>The application is packaged using Docker, so our idea was to <strong>run a single Docker image on an instance</strong> and have it utilise as much of the hardware as possible. We didn’t want to go to the Packer and baking AMI’s route, we use userdata and upstart to manage the docker processes.</p><p>This approach may seem odd, why not use ECS for this as well? Like I said, we wanted to keep things simple and given that we deploy this API once a week, this approach felt the simplest possible. We do install some utilities like datadog on the instance itself, but that’s it in terms of “native” packages.</p><blockquote>We have 2 other containers on the instance that actually run on our ECS cluster too, a pgbouncer and fluentd container. Our pgbouncer container is there to ensure that if the application restarts, it doesn’t overwhelm the database it’s attached to with connections. The Fluentd container is where all our of logs go, this uses the kinesis plugin from AWS to send all our logs in JSON format to Kinesis before processed in a Lambda, more on that later.</blockquote><p>I would have to say, even though the approach of using upstart to run a container works, I would probably use ECS for this if I were doing it again. I think at conferences, people suggest going very minimal on orchestration, pushing aside ECS and Kubernetes for systemd and upstart. After doing it though, what you have in simplicity, you lack in visibility. You are much more reliant on logs to tell you whats going on, rather than the ECS console, often with only one way of figuring out what your container exit code is, SSH.</p><h3><strong>Load Balancing</strong></h3><p>We initially began our work on the API with a combination of Elastic Load Balancer (ELB) and autoscaling group. This was a simple enough strategy, but ultimately we ended up using two Application Load Balancers (ALB) and a single autoscaling group, and this is why.</p><p>Once we turned on some traffic, we started seeing increased 504’s coming from the single ELB. After some investigation, we thought that the source of the problem was pre-open connections (an AWS ELB feature) combined with our usage of uWSGI. We asked AWS to disable this feature but that made things worse.</p><p>AWS suggested that we switch to an ALB for a couple of reasons:</p><ol><li>ALB doesn’t use pre-open connections.</li><li>It also doesn’t handle the closure of backend connections, this will be entirely on the backend.</li></ol><p>We followed this suggestion and the issues disappeared!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u7BGaGAQN7SZhUDZgSjFFw.png" /><figcaption>Request count of our internal ALB increasing as we switch services over</figcaption></figure><p>Why two ALB’s though? We have a public and private API, the private API is between services, specifically between our current Scala services and our migrated Python API. The networking for this setup is complex as our Scala services run in one AWS account while our Python API runs in another (did I mention we are building a new platform?).</p><p>If we were to run the traffic from the Scala services over the public load balancer, it would mean opening more security group rules, instead, an internal ALB was much more preferable. It allows traffic from a specific CIDR range from the peered AWS account and means one less hop.</p><blockquote>We found that using AWS Athena to process ALB access logs was a fast and convenient way to discover the errors. If you need a quick way to get tabular data from your logs without having to download or sending the logs to another service, Athena should be your go to service.</blockquote><h3>Conclusion</h3><p>Once we solved the issues with the load balancer, we moved onto the migration phase, which we completed in 2 weeks as we slowly increased the traffic starting from the public API and closing with the private.</p><p>We learnt a lot during this project, both about new technologies and tools, but also about the infrastructure we had been using for years.</p><p>One aspect about a migration that is non technical is team momentum. Have clear sprint goals, targets and keep progressing at the same pace. It’s very easy to leave a migration for a while if there are more interesting tasks to be done.</p><p>I think we achieved much more than we set out to do. We took a big project and made it even bigger, but ultimately we reduced our costs, improved the platform and most importantly, had no downtime!</p><h3>We’re hiring!</h3><p>You can check out our open roles <a href="https://www.depop.com/about/jobs">here</a> and follow our <a href="https://www.linkedin.com/company/2347595/">LinkedIn page</a>. If you have a background in engineering but don’t see the perfect role for you, or just want to pop by our offices for a chat and a tour get in touch: work@depop.com</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1444e9aaad31" width="1" height="1" alt=""><hr><p><a href="https://engineering.depop.com/aws-migration-a-depop-story-1444e9aaad31">AWS Migration: A Depop story</a> was originally published in <a href="https://engineering.depop.com/">Engineering at Depop</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>