Although from a distance, this grey creature might look like a cute garden snake, it's not. This is actually a swarm of fungus gnat larvae. Their larval stage lasts about 2 to 3 weeks, during which they seek out fungus and algae to eat.
Why do the gnats swarm like this? It's a defensive technique. The gnats inside are less likely to be eaten by predators. So the strongest gnats are deep in the interior of the swarm and the weaker ones are on the outside. The Daily Mail explains:
This is when individuals within a population – in this case gnat larvae - attempt to reduce the odds of being eaten by putting others of their own species between themselves and predators, resulting in an aggregation, or swarm.
The theory was proposed by W. D. Hamilton in 1971 to explain the gregarious behaviour observed in a variety of animals, including insects, schools of fish and even herds of wildebeest.
‘The outer layer of larvae are more exposed than creatures towards the centre, so the less dominant ones are on the outside,’ she said.
The idea is that subordinate animals will be forced into higher risk positions and you can see the larvae changing places in the swarm as it wiggles along.