A Consul Locking Library in Elixir

Andrew Mager ♫
Postmates
Published in
2 min readMar 20, 2017

On the Postmates engineering team, we’re quickly becoming an Elixir shop.

One of our new Elixir tools is called Bazaar, which lets our sales team onboard partners more efficiently.

A secret internal tool that we use to onboard merchants. Sweet gradient right?

Bazaar also handles a few other tasks behind the scenes, including a nightly email to merchants that lets them know which inventory is out of stock.

A daily inventory report sent to merchants. Time to re-stock the bubbly.

We decided to send this email as a nightly cron job task but there was one problem: we have two instances of Bazaar running, so we needed to make sure we didn’t send the email twice.

In order to avoid duplication of this work, we needed to obtain a lock across multiple hosts. We thought about using a database lock, but our infra team guided us towards Consul’s KV store. When we looked for Elixir locking libraries using Consul, there wasn’t a good choice out there.

At the end of the day, we decided to write our own solution that worked with Consul and also supported adding other backends, including ets, Erlang’s in-memory database.

It was important that we open-sourced it. Today, I’m proud to unveil ConsulMutEx to the world: https://github.com/postmates/consul_mut_ex

It’s super simple to use:

Besides the daily inventory report, we also use ConsulMutEx to acquire a lock on our place onboardings table to ensure that no other task is performing the same operation.

Although Elixir locking libraries may not be the sexiest thing in the world, we’re super excited to open source what we’ve built, and see what the developer community does to evolve it.

This seems like a perfect opportunity to let you know that we’re hiring for just about every type of engineering position you could imagine: front-end, back-end, mobile, data scientists, engineering managers, you name it.

We’ve also recently formulated our open-source policy, so keep an eye out for more projects from our team very soon.

As for this library, we’d love for you to fork it, send us a pull request, and let us know if you find any issues. Happy locking!

Pop and lock. Credit: Giphy.

--

--

Andrew Mager ♫
Postmates

Full Stack Engineer at @Uber. Blogger turned coder. Music charms the soul. On Twitter: @mager.