rest style web services (google protocol buffers) - prasad nirantar
DESCRIPTION
Session presented at the 6th IndicThreads.com Conference on Java held in Pune, India on 2-3 Dec. 2011.http://Java.IndicThreads.comTRANSCRIPT
![Page 1: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/1.jpg)
1
REST web services and Google Protocol Buffers
Prasad Nirantar
BMC Software
![Page 2: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/2.jpg)
2
Agenda
• REST concepts
• Introduction Google Protocol Buffers (GPB)
• Details of Google Protocol Buffers messages
• Demo
• Performance Comparison with other serialization techniques
• Conclusions
![Page 3: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/3.jpg)
3
REST (Representational State Transfer)
• Identifies principles why web is so prevalent and ubiquitous
• Representational State Transfer• Architectural style for building loosely
coupled systems
• Architectural style – General principles informing/guiding creation of an architecture
![Page 4: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/4.jpg)
4
REST Style Principles
• Resource identification
• Uniform interface
• Self Describing messages
• Hypermedia driving application state
• Stateless transactions
![Page 5: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/5.jpg)
5
Resource and its Representation
Resource
http://example.org/Resource
GEThttp://example.org/Resource1HTTP/1.1Accept: Application/xml
HTTP/1.1 HTTP 200 OK…<Resource>
…</Resource>
Browser
![Page 6: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/6.jpg)
6
Multiple Resource representation with single URI
Resource
http://example.org/Resource
XML representation
Text representation
![Page 7: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/7.jpg)
7
Content negotiation
• Content selection
• Different types of content supported• JSON
• XML
• A resource can give response in a representation as per the request
![Page 8: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/8.jpg)
8
Need for efficient mechanism
•Data intensive systems
•Efficient mechanism to send/receive data over the wire
![Page 9: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/9.jpg)
9
Problems with XML
• Verbose
• Tends to be inefficient
![Page 10: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/10.jpg)
10
Solution - Use of Binary content
Problems it tries to solve• Performance (with Interoperability and
Portability)
Examples• Google Protocol Buffers
• Avro
![Page 11: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/11.jpg)
11
Introducing Google Protocol Buffers
• Language neutral, platform neutral, extensible way of serializing structured data
• Flexible, efficient, automated
• Supported languages - Java, C++, Python
• Developed and used by Google
• GPB project has BSD license
![Page 12: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/12.jpg)
12
Protocol buffer messages
Message – • Specify structure and details of the
information being serialized.
• Each protocol buffer message is a small logical record of information, containing a series of name-value pairs.
![Page 13: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/13.jpg)
13
Details: .proto Message• Data is hierarchically structured in messages• Each message has one or more uniquely
numbered fields• Each field has name and value• Value types can be
• numbers (integer or floating-point)• boolean • strings, • raw bytes• other protocol buffer message types,
![Page 14: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/14.jpg)
14
Details: .proto MessageFields types
• Required• Optional• Repeated
Tag• Unique number with each field in message• From 1-15 take 1 byte to encode (for frequent)• 16-2047 take 2 bytes and so on• Smallest 1 largest is 229 -1
![Page 15: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/15.jpg)
15
.proto file examplemessage Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; }
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
} repeated PhoneNumber phone = 4;
}
![Page 16: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/16.jpg)
16
Compilation of .proto messages
• A “protoc” compiler should be downloaded first.
• From .proto file, the PB compiler generates code of your choice of language from -Java, C++, Python
• For Java – The compiler generates Java classes containing POJO and Builder
• Parsing and serialization API is available
![Page 17: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/17.jpg)
17
Protocol Buffer and REST
• Suits for the cases where you want to optimize on performance and size of data transferred
• Use the protocol buffer as a content
• Adding new MIME types for JAX-RS is easy
• The examples in demo are based on JAX-RS
![Page 18: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/18.jpg)
18
Resource & Representation-protobuf
Resource
http://example.org/Resource
GEThttp://example.org/Resource1HTTP/1.1Accept: Application/x-protobuf
HTTP/1.1 HTTP 200 OK
Binary representation …
![Page 19: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/19.jpg)
19
Stack - REST with GPB
JAX-RS libraries
Protocol buffer libraries
Server component
Protocol buffer libraries
JAX-RS client libraries
Protocol buffer libraries
Http Client
C/Python clientC/Python client
Java clientJava client
Server
![Page 20: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/20.jpg)
20
Demo
![Page 21: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/21.jpg)
21
Alternatives for binary content
• Use of Java serialized objects
• Use of AVRO• Similar data format by Apache
• Uses JSON object in string format to describe schema
• Compatible with C, Java, Python
![Page 22: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/22.jpg)
22
Comparison - GPB vs XML
• Where GPB wins• 3 to 10 times smaller
• 20 to 100 times faster
• Less ambiguous
• Generate data classes for easy use
• Where XML wins• Humanly readable and editable
• A good markup choice
![Page 23: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/23.jpg)
23
Comparison : serialization techniquestechnology Object create Serialize Deserialize Total Time Serialized Size java 169 25773 71574 97347 919protobuf 471 7227 3479 11598 231avro-generic 4024 8019 4779 12798 211json (jackson) 176 7224 6084 13308 378thrift 228 7303 7949 15252 353java(Externalizable) 170 2874 3043 5917 264JsonMarshaller 171 24618 41115 65733 370
![Page 24: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/24.jpg)
24
Disadvantages of protocol buffers
• Careful with type of fields (required or optional) for backwards compatibility
• Debugging concerns
• Not humanly readable
![Page 25: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/25.jpg)
25
Conclusions
• With the JAX-RS the newer data serialization formats can be plugged into REST
• Useful for the data intensive operations, in such cases performance benefits can be achieved
• Not to be used when humanly readable formats/markups are desired
![Page 26: REST Style Web Services (Google Protocol Buffers) - Prasad Nirantar](https://reader034.vdocument.in/reader034/viewer/2022051209/547f44c65806b5d65e8b47d7/html5/thumbnails/26.jpg)
26
ReferencesDeveloper Guide - Protocol Buffers - Google Code
Rest in Practice – Webber, Parastaridis, Robinson Oreilly Publication
Restful Web Services : Principles, patterns, emerging technologies – Erik Wilde
Thrift-protobuf-compare Comparing various aspects of Serialization libraries on the JVM platform
(http://code.google.com/p/thrift-protobuf-compare/ )
Using JAX-RS with Protocol Buffers for high-performance REST APIs (http://www.javarants.com/)