Distributed Systems

Section I: The Basics
In this section, we'll learn the basic programming constructs and libraries we need to write distributed systems in OCaml. After this section, we'll know how to

Chapter 1: Async
Concurrency and parallelism are at the heart of distributed systems. When programs scale to multiple threads, processes, or machines, we have to reason about multiple computations that are all happening at the same time. In this chapter, we explore Jane Street's monadic cooperative multithreading library: Async. Async is a library that makes writing concurrent code fun and easy! Async will also serve as a building block throughout the rest of the book; all the code we write will involve async. Before we dive into some code, I recommend you read Chapter 18 of Real World Ocaml!

  1. HelloWrong
  2. HelloRight
  3. HelloCommand
  4. Basics
  5. Choices
  6. Pipes
  7. Ivars
  8. MoreChoices
  9. MorePipes

Resources

Chapter 2: Reader/Writer
Whether it be files or TCP connections, programs often interact with the world by reading and writing data to and from an external source. In this chapter, we'll examine Async's Reader and Writer interfaces. These interfaces provide a uniform way to read and write from all sorts of external sources. After this chapter, you'll be able to read strings from stdin, and you'll be a master a file IO. Readers and Writers will also crop up again when we cover TCP, so you'll be prepared for that too.

  1. StdinLine
  2. StdoutLine
  3. StdinAll
  4. WriteString
  5. WriteLine
  6. WriteWith
  7. Diary
  8. Cat

Resources

Chapter 3: TCP
Distributed systems involve multiple processes running on different computers, different data centers, or even different continents. Despite the geographic separation, the processes can cooperate and function by communicating with one another with networking protocols like UDP and TCP. In this chapter, we'll explore TCP network programming in OCaml.

  1. PrintServer
  2. EchoOnceServer
  3. EchoServer
  4. ExitEchoServer
  5. PrintClient
  6. EchoClient
  7. TcpShell
  8. KeyValueStore
  9. RedisOp
  10. Redis

Resources

Chapter 4: Serialization
  1. HandCard
  2. SexpCard
  3. MarshalCard
  4. BinioCard

Resources

Chapter 5: Messaging
  1. ArithMarshal
  2. MarshalClient
  3. MarshalServer
  4. ArithSexp
  5. SexpClient
  6. SexpServer
  7. ToStringProtocol
  8. ToStringClient
  9. ToStringServer
  10. ArithRpc
  11. RpcClient
  12. RpcServer

Resources

Chapter 6: Persistence
  1. Ephemeral
  2. Fsync
  3. LevelDb
  4. MoreLevelDb

Appendix
The appendix contains miscellaneous programs and snippets on topics that may be useful, but not necessary, to writing distributed systems.

HTTP
  1. HelloServer
  2. HelloGetServer
  3. HelloHtmlServer