Skip to main content

Problem Set 4: Logical Clocks and Sharding

Due: Tuesday, November 24, 6:30pm. Individual work. Submit a PDF of your answers on Gradescope.

Points: 78 total.

Part 1: Virtual Time

Problems 1 through 3 refer to the figure below. Events are labelled A-N. Messages are labelled u-z (each of which has a send and receive event).

Space-time diagram with three servers, S1, S2 and S3, drawn as vertical lines with time flowing down, as a gray arrow on the left shows. Each event is a dot labelled with a letter. From top to bottom, S1 has events A (a local event), B (receives u), C (sends v), D (receives w) and E (receives z). S2 has events F (sends u), G (sends x), H (receives v), I (sends y) and J (sends w). S3 has events K (a local event), L (receives x), M (receives y) and N (sends z). So the six messages are: u from F to B, v from C to H, w from J to D, x from G to L, y from I to M, and z from N to E.
  1. Recall the definition of "happens before".

    e1 happens before e2 if:

    • they happen on the same node and e1 is first, or
    • e1 sends a message and e2 receives that message, or
    • there is some e3 with e1 HB e3 and e3 HB e2 (transitivity).

    Now answer the following questions about happens before.

    1. (1 point) How many events (labelled A-N) "happen before" event M?

    2. (1 point) For how many events (labelled A-N) does G "happen before" the event?

  2. Recall Lamport's algorithm for computing logical clocks.

    The algorithm assigns a positive integer timestamp to each event.

    • First event on each node gets timestamp 1.
    • For a local event, add 1 to the previous timestamp.
    • Message send: timestamp the send event like a local event (add 1 to the previous timestamp), and attach that timestamp to the message.
    • Message receive: max(receiver's timestamp, message timestamp) + 1.

    Now answer the following questions about Lamport clocks.

    1. (1 point) What timestamp is computed for event M?

    2. (1 point) What timestamp is attached to message w?

    3. (2 points) What timestamps are computed for events L and J? What does that tell you about their happens-before relationship?

  3. Recall the vector clock algorithm.

    A vector clock is a list of non-negative integers of length \(n\), where \(n\) is the number of nodes.

    Each event gets a vector clock as its timestamp. Component i is the timestamp of the latest event of node i that this event has heard about.

    • First event on each node gets 1 in its own component and 0 elsewhere.
    • For a local event, add 1 to the node's own component of the previous vector.
    • Message send: timestamp the send event like a local event (add 1 to the node's own component of the previous vector), and attach that vector to the message.
    • Message receive: componentwise max(receiver's vector, message vector), then add 1 to the receiver's own component.

    In the problems below, we use [S1, S2, S3] as the vector clock components.

    1. (1 point) What vector clock is computed for event M?

    2. (1 point) What vector clock is attached to message w?

    3. (2 points) What vector clocks are computed for events L and J? What does that tell you about their happens-before relationship?

  4. (4 points) Explain what it means if two different events have the same logical timestamp in Lamport's algorithm. Justify your answer.

  5. (4 points) Explain why two different events can never have equal vector clocks.

Part 2: Impossibility

Recall the coordinated attack problem from lecture. Two generals, \(B_1\) and \(B_2\), each start with a boolean input saying whether they think they should attack. The generals then run a deterministic protocol by exchanging messages, and at some point each general may decide true (attack) or false (do not attack). A protocol solves the coordinated attack problem if every execution satisfies the following three requirements.

  • Agreement: if \(B_1\) decides \(v\) and \(B_2\) decides \(w\), then \(v = w\).
  • Validity:
    • if both inputs are false, then neither general decides true;
    • if both inputs are true and no messages are lost in this execution, then both generals decide true.
  • Termination: each general eventually decides some value.

The fault model is an asynchronous network with message drops but not node failures. Assume that if a message is not dropped, then it is eventually delivered.

In lecture we proved that no protocol solves this problem.

Problems 6 to 8 below are variants of the coordinated attack problem. For each variant, say whether the problem is solvable in the asynchronous network model with message drops, or whether it is impossible. If the problem is solvable, describe (at a high level) a protocol to solve the problem. If it is impossible, say (at a high level) how to adapt the proof from lecture to show it is impossible.

  1. (4 points) Suppose we add a third node \(C\) to the system, which can exchange messages with \(B_1\) and \(B_2\) over a lossy network. Further suppose that \(C\) magically has access to \(B_1\) and \(B_2\)'s input booleans. \(C\) is allowed to assist \(B_1\) and \(B_2\) by exchanging messages, but \(C\) does not have to make a decision. The agreement, validity, and termination conditions apply only to \(B_1\) and \(B_2\), not \(C\).

  2. (4 points) Suppose messages from \(B_1\) to \(B_2\) may be dropped, but that messages from \(B_2\) to \(B_1\) are always eventually delivered. Adjust the second validity requirement so that it reads "... if no messages from \(B_1\) to \(B_2\) are lost...".

  3. (4 points) Suppose the network is "bounded lossy" instead of lossy, i.e., in any single execution, at most \(k\) messages are lost by the network, and \(k\) is fixed and known by the protocol.

Part 3: Consistent Hashing

Lab 4 implements sharding via an indirection table, i.e., an explicit mutable map from shards to the group that owns that shard. In lecture, we also considered several alternative stateless approaches to sharding based on hashing.

Let \(n\) be the number of groups, and number the groups \(0\), to \(n - 1\). Let \(h\) be a hash function from keys or group ids to non-negative integers. Recall these approaches from lecture:

  • Hash placement: key \(k\) is stored by group \(h(k) \bmod n\).
  • Consistent hashing: groups and keys are hashed to positions on a ring, and each key goes to the next group around the ring.
  • Consistent hashing with virtual nodes/groups: the same, but each group is hashed to several positions.
  1. Consider the "hash placement" approach. Suppose \(n = 3\), and suppose keys a to e and x to z are in use and have the following hash values:

    key \(k\) a b c d e x y z
    hash \(h(k)\) 3 9 14 17 29 40 45 56
    1. (1 point) For each group \(G_0\), \(G_1\), and \(G_2\), list the keys that it stores.

    2. (1 point) Suppose we add a fourth group \(G_3\). So \(n\) is now 4, which changes the placement of keys. How many keys changed group when \(G_3\) was added? (No need to list them, just say how many moved.)

    3. (1 point) Starting from the configuration from part a (just three groups), now suppose that we remove \(G_2\). So \(n\) is now 2. Again, this changes the placement of the keys. How many keys changed group when \(G_2\) was removed? (Again, no need to list them, just say how many moved.)

  2. Now consider the "consistent hashing" approach (without virtual nodes/groups).

    • Each group and each key is hashed to a position on the ring.
    • A key is stored by the group at the same position as the key or, if there is none, at the next higher position, wrapping around if needed from the highest hash value to zero.

    The positions 0 to 59 around a circle, like the minute marks on a clock face, with ticks labelled every 5: 0 at 12 o'clock, 15 at 3 o'clock, 30 at 6 o'clock, and 45 at 9 o'clock, with 59 next to 0.

    For the purposes of this problem set, we will use hash values of 0 to 59, which makes it convenient to think of the ring as a clock and to think of hash values as "minutes" on the clock. So hash value 0 is at 12 o'clock, hash value 15 is at 3 o'clock, and so on.

    Suppose a similar setup as in problem 9 (same keys and hashes), and suppose that the three groups have the hash values in the table below. Both the groups' hash values and the keys' hash values are also shown on the ring below.

    group position
    \(G_0\) 14
    \(G_1\) 36
    \(G_2\) 48

    Ring of positions 0 to 59, drawn like a clock face with position p at minute p. Position 0 is at 12 o'clock, marked with a thick line; the gray ticks outside the ring are every 5 positions, like hour marks. Keys are blue tick marks inside the ring labelled a to e and x to z: a at 3, b at 9, c at 14, d at 17, e at 29, x at 40, y at 45, z at 56. Groups are black dots on the ring: G0 at 14, G1 at 36, G2 at 48.

    Now answer the following questions about consistent hashing.

    1. (1 point) For each group, list the keys that it stores.

    2. (1 point) Suppose we add a fourth group \(G_3\) with hash 20. List all the keys that change group.

    3. (1 point) Starting again from the assignment in part a (just three groups), now suppose group \(G_1\) leaves. List all the keys that change group.

  3. Now consider using "virtual groups/nodes" with consistent hashing.

    • Each group is hashed to several positions on the ring, called its virtual groups. Each key is hashed to one position.
    • A key is stored by the group that owns the virtual group at the same position as the key or, if there is none, at the next higher position, wrapping around if needed from the highest hash value to zero.
    • When a group joins or leaves, all of its virtual groups are added or removed together.

    Suppose we have the same keys and hashes as before, and that we still have three groups. But now each group has three virtual groups, with hashes shown in the table and ring below.

    group positions
    \(G_0\) 11, 25, 42
    \(G_1\) 15, 32, 36
    \(G_2\) 5, 21, 51

    Ring of positions 0 to 59, drawn like a clock face with position p at minute p, with virtual groups. Position 0 is at 12 o'clock, marked with a thick line; the gray ticks outside the ring are every 5 positions, like hour marks. Keys are blue tick marks inside the ring labelled a to e and x to z: a at 3, b at 9, c at 14, d at 17, e at 29, x at 40, y at 45, z at 56. Virtual groups are black dots on the ring: G2 at 5, G0 at 11, G1 at 15, G2 at 21, G0 at 25, G1 at 32, G1 at 36, G0 at 42, G2 at 51.

    Now answer the following questions about consistent hashing with virtual groups.

    1. (1 point) For each group, list the keys that it stores.

    2. (1 point) Suppose we add a fourth group \(G_3\) with virtual groups at positions 10, 30, and 58. List all the keys that change group.

    3. (1 point) Starting again from the assignment in part a (just three groups), now suppose group \(G_1\) leaves. List all the keys that change group.

  4. Lab 4's ShardMaster also tries to move little data. Recall from the Lab 4 README that on Join and Leave, "the new configuration should divide the shards as evenly as possible among the groups, and should move as few shards as possible to achieve that goal". Suppose there are 10 shards, \(s_1\) to \(s_{10}\). In the current configuration, \(G_1\) owns \(s_1\) to \(s_5\) and \(G_2\) owns \(s_6\) to \(s_{10}\).

    shard \(s_1\) \(s_2\) \(s_3\) \(s_4\) \(s_5\) \(s_6\) \(s_7\) \(s_8\) \(s_9\) \(s_{10}\)
    group \(G_1\) \(G_1\) \(G_1\) \(G_1\) \(G_1\) \(G_2\) \(G_2\) \(G_2\) \(G_2\) \(G_2\)
    1. (1 point) The ShardMaster executes \(\mathtt{Join}(G_3)\). Give one possible configuration the ShardMaster might choose to change to. (Your configuration should satisfy the property that it divides the shards as evenly as possible among the groups, and should move as few shards as possible".)

    2. (2 points) From the configuration you described in part a, now suppose the ShardMaster executes \(\mathtt{Join}(G_4)\). Give one possible configuration the ShardMaster might choose to change to. How many shards change group?

    3. (2 points) Using your configuration from part b, if every shard receives the same number of requests per second, what is the difference in workload between the busiest group and the least busy group? What about if there were 11 groups and 10 shards? What does this tell you about how to choose the number of shards relative to the expected number of groups?

  5. Suppose there are 100 groups and 1,000,000 keys. One of the keys is "hot", meaning it receives more requests than the others. Suppose \(k_\mathrm{hot}\) receives 500 requests per second, while all 999,999 keys except for \(k_\mathrm{hot}\) receive a request once every 100 seconds.

    Ideally, we'd like the workload to be spread roughly evenly across the 100 groups. On average, each group should be doing about 1/100th = 1% of the work. Due to slight variation, let's say we are ok with each group doing between 0.9% and 1.1% of the work.

    1. (2 points) Explain why the presence of the hot key means that no technique that stores each key on exactly one group can achieve the desired level of "evenness".

      (Hint: what percentage of the workload is attributable to the hot key?)

    2. (2 points) Suppose instead that the hot key is copied to \(r\) groups and each operation that reads the hot key is assigned to one of the groups uniformly at random, while the other keys are spread evenly over all 100 groups. Ignore writes to the hot key for now. What is the smallest \(r\) that meets the desired level of evenness?

    3. (2 points) In the system from part b, suppose we want to support linearizable writes. At a high level, what needs to be done to linearizably write to the hot key?

Part 4: Dynamic Partitioning

Consider a high-level protocol for a partitioned key-value store where keys are statically assigned to "shards" and shards are dynamically assigned to groups by a ShardMaster that numbers these configurations, similar to lab 4 part 2. Each group is a Paxos cluster that provides a key-value API. Groups move through the sequence of configurations in order without skipping (i.e., if a group is currently in config 3 but config 5 is the most recent, then the group will first change to config 4 before changing to config 5). When a group receives a new configuration, it "reconfigures" by exchanging data with other groups.

More specifically:

  • Each configuration is numbered.
  • Each configuration maps shards to groups.
  • Each group has a current configuration.
  • When a group receives a new configuration, it enters a "reconfiguring" state where it refuses to execute client requests until it has finished exchanging data with other groups.
    • During reconfiguration, each group tracks what data it needs to receive and what data it needs to send.
      • "Data it needs to receive" means key-value data for keys that the group did not own in the previous configuration but does own in the next configuration.
      • "Data it needs to send" means key-value data for keys that the group owned in the previous configuration but does not own in the next configuration.
    • Groups send data to other groups in "ShardMove" messages. A ShardMove message contains a configuration number and a bunch of key-value store data that is being transferred to the receiving group.
    • Each group repeatedly retransmits the data it needs to send that has not been acknowledged by the new owner yet.
    • When a group is in reconfiguration and receives a ShardMove message from another group in the same configuration number, it adds the data to its key-value store, marks the data as received, and sends a ShardMoveAck to the sending group.
    • When a group is in reconfiguration and receives a ShardMoveAck message from another group in the same configuration number, it marks that data as acknowledged locally.
    • Once a group has received all required data and received acks for all sent data, it exits reconfiguration and enters the new configuration, deleting any data it no longer owns. It then begins accepting client requests again.

In this problem set, we ignore the Paxos layer and just pretend that each group is a single very reliable machine.

  1. (4 points) Suppose we "optimize" the protocol by modifying the reconfiguration protocol so that a group does not wait for sent data to be acknowledged before deleting it. Give an example execution where permanent data loss occurs (i.e., the system reaches a state where no group holds the data for a key that should be somewhere but isn't).

  2. (4 points) Suppose we "optimize" the protocol by allowing client requests during reconfiguration. Give an example execution that violates linearizability. In 1-2 sentences, explain why your execution is not linearizable.

    Hint: Consider client operations that modify data that is being moved.

  3. (4 points) Would it be linearizable to allow client operations during reconfiguration that modify data that is not being moved (i.e., the data is owned by the same group in the old configuration and the new configuration)? If yes, explain why in 1-2 sentences. If not, give an execution that violates linearizability while only modifying data that is not moved during reconfiguration (in this case, no need to explain your execution, just give it).

  4. (4 points) Different groups enter the reconfiguration phase at different times. Suppose that all groups are currently in configuration 1. Then:

    • group G1 hears about configuration 2, enters reconfiguration, finishes reconfiguration, and moves fully to configuration 2 and begins serving client requests again
    • group G2 hears about configuration 2, enters reconfiguration, but has not finished reconfiguring yet, so it does not serve client requests at the moment
    • group G3 has not yet heard about configuration 2, but it continues to serve client requests in configuration 1

    In this situation, G1 is serving client requests in configuration 2, and G3 is serving client requests in configuration 1. Why does this situation not violate linearizability from a client's perspective? Justify your answer in 1-2 sentences.

  5. (4 points) In lecture we discussed the problem of moving the whole AMOApplication, not just the KVStore data. Suppose we "optimize" the protocol by not doing that, and instead just sending the KVStore data during reconfiguration. Give an example execution with only a single message drop and no other failures, where a client receives a response that violates linearizability (or, in other words, the only correct linearizable act the system can take is to purposefully ignore the client). Also, in 1 sentence, explain intuitively why the client would receive a non-linearizable response (or must wait forever and cannot recover).

  6. (4 points) At a high level, describe an execution where two groups are two or more configuration numbers "apart" from each other. (In other words, an execution that reaches a state where one group is currently in configuration number \(n\) and another group is in configuration number \(m\), and \(n\) and \(m\) have a difference of two or more.)

  7. (4 points) Suppose we "optimize" the protocol so that groups send \(\mathtt{Query}(-1)\) instead of \(\mathtt{Query}(n+1)\), and whenever a not-currently-reconfiguring group learns about a higher configuration, it enters reconfiguration to jump directly to that higher configuration. Describe an infinite execution in which some group stays in reconfiguration forever, even though only a finite number of messages are dropped. Then explain in one sentence how moving through configurations one-by-one in order without skipping configurations prevents this.