At Janrain we like to use the right tool for the right job. The realm of social networking presents many challenges for implementers. There are components living in browsers, user interfaces, databases, and many web server applications. Different programming languages offer different strengths and weaknesses in each of these areas. We have found that functional programming offers significant advantages in the design and implementation of web servers. At Janrain we have been using the purely functional language Haskell, and the hybrid functional and object oriented language Scalaas part of our toolbox for web servers.What makes functional programming a good choice for web servers in the social network? There are several factors:
- Data structure intensive. The fundamental concept in social networking is the network. There are many ways to represent networks, including graphs, hypergraphs, and simplicial sets. Functional programming provides elegant ways to define and manipulate such libraries, and extensive libraries to assist the programmer in this area. Furthermore, both Haskell and Scala offer support for generic programming, wherein social network operations can often be defined once and reused for many relevant data structures.
- Concurrency: The services must be able to process many requests in parallel, and processing each request requires many activities that may be done in parallel, such as accessing a database, and contacting social network service providers. As a request is processed, the server may access or construct data structures; for example the server may construct a network of friends and friends of friends from several service providers. Another example is the processing of OpenID discovery for social login. OpenID discovery involves a sequence of discovery steps to different endpoints on the web. Many of these steps can be carried out in parallel. Haskell offers lightweight threads in support of concurrent programming, while Scala offers actors in support of concurrency. Concurrency and data structures are easy to combine in either Haskell or Scala. A sample that forks a thread for each element of a list in Haskell is shown below. Similar combinators can be defined for graphs and other data structures.
- High Availability: The services for the social network need to operate 24/7 in a highly available manner. Functional programming offers several features that support high availability. Scala has Finagle, which is an RPC system. Finagle can be used to implement availability concepts such as failover or m of n policies. Code should also be tolerant of errors. For example, in the code snippet below, each individual request could fail. The code returns a list of responses for those requests that succeed, and error codes for those that failed. The processing of the requests and responses will continue in spite of some failing requests. Thus the code snippet includes data structures, concurrency, and fault tolerance in just two lines of code.
– | Sample Haskell code that combines data structures and concurrency.
– | Do proxy HTTP over a list of requests
proxyHTTPs::HostPort – ^ The host/port of the dest
-> [ Request B.ByteString ] – ^ HTTP Requests to send
->IO [ EitherSomeException ( ResponseB.ByteString ) ]
proxyHTTPs hp reqs = do
– Perform each request in parallel.
rsps <- mapM ( proxyHTTPWrap hp ) reqs
– Get the response data out of the MVars.
mapM takeMVar rsps
At Janrain have found functional programming to be an important tool in support of social networking applications. We will continue to explore this valuable technology in support of social applications.