partial-response/README.md
To enable an application to return a partial response to a client, improving perceived performance and enabling the client to start processing parts of the data before the entire response is available.
Real-world example
Imagine a restaurant where a customer orders a multi-course meal. Instead of waiting until all courses are prepared before serving, the restaurant brings out each dish as soon as it's ready. This allows the customer to start enjoying the meal without delay, improves the dining experience, and optimizes the kitchen's workflow by letting them prepare and serve courses incrementally. Similarly, in software, the Partial Response design pattern delivers portions of data as they become available, allowing the client to begin processing immediately and improving overall performance and responsiveness.
In plain words
The Partial Response design pattern allows a system to send portions of data to the client as they become available, enabling the client to start processing the data before the complete response is received.
Sequence diagram
The Partial Response design pattern allows clients to specify which fields of a resource they need. This pattern is useful for reducing the amount of data transferred over the network and allowing clients to start processing data sooner.
The programmatic example shows a simple video streaming application.
Video class represents a video object with several fields.
public class Video {
private String id;
private String title;
private String description;
private String url;
// Getters and setters...
}
FieldJsonMapper utility class converts video objects to JSON, including only the requested fields. Method mapFields takes a Video object and a set of field names. It creates a JSON object including only the specified fields. The ObjectMapper from Jackson library is used to build the JSON object.
public class FieldJsonMapper {
private static final ObjectMapper mapper = new ObjectMapper();
public static ObjectNode mapFields(Video video, Set<String> fields) {
ObjectNode node = mapper.createObjectNode();
if (fields.contains("id")) {
node.put("id", video.getId());
}
if (fields.contains("title")) {
node.put("title", video.getTitle());
}
if (fields.contains("description")) {
node.put("description", video.getDescription());
}
if (fields.contains("url")) {
node.put("url", video.getUrl());
}
return node;
}
}
VideoResource class handles HTTP requests and returns only the requested fields of the video data.
VideoResource class is a RESTful resource handling HTTP GET requests.getVideo method fetches a Video by its ID and processes the fields query parameter.fields parameter into a set of field names, uses FieldJsonMapper to include only those fields in the response, and returns the partial JSON response.@Path("/videos")
public class VideoResource {
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getVideo(@PathParam("id") String id, @QueryParam("fields") String fieldsParam) {
Video video = findVideoById(id); // Assume this method fetches the video by ID
Set<String> fields = new HashSet<>(Arrays.asList(fieldsParam.split(",")));
ObjectNode responseNode = FieldJsonMapper.mapFields(video, fields);
return Response.ok(responseNode.toString()).build();
}
private Video findVideoById(String id) {
// Dummy data for demonstration purposes
Video video = new Video();
video.setId(id);
video.setTitle("Sample Video");
video.setDescription("This is a sample video.");
video.setUrl("http://example.com/sample-video");
return video;
}
}
App class initializes the web server and registers the VideoResource.
App class sets up the server using Jersey.VideoResource class, which will handle incoming HTTP requests.http://localhost:8080/.public class App {
public static void main(String[] args) {
ResourceConfig config = new ResourceConfig();
config.register(VideoResource.class);
SimpleContainerFactory.create("http://localhost:8080/", config);
}
}
To summarize, in this example:
Video class defines the video data structure.FieldJsonMapper class helps create JSON responses including only the requested fields.VideoResource class processes client requests, fetching the necessary video data and returning partial responses based on the specified fields.App class configures and starts the web server.By implementing the Partial Response design pattern, clients can request only the necessary data, enhancing performance and reducing bandwidth usage.
Use the Partial Response pattern when
This pattern is widely adopted in
Benefits:
Trade-offs: