# Scala algorithm: Monitor success rate of a process that may fail

Published

## Algorithm goal

In a distributed system, we wish to monitor processes which may succeed or fail; if a process fails too often, we should kill it. Implement an algorithm or data structure that, based on a set of observations of successes and failures, can tell us whether the process needs to be killed.

In software in practice, this is called flapping, where a service keeps on dying or not performing in some way; it could be due to a memory leak for instance, and such a service can recover by killing it and restarting it.

Assume the following inputs:

1. A # of target successes ($$a$$)
2. A # of attempts minimum ($$b$$) (meaning, success ratio is $$a / b$$ once $$b$$ attempts are reached; as we'd otherwise have 100% failure rate if we only considered one attempt)

## Test cases in Scala

assert(
!SuccessMonitor(targetSuccesses = 4, outOfAttempts = 5).succeeded.shouldKill
)
assert(
!SuccessMonitor(
targetSuccesses = 4,
outOfAttempts = 5
).succeeded.failed.shouldKill
)
assert(
!SuccessMonitor(
targetSuccesses = 4,
outOfAttempts = 5
).succeeded.failed.succeeded.shouldKill
)
assert(
!SuccessMonitor(
targetSuccesses = 4,
outOfAttempts = 5
).succeeded.failed.succeeded.failed.shouldKill
)
assert(
SuccessMonitor(
targetSuccesses = 4,
outOfAttempts = 5
).succeeded.failed.succeeded.failed.succeeded.shouldKill
)
assert(
!SuccessMonitor(
targetSuccesses = 4,
outOfAttempts = 5
).succeeded.succeeded.succeeded.failed.succeeded.shouldKill
)
assert(
!SuccessMonitor(
targetSuccesses = 4,
outOfAttempts = 5
).failed.failed.shouldKill
)


## Algorithm in Scala

17 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!

## Explanation

This is a State machine, which enables us to represent transitions in an immutable fashion. While the algorithm is fairly straightforward, the way to implement it in Scala in the neatest way is using an immutable Queue combined with pattern matching: basically, we dequeue from a list of results only when we had reached the # of attempts $$b$$. (this is Â© from www.scala-algorithms.com)

## Scala concepts & Hints

1. ### Pattern Matching

Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.

assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")


# Scala Algorithms: The most comprehensive library of algorithms in standard pure-functional Scala

## How our 100 algorithms look

1. A description/goal of the algorithm.
2. An explanation with both Scala and logical parts.
3. A proof or a derivation, where appropriate.
4. Links to Scala concepts used in this specific algorithm, also unit-tested.
5. An implementation in pure-functional immutable Scala, with efficiency in mind (for most algorithms, this is for paid subscribers only).
6. Unit tests, with a button to run them immediately in our in-browser IDE.

### Study our 100 Scala Algorithms: 6 fully free, 100 published & 0 upcoming

Fully unit-tested, with explanations and relevant concepts; new algorithms published about once a week.

### Explore the 22 most useful Scala concepts

To save you going through various tutorials, we cherry-picked the most useful Scala concepts in a consistent form.

## Subscribe to Scala Algorithms

Maximize your Scala with disciplined and consistently unit-tested solutions to 100+ algorithms.

Use it from improving your day-to-day data structures and Scala; all the way to interviewing.