Elevator Control System using ZIO

Wiem Zine
Wiem Zine
Sep 23, 2018 · 4 min read

Modern Elevator system

Image for post
Image for post

Scenario: The passengers in a floor “a” choose their destination “b” then the elevator system decides which elevators are convenient to serve their requests and the passengers will be guided to the assigned elevator.


  1. Every elevator has its state which contains the current floor, and a set of the next stops.
  2. The elevators will be moved step by step to their destination, and every step takes a duration depending to the elevator speed.
  3. A passenger send a Pickup request on the floor “a” to a destination floor “b”.
  4. the system will search which elevator is the nearest to “a” and is moving at the same direction to the floor “b”.
  5. The chosen elevator will add in its stops the floor of the pickup request and its destination and continue to move step by step to the pickup request floor.
  6. If the elevator is at its next stop floor, this stop should be deleted from the elevator state.
  7. The request should be deleted when the elevator arrive to the requested floor.

Data Structure

It’s clear that we have:

  • ElevatorState that contains floor and set[Stop].
  • PickupRequest that has floor and destination.
  • ElevatorSystem that has elevators and requests.

It seems like this:

case class ElevatorState(floor: Int, stops: Set[Int])
case class PickupRequest(floor: Int, destinationFloor: Int)
class ElevatorSystem(elevators: List[ElevatorState],
requests : immutable.Queue[PickupRequest])

But wait !

  1. we need to preserve the elevators state and to update the current floor in every step also every step takes a duration.
  2. For requests, I thought about a Queue because when the elevator consumes the first request that request would be removed from the queue but there are those problems:
  • If two or more persons send the same pickup request and they are on the same floor at the same time and they are waiting together, while the closest elevator is not yet arrived, a search process should be executed for the concurrent requests.
  • We need an asynchronous queue because every request will wait until finding a close elevator to pick up.
  • Remember that the elevators should take always the pickup requests to add new stops. But what if there is no requests and the elevators want to take a request in an empty queue? Or what if the requests are waiting and all elevators are busy? We need a Back pressure strategy 🤔
Image for post
Image for post

Which library can we use to solve this problem ?

Scalaz-zio is an open source scala library which lets developers build high-performance, type-safe, concurrent, asynchronous applications that don’t leak resources or deadlock, and are easy to reason about compositionally, test, and refactor using purely functional code.

Data Structure using ZIO

Which data structures in ZIO can solve the problems above?

  1. We need a mutable data structure, the elevators will be moved step by step to their next stops but if there is a pickup request to the next stop?! 🤯 So we will have a mutable data structure + concurrency. We should avoid race conditions when threads are updating the same state! 😱 We can use zio-Ref so we can atomically update the state.
  2. We can use the asynchronous zio-Queue which provides a composable back-pressure. We can run the producers/consumers concurrently in different fibers.

But how can the elevators move continuously and take some duration between each floor? And how could the elevators consume the pickup requests forever?

In ZIO, we can repeat our actions using different strategies which are defined at the Schedule type.

class ElevatorSystem(elevators: scalaz.zio.Ref[List[ElevatorState]],
requests : scalaz.zio.Queue[PickupRequest])

Sounds great!

Image for post
Image for post


  • Create the ElevatorSystem with n elevators
def apply(n: Int): IO[Nothing, ElevatorSystem] = {
val elevators =
for {
queue <- Queue.bounded[PickupRequest](n)
state <- Ref(elevators)
} yield new ElevatorSystem(state, queue)
  • Send pickup request
def request(pickupRequest: PickupRequest): IO[Nothing, Unit] =
  • Search the closest elevator
def search(elevators: List[ElevatorState],
request : PickupRequest): Option[Int] =
.filter(_._1.isOnWay(request.floor, request.destinationFloor))
  • Move the elevatorState with Time-stepping:
val moveElevator: IO[Nothing, Unit] = elevators
  • add the pickup request’s floor and its destination to the stops of the chosen elevator:
val processRequests: IO[Nothing, Unit] = (for {
consumer <- requests.take
_ <- elevators.update { state =>
ElevatorSystem.search(state, consumer) match {
case None => state
case Some(index) =>
} yield ()).repeat(Schedule.forever).void
  • Run the elevatorSystem:
def run(duration: Duration): IO[Nothing, Unit] = 
moveElevator.fork *> processRequests

moveElevator is called in a different fiber and processRequest will be computed using the updated Elevator state.

The whole implementation is here.


You can improve this implementation and add more features like openLiftDoor, closeLiftDoor that will be called when the elevator is on its next stop, then it will wait some duration, and sometimes if someone want to enter without sending pickupRequest and the elevator’s door is still opened, we can create a new scalaz.zio.Promise when the lift door is opened which will be suspended until the door would be closed.

With Scalaz-zio we can solve a real world problem, with a testable and simple code.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store