Lichess Move: Behind-the-Scenes Technical Breakdown

BY Mark Howell 29 days ago6 MINS READ
article cover

Have you ever wondered what goes on behind the scenes every time you make a move in your favorite online chess platform? Lichess.org is a popular, free, and open-source chess platform that attracts millions of players worldwide. Its seamless, real-time gameplay experience must be powered by a robust backend infrastructure. In this post, we'll peek behind the curtain and explore the technical processes involved when you play a move.

Inspecting Network Activity with Chrome DevTools

To understand the flow of data when making a move, we'll start by utilizing Chrome DevTools, particularly the Network tab, which allows us to monitor communication between the client (your browser) and the server. For more insights on how to use Chrome DevTools, check out Google's official guide.

WebSocket Connections

The first notable network activity is a WebSocket connection to a URL similar to: The protocol, wss, indicates an encrypted WebSockets connection using TLS. No surprises here, WebSockets are the obvious choice for real-time browser apps like online chess because they allow for full-duplex communication, enabling instant updates between the client and server without the overhead of repeated HTTP requests.

Image: Diagram of WebSocket connection process.

Local Player's Turn

When we make a move, several packets of data are exchanged. Here's the first packet, sent by us: Clear enough, right? Let's break it down: We then receive the following message: This message from the server acknowledges that it has received our move. Notice that the d field contains the same acknowledgment counter we sent in our move message, in this case, 1. Right after, we receive the following message: This message provides details about the move we just made and the updated game state.
In summary, when a move is made, the client sends a move message, receives an acknowledgment, and then receives a detailed update about the move and the game's new state.

Opponent's Turn

When the opponent makes a move, we receive a similar packet from the server. One key difference is the dests parameter, which lists all possible moves available from the current position. This parameter is used to highlight the possible moves on the chessboard that we can make after our opponent's move. While these moves could be calculated client-side, providing them server-side ensures consistency - especially for complex or esoteric chess variants - and optimizes performance on clients with limited processing capabilities or energy restrictions.

Lichess's Architecture

Lichess's real-time playing system is primarily composed of two main services (both written in Scala):

Architecture Overview

As this implies, lila communicates with lila-ws through Redis, which in turn manages WebSocket connections with clients. If lila is momentarily down, lila-ws can still handle WebSocket connections and maintain real-time communication with clients (perhaps features like chat would still work!). Conversely, if lila-ws is down, the lichess.org website will still be online, but you won't be able to play games. This separation also allows for independent scaling of the two components.

Image: Overview of Lichess's architecture.

Communication using Redis Pub/Sub

The move event is published to a Redis Pub/Sub channel, to which lila is subscribed and processes the move. Redis Pub/Sub offers at-most-once delivery. This means that each message is delivered no more than once to each subscriber, if at all. If a subscriber fails while processing a message, that message is lost and not re-delivered. This has the benefit that once a message is delivered, it can be removed from Redis, reducing memory usage. However, it also means that message loss is possible.

Eventual Data Persistence with MongoDB

While lila primarily stores game states in MongoDB, it optimizes database load by not saving every single move immediately. Instead, it buffers progress by accumulating moves and periodically saving them unless a significant event occurs, such as a game conclusion. This strategy lightens the load on the database while maintaining data integrity.

Joining a Game In Progress

As mentioned earlier, when a player connects, they provide the v parameter, which tells the system the latest version of the game they know about. Since the game state in MongoDB syncs up eventually (and your opponent might move just as you join), a player might initially get a state that's a bit behind the latest action. To handle this, lila-ws uses a trusty java.util.concurrent.ConcurrentHashMap: This setup keeps track of all events for any ongoing game and clears them once the game wraps up. It helps players reconnect to an active game by giving them all the events they need from point v onwards, without missing any or doubling up. Understandably, it has to be a concurrency-safe data structure, since multiple threads can be serving multiple players at the same time.

Wrapping Up

We are now ready to sum up the process of making a move in Lichess: The client establishes a WebSocket connection to lila-ws at a URL like wss://socket2.lichess.org/play/.... When a player makes a move, the client sends a move event to lila-ws with details like the move's UCI string and acknowledgment counter. lila-ws responds with an acknowledgment (ack) to confirm that the move has been received. The move event is published to a Redis Pub/Sub channel, to which lila is listening and processes the move. lila receives the move, updates the game state accordingly, and (eventually) stores it in a MongoDB database. An updated game state is then sent back through lila-ws to the client. The client receives the updated game state, reflecting the new move and any changes in the game's status.

