memcached binary protocol in a nutshell

44
T o r u M a e s a k a memcached Binary Protocol in a Nutshell http://torum.net memcached Night #1 in Tokyo September 17th, 2008

Upload: toru-maesaka

Post on 25-Dec-2014

23.929 views

Category:

Technology


0 download

DESCRIPTION

Presentation on the upcoming Binary Protocol in memcached 1.3 This presentation covers how we can benefit from the binary protocol and how it actually works.

TRANSCRIPT

Page 1: memcached Binary Protocol in a Nutshell

T o r u M a e s a k a

memcached Binary Protocolin a Nutshell

http://torum.net

memcached Night #1 in TokyoSeptember 17th, 2008

Page 2: memcached Binary Protocol in a Nutshell

Binary Protocol

Page 3: memcached Binary Protocol in a Nutshell

A new protocol that will beintroduced from version 1.3

Page 4: memcached Binary Protocol in a Nutshell

For those that aren’tfamiliar…

Page 5: memcached Binary Protocol in a Nutshell

bradfitz++

The original memcachedprotocol is a very simpleand small, yet an effectivetext based protocol.

Page 6: memcached Binary Protocol in a Nutshell

Then why a new protocol?

Page 7: memcached Binary Protocol in a Nutshell

• Performance• Extensibility

Page 8: memcached Binary Protocol in a Nutshell

• Performance

Protocol parsing can beperformed millions of times in

a given instance’s lifetime.

Page 9: memcached Binary Protocol in a Nutshell

• Performance

Protocol parsing canhappen thousands

of times per second.

Page 10: memcached Binary Protocol in a Nutshell

• Performance

Parse Text String

Parse Binary Stringvs

Page 11: memcached Binary Protocol in a Nutshell

• Performance

Ingredient for afaster Protocol

Parser :)

Page 12: memcached Binary Protocol in a Nutshell

Benchmark

Page 13: memcached Binary Protocol in a Nutshell

• Performance

memslap v1.0Concurrent GET OP on 100,000

pre-loaded items

Page 14: memcached Binary Protocol in a Nutshell

• Performance

Page 15: memcached Binary Protocol in a Nutshell

• Performance

Binary ProtocolPerformed better than

ASCII with moreconcurrent access(more parse/sec)

Page 16: memcached Binary Protocol in a Nutshell

Extensibility

Page 17: memcached Binary Protocol in a Nutshell

• Extensibility

Standard support ofadditional data in the

protocol.

Page 18: memcached Binary Protocol in a Nutshell

• Extensibility

Able to give memcachedmore information about

the request

Page 19: memcached Binary Protocol in a Nutshell

• Extensibility

Opens up newpossibilities

Page 20: memcached Binary Protocol in a Nutshell

So… a…

Page 21: memcached Binary Protocol in a Nutshell

New Protocol

Page 22: memcached Binary Protocol in a Nutshell

Worried that yourapplication would break

after upgrading?

Page 23: memcached Binary Protocol in a Nutshell

Don’t ;)

Page 24: memcached Binary Protocol in a Nutshell

memcached-1.3 isBilingual!

Page 25: memcached Binary Protocol in a Nutshell

Understands both ASCIIand the Binary Protocol

Page 26: memcached Binary Protocol in a Nutshell

Specification

Page 27: memcached Binary Protocol in a Nutshell

CommunityDriven

Specification

Page 28: memcached Binary Protocol in a Nutshell

Packet Structure

EXTRA FIELD(Command Specific)

HEADER

KEY

VALUE

24 bytes Required

As neededSizes aredefined inthe header

* Values must be in network byte order.

Page 29: memcached Binary Protocol in a Nutshell

Packet Structure

HEADER

Two types of headers:• Request Header• Response Header

Page 30: memcached Binary Protocol in a Nutshell

Inside the Request Header

MAGIC(1 byte)

Key Length(2 bytes)

Opcode(1 byte)

Extra Length(1 byte)

Data Type(1 byte)

Reserved(2 bytes)

Total Body Length(4 bytes)

Opaque(4 bytes)

CAS (Compare and Swap)(8 bytes)

Page 31: memcached Binary Protocol in a Nutshell

Inside the Response Header

MAGIC(1 byte)

Key Length(2 bytes)

Opcode(1 byte)

Extra Length(1 byte)

Data Type(1 byte)

Status(2 bytes)

Total Body Length(4 bytes)

Opaque(4 bytes)

CAS (Compare and Swap)(8 bytes)

Page 32: memcached Binary Protocol in a Nutshell

MAGIC Field• Distinguish between Response and Request messages

• Check the validity of the packet

0x80 = Request Packet0x81 = Response Packet

Page 33: memcached Binary Protocol in a Nutshell

OPCODERepresents the Command Type

0x00 => Get0x01 => Set0x02 => Add0x03 => Replace0x04 => Delete0x05 => Increment0x06 => Decrement0x07 => Quit

0x08 => Flush0x09 => GetQ0x0A => No-op0x0B => Version0x0C => GetK0x0D => GetKQ0x0E => Append0x0F => Prepend0x11 => Stats

Page 34: memcached Binary Protocol in a Nutshell

Key Length• Length of the key in bytes

Note that the key length fieldis 2 bytes, so theoretically a

key can be up to 65,536characters long!

Page 35: memcached Binary Protocol in a Nutshell

Extra Length

Length of the ‘extra’field in the packet

Page 36: memcached Binary Protocol in a Nutshell

Data Type

Reserved forfuture use

Page 37: memcached Binary Protocol in a Nutshell

Status

Status of theresponse

(e.g. if an error had occurred or not)

Page 38: memcached Binary Protocol in a Nutshell

Total Body Length

Length of extra field,key, and value

combined in bytes.

Page 39: memcached Binary Protocol in a Nutshell

Opaque

(like echoing)

The same opaque valuein the request is copiedback in the response.

Page 40: memcached Binary Protocol in a Nutshell

CAS

A 64bit uint for theCompare and Swap

check.

Page 41: memcached Binary Protocol in a Nutshell

Much moreinformation in the

binary protocoldocumentation

Page 42: memcached Binary Protocol in a Nutshell

http://github.com/trondn/memcached/tree/binprot/doc/protocol-binary.txt

Latest SpecificationFile available from

Trond Norbye’s tree:

Page 43: memcached Binary Protocol in a Nutshell

Thank You!and…

Page 44: memcached Binary Protocol in a Nutshell

for providing thefacilities for

memcached Night #1