Thrift Scalable Cross Language Services Implementation Sanjoy Singh Talentica Software
Scalability ?? <ul><li>Design/Program is said to scale …  </li></ul><ul><ul><li>- if it is suitably efficient and practica...
Agenda <ul><li>Key Components/Challenges for Cross Language Interactions </li></ul><ul><li>Various System for Cross Langua...
LAMP  +  Services High-Level Goal: Enable transparent interaction between these. … and some others too.
High Level Goals ! <ul><li>Transparent Interaction between multiple programming languages. </li></ul><ul><li>Maintain Righ...
Simple Distributed Architecture <ul><li>Waiting for requests </li></ul><ul><li>(known location, </li></ul><ul><li>known po...
Key Components/Challenges ! <ul><li>Type system </li></ul><ul><li>Transport system </li></ul><ul><li>Protocol system </li>...
Hasn’t this been done before?  (yes.) ‏ <ul><li>SOAP </li></ul><ul><li>CORBA </li></ul><ul><li>COM </li></ul><ul><li>Pilla...
Should we pick up one of those?  (not sure) ‏ <ul><li>SOAP </li></ul><ul><ul><li>XML, XML, and more XML </li></ul></ul><ul...
Decision Time ! As a developer, what are you looking for? Be Patient, I have something for you in the subsequent slides !!
Solution <ul><li>Apache Thrift </li></ul><ul><li>Software framework for scalable cross-language services development.  </l...
Apache Thrift - Introduction <ul><li>Originally developed at Facebook  </li></ul><ul><li>Open sourced in April 2007 </li><...
Lets Dive It..
Principle Of Operation Thrift Code Generator Tool (written in C++) Create a thrift file eg demo.thrift Define Data types a...
Thrift Cares About <ul><li>Type Definitions </li></ul><ul><li>Service Definitions </li></ul><ul><li>Thrift Doesn’t Care Ab...
Enough Banter. Show Us the Goodz. <ul><li>// Include other thrift files </li></ul><ul><li>include &quot;shared.thrift“ </l...
Enough Banter. Show Us the Goodz. <ul><li>// Exception </li></ul><ul><li>exception InvalidOperation { </li></ul><ul><li>1:...
Enough Banter. Show Us the Goodz. <ul><li>// Include other thrift files </li></ul><ul><li>include &quot;shared.thrift“ </l...
What DOES that do? <ul><li>Generates definitions for all the types in each language </li></ul><ul><li>Generates Client and...
Magically Generated Files <ul><li>gen-java/calculatordemo </li></ul><ul><li>Calculator.java </li></ul><ul><li>InvalidOpera...
<ul><li>Thrift Philosophy </li></ul><ul><li>Create a system that is abstracted in a systematic way, such that developers c...
<ul><li>Structs don’t have any code to do with serialization or sockets, etc.  </li></ul><ul><li>But they know how to read...
The Thrift Stack <ul><li>The Thrift stack is a common class hierarchy implemented in each language that abstracts out the ...
The Thrift Stack Object write() TTransport TProtocol TTransport TProtocol Object read() Information Flow!
Versioning  (applications change a lot, not protocols!) <ul><li>What happens when definitions change? </li></ul><ul><ul><l...
Versioning  - Case Analysis <ul><li>Add a Field </li></ul><ul><ul><li>New Client, Old Server </li></ul></ul><ul><ul><li>Se...
Why to use Thrift … <ul><li>Less time wasted by individual developers </li></ul><ul><ul><li>No duplicated networking and p...
Why to use Thrift … <ul><li>Cross-language serialization with lower overhead than alternatives such as SOAP due to use of ...
Why to use Thrift … <ul><li>The predefined serialization styles include: binary, HTTP-friendly and compact binary. </li></...
Limitations / Non-Features <ul><li>Is struct inheritance/polymorphism supported? </li></ul><ul><ul><li>No, it isn’t </li><...
<ul><li>Steps/Code Walkthrough </li></ul><ul><li>(Lets build the example described earlier) </li></ul>
Some Real Time Example <ul><li>Facebook Search Service </li></ul><ul><li>AdServer, Blogfeeds, CSSParser, Memcached, Networ...
<ul><li>Why Should I not try this? </li></ul><ul><li>Guess the answer? </li></ul><ul><li>Answer: Please do let me know at ...
References <ul><li>http://incubator.apache.org/thrift/ </li></ul><ul><li>http://incubator.apache.org/thrift/static/thrift-...
<ul><li>Thanks !!! </li></ul>
Building scalable and language independent java services using apache thrift
Upcoming SlideShare
Loading in …5
×

Building scalable and language independent java services using apache thrift

3,791 views

Published on

This presentation is about the key challenges of cross language interactions and how they can be overcome. We discuss the Apache Thrift as a solution and understand its principle of Operation with code snippets and examples.

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,791
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide
  • First of all, in *.thrift file you declare objects and procedures, that you would like to interchange between applications written in different languages. Then, using thrift tool, you build Thrift platform files for programming languages of your choice. Most important step is to create server and client applications, using code generated in the previous step. Server application should implement procedures declared in the thrift file, while client should call them. Thrift itself takes care about creating transport classes, defining objects, etc. so your responsibility is only to use them in your code. Last, but not least :) is to run your server. Then, using client script, you can perform actions using previously defined objects and methods.
  • Building scalable and language independent java services using apache thrift

    1. 1. Thrift Scalable Cross Language Services Implementation Sanjoy Singh Talentica Software
    2. 2. Scalability ?? <ul><li>Design/Program is said to scale … </li></ul><ul><ul><li>- if it is suitably efficient and practical when applied to large situations </li></ul></ul>Measures Load Functional
    3. 3. Agenda <ul><li>Key Components/Challenges for Cross Language Interactions </li></ul><ul><li>Various System for Cross Language Interactions </li></ul><ul><li>Dive Into Apache Thrift </li></ul><ul><ul><li>Principle Of Operation </li></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><li>Thrift Stack </li></ul></ul><ul><ul><li>Versioning </li></ul></ul><ul><li>Why to use Thrift. Limitations? </li></ul><ul><li>Quick Code Walkthrough </li></ul>
    4. 4. LAMP + Services High-Level Goal: Enable transparent interaction between these. … and some others too.
    5. 5. High Level Goals ! <ul><li>Transparent Interaction between multiple programming languages. </li></ul><ul><li>Maintain Right balance between </li></ul><ul><ul><li>Performance </li></ul></ul><ul><ul><li>Ease and speed of development </li></ul></ul><ul><ul><li>Availability of existing libraries. etc </li></ul></ul>
    6. 6. Simple Distributed Architecture <ul><li>Waiting for requests </li></ul><ul><li>(known location, </li></ul><ul><li>known port) </li></ul>Communication protocol, Data format Sending requests, getting results <ul><li>Basic questions are: </li></ul><ul><li>What kind of protocol to use, and what data to transmit </li></ul><ul><li>What to do with requests on the server side </li></ul>
    7. 7. Key Components/Challenges ! <ul><li>Type system </li></ul><ul><li>Transport system </li></ul><ul><li>Protocol system </li></ul><ul><li>Versioning </li></ul><ul><li>Processor </li></ul><ul><li>Performance </li></ul>No problem can stand the assault of sustained thinking.
    8. 8. Hasn’t this been done before? (yes.) ‏ <ul><li>SOAP </li></ul><ul><li>CORBA </li></ul><ul><li>COM </li></ul><ul><li>Pillar </li></ul><ul><li>Protocol Buffers etc </li></ul>
    9. 9. Should we pick up one of those? (not sure) ‏ <ul><li>SOAP </li></ul><ul><ul><li>XML, XML, and more XML </li></ul></ul><ul><li>CORBA </li></ul><ul><ul><li>Over designed and Heavyweight </li></ul></ul><ul><li>COM </li></ul><ul><ul><li>Embraced mainly in Windows Client Software </li></ul></ul><ul><li>Pillar </li></ul><ul><ul><li>Slick! But no versioning/abstraction. </li></ul></ul><ul><li>Protocol Buffers etc </li></ul><ul><ul><li>Closed source Google deliciousness </li></ul></ul>
    10. 10. Decision Time ! As a developer, what are you looking for? Be Patient, I have something for you in the subsequent slides !!
    11. 11. Solution <ul><li>Apache Thrift </li></ul><ul><li>Software framework for scalable cross-language services development. </li></ul>
    12. 12. Apache Thrift - Introduction <ul><li>Originally developed at Facebook </li></ul><ul><li>Open sourced in April 2007 </li></ul><ul><li>Easy exchange of data </li></ul><ul><li>Cross language serialization with minimal overhead . </li></ul><ul><li>Thrift tools can generate code for C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk and OCaml </li></ul>
    13. 13. Lets Dive It..
    14. 14. Principle Of Operation Thrift Code Generator Tool (written in C++) Create a thrift file eg demo.thrift Define Data types and Service interfaces Build Thrift platform files Demo.php Demo.java Demo.py Demo.cpp Create Server/Client App Run the Server Server implements Services and Client calls them
    15. 15. Thrift Cares About <ul><li>Type Definitions </li></ul><ul><li>Service Definitions </li></ul><ul><li>Thrift Doesn’t Care About </li></ul><ul><li>Wire Protocol (internal XML...) </li></ul><ul><li>Transport (HTTP? Sockets? Whatevz!) </li></ul><ul><li>Programming Languages </li></ul>
    16. 16. Enough Banter. Show Us the Goodz. <ul><li>// Include other thrift files </li></ul><ul><li>include &quot;shared.thrift“ </li></ul><ul><li>namespace java calculator </li></ul><ul><li>enum Operation { // define enums </li></ul><ul><li>ADD = 1, </li></ul><ul><li>SUBTRACT = 2, </li></ul><ul><li>MULTIPLY = 3, </li></ul><ul><li>DIVIDE = 4 </li></ul><ul><li>} </li></ul><ul><li>struct Work { // complex data structures </li></ul><ul><li>1: i32 num1 = 0, </li></ul><ul><li>2: i32 num2, </li></ul><ul><li>3: Operation op, </li></ul><ul><li>4: optional string comment, </li></ul><ul><li>} </li></ul>
    17. 17. Enough Banter. Show Us the Goodz. <ul><li>// Exception </li></ul><ul><li>exception InvalidOperation { </li></ul><ul><li>1: i32 what, </li></ul><ul><li>2: string why </li></ul><ul><li>} </li></ul><ul><li>// Service </li></ul><ul><li>service Calculator extends shared.SharedService { </li></ul><ul><li>void ping(), </li></ul><ul><li>i32 add(1:i32 num1, 2:i32 num2), </li></ul><ul><li>i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), </li></ul><ul><li>oneway void zip() </li></ul><ul><li>} </li></ul>
    18. 18. Enough Banter. Show Us the Goodz. <ul><li>// Include other thrift files </li></ul><ul><li>include &quot;shared.thrift“ </li></ul><ul><li>namespace java calculator </li></ul><ul><li>enum Operation { // define enums </li></ul><ul><li>ADD = 1, </li></ul><ul><li>SUBTRACT = 2, </li></ul><ul><li>MULTIPLY = 3, </li></ul><ul><li>DIVIDE = 4 </li></ul><ul><li>} </li></ul><ul><li>struct Work { // complex data structures </li></ul><ul><li>1: i32 num1 = 0, </li></ul><ul><li>2: i32 num2, </li></ul><ul><li>3: Operation op, </li></ul><ul><li>4: optional string comment, </li></ul><ul><li>} </li></ul>// Exception exception InvalidOperation { 1: i32 what, 2: string why } // Service service Calculator extends shared.SharedService { void ping(), i32 add(1:i32 num1, 2:i32 num2), i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), oneway void zip() }
    19. 19. What DOES that do? <ul><li>Generates definitions for all the types in each language </li></ul><ul><li>Generates Client and Server interfaces for each language </li></ul><ul><li>What DOESNT that do? </li></ul><ul><li>Anything to do with sockets </li></ul><ul><li>Anything to do with serialization </li></ul>
    20. 20. Magically Generated Files <ul><li>gen-java/calculatordemo </li></ul><ul><li>Calculator.java </li></ul><ul><li>InvalidOperation.java </li></ul><ul><li>Operation.java </li></ul><ul><li>Work.java </li></ul><ul><li>gen-php/ </li></ul><ul><li>Calculator.php </li></ul><ul><li>calculator_types </li></ul><ul><li>gen-py/ </li></ul><ul><li>ttypes.py </li></ul><ul><li>Calculator.py </li></ul><ul><li>Calculator-remote </li></ul>
    21. 21. <ul><li>Thrift Philosophy </li></ul><ul><li>Create a system that is abstracted in a systematic way, such that developers can easily extend it to suit their needs and function in custom environments. </li></ul>
    22. 22. <ul><li>Structs don’t have any code to do with serialization or sockets, etc. </li></ul><ul><li>But they know how to read and write themselves… How does that work? </li></ul>
    23. 23. The Thrift Stack <ul><li>The Thrift stack is a common class hierarchy implemented in each language that abstracts out the tricky details of protocol encoding and network communication. </li></ul><ul><li>It provides a simple interface for generated code to use. </li></ul><ul><li>There are two key interfaces: </li></ul><ul><li>TTransport </li></ul><ul><ul><li>De-coupled the transport layer from Code Generation Layer. </li></ul></ul><ul><ul><li>Provides read() and write(), with a set of other helpers like open(), close(), etc. </li></ul></ul><ul><ul><li>Implementation - TSocket, TFileTransport, TBufferedTransport, TFramedTransport, TMemoryBuffer. </li></ul></ul><ul><li>TProtocol </li></ul><ul><ul><li>Separate Data Structure from Transport representation. </li></ul></ul><ul><ul><li>Provides the ability to read and write various types of data, i.e. readI32(), writeString(), etc. </li></ul></ul><ul><ul><li>Supports Bi-directional sequenced messaging and encoding of base types, container and struts. </li></ul></ul>
    24. 24. The Thrift Stack Object write() TTransport TProtocol TTransport TProtocol Object read() Information Flow!
    25. 25. Versioning (applications change a lot, not protocols!) <ul><li>What happens when definitions change? </li></ul><ul><ul><li>Struct needs a new member </li></ul></ul><ul><ul><li>Function needs a new argument </li></ul></ul><ul><li>No Problem! We’ve got Field Identifiers! </li></ul><ul><li>Example: </li></ul><ul><li>struct Work { </li></ul><ul><li>1: i32 num1 = 0, </li></ul><ul><li>2: i32 num2, </li></ul><ul><li>3: Operation op, </li></ul><ul><li>4: optional string comment, </li></ul><ul><li>} </li></ul>
    26. 26. Versioning - Case Analysis <ul><li>Add a Field </li></ul><ul><ul><li>New Client, Old Server </li></ul></ul><ul><ul><li>Server sees a field id that it doesn’t recognize, and safely ignores it. </li></ul></ul><ul><ul><li>Old Client, New Server </li></ul></ul><ul><ul><li>Server doesn’t see the field id it expects. Leaves it unset in object, server implementation can properly handle </li></ul></ul><ul><li>Remove a Field </li></ul><ul><ul><li>New Client, Old Server </li></ul></ul><ul><ul><ul><li>Server doesn’t see field it expects. Analogous to above. </li></ul></ul></ul><ul><ul><li>Old Client, New Server </li></ul></ul><ul><ul><ul><li>Old client sends deprecated field. Server politely ignore it. Analogous to the top case. </li></ul></ul></ul>
    27. 27. Why to use Thrift … <ul><li>Less time wasted by individual developers </li></ul><ul><ul><li>No duplicated networking and protocol code </li></ul></ul><ul><ul><li>less time dealing with boilerplate stuff </li></ul></ul><ul><ul><li>Write your client and server in about 5 minutes </li></ul></ul><ul><li>Less maintenance </li></ul><ul><ul><li>One networking code base that needs maintenance </li></ul></ul><ul><ul><li>Fix bugs once, rather than repeatedly in every server </li></ul></ul><ul><li>Division of labour </li></ul><ul><ul><li>Work on high-performance servers separate from applications </li></ul></ul><ul><li>Common toolkit </li></ul><ul><ul><li>Code reuse and shared tools </li></ul></ul>
    28. 28. Why to use Thrift … <ul><li>Cross-language serialization with lower overhead than alternatives such as SOAP due to use of binary format </li></ul><ul><li>A lean and clean library. No framework to code to. No XML configuration files. </li></ul><ul><li>The language bindings feel natural. For example Java uses ArrayList<String>. C++ uses std::vector<std::string>. </li></ul><ul><li>The application-level wire format and the serialization-level wire format are cleanly separated. They can be modified independently. </li></ul>
    29. 29. Why to use Thrift … <ul><li>The predefined serialization styles include: binary, HTTP-friendly and compact binary. </li></ul><ul><li>Soft versioning of the protocol. </li></ul><ul><li>No build dependencies or non-standard software. No mix of incompatible software licenses. </li></ul>
    30. 30. Limitations / Non-Features <ul><li>Is struct inheritance/polymorphism supported? </li></ul><ul><ul><li>No, it isn’t </li></ul></ul><ul><li>Can I overload service methods? </li></ul><ul><ul><li>Nope. Method names must be unique. </li></ul></ul><ul><li>Heterogeneous containers Not supported </li></ul><ul><li>Is there any enough documentation on Thrift development? </li></ul><ul><ul><li>I think this is one weak area. </li></ul></ul>
    31. 31. <ul><li>Steps/Code Walkthrough </li></ul><ul><li>(Lets build the example described earlier) </li></ul>
    32. 32. Some Real Time Example <ul><li>Facebook Search Service </li></ul><ul><li>AdServer, Blogfeeds, CSSParser, Memcached, Network Selector, News Feed, Scribe etc </li></ul>PHP based Web App Thrift PHP Lib Search Service (implemented in C++
    33. 33. <ul><li>Why Should I not try this? </li></ul><ul><li>Guess the answer? </li></ul><ul><li>Answer: Please do let me know at [email_address] </li></ul><ul><li>Skpe_id/Gtalk_id : sanjoy_17 /sanjoy17 </li></ul>
    34. 34. References <ul><li>http://incubator.apache.org/thrift/ </li></ul><ul><li>http://incubator.apache.org/thrift/static/thrift-20070401.pdf </li></ul>
    35. 35. <ul><li>Thanks !!! </li></ul>

    ×