Image: Visualization of game state update process.
Remember these 3 key ideas for your startup:

  1. Utilize WebSockets for Real-Time Communication: For startups developing real-time applications, WebSockets provide a robust solution for seamless, full-duplex communication, ensuring instant updates without the overhead of repeated HTTP requests. Learn more about WebSockets on Mozilla Developer Network.

  2. Optimize Backend Architecture for Scalability: Lichess's separation of services allows for independent scaling, ensuring that different components can be optimized without affecting the overall system. This approach can be beneficial for startups looking to scale efficiently. For more on scaling architectures, see AWS's guide on scalable web applications.

  3. Implement Efficient Data Persistence Strategies: By buffering data and saving it periodically, Lichess reduces database load while maintaining data integrity. Startups can adopt similar strategies to optimize performance and resource usage.

Edworking is the best and smartest decision for SMEs and startups to be more productive. Edworking is a FREE superapp of productivity that includes all you need for work powered by AI in the same superapp, connecting Task Management, Docs, Chat, Videocall, and File Management. Save money today by not paying for Slack, Trello, Dropbox, Zoom, and Notion.
For more details, see the original source.

article cover
About the Author: Mark Howell Linkedin

Mark Howell is a talented content writer for Edworking's blog, consistently producing high-quality articles on a daily basis. As a Sales Representative, he brings a unique perspective to his writing, providing valuable insights and actionable advice for readers in the education industry. With a keen eye for detail and a passion for sharing knowledge, Mark is an indispensable member of the Edworking team. His expertise in task management ensures that he is always on top of his assignments and meets strict deadlines. Furthermore, Mark's skills in project management enable him to collaborate effectively with colleagues, contributing to the team's overall success and growth. As a reliable and diligent professional, Mark Howell continues to elevate Edworking's blog and brand with his well-researched and engaging content.

Trendy NewsSee All Articles
CoverVisual Prompt Injections: Essential Guide for StartupsThe Beginner's Guide to Visual Prompt Injections explores vulnerabilities in AI models like GPT-4V, highlighting security risks for startups and offering strategies to mitigate potential data compromises.
BY Mark Howell 8 days ago
CoverGraph-Based AI: Pioneering Future Innovation PathwaysGraph-based AI, developed by MIT's Markus J. Buehler, bridges unrelated fields, revealing shared complexity patterns, accelerating innovation by uncovering novel ideas and designs, fostering unprecedented growth opportunities.
BY Mark Howell 8 days ago
CoverRevolutionary Image Protection: Watermark Anything with Localized MessagesWatermark Anything enables embedding multiple localized watermarks in images, balancing imperceptibility and robustness. It uses Python, PyTorch, and CUDA, with COCO dataset, under CC-BY-NC license.
BY Mark Howell 8 days ago
CoverJungle Music's Role in Shaping 90s Video Game SoundtracksJungle music in the 90s revolutionized video game soundtracks, enhancing fast-paced gameplay on PlayStation and Nintendo 64, and fostering a cultural revolution through its energetic beats and immersive experiences.
BY Mark Howell 8 days ago
CoverMastering Probability-Generating Functions: A Guide for EntrepreneursProbability-generating functions (pgfs) are mathematical tools used in probability theory for data analysis, risk management, and predictive modeling, crucial for startups and SMEs in strategic decision-making.
BY Mark Howell 21 days ago
CoverMastering Tokenization: Key to Successful AI ApplicationsTokenization is crucial in NLP for AI apps, influencing data processing. Understanding tokenizers enhances AI performance, ensuring meaningful interactions and minimizing Garbage In, Garbage Out issues.
BY Mark Howell 29 days ago
CoverReviving Connection: What We Lost with the Decline of Letter WritingThe shift from handwritten letters to digital communication has reduced personal connection, depth, and attentiveness, impacting how we communicate and relate in both personal and business contexts.
BY Mark Howell 29 days ago
CoverExploring PlayStation Vita's Architecture: A Deep Dive (Part 1)The PlayStation Vita, released in 2011, exemplifies strategic tech adoption, balancing innovation and market positioning, offering insights for startups and SMEs in competitive tech markets.
BY Mark Howell 29 days ago
Try EdworkingA new way to work from  anywhere, for everyone for Free!
Sign up Now