Software Engineering in different perspectives

In the last few months, I have been exploring and experiencing (a.k.a playing) Cities: Skylines. For those who are not aware, Cities: Skyline is a simulated city-building video game. Although this game focuses on city planning, I found it interestingly sharing similar problems to what we deal with in Software Engineering.
In this article, we would be looking at Traffic, the similarities in its impacts, and approaches to solve the problems in both City Planning and Software Engineering. These problems arise due to handling high load while having resource constraints.
Let me show you how. But, please bear in mind that my knowledge with city planning is limited to exposure to the game and personal casual life experiences.
Traffic
Traffic unarguably is one of the biggest issues in Cities: Skylines or any cities in the world. The longer the time to get from A to B, the less efficient things can be run in a city.

Let's take the Economy and Healthcare aspects as examples.
Impacts on the Economy
If traffic is bad, raw materials cannot be transported in time. This leads to goods that cannot be produced in time. Commercials do not have enough to sell. People do not have enough to buy and consume. The economy collapses.
People start leaving the city. POOFS, the city is gone.
Impacts on Healthcare
If traffic is bad, it takes longer for ambulances to travel from hospitals to patients and get back. With a limited number of ambulances, longer round trip time means fewer people can reach hospitals.
Imagine there are 10 ambulances in a hospital. If all of them are stuck in traffic jams, no patient would be treated in time.
Or about deathcare, there are a limited number of hearses in each funeral company to transport the deads. If all of the hearses are stuck in traffic or take a long time to collect the dead, eventually, there would be enough dead bodies lying in buildings making the city unlivable.
People start leaving the city. POOFS, the city is gone.
Traffic problems in Software Engineering
The same challenge applies to software applications too.
The higher the latency in network calls resulted in longer response time to end-users' requests. Given a limited number of concurrent requests can be served, fewer requests can be handled, and the service would eventually reach denial-of-service (DoS).
Solving the traffic issues
There is quite a number of patterns we could apply from Software Design into City Planning.
But first, to solve traffic problems, we would first need to
- Understand where traffic comes from
- Identify hot spots
- And then solve the problems
Let's start!
The sources of traffic
In city planning

Traffic comes from people going to work or going for entertainment.
Traffic comes from trucks transporting raw materials to industrial zones, transporting goods from industrial zones to commercial zones.
Traffic comes from public services like healthcare, deathcare, firetrucks, police cars, …
In software engineering

Traffic comes from users requesting for webpages, APIs, videos, images, …
Traffic comes from analytics trackings.
In the context of microservices, besides traffic originated from end-users, there is a large amount of internal traffic happening due to communication between services.
Identify hot spots
I love making data-driven decisions.
It is pretty awesome that Cities: Skylines provides a set of tools for that. You can get more tooling from the modding workshops too.

Looking at the traffic analyzer, we would be able to identify the hot spots that need looking at. Most of the time, they would be industrial or commercial areas where a lot of trucks operating.
You can observe a lot of red areas around intersections, those are places vehicles making turns to their destinations. This means that, if there are a lot of vehicles going to the same destinations, making the same turns, traffic would be bad.
In software development
Great toolings would also help make our lives easier.

It is crucial to have your endpoints and system metrics monitored. Some of the common tools are SignalFx, Datadog, Prometheus, …
Monitoring would help to identify long-running requests or detect sudden spikes in traffic.
Now let's talk about some of the ways to solve the traffic problems.
Public transport vs. Batch requests
A bus can transport around 30 people. A metro train can transport around 150 people.
Without public transport, those 30–150 people using private vehicles would result in 15–50 times more traffic than a single bus or train.
In software engineering, a similar strategy is applied by batching network requests in larger batch requests. Each request has its own request headers and response headers. Batching requests help minimize bandwidth consumption thanks to avoiding the headers of each individual request.
It would also help significantly reduce the number of IO calls, i.e. reduce latencies when communicating between services.
Dividing high traffic zones vs. Sharding
As explained above, industrial zones are major sources of traffic due to several reasons
- Trucks delivering raw materials to factories
- Trucks delivering produced goods from factories to commercial zones
- Private vehicles of people coming for work
- Public services (especially firetrucks as factories being highly prone to fire)
- And, trucks are significantly bigger than normal vehicles, occupy more space when queuing, and take a longer time to turn or accelerate
Therefore, large industrial zones easily cause traffic jams on the main roads in-and-out the areas since even 5-lane-road would not be sufficient to handle such traffic.
One of the approaches to this problem is "Sharding".

"Sharding" means dividing large industrial zones into small distributed industrial zones. This would significantly reduce the load on the main roads leading into or leading out of the zones.
In software engineering

Sharding is used to divide giant data sets into smaller distributed data sets. It's a popular technique in database optimization.
Dedicated resources and routings for components
Let's revisit the ambulance example. The last thing you would like to is for AMBULANCES to be stuck in traffic with TRUCKS as they are serving different "stakeholders". Ambulances serve residentials, trucks do not.
It seems to be common sense to mix between residential areas and commercials areas. However, traffic would easily be mixed up between the two.
It is more efficient to split residential, commercial, and industrial zones to its own areas. Each of these zones would need dedicated roads leading from highways to them.
In software engineering
Similarly in software, it is not desirable for traffic from analytics collectors or asset serving to affect core Web/API servers.
Therefore, in practices, different domains are used for each of them to maximize browser concurrency since browsers limit concurrency per domain.
Different sets of servers are also used for each of the purposes. Web/API servers would be separated from Analytics collection servers. Assets would usually utilize CDN to optimize both delivery speed and traffic.
Conclusion
Those are just some of the aspects that I find interesting similar between City planning and Software design. What do you think about it? I would like to hear your thoughts.
Thanks for reading!
If you find this article helpful, you can buy me a beer here!