Safely adding a new background job for our new project at Framer

Safely adding a new background job for our new project at Framer

Safely adding a new background job for our new project at Framer

The code was the easy bit. How we sequenced infra, config, code and scheduling so a new background job could land in production safely.

The code was the easy bit. How we sequenced infra, config, code and scheduling so a new background job could land in production safely.

Queue, config, code, then schedule

Categories

Engineering

Last week at @framer I was adding a background job to poll our analytics engine @tinybird, get the latest results and store them.

Pretty standard backend stuff and honestly, the implementation itself wasn’t particularly difficult: essentially what we wanted to do was call our analytics platform on a schedule, ingest some data and store it into a database. Easy, right?…

But like most things in software engineering, and honestly most things in life, it’s never quite as simple as it first appears once production systems are involved.

The interesting part wasn’t the code. It was the sequencing.

Because once systems become distributed the challenge becomes:

How do we introduce change safely?

People usually imagine software shipping as:

  • open PR

  • merge PR

  • deploy feature

  • done

But in reality a single “feature” often becomes a sequence of smaller independent changes landing in a very specific order.

For this particular change we needed some infrastructure to be in place, so you might think:

Why not just deploy the app first and add the infra later?

Which sounds completely reasonable, except the application needs to know what queue to listen to (it’s essentially just a url) as an environment variable at deployment time.

Because we run on AWS ECS and validate environment variables during startup, we can’t really just deploy it and hope the infrastructure appears later.

If the queue doesn’t exist yet, then..

  • the env var can’t be populated correctly

  • the application config becomes invalid

  • containers fail startup validation

  • deployments fail

Which honestly is a good thing in most instances, failing early is significantly better than silently half-working in production.

Sooo, now we’ve got a chicken-and-egg problem: The application needs the queue to exist but we also don’t want messages flowing before consumers are healthy.

So how do we solve that?

We separate our infrastrure code, app code & activation of the scheduling

That’s really the trick here, nothing fancy or over-engineered, just plain and simple sequencing!

Instead of thinking:

deploy feature

…we break need to think:

progressively move the system into the new state safely

So the rollout becomes staged, safe keeping the blast radius of any one change isolated (we hope!) and small.

What does this look like in practice?

PR 1: Queue setup (think of it like a checkout line in a supermarket)

First we create the queue, At this point we setup our infrastructure so that our queue now exists so that we know it’s URL. At this point, nothing writes to it, and it’s not hooked up to our app.

Safe and totally isolated (yes, we could have altered our app’s environment variables, but that breaks the whole idea here about being safe and isolating change)

PR 2: Application configuration

Now that we know the URL of our queue, we can now add it as an environment variable and ensure that our app has permissions to consume and manage the queue’s contents

With this, our app starts as it did before, just with a new environment variable - nothing changes to our app at this point.

Again, still pretty safe…

PR 3: Application code & deployment

Now, we add support for what we want to happen with our when we consume a message - in reality this would could be several smaller PRs but for the purpose of this article, let’s assume that everything lands in a single PR & deploys automagically

With this, everything is working right? Success!

Not quite, because we’ve not actually configured anything to produced to the queue, so again - a safe deployment that doesn’t in reality affect our apps runtime behaviour!

Now we can manually produce a message, validate that everything is working and prepare for the final PR

PR 4: Enable scheduling

Finally we enable the scheduler/producer side and start introducing traffic into the system.

Now jobs actually start flowing and because our app already has the magic in place and we have a healthy service:

  • messages process immediately

  • backlog of messages stays controlled

  • rollout risk stays low

That’s really the whole point here, not complexity for the sake of complexity, no over-engineering, just careful, important but “boring” orchestration.

It Is also important to note, that designing and building software this way also allows you to easily restore or revert a “bad” change, which is critical because eventually, you will mess up.

The actual feature here was simple, but coordinating the operational rollout around the feature is the thing that mattered.

Hopefully this helps explain some of the hidden challenges behind “simple” software changes. And this isn’t even going into how we test things at scale with lots of moving parts!

The code is usually the easy bit; safely introducing change into a live system is the real engineering work we all love to solve @framer!

Originally posted on X.

Say hi

Building something fun? Let’s talk.

Got a project or an idea? I’d love to hear about it.

Contact

Cal Morris

Staff Engineer @ Framer, working on the Framer Community

Say hi

Building something fun? Let’s talk.

Got a project or an idea? I’d love to hear about it.

Contact

Cal Morris

Staff Engineer @ Framer, working on the Framer Community

Say hi

Building something fun? Let’s talk.

Got a project or an idea? I’d love to hear about it.

Contact

Cal Morris

Staff Engineer @ Framer
working on the Framer Community