Author: Eiko

Time: 2025-03-17 20:45:39 - 2025-03-18 01:30:54 (UTC)

Reference: Parallel and Concurrent Programming In Haskell

Distributed Programming In Haskell

Distributed programming is running program on multiple machines simultaneously. We want to do that because

  • Utilizes more power of paralellism

  • Distributed servers speed up client response

  • Distributed program can exploit heterogeneous enviroment

Distributed-Process Family

  • Core API distributed-process (independent of transport layer)

  • Use together with transport layer that provides sending and receiving messages between nodes

Terminology

  • Threads are always created on the current node, process can be created on a remote node.

  • A distributed program consists of a set of processes that may communicate with one another by messages.

  • Each process has a unique ProcessId

Example

A ping-pong message exchange.

  • A single master process creates a child process

  • M —ping–> C

  • M <–pong— C

  • exit

data Process

data NodeId
data ProcessId

getSelfPid  :: Process ProcessId
getSelfNode :: Process NodeId

spawn :: NodeId -> Closure (Process ()) -> Process ProcessId

send   :: Serializable a => ProcessId -> a -> Process ()
expect :: Serializable a => Process a

terminate :: Process a

say :: String -> Process ()
data Message
	= Ping ProcessId
	| Pong ProcessId
    deriving (Typeable, Generic)
    deriving anyclass Binary

Ping Server

pingServer :: Process ()
pingServer = do
    Ping from <- expect
    say $ printf "ping received from %s" (show from)
    mypid <- getSelfPid
    send from (Pong mypid)