Skip to content

Design a Distributed Cache (Memcache), stage 4 of 9: break it

Write the lease-aware read

The cache's get now returns one of three things: a value, a lease token (you should refill), or "wait" (someone else is refilling). setWithLease returns false if the token was invalidated. Write the web server's read helper.

System so far· 5 parts
1234CLIENTUsersSERVICEWeb serversSERVICEmcrouterCACHEmemcached poolDATABASEMySQL

Select a component to see what it is responsible for and which state it owns.

  1. 1Users → Web servers: Page request
  2. 2Web servers → mcrouter: get / multiget, delete
  3. 3mcrouter → memcached pool: Keys by consistent hash
  4. 4Web servers → MySQL: Query on miss; writes

What you need to know

0 of 2 checks done
  1. A lease-aware get returns one of three results, and the client has a branch for each:

    ResultMeaningClient does
    hitthe valuereturn it
    leaseyou should refillload from the database, set with the token
    waitsomeone else is refillingsleep briefly, ask again
  2. Check

    The client loads the value and its setWithLease returns false (a delete invalidated the token). What should it return to its caller?