TL;DR
Chubby is Google's lock service. A small cell of five replicas agrees on its state through Paxos, and clients use it for two things: advisory locks, to decide which of several machines is in charge, and small files, to publish the answer. Clients keep a session alive with periodic KeepAlive calls, cache data under the master's invalidations, and survive a master failover through a grace period. The paper reports that the service was used far more as a name service than as a lock service.
Builds on: Paxos, Distributed Lease, Distributed Mutual Exclusion, File System, Leader Election · Leads to: The Google file system and Bigtable: A distributed storage system for structured data, which use Chubby to elect their masters
Why it mattered
Before Chubby, most distributed systems at Google chose a primary in one of two ways. Where duplicated work was harmless they used ad hoc methods, and where correctness mattered they needed an operator to intervene when a primary failed. Chubby replaced both with a service that elects a primary automatically, and the paper says this gave a significant availability improvement in systems that no longer needed a human on failure.
The paper also changed how programmers thought about a shared service. Chubby was not a research result. The author states that the paper claims no new algorithms and describes what was built and why. Its value is the record of design choices, from coarse-grained locks to consistent caching, and of what went wrong in production. Several of its lessons apply to any service that other teams depend on.
Chubby became Google's primary internal name service. The Google File System and Bigtable use it to elect a master and to store a small amount of metadata, so it is the root of their data structures.
The problem
A loosely-coupled system is a set of many small machines connected by a network that can lose, delay and reorder packets. In such a system, electing one primary among peers is an instance of distributed consensus. The paper says it needs an asynchronous solution, because the network gives no timing guarantee. Paxos solves consensus without timing assumptions for safety, but liveness needs clocks, which is the gap the paper's design fills.
Building the service was an engineering problem more than a research one. The team needed one primary chosen reliably, a way to advertise its address to many clients, and a way for those clients to notice a change without polling. Failures were the normal case. The paper states that a lock held by a process that then fails is the hard part, because a delayed request from the old holder may arrive after a new holder has acted.
A design decision had to be made about the kind of locks. The paper says Chubby expects coarse-grained locks, held for hours or days rather than seconds, so the lock server sees little load and a short outage delays few clients. Fine-grained locks, held for seconds or less, would make clients depend on the lock server constantly, so the paper designs for coarse-grained use only and leaves fine-grained locks to servers that applications build themselves.
How it works
Chubby has two parts that talk over RPC: a server and a client library that every application links against. A cell has about five replicas, placed in different racks so they do not fail together. The replicas elect a master by Paxos, and the master holds a master lease, renewed while it keeps winning a majority. Only the master reads and writes the database, and writes reach the other replicas through the consensus protocol. A write is acknowledged once a majority of replicas have it.
The walkthrough below follows one election, from the first client to the last, using the paper's API.
- Find the master. A client asks the replicas, located through DNS, who the master is. Non-master replicas answer with the master's identity, and the client then sends all its requests there.
- Open a file. The client calls
Open()on a node such as/ls/cell/election/primary, with the permissions and events it wants. The master checks the access control list once and returns a handle, which carries check digits so that a handle cannot be forged. - Try to acquire the lock. Every candidate calls
Acquire()on the same node. The master grants exclusive mode to one handle, and the other candidates do not get the lock. Check: exactly one candidate holds the lock. - Publish the winner. The winner calls
SetContents()with its own address andGetSequencer()to obtain a sequencer, a byte-string naming the lock and its generation. Losing candidates act as replicas and wait for a file-change event. - Protect the work. When the primary calls a file server, it passes the sequencer. The file server calls
CheckSequencer(), or compares the sequencer with the most recent one it has seen, and rejects the call if the lock has since moved on. Check: a delayed request from the old primary carries an old generation and is refused. - Keep the session alive. The client keeps one KeepAlive RPC blocked at the master. The master holds it until the lease is near expiry, then extends the lease, by 12 seconds by default. Invalidations and events travel back in the KeepAlive reply, so a client cannot keep its session without acknowledging them.
- Fail over. The old master dies. The client's local approximation of its lease runs out. It flushes its cache, tells the application through a jeopardy event, and waits out a grace period of 45 seconds by default. A new master is elected. The client's first KeepAlive is rejected because it carries the old epoch number, and the retried KeepAlive succeeds. Check: if the new master is elected inside the grace period, the client sees only a delay; if not, the session is lost.
flowchart LR C["Client with cached data and a session"] -->|KeepAlive RPCs| M["Master, one of five replicas"] M -->|writes through consensus| R["Other replicas"] M -->|invalidations and events in the KeepAlive reply| C
A tiny trace. Two processes, A and B, race for the lock on /ls/cell/election/primary. A's Acquire() arrives first and succeeds. B's fails and B reads A's address from the file. Now A's machine stops responding. The master notices when A's session lease expires and frees the lock, but B may not take it at once. The lock-delay, which the paper caps at one minute, stops other clients from claiming a lock whose holder failed until the delay ends. Only then can B's Acquire() succeed, and B gets a new sequencer with a higher generation number.
Two details that make it safe
The first is how the client decides it still holds its session. The master's clock and the client's clock both run, and the client must be conservative. The paper requires that the server's clock advance no faster than a known constant factor relative to the client's. The client therefore keeps a local lease that ends a little before the master's, so it never believes it holds a session longer than the master will allow.
The second is what the new master does after a failover. It cannot know what the old master had promised, so it rebuilds conservative state. It picks a new client epoch number, which every call must present, so that a very old packet sent to the previous master is rejected. It rebuilds sessions and handles from the replicated database, asks clients to check in, and holds every session lease at the longest value the old master might have granted. Then it sends each session a failover event, so clients flush their caches and warn applications that events may have been lost.
The math
The paper is mostly a systems description, so the math is light. Two formal rules carry the safety argument.
Rule 1: the client's lease never outlives the master's. Write for the session lease the master holds for a client, for the client's local approximation, and for the known constant bound on how much faster the server's clock may run than the client's. The client sets its approximation so that the time it believes it holds the session is shorter than the time the master is bound to honour:
The paper does not give a numeric value for . The rule is what matters: a conservative client cannot read cached data after the master has freed its session.
Rule 2: a grace period covers the gap. Let be the grace period, 45 seconds by default, and let be the time from the client's lease ending to the new master accepting its KeepAlive. A session survives the failover when
The paper's example in Figure 2 is this inequality: the grace period was long enough to cover the interval between the end of one lease and the start of the next, so the client saw only a delay.
Sequencers add one more check, a comparison of generation numbers. A server accepts a request protected by a sequencer only when the sequencer's generation is the current one for that lock.
Results
The paper's evidence is operational: a snapshot of one busy cell, a count of outages, and the failures and fixes the team recorded over years of running it.
Load. The snapshot shows a cell with 18 days since its last failover, a failover that took 14 seconds, about 22k direct clients and 32k more proxied clients, and 12k open files. The RPC rate was 1–2k per second, of which 93% were KeepAlives. Writes, including lock acquisitions, were well under one percent of the workload. The paper's reading is that most traffic is session upkeep, which is why scaling the service meant cutting KeepAlive traffic.
Scale. The team saw 90,000 clients communicating directly with a single master, far more than the number of machines involved. A 2-CPU master could handle that many clients because most of the work was KeepAlives, which the master could make cheaper by lengthening the lease.
Outages. Over a few weeks, the team recorded 61 outages across 700 cell-days of data. Most lasted 15 seconds or less, and 52 were under 30 seconds. The nine longer or unexplained outages had these causes: network maintenance (4), suspected network connectivity problems (2), software errors (2), and overload (1). Over a few dozen cell-years, Chubby lost data six times, from database software errors (4) and operator error (2), with no hardware errors. The operator errors happened during upgrades that were meant to fix the software errors.
Name service. A DNS record with a 60-second time-to-live would force a job of 3,000 clients to make 150,000 lookups per second, while a 2-CPU DNS server might handle 50,000. Chubby's explicit invalidations avoid that polling. Its name service still had a load spike: starting a 3,000-process job produced 9 million requests at once. The fix was to return names in batches, typically about 100 related processes per lookup.
Limits the paper admits. Its outage data exclude planned data-centre shutdowns. The fail-over code was exercised rarely and was a rich source of bugs. Developers often ignored that Chubby can be down, and many crashed their applications on a fail-over event that was meant as a hint to re-check. The publish/subscribe use was slow, because invalidations are heavier than updates. Without quotas, one service stored a 1.5 MB file that was rewritten on every user action, so the team capped files at 256 kB and it took about a year to migrate the data.
Reading guide
The paper is organised as a design, its use, and its comparison with related work. Map each section to the ideas it needs.
- Section 1 (Introduction). Needs Distributed Consensus and Paxos. The paper names the consensus problem and says Paxos is the protocol at the core of working asynchronous consensus.
- Section 2.1 (Rationale). Needs Quorum and Distributed Mutual Exclusion. The argument for a lock service over a consensus library rests on quorums and on how a lock held by a crashed process behaves.
- Section 2.2 to 2.3 (Structure, files). Needs State Machine Replication and File System. The replicas keep one database, and the API is a file system with ACLs and metadata.
- Section 2.4 (Locks and sequencers). Needs Distributed Lease and Linearizability. The lock-delay and sequencer solve the problem of a delayed request reaching a server after a lock has moved on.
- Sections 2.5 to 2.6 (Events and API). Needs Logical Clock only for the ordering intuition behind events. The API is the reference for the walkthrough.
- Section 2.7 to 2.8 (Caching and sessions). Needs Consistency Model (Distributed Systems) and Distributed Lease. The lease rule and grace period are the core safety argument.
- Section 2.9 (Fail-overs). Needs Primary-Backup Replication and Leader Election. Figure 2 is the key diagram.
- Section 2.10 to 2.12 (Database, backup, mirroring). Needs Write-Ahead Logging for the rewritten database and the snapshots.
- Sections 3 and 4 (Scaling and experience). No new theory. Read the proxy and partitioning proposals as design sketches, and the outage and use statistics as the evidence.
- Section 5 (Related work). Needs Time, clocks, and the ordering of events in a distributed system and Impossibility of distributed consensus with one faulty process only as context. The comparison with Boxwood is about defaults and design goals.
Read next: Paxos for the protocol under the replicas, then The Google file system and Bigtable: A distributed storage system for structured data to see the two systems that depend on Chubby.
Check yourself
QuestionWhy does Chubby use coarse-grained locks rather than fine-grained ones, and what does that choice change about the lock server?Show answer
Coarse-grained locks are acquired rarely, so the lock server sees little load, and a short outage delays few clients. A modest set of replicas can therefore serve a large cell, and the grace period lets sessions ride out a brief master failover. Fine-grained locks would make even brief outages stall many clients, which is why the paper says Chubby is built for coarse-grained use only.
QuestionA master lease is 12 seconds and the grace period is 45 seconds. A failover takes 30 seconds to elect a new master after the client's approximate lease ends. Does the session survive, and why?Show answer
Yes. The client's lease ends, it flushes its cache, and it waits up to the grace period. The new master is elected after 30 seconds, which is inside the 45-second grace period, so the client's next KeepAlive succeeds and the session survives. If the election had taken longer than 45 seconds, the client would give up the session and report the failure to the application.
QuestionWhy does a file server accept a sequencer rather than simply trusting that the caller holds the lock?Show answer
A lock holder can pause, be partitioned, or fail, and its requests can arrive late, after another client has acquired the lock. The sequencer names the lock and its generation number. The file server checks it against the current generation, so a request sent under an old lock is refused even though the old holder still believes it is the primary.
QuestionThe paper says most KeepAlive traffic is cheap to handle at a larger lease, but the master must not lengthen leases indefinitely. What constrains the lease length?Show answer
A longer lease means a failed master's sessions take longer to expire, and a fail-over must wait out the lease before new behaviour is safe. The paper also notes that the master may raise leases from 12 seconds to about 60 seconds under heavy load, so the lease is a tradeoff between KeepAlive load on the master and how long clients and locks are stuck after a failure.
QuestionChubby's name service avoided DNS-style polling. Why does invalidation keep the load low, and what did it cost?Show answer
With invalidations, a client's cached entries stay valid until the master says otherwise, and a steady stream of KeepAlives keeps any number of entries alive without any new traffic. The cost is that the master must track which clients cache each file, and a write must wait until every client has acknowledged the invalidation, which makes writes more expensive than reads.
Sources
- Burrows (2006), The Chubby lock service for loosely-coupled distributed systems, OSDI. https://research.google.com/archive/chubby-osdi06.pdf
- Paxos (computer science), Wikipedia. https://en.wikipedia.org/wiki/Paxos_(computer_science)