Master-slave replication protocol analysis of Tokyo tyrant (ttserver)
The replication protocol of Tokyo tyrant (ttserver)
[ArticleAuthor: Sun Li link: http://www.cnblogs.com/sunli/ updated by: 2010-06-26]
Purpose
Ttserver already comes with the master-slave replication function, which runs very stably and efficiently, so that we can use ttserver to achieve high reliability. Why should I analyze its replication protocol?
1. data needs to be synchronized from ttserver to a heterogeneous database in real time. For example, MySQL and other nosql databases.
2. seamless data migration. If you need to seamlessly switch from the ttserver database to other databases, you can use this replication protocol.
3. for high-performance asynchronous processing queues, you can set the primary database to the mem type and use the synchronization protocol to obtain data. This is actually a queue and a persistent queue service. 4. Write a service for other storage, and use ttserver as the slave.
Protocol Structure Analysis
Let's start with the data flow process:
-
- Connect the client (slave) to the server (master)
-
- After the slave connection is successful, the Server Load balancer sends it to the master.
+ -------------------------------------- +
| 0xc8 | 0xa0 | slave RTS | slave Sid |
| 1 byte | 1 byte | 8 bytes | 4 bytes |
+ -------------------------------------- +
-
- After receiving the message, the master wants the client to send the message:
+ --------- +
| Mid |
| 4 bytes |
+ --------- +
-
- After the Server Load balancer receives the message, it can obtain the master SID of the server.
- Replication starts here. The master continues to want the client to repeatedly send the copied data object.
+ ------ +
| 0xca | no operation
| 1 byte |
+ ------ +
Send an operation item:
+ -------------------------------------------------- +
| 0xc9 | RTS | rsid | rsize | data |
| 1 byte | 8 bytes | 4 bytes | 4 bytes | rsize bytes |
+ -------------------------------------------------- +Data structure:
+ ----------------------------------- +
| Magic | cmd | [data] |
| 1 bytes | 1 bytes | key-value data |
+ ----------------------------------- +
CMD indicates various operation types, such as put putkeep.
-
- Slave cyclically accepts the data for OK data.
Protocol Implementation
The replication protocol of ttserver is very simple, because the copy protocol is not officially written to the document, and other operations have documentation. You need to analyze its cCodeStructure analysis is very simple. I will make some implementations available.Www.cnblogs.com/sunli/announcement