Facebook chat]

Source: Internet
Author: User
Tags failover

Facebook chat, offered a nice set of software engineering challenges:

Real-time presence notification:

The most resource-intensive operation completed MED in a chat system is not sending messages. it is rather keeping each online user aware of the online-idle-Offline states of their friends, so that conversations can begin.

The naive Implementation of sending a notification to all friends whenever a user comes online or goes offline has a worst case cost of O (average friendlist size * peak users * chiln rate) messages/second, where Churchill rate is the frequency with which users come online and go offline, in events/second. this is wildly inefficient to the point of being untenable, given that the average number of friends per user is measured in the hundreds, and the number of concurrent users during peak site usage is on the order of several millions.

Surfacing connected users 'idleness greatly enhances the chat user experience but further compounds the problem of keeping presence information up-to-date. each Facebook chat user now needs to be notified whenever one of his/her friends
(A) takes an action such as sending a chat message or loads a Facebook page (if tracking idleness via a last-active timestamp) or
(B) transitions between idleness states (if representing idleness as a state machine with states like "Idle-for-1-minute", "Idle-for-2-Minutes ", "Idle-for-5-Minutes", "Idle-for-10-Minutes", etc .).
Note that approach (a) changes the sending a chat message/loading a Facebook page from a one-to-one communication into a multicast to all online friends, while approach (B) ensures that users who are neither chatting nor browsing Facebook are nonetheless generating server load.

Real-time messaging:

Another challenge is ensuring the timely delivery of the messages themselves. the method we chose to get text from one user to another involves loading an IFRAME on each Facebook page, and having that IFRAME's JavaScript make an http get request over a persistent connection that doesn't return until the server has data for the client. the request gets reestablished if it's interrupted or times out. this isn't by any means a new technique: It's a variation of Comet, specifically xhr long polling, and/or bosh.

Having a large-Number of long-running concurrent requests makes the Apache part of the standard LAMP stack a dubious implementation choice. even without accounting for the sizeable overhead of spawning an OS process that, on average, twiddles its thumbs for a minute before reporting that no one has sent the user a message, the waiting time cocould be spent servicing 60-some requests for regular Facebook pages. the result of running out of Apache processes over the entire Facebook Web Tier is not pretty, nor is the dynamic configuration of the Apache process limits enjoyable.

Distribution, isolation, and failover:

Fault tolerance is a desirable characteristic of any big system: if an error happens, the system shocould try its best to recover without human intervention before giving up and informing the user. the results of inevitable programming bugs, hardware failures, Et Al ., shocould be hidden from the user as much as possible and isolated from the rest of the system.

The way this is typically accomplished in a web application is by separating the model and the View: data is persisted in a database (perhaps with a separate in-memory cache ), with each short-lived request retrieving only the parts relevant to that request. because the data is persisted, a failed read request can be re-attempted. cache misses and database failure can be detected by the non-database layers and either reported to the user or worked around using replication.

While this architecture works pretty well in general, it isn't as successful in a chat application due to the high volume of long-lived requests, the non-relational nature of the data involved, and the statefulness of each request.

For Facebook chat, we rolled our own subsystem for logging chat messages (in C ++) as well as an epoll-Driven Web Server (in Erlang) that holds online users 'conversations in-memory and serves the long-polled HTTP requests. both subsystems are clustered and partitioned for reliability and efficient failover. why Erlang? In short, because the problem domain fits Erlang like a glove. erlang is a functional concurrency-oriented language with extremely low-weight user-space "processes", share-nothing message-passing semantics, built-in distribution, and a "crash and recover" philosophy proven by two decades of deployment on large soft-Realtime production systems.

Glueing with thrift:

Despite those advantages, using Erlang for a component of Facebook chat had a downside: That component needed to communicate with the other parts of the system. glueing together PHP, JavaScript, Erlang, and C ++ is not a trivial matter. fortunately, we have thrift. thrift translates a service description into the RPC glue Code necessary for making cross-language CALS (grouping alling arguments and responses over the wire) and has templates for servers and clients. since going open source a year ago (we had the gall to release it on every L Fool's Day, 2007 ), the thrift project has steadily grown and improved (with multiple iterations on the Erlang binding ). having thrift available freed us to split up the problem of building a chat system and use the best available tool to approach each sub-problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.