Building a Resilient Ride-sharing App

Sep 9, 2024

Lecture Notes on Building a Ride-sharing App with Elixir

Overview

  • Project Description: Building an app similar to Uber/Lyft using Elixir.
  • Goal: Demonstrate Elixir's GenServer and its application in developing resilient applications.
  • Caveat: This is a research project, not tested in production.

Motivation

  • Learning Tool: To tackle a complex problem and learn Elixir.
  • Ride-sharing App: Involves high asynchronicity and failure conditions, making it a suitable learning project.

Key Concepts

GenServer as an Actor

  • GenServer: Similar to objects in Ruby/JavaScript but more object-oriented, allowing asynchronous operations.
  • Actor Model: Supports concurrent and fault-tolerant programming.

Supervision

  • System Monitoring: Ensure components are resilient to failure.
  • Self-Repair: Automatic restarting of failed components.

The Grid Model

  • Structure: Resembles a cell phone network, using a tiling data structure.
  • Function: Each tile, akin to a GenServer, manages a specific region.
  • Resilience: Failure in one region does not affect others.

Implementation Details

Initial Experiments

  • Tried building with Phoenix and used a grid-based approach to map the Earth.
  • Each quadrant represents a latitude and longitude area.

Agent and GenServer

  • Agent: A simple actor for state management.
  • GenServer: Needed for more complex lifecycle control (e.g., monitoring device failure).
  • Code Example: Demonstrated through grid and tile implementation.

Supervisor

  • Role: Monitors different actors and restarts them upon failure.
  • Implementation: Supervisor manages multiple tiles in the grid.

Tasks and Asynchronous Operations

  • Task Module: Allows parallel execution, useful for querying multiple tiles.
  • Performance Considerations: Trades off immediate response for scalability.

Advanced Concepts

Handling Device Failure

  • Monitoring PIDs: Allows detection of device disconnection or failure.
  • Refactor to GenServer: To set up monitors and manage process lifecycle.

State Machines

  • Usage: For managing systems with distinct states and transitions.
  • Gen State M: A newer implementation for state machines.

Tips and Techniques

  • Experimental Approach: Start with small experiments and gradually build up.
  • System Design: List actors, states, events, and collaborators to clarify requirements.
  • Supervision Trees: Visualize process dependencies and failure recovery.

Conclusion

  • Natural Sharding: System can run across multiple nodes or data centers.
  • Open for Questions: Encourages further inquiry and discussion on project implementation.