**The Idempotence Principle in Software Architecture**

197 reads

by Alex ShevtsovMay 8th, 2024

- We’ll describe the
**problem-solving thought process**by an engineer, based on a real-world problem. And how to approach this process more systematically.

- How the ability to draw
**analogies from other areas of knowledge**can help in this process. And what**mental models**have to do with it.

- Based on a specific example, we will see how even basic mathematical knowledge can help solve practical problems.

- And we will find out how IMDB sorts its top movies list :)

So, we need to automate the process of assigning hotel rooms for cleaning. This is a vital operational process, as a poor cleaning plan can lead to service issues and decreased guest loyalty.

Here are a few situations of varying criticality that a hotel should avoid:

- Guests arriving in the evening should not have to wait for their rooms, which were vacated that same morning, to be cleaned.

- The rooms of VIP guests should be cleaned as a first priority, even if the hotel is fully booked and there are many check-outs and check-ins today.

- Personal requests from guests for cleaning today should not be missed.

Keeping all these rules in mind and arranging hundreds of rooms in the correct order for cleaning is not an easy task. Let's help the room manager!

After the introduction above, we could rush to write code with conditions like "if a VIP guest checks in..." We could try to solve the problem a bit more abstractly so that the logic of setting priorities could be adjusted. We could arm ourselves with machine learning tools. And in different contexts, each of these strategies may be more optimal than others.

There is a helpful principle that helps to approach problem-solving more systematically. We'll call it "First, take a step up.” If we elaborate, it is **"investigate the problem, understand what to solve, comprehend possible solutions, implement the most suitable one"**

This principle is not invented out of thin air. Many known decision-making and problem-solving models propose this exact approach, for example, the double diamond model from the British Design Council and the human-centered design from IDEO.

So, let's start with enriching the context.

Each room can have several features that affect the priority of cleaning. A room may have multiple features at once. The importance of features varies.

We arm ourselves with the breadth of our knowledge, Google, and ChatGPT. Let's try to compile a list of similar tasks from other subject areas and understand the general patterns of their solutions.

A key parameter (like the percentage of page crashes) is affected by another parameter (the number of views of this page), which is used as a "weight" when sorting by statistically significant values.

The formula for calculating the value looks like this:

```
S = (F2 / F2_max * F1) + ((1 - (F2 / F2_max)) * F1_avg)
F1 - the metric of interest to us
F2 - a secondary parameter determining statistical significance
```

```
S = (w1 * F1) + (w2 * F2) + (w3 * F3)
```

A simple sum of products of parameters and their weights.

In CSS, different types of selectors are assigned fixed weights. This allows very flexible styling at the level of HTML elements, classes, IDs, or at the level of the final DOM element. For example, it allows you to describe common styles for all <input> elements once, and describe differing details at the level of specific elements in specific places on the site or application.

About specificity, Specificity calculation example

Lucene uses the TF-IDF (Term Frequency - Inverse Document Frequency) technique to assign weights to each word in each document. Each text document is represented by a TF-IDF vector of weights for each word. Queries to the engine are also represented by this vector. In N-dimensional space, angles between document vectors and the query vector are calculated. Results are sorted by the size of this angle.

https://stackoverflow.com/questions/1411199/what-is-a-better-way-to-sort-by-a-5-star-rating

```
weighted rating (WR) = (v ÷ (v+m)) × R + (m ÷ (v+m)) × C
R = average rating of the movie
v = number of votes
m = minimum number of votes required to be in the Top 250 ranking (about 25,000)
C = average rating among all movies in the rating (about 7.0)
```

All of the above examples used a **vector calculation model** to obtain a sorting/ranking function. We came up with a specific function for calculating the scoring sum of each element and a logic for choosing coefficients to account for the weight of each parameter affecting the result.

Another path we could take is to use a **machine learning model** for "predicting" scoring sums. This solution strategy may be helpful when we can't develop a clear and simple formula, when the system has many more parameters, or when parameters and their influence can change over time or depending on input data.

Decision trees, random forests, gradient boosting, and neural networks - we can resort to these techniques. However, we will not delve into them in this article, as our case does not seem complex enough to be solved in this way.

Another essential principle when making decisions - finding the right balance. Let's find a good balance between the **complexity, accuracy, and flexibility** of the algorithm.

Suppose we have determined that the priority of a room for cleaning depends solely on its parameters and does not depend on the status of other rooms or other external circumstances. In this case, the example of calculating CSS specificity seems most closely related in concept.

```
S = (w1 * F1) + (w2 * F2) + (w3 * F3)
```

Great, we've chosen a solution algorithm! Now we must understand the logic of assigning the weight to each parameter.

We delve deeper into the subject area, asking ourselves: how important for the cleaning ranking of a room is each of its parameters - the fact of a VIP guest's stay, the fact that the current guest will be leaving the room today, and the next one will be arriving, etc.

Suppose, we have established two points:

- There is always a strict order from a more important characteristic to a less important one, but this can differ between hotels.

- If a room has a more important feature, then even the simultaneous presence of all other features does not make cleaning more critical (and this is true for each feature). For example, if the room has been vacated and is expecting a new arrival, cleaning must be done there first, even if another room has a VIP guest who has hung a "please clean the room" sign.

From the first point, we can decide that it would be beneficial for the hotel to have a customizable list of priorities where they can set their order. Also, this means that we can't just write the logic from nested conditions, we have to calculate the weight of each parameter.

The second point will allow us to select the correct function to assign weights, using the sacred knowledge of mathematics, namely, the understanding of function growth.

If one room has the first feature and the second has all the other features, the first room should still be ranked higher. This means that the sum of all weights, starting from the second one, should be less than the first feature. And this is true for each position.

It seems we need a strictly monotonic, increasing function. Moreover, we could take a power function to guarantee that each of its values is greater than the sum of the previous ones (and it will be since raising to the power of 2 is already the addition of the number N - N times, and this will yield a larger sum than the addition of N numbers that are less than the current one).

Not straying too far, let's take the function 2^x.

1 2 4 8 16 32 - each number is greater than the sum of the previous ones

- take the cleaning influencing features in order
- assign them weights based on the function we found
- calculate the total weight for each room
- sort the rooms by weight
- you're ready to start cleaning the rooms 🧹

Sometimes, people say, "You don't need to know [insert any discipline] to be good at programming." The problem-solving process in this article can serve as an example of the opposite thesis - **the more mental models you can gather from different disciplines and learn to apply, the more complex they can assist you in solving problems.**

Design thinking model "double diamond" or the theory of inventive problem solving can help to correctly identify the problem and not get stuck in straightforward solutions.

Being well-versed in different fields and the ability to search for information can help expand your perspective on the problem and see patterns in solving similar tasks.

Knowledge of mathematics often comes in handy when designing systems (the idempotence principle when designing information systems deserves a separate article). Basic knowledge about the growth of functions helped us quickly choose the right feature weights for sorting. Machine learning is inextricably linked with statistics.

Know your tools, and enjoy your problem-solving process!

L O A D I N G

. . . comments & more!

. . . comments & more!