Building Secure Distributed Applications in Haskell

UC Santa Cruz Science Internship Program · June–August 2024

How can a distributed application keep working when its replicas fail or return conflicting values? During the 2024 Science Internship Program (SIP) at UC Santa Cruz, I worked with Allison Shi and Amy Vitek under the mentorship of Priyanka Mondal to explore this question in Haskell. Mondal was then a PhD candidate at UC Santa Cruz, advised by Prof. Owen Arden and Prof. Ioannis Demertzis.

We used choreographic programming to describe communication across participants in one program, then implemented a two-out-of-three majority quorum using two abstractions: compare and select. Our bank-balance example brought the ideas together in a program running across four terminals.

My work included the Haskell API and quorum implementation. I explained the comparison and selection logic, its integration into the bank example, and the three execution cases in our final research talk.

Coordinating communication

Replication addresses one weakness of a single-server system: if that server stops responding, the service becomes unavailable. Additional replicas can keep a service available, but they introduce a different question. When two replicas return different answers, which response should the application accept?

Before implementing a decision rule, we needed a way to express the participants’ communication reliably. In conventional message passing, each participant has its own program. A send at one endpoint must match a receive at another, including when execution takes different branches. A missing or mismatched operation can leave a participant waiting indefinitely.

A choreography describes the interaction globally. Endpoint projection derives the behavior of each participant from that shared description: one communication produces both a send and its corresponding receive. We used HasChor, a library that embeds functional choreographic programming in Haskell.

A single choreography with three communications projects into matching send and receive operations for participants p and q.
One global communication sequence determines the matching operations at both endpoints. This helps prevent deadlocks caused by mismatched communication.

The scope of that guarantee matters. Coordinating sends and receives does not, by itself, make a failed computer respond or make an incorrect value trustworthy. Communication structure and fault-handling logic address different parts of the problem.

From two programs to one choreography

We first implemented a buyer–seller interaction in two forms: separate concurrent programs and a HasChor choreography. The buyer sends a book title, the seller computes its price, and the buyer checks that price against a budget. An accepted purchase leads to a delivery date; a declined purchase ends the interaction.

In the choreography, the initial exchange and budget check are expressed as follows:

title' <- (buyer, title) ~> seller
price <- seller `locally` \un -> return (priceOf (un title'))
price' <- (seller, price) ~> buyer
decision <- buyer `locally` \un -> return (un price' <= budget)

The full choreography returns an optional delivery date located at the buyer, represented by Maybe Day @ "buyer". We demonstrated the buyer and seller in two terminals on the same machine, with logging to expose the messages and decisions.

Combining agreement and availability

The quorum example separates two operations that serve different purposes:

select does not establish agreement on its own. We first compare every pair of replica responses—A with B, B with C, and A with C—then combine those comparison results using select. Any successful comparison represents two replicas agreeing on a value.

Three replicas A, B, and C are compared in pairs; nested select operations combine the AB, BC, and AC comparison results.
The two-out-of-three quorum compares all three pairs of responses and selects a successful agreement. Comparing pairs supplies the agreement check; selection supplies a usable result.

For example, responses of $1,500, $1,000, and $1,000 contain one agreeing pair. The quorum accepts $1,000. This is correct under the example’s assumption that at most one replica returns an incorrect value and the other two agree on the correct balance.

Putting the quorum into Haskell

Our application has four participants: user, bank1, bank2, and bank3, identified by Haskell proxies. Each bank reads a balance locally and communicates it to the user. The received values, b1, b2, and b3, are then available for the user’s local quorum computation.

comp1 <- user `locally` \un -> compare (un b1) (un b2)
comp2 <- user `locally` \un -> compare (un b2) (un b3)
comp3 <- user `locally` \un -> compare (un b1) (un b3)

sel1 <- user `locally` \un -> select (un comp1) (un comp2)
sel2 <- user `locally` \un -> select (un comp3) (un sel1)

user `locally` \un -> do
  finalResult <- wait (un sel2)
  putStrLn $ "Balance: " ++ finalResult

The three comparisons cover all pairs. The first selection combines the A–B and B–C results; the second combines that result with A–C. Finally, the user waits for the selected result and prints the balance. These are implementation excerpts, with surrounding declarations and setup omitted.

Haskell’s do notation lets the choreography compose communication and local computations in sequence. The located values keep the data flow explicit: balance retrieval happens at each bank, while the quorum decision happens at the user.

Three execution cases

We ran one terminal for each bank and one for the user. The three cases exercise unanimous agreement, agreement despite one conflicting response, and complete disagreement.

Replica inputs and user output in the recorded demonstration.
CaseBank 1Bank 2Bank 3User output
All agree1,0001,0001,0001,000
Two agree1,0001,5001,0001,000
None agree1,000100500fail
Original terminal captures for three cases: all banks return 1000 and the user receives 1000; banks return 1000, 1500, 1000 and the user receives 1000; banks return 1000, 100, 500 and the user receives fail.
Recorded runs across the three bank processes and the user process. The circled outputs are 1000, 1000, and fail, matching the cases above.

The second run shows why pairwise comparison matters: two comparisons encounter conflicting values, but the remaining pair agrees on 1,000. In the third run, every pair disagrees, so selection cannot produce a majority balance and the program reports fail.

Scope of the demonstration

These runs demonstrate the quorum’s behavior for supplied balance values. They do not measure throughput, network latency, or behavior under arbitrary network failures. The disagreement cases use manually entered inputs rather than an actual attack on a bank service.

A majority establishes agreement, not truth by itself. Two replicas returning the same incorrect balance could outvote a correct third replica. The example also does not implement a full banking service or a general Byzantine consensus protocol. Its focus is composing communication and quorum operations with explicit assumptions about the replicas.

Research context

This was a team project completed with Allison Shi and Amy Vitek, mentored by Priyanka Mondal at UC Santa Cruz. Our work built on existing research in functional choreographic programming and fault-tolerant language abstractions.