Using S3 versioning to sort PretzelBox emails

Sai Ramachandran
4 min readFeb 2, 2022

(tl;dr; — PretzelBox is a serverless inbox for AWS SES; emails in inboxes need to be LIFO sorted; sorting is done by leveraging S3 versioning)

Fundamentally, PretzelBox is an inbox for your domain.

The semantics of being an inbox dictate that emails be shown in descending order by date i.e., emails in inboxes are LIFO or equally, your inbox is a stack.

(The stack like behaviour of inboxes should give you a hint as to why we use the word STACK in our DSL::response)

Sorting of emails can be solved in two ways — the easy way and the hard.

The Easy Way

The easy way would be to put email metadata into a relational database, then use SQL’s sorting mechanisms to retrieve the data in the correct sort order.

Easy as ABC, right?

Easy as ABC, right?

Well,…wrong.

PretzelBox is fully serveless and is not backed by a database. At some point in the product journey, it was decided that we’d avoid using a database for as long as we could, probably for cost reasons, and now, I honestly don’t think a DB is necessary for where we are going.

The Hard Way

Before I proceed, I want to get it out there that hard here refers to the fact that I had to think a little bit about the features AWS provides out of the box, learn their consistency models, then implement some scaffolding code on top of it. The code itself isn’t very complicated to write.

So, without further delay, here’s how we are sorting inbound emails in LIFO order.

Our backend store for PretzelBox is S3.

S3’s default behaviour

In general, when you query S3 for a list of files under a directory (i.e., files having the same prefix), it returns the list of prefixes in ascending alphabetical sort.

The names of emails stored in S3 don’t have no set patterns so emails received one after the other could have wildly different names with no guarantees of “sortedness”.

--

--

Sai Ramachandran

Building https://squadgpt.ai - GPT for teams. Manage AI costs and retain visibility with SquadGPT. All views personal. Email = sai@squadgpt.ai