Craig Walls and I presented a new approach to client-server communication at SpringOne2GX 2014.
Abstract:
The world of client-server has changed. The traditional application of REST is no longer the best fit. We're depolying applications into a world where users expect responsive UIs, on all their devices, even while disconnected. We're deploying into a world where connection latency, mobile radio usage and battery life have become primary concerns.
Differential Synchronization (DS) is an algorithm that syncs data across N parties, even in the face of dropped connections, offline devices, etc. It makes more efficient use of connections by batching and sending only changes, in both directions, from client to server and from server to client. We’ll look at how it can be used with JSON Patch to synchronize application data between clients and servers over HTTP Patch, WebSocket, and STOMP, and how it can be integrated into the Spring ecosystem.
7. Isn’t “typical REST” good enough?
• “Typical REST”: 1 request per entity per operation type
• create 2 entities, update 3, delete 1 = 6 requests
• Expensive for mobile: connection latency, battery, data ($$$)
• Doesn’t exploit return payload
8. Motivation: Goals
• More efficient data transfer
• More efficient use of mobile radios, networks and batteries
• Take advantage of WebSocket & messaging
• Data synchronization with multiple clients
• Offline / disconnected
9. How can we
get data from a Spring back-end all the way to
the pixels more efficiently?
11. Differential Sync
• Algorithm for syncing N copies of a document
• Potentially supports any kind of document/data
• As long as diff & patch algorithms are available
• Text, DOM, JSON
• Nice properties
• Efficient: Transfer only differences
• Symmetrical: same algorithm at each node
• Recoverable: disconnected/offline clients, lost messages
• Published by Neil Fraser in 2009
• https://neil.fraser.name/writing/sync/
16. JSON Patch
• JSON-formatted Patch for structured documents
• RFC 6902, plus related JSON Pointer RFC 6901
• https://tools.ietf.org/html/rfc6902
• https://tools.ietf.org/html/rfc6901
• Suitable for sending via HTTP Patch
• Defines operations, format, algorithm, and mime type
• application/json-patch+json
• Can coexist w/handlers at same url via Content-Type routing
• Does not define diff algorithm
• Sensible requirement: patch(diff(a, b), a) === b
19. JSON Patch
• Moves the operation type inside request payload
• create 2 entities, update 3, delete 1 = 1 request
• Moves the identifier inside request payload
• Potentially patch many entity types in a single request
• Patches are atomic
• If any part of a patch fails, whole patch must fail (as per RFC)
• Think: data integrity
27. jiff.js
var jiff = require(‘jiff’);
var rest = require(‘rest’);
!
var changedData = jiff.clone(data);
!
// … data changes via user interactions
!
var patch = jiff.diff(data, changedData);
!
rest({ method: ‘PATCH’, entity: patch });
28. Hmmmm ….
Differential synchronization requires diff and patch algorithms
JSON Patch defines a patch format and algorithm
29. Differential Sync + JSON Patch
What if we put these two things together to synchronize
structured data?
30. Differential Sync
JSON Patch
data
model shadow data
shadow model
diff
diff
patch
patch
changes changes
JSON Patch
Spring Server
Client
(web browser,
phone, etc.)
35. Challenges with JSON Patch and Java
• How do you “remove” or “move” a property?
• How do you “move” a list item to a different index?
• How do you “add” a list item to a specific index?
• How do you avoid saving the entire list when patching a list?
• How do you delete an item as the result of a “delete” op?
• What if a “remove” is only intended for a particular view?
36. Introducing Spring Sync
• GitHub: http://github.com/spring-projects/spring-sync
• Maven/Gradle: org.springframework.sync:spring-sync:0.5.0.BUILD-SNAPSHOT
• JsonPatch
• Applies a JSON Patch to a Java object graph
• DiffSync
• Applies Differential Synchronization algorithm (leveraging JsonPatch)
• DiffSyncController
• Handles PATCH requests for “application/json-patch+json”
• Returns a JSON Patch to update client
• @EnableDifferentialSynchronization
37. Enabling Spring Sync
@Configuration
@EnableDifferentialSynchronization
public class DiffSyncConfig extends DiffSyncConfigurerAdapter {
!
@Autowired
private PagingAndSortingRepository<Todo, Long> repo;
@Override
public void addPersistenceCallbacks(
PersistenceCallbackRegistry registry) {
!
registry.addPersistenceCallback(
new JpaPersistenceCallback<Todo>(repo, Todo.class));
!
}
}
39. Differential Sync in JavaScript
DOM User
Interface
patch patch
JavaScript objects,
arrays, etc
Network
patch patch
Spring Server
40. Differential Sync in JavaScript
• Synchronize from the Spring data model to the pixels
• Decouple change frequency from communication frequency:
• fast sync = responsive, but network/resource intense
• slow sync = slower UI updates, but less network/resource
intense
• Current Status
• Incubator JavaScript implementation based on cujojs/jiff.js
41. When you have a system based on patches,
you can do some interesting things
45. Patch Algebra
• Inverse patches
• think Undo & Redo with no application specific logic
• Merge and Rebase
• Apply parallel changes from multiple parties without locking
• jiff.js supports inverse, rebase
47. Challenges
• DS in an Entity-oriented world
• What constitutes a document?
• Each participant must maintain a shadow copy of the document
• Lists and arrays are tricky
• Conflict resolution (not a big deal in practice?)
• Hypothesis: Conflicts no more likely to occur than REST
• DS in itself does not solve conflict resolution, but neither does
REST
48. Today
• Spring Sync
• http://github.com/spring-projects/spring-sync
• org.springframework.sync:spring-sync:0.5.0.BUILD-SNAPSHOT
• JSON Patch + diff in JavaScript
• https://github.com/cujojs/jiff
• Experimental Spring support for Differential Sync and JSON
Patch over HTTP Patch
• https://github.com/royclarkson/spring-rest-todos
49. The Future
• Continue evolution of Spring Sync and JavaScript DS
implementations
• Further Integrate with Spring ecosystem
• Messaging & WebSocket
• Smart patching
• Offline/disconnected client support
• Guidance (when to use it, how to tune it, etc.)