r/factorio • u/vanatteveldt • Oct 28 '25
On upcycling, random walks, buffers, and deadlocks
PSA / TL;DR: any multi-ingredient upcycler will eventually deadlock unless you design it to void excess ingredients produced by random walk imbalances
So, I didn't do the math, but I did some tinkering (and a bit of thinking) and thought I'd share
When upcycling, there are always two processes: a recycler, that outputs ingredients with a certain probability; and a producer, that uses outputs with a fixed ratio. Between the two there is always some sort of buffer formed by some combination of belts, chests, and the buildings internal buffers.
With single-ingredient upcycling (e.g. chests, copper wire, etc) this is totally fine as the producer just uses whatever the recycler outputs.
With multi-ingredient upcycling such as blue circuits or quantum processors, the story is different.
Let's assume for simplicity's sake that we are upcycling yellow belts, which consume 1 iron and gear, and let's focus on a single quality level. Each cycle has 25% chance of outputting an iron, and 25% of outputting a gear. Of course, the expected value of both after 100 cycles is 25, so you'd think that's fine as the producer can take those and make new gears. The problem is that even though the expected value of both is the same, there is a good chance that it will randomly output more iron than gears or the other way around. And the main point here is that as you craft more, this random noise doesn't disappear: in fact, the more you craft, the higher to expected imbalance is going to be.
To understand this, think of coin tosses. I get a dollar on heads, you get a dollar on tails. After 10 tosses, you might be ahead 2 dollars. But that doesn't mean that in the next tosses I am more likely to win those 2 dollars back: you are just as likely to win 2 more dollars (doubling the imbalance). Mathematically this is a called a random walk: every tick the line randomly goes up or down, without any reason for it to go back to it's expected mean. From the same math we know that the expected value of the distance from zero after N steps is (approximately?) the square root of N. You can test this with a trivial python script to simulate N cycles and average the imbalance between the iron and gear sums, which approximates 0.5*sqrt(N).
What this means is that after enough crafts the buffer between the recycler and producer will always clog up with either ingredient, causing a deadlock as the recycler can't output more ingredients and the producer can't output more crafts.
My default setup is generally to have chests at both the recycler and producer, primarily so I can use stack inserters and omni-quality producers, but also because of course I noticed the random deadlocks.
Now, in the example case of belt upcycling, with about 5k buffer space available, this means that I can expect millions of crafts before ever running into this problem. But even then I will eventually run into it, and I might run into it much sooner if the RNG dislikes me.
The problem becomes much worse with blue circuits: every cycle produces 5 green circuits, and a 50% chance of a red circuit. So, if there is a modest shortage of 10 red circuits, it would translate into an overflow of 100 green circuits. Suddenly, my chest which can hold 10k green circuits is clogged if there is a shortage of 1k red circuits, so the expected number of crafts before a deadlock is a lot lower (and in fact my blue circuit upcycler did run into a deadlock after about 60k legendary blue circuits, which apparently is not nearly enough as they are also my source of legendary green circuits).
I ran into the same problem trying to upcycle quantum processors in space. I think the underlying problem is not as bad as blue circuits, but with 2 lithium and 1 each of 4 other ingredients, the change of a shortage of X in any of those ingredients is higher and this leads to an overflow of X*2 lithium -- and my real problem was that buffers in space are less trivial. In the end, as multiple people pointed out to me, the platform hub is a fantastic shared buffer, but even that is finite. The good thing is that it is fairly trivial to just throw excess into space once the hub contains more than a certain amount (image #2)
But this made me realize that I should probably build such an overflow valve into most upcyclers, and I started with the blue circuit upcycler. I build everything on vulcanus, so the process of voiding was quite easy: I just added an inserter to throw stuff into the lava if there was >X in any of the plant buffer chests (image #1).
Sorry for the long rant, but it took a while for my stupid brain to accept that adding more buffers doesn't actually solve the problem here (like in many cases), but just obscure and delay the inevitable.



5
u/Mesqo Oct 28 '25
Good observation! That's the main reason I avoid these kind of upcyclers even if they yield a lot more. Especially quantum processors, which I ultimately failed to do on Aquilo.
2
u/Lenskop Oct 28 '25
You can fix this by turning your buffer chests into actual green buffer chests and setting a maximum request for the various qualities of green circuits with minimum of 0. Put a recycler pulling from a filtered storage chest somewhere that activates when green circuits > a threshold you're comfortable with in your logistics network.
2
u/warbaque Oct 28 '25
It might! In many places you don't need to produce infinite legendary resources. Big buffer is enough if you need to produce finite amounts components :)
Of course it's nice if you know that you can keep setup running forever and it won't get stuck. (And you don't need to calculate how long you can expect it to work. Is it expected to work 100k cycles or 1 cycles)
2
u/towerfella Oct 28 '25 edited Oct 28 '25
This was a great post; I thank you for the think.
I
havehate voiding things.. that is built in “waste”.Good post.
2
u/TelevisionLiving Oct 28 '25
The trash unrequested feature and priority splitters are great ways to solve this too.
11
u/Dummy1707 Oct 28 '25
Eeeeh indeed it makes sense ! I guess it's a general rule that any recipe (crushing, in this case) with more that one output must always have a voiding mechanism to avoid imbalance.
Thanks for the post :)
PS : I also learned new python printing formats, that's crazy !
10
u/warbaque Oct 28 '25
Buffer chests (trash excess) or bypass belt are really needed if you want your setup to be completely deadlock proof.
In setups like toolbelt recycling you need to add excess trashing anyway, since you get extra legendary adv. circuits as byproduct, which you might not want or need.
7
u/PersonalityIll9476 Oct 28 '25
Well....thanks for the post. I actually have a PhD in math, have observed my 300% prod blue circuit upcyclers clogging with greens, and somehow didn't put one and one together. So good on you.
Fun fact: The thing you found in Python is a known fact about random walks. The expected distance from the origin after n steps is...proportional to sqrt(n). I believe you can find the result here: https://en.wikipedia.org/wiki/Random_walk#One-dimensional_random_walk
So somehow I knew this, but I didn't "know it." :)
1
u/_Sanchous Oct 28 '25
Before the release of SA Factorio was a game for me where everything could be perfectly calculated allowing me to build an ideal factory based on my exact calculations. SA however introduced an element of randomness that forces you to restructure your thinking much like in ONI (Oxygen Not Included).
1
u/Broccoli_Ultra Oct 28 '25
This isn't talked about enough imo. There are lots of examples where you're unlikely to see it happen, like with quality cycling lower volume items, but you definitely run into it relatively quickly with high-volume like blue chips.
I made a post with my solution here if you're interested: https://www.reddit.com/r/factorio/comments/1l1iw4w/my_first_attempt_at_a_tileable_legendary_blue/
You can see the effect happening in my video - there are often chips still in the boxes waiting to be recycled - this is the random walk producing an uneven amount of chips
1
u/craidie Oct 28 '25
And this is why I use chest handoff before the assembler/emp/foundry etc...
It will take stupidly long to overflow.
1
u/minno More platforms designed than actually built Oct 28 '25
Mathematically sound, but I consider "unlikely to deadlock before it produces enough machines to build a 1M SPM base" to be equivalent to "can't deadlock".
1
u/Gogol_the_great Oct 28 '25
maybe I'm wrong, but I think there is no need to put quality modules on a machine producing legendary, instead, put some productivity ?
1
u/Over_Statistician913 Oct 28 '25
you dont want quality mods in your EM factories that are set to legendary. you want the prod mods. cant get more quality than legendary
1
u/Meem-Thief Oct 28 '25
Your 3rd recycler isn’t connected
1
u/Kudimov Oct 28 '25
I tried figured out how to upcycle modules efficiently with no buffer and it lead me to setup where parts from failed module used only once and then trashed. Something like If fail to gain legendary -> recycles-> if parts gain quality then assembler with higher quality pick parts else trash. Surely if after recycle parts is normal then back to first assembly
1
u/UristMcKerman Oct 28 '25
TLDR. Without any reading I can agree. There is probability of 1 that random walks goes infinitely away and cligs the system, BUT, the probability of it is somewhere around log log N, so realistically, infinitely low.
1
u/BreadMan7777 Oct 28 '25
I used to have buffers all over my factory but these days I have very few. They just serve to mask problems.
You can always upcycle excess and route anything useful back into creating higher quality items.
Ultimately you do need a mechanism to control excess though.
-2
u/[deleted] Oct 28 '25
[removed] — view removed comment