When you open your browser and navigate to facebook.com, the seamless experience of scrolling through a news feed, sharing a photo, or sending a message happens in seconds. Yet, behind this simplicity lies a complex architecture built on a specific foundation. Understanding what Facebook is written in reveals the intricate blend of technologies that power one of the world’s largest social platforms, balancing performance, scalability, and developer efficiency.
The Core Backend: PHP and the Hack Language
Facebook’s primary server-side logic is written in PHP, a decision that was instrumental in its early growth. As the platform exploded in popularity, the challenges of maintaining vast amounts of PHP code led to the creation of Hack, a programming language developed internally at Facebook. Hack runs on the HipHop Virtual Machine (HHVM), transforming Hack and PHP code into high-performance native code. This evolution allowed Facebook to retain the rapid development cycle of PHP while introducing static typing and other features that improved reliability and performance at massive scale.
From LAMP to HHVM
Initially, Facebook operated on a LAMP stack (Linux, Apache, MySQL, PHP). However, the limitations of this setup for a site of its magnitude became apparent. The introduction of HHVM was a game-changer, enabling Facebook to execute code much faster and more efficiently. By creating Hack, Facebook addressed the need for a more robust language that could coexist with its existing PHP codebase, offering gradual typing and enhanced performance without a complete rewrite.
The Infrastructure: Hardware and Optimization
Facebook’s infrastructure is a key part of its engineering story. The platform was designed from the ground up to be hardware-friendly, optimizing the software to run efficiently on commodity servers. This approach, detailed in their Open Compute Project initiatives, involves custom-built server designs, streamlined power supplies, and efficient networking solutions. The software stack, including the HHVM and custom caching systems, is meticulously tuned to minimize latency and maximize throughput across its global data centers.
The Frontend: A Shift to React
While the backend handles the heavy lifting, the user interface is built with different tools. Facebook was an early adopter of JavaScript libraries to manage complex frontend interactions. Originally, the platform relied on a custom UI stack. However, the frontend development landscape shifted significantly with the adoption of React, a JavaScript library for building user interfaces. React’s component-based architecture and virtual DOM enable the creation of fast, dynamic, and maintainable user interfaces, which is critical for the fluidity of the Facebook experience.
Integration with Backend Services
The frontend does not operate in isolation. It communicates with a vast array of backend services through APIs. These services handle everything from retrieving your news feed to processing payments. The communication between the React frontend and the PHP/Hack backend is orchestrated with precision, ensuring that data is fetched and rendered efficiently. This separation of concerns allows different engineering teams to work on the client and server sides simultaneously, accelerating development and innovation.
Supporting Technologies and Databases
A platform like Facebook relies on a diverse ecosystem of databases and storage systems to manage its colossal data load. While MySQL remains a cornerstone for storing critical user data, due to its origins and the massive scale of friend graphs and interactions, the company also utilizes a variety of other systems. These include TAO for graph data, which powers the social connections, and a key-value store called MyRocks, optimized for efficient storage usage. Caching systems like Memcached and Redis are also heavily employed to ensure that frequently accessed data is delivered instantly.
The Engineering Culture and Open Source
Facebook’s technology is not defined solely by its programming languages but also by its engineering philosophy. The company has a strong culture of open sourcing its internal tools and infrastructure. Projects like React, the Jest testing framework, and numerous machine learning libraries have been released to the public. This approach not only contributes to the broader developer community but also allows Facebook to benefit from external feedback and improvements, creating a symbiotic relationship between the company and the global tech ecosystem.