Article Image

What do Software Engineering and Factorio have in Common?

by Ivan Chebykin 13th May 2023

There is an unique kind of popularity of the factory-building simulators like Factorio among software engineers. This always fascinated me because they manage to make engineers do the same job during their free time and enjoy it.

But what makes sitting for hours, building and scaling a factory so appealing? Maybe there is something that we can learn from Factorio to make our day-to-day engineering better? With that in mind, let's try to model a service in Factorio and we'll see!

Creating a playground world

So, let's grab a copy of the game and start it! We'll create a sandbox world, because it is easier to mock input and output resource flow, instead of building all mining and electricity infrastructure from scratch, we're here for science, not for gaming!

After you created a sandbox world, open the console (yes, even here we need to run code) and add some infinity chests:

/c game.player.insert{name="infinity-chest", count=50}

They will act as a input resource flow, i.e. hardware and output representing users who are calling our service.

Also get "electric energy interface" for infinite electricity:

/c game.player.insert{name="electric-energy-interface", count=50}

Building a simple version

In Factorio there is a variety of things you can build a factory for, and some complex items would take a great deal of time to put all components together, so let's take something moderately simple as a model for service - green circuits.

Hardware

When we thinking of running the service, first thing that comes to mind is getting enough compute resources and hosting provider to run it. Same here, for green circuits we need to provide raw resources on which most of the Factorio items are built: iron and copper plates.

requirements

Let's just imagine them as our CPU and RAM. To begin, put an infinity chest with a manipulator to the conveyor to generate iron and copper like this:

initial-hardware

Service logic

Alright, so after we've got hardware to run the service, it's time to implement the logic to process copper and iron plates to green circuits.

According to the recipe, copper plates are processed to wires and together with iron they should be passed to the circuits assemblers, so let's do that:

initial-logic

Creating handler

After wiring all assemblers we need to connect the output conveyor to the infinity chest or, in other words, calling the service logic in handler and from the client.

initial-handler

Now we can see that green circuits are flowing to the output chest at a regular pace.

initial-handler2

One thing that we can immediately notice here, is how simple it is to connect stuff, here it is just a matter of putting manipulators and conveyors, no need for writing a ton of configuration and then sitting and debugging integrations, but I digress, let's carry on.

Scaling

Let's say that suddenly people need more circuits. To simulate that let's add more output chests and improve the consumption logic by using so-called balancers which will evenly distribute resources from the conveyor to the chests:

scaled-handler

Now we see that all circuits are flying through the conveyor and nothing is piling up, we can see it as if the distance between different green circuits is like request time and it got too big. Now, let's scale the circuits to fix that.

Blueprints

Factorio has a nifty way to reuse the design called blueprints. A player can copy and paste the area of the factory using simple CTRL+C/CTRL+V shortcuts. Before doing that, add yourself a power armor with a reactor with a portable roboport, and add around 50 construction robots, the armor layout should look like that:

example-armor

This will allow you to automatically construct copied blueprints, or in other words, do code generation, think of it if you could just run some command to add service and it would just generate everything for you.

Scaling the logic

Having blueprints we can easily horizontally scale our logic by copying and pasting individual circuit production units, and as we connect more of them we can see that more circuits are flowing to the output:

scaled-logic

But wait, now there isn't enough copper.

Scaling the hardware

If copper is a bottleneck then we can do the same thing as we did with the output and add more chests to scale it.

scaled-hardware

Conclusion

What we essentially done here is everything that average engineer does during their job. But the tools that Factorio provides allow us to just horizontally scale bottlenecks and connect conveyors.

Factorio's blueprint library acts as a GitHub for factories and allows you to just paste the optimized solution wherever you want. Imagine if you could just copy some recommendation algorithm to your app and connect it to users as simply as placing conveyors.

With Mify we're aiming for that kind of development. We want to generate away all the glue and friction of integrating things, check it out, maybe it can make your development as fun as Factorio.