Ensuring consistent state across clients

Last Updated: Release 4.3

Issue/Question

I have a service application with a list of tens of thousand of strings, and this list gets updated about once every minute. All clients need to have read-only access to this list.

So far, I've added a timestamp element in application state that gets updated every time the list changes; the clients listen for the timestamp change and get the updated list when it occurs. However, I'm concerned that the clients could sometimes receive a list which is in an inconsistent state. Is there a way to ensure that all clients have the same version of application state?

Solution

Because application state operates in a best-effort fashion, all clients will eventually have the most up-to-date version, but there are no guarantees around when this will happen. To ensure that all clients get the same updates in the same window of time, you could use PureWeb commands in the application state handler.

  1. Update your list of strings.
  2. Set the timestamp in application state on the service; this is the signal that the update is complete.
  3. On the client, create a state changed handler that watches the timestamp.
  4. Within the state changed handler, send a command to fetch the list of strings; include this list in the command response from the service.
    For examples of how to queue commands from a client and populate command responses on the service, see Work with command parameters.
  5. On the service, keep track of which clients were sent the command response. Here are a few tips:
    • To get a list of active clients, you can read from the /PureWeb/Sessions path in application state.
    • Command handlers on the service have a SessionID parameter that indicates which session sent a given command.

  6. When all the clients have been sent a response, loop back to step 1 and run another update to the list of strings.

This approach requires an extra roundtrip to get the list changes to each client, which means the list can only be updated as fast as the slowest client. Also, the service must keep track of what changed in the list.

 

April 11, 2017