Swipe Right on Redshift
Tags: redshift, postgres, todayilearned, database
Published: 2022-08-25 16:30:00

As a “top pick” in the direct mail automation space, Lob prints and sends millions of mailpieces each year. As such, it's been Lob's commitment from the get-go to minimize our footprint and build a more sustainable approach to direct mail. As a part of this initiative, in addition to ensuring we use responsibly sourced raw materials, **we plant two trees for every one that we use in the production of our mailpieces, through our partnership with [Eden Reforestation Projects](https://www.edenprojects.org/)**.  (For more on our partnership check out this blog post/video: [“The Power of Trees: Partnering for Positive Environmental and Community Impact.”](https://www.lob.com/blog/sustainability-lob-eden-reforestation-projects-partnership))

As awesome as that is, this is not a sustainability story, it’s about the technology we use to accomplish this and why we shifted (excuse the pun) tools.

During a hackathon in 2017, our Engineering team developed automation that runs every month. This involves a Postgres query that pulls how much mail (letters, checks, postcards, etc.) Lob sent the previous month, a calculation the equivalent in paper, which is then translated into a number of trees. (Then we offset our carbon footprint by sponsoring Eden Reforestation to plant double that number of trees.)

Snippet of query:

{%gist https://gist.github.com/Meussdorffer/f6b91c8e21d1fdca7446e484cbe587a2 %}

The result is a summary communication sent via Slack to the community and stakeholders.

![slack message showing donation to Reforestation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5elpcgvc7gvj6ghfchc3.png)

Unfortunately, as of late, **the automation was pretty flaky**: often the query would time out requiring an engineer to run it again in off-peak hours when the Postgres database was under less load;  this is not a scalable solution and was yielding less and less success.

Since the query looked fine (accurate), an initial idea was to break it out into separate queries and then combine the results. But this and some other brainstorms just seemed like a hack; sure they would fix the current problem but _eventually,_ those would get big enough to timeout too. So, Platform Engineer Elijah Voigt turned to the rubber ducky channel.

![reforestation query is failing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsmw2shve7wn7x1dy8f4.png)

Enter Jack Meussdorfer from the Data team to the rescue.

His keen sense of smell indicated this was a too much/big data problem. He suggested instead of running the query against [PostgreSQL](https://www.postgresql.org/), we run it instead against [Amazon Redshift](https://aws.amazon.com/redshift/), which the Data team already uses for similar data pipelines.

In short, Postgres is a transactional db, whereas Redshift is specifically designed for large-scale data storage and analysis. Because Redshift stores data in a different way—it uses column stores instead of row stores—there is less overhead on queries spanning a large selection of data. (This also means differences in how constraints and indexes are implemented.)  Redshift also has a higher capability of processing large amounts of data because it runs [massively parallel processing](https://docs.aws.amazon.com/redshift/latest/dg/c_challenges_achieving_high_performance_queries.html#massively-parallel-processing) (MPP). If you want a deep dive check out this article [Redshift vs. Postgres: Detailed Comparison of Performance and Functionality](https://www.integrate.io/blog/redshift-vs-postgres/).) 


![little mermaid brushing hair with fork](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y80hhsy7p7kydmmpgyrr.jpeg)
_Is that really the right tool for the task?_

Since the necessary data already exists in Redshift, Jack suggested we: 

1. Update the query to be compatible with Redshift (alter the query syntax and add the proper schema/tables) and then
2.  Point the Reforestation query at Redshift.

Boom!** Lifting and shifting the data source from Postgres to Redshift solved the problem immediately: the query went from timing out after 1 hour to literally taking 40 seconds.**

The takeaway? Rubber ducky rules. And, it’s important to evaluate whether you are using the right tool for the job at hand. Postgres was the right tool for the job in 2017, but our data changed—which means our problem changed. While we are [big fans of Postgres](www.lob.com/blog/kickstart-your-engineering-book-club), our automation needed to scale to our new needs, so for this important query, it’s Redshift FTW.