Skip to content
This repository
Newer
Older
100644 571 lines (386 sloc) 23.499 kb
37db4e00 » nicotaing
2012-10-15 [doc] add protocol.md
1 # Beanstalkd
2
3 ## Protocol
4
5 ### Description
6
7 The beanstalk protocol runs over TCP using ASCII encoding. Clients connect, send commands and data, wait for responses, and close the connection. For each connection, the server processes commands serially in the order in which they were received and sends responses in the same order. All integers in the protocol are formatted in decimal and (unless otherwise indicated) nonnegative.
8
9 ### Name convention
10
11 Names only supports ASCII strings.
12
13 #### Characters Allowed
14
15 * **letters** (A-Z and a-z)
16 * **numerals** (0-9)
17 * **hyphen** ("-")
18 * **plus** ("+")
19 * **slash** ("/")
20 * **semicolon** (";")
21 * **dot** (".")
22 * **dollar-sign** ("$")
23 * **underscore** ("_")
24 * **parentheses** ("*(*" and "*)*")
25
26 **Notice:** They may not begin with a hyphen and they are terminated by white space (either a space char or end of line). Each name must be at least one character long.
27
28 ### Errors
29
30 | Errors | Description |
31 | --------------------| ------------- |
32 | `OUT_OF_MEMORY\r\n` | The server cannot allocate enough memory for the job. The client should try again later.|
33 | `INTERNAL_ERROR\r\n` | This indicates a bug in the server. It should never happen. If it does happen, please report it at http://groups.google.com/group/beanstalk-talk. |
34 | `BAD_FORMAT\r\n` | The client sent a command line that was not well-formed. This can happen if the line does not end with \r\n, if non-numeric characters occur where an integer is expected, if the wrong number of arguments are present, or if the command line is mal-formed in any other way. |
35 | `UNKNOWN_COMMAND\r\n` | The client sent a command that the server does not know. |
36
37
38 ### Job Lifecycle
39
40 A job in beanstalk gets created by a client with the `put` command. During its life it can be in one of four states:
41
42 | Status | Description |
43 | --------------------| ------------- |
44 | `ready` | it waits in the ready queue until a worker comes along and runs the "reserve" command |
45 | `reserved` | if this job is next in the queue, it will be reserved for the worker. The worker will execute the job |
46 | `delayed` | when it's waiting "x" seconds before to be `ready` |
47 | `buried` | when it is finished the worker will send a "delete" ; when it is finished the worker will send a "delete" |
48
49
50
51 Here is a picture of the typical job lifecycle:
52
53 ```
54 put reserve delete
55 -----> [READY] ---------> [RESERVED] --------> *poof*
56 ```
57
58
59
60 Here is a picture with more possibilities:
61
62 ```
63 put with delay release with delay
64 ----------------> [DELAYED] <------------.
65 | |
66 | (time passes) |
67 | |
68 put v reserve | delete
69 -----------------> [READY] ---------> [RESERVED] --------> *poof*
70 ^ ^ | |
71 | \ release | |
72 | `-------------' |
73 | |
74 | kick |
75 | |
76 | bury |
77 [BURIED] <---------------'
78 |
79 | delete
80 `--------> *poof*
81 ```
82
83 ### Tubes
84
85 The system has one or more tubes. Each tube consists of a ready queue and a delay queue. Each job spends its entire life in one tube. Consumers can show interest in tubes by sending the `watch` command; they can show disinterest by sending the `ignore` command. This set of interesting tubes is said to be a consumer's `watch list`. When a client reserves a job, it may come from any of the tubes in its watch list.
86
87 When a client connects, its watch list is initially just the tube named `default`. If it submits jobs without having sent a `use` command, they will live in the tube named `default`.
88
89 Tubes are created on demand whenever they are referenced. If a tube is empty (that is, it contains no `ready`, `delayed`, or `buried` jobs) and no client refers to it, it will be deleted.
90
91 ## Commands
92
93 ### Producer Commands
94
95 #### `put` command
96
97 The `put` command is for any process that wants to insert a job into the queue. It comprises a command line followed by the job body:
98
99 ```
100 put <pri> <delay> <ttr> <bytes>\r\n
101 <data>\r\n
102 ```
103
104 #####`put` options
105
106 It inserts a job into the client's currently used tube (see the `use` command below).
107
108 * `<pri>` is an integer < 2**32. Jobs with smaller priority values will be scheduled before jobs with larger priorities. The most urgent priority is 0;the least urgent priority is 4,294,967,295.
109 * `<delay>` is an integer number of seconds to wait before putting the job in the ready queue. The job will be in the "delayed" state during this time.
110 * `<ttr>` -- time to run -- is an integer number of seconds to allow a worker to run this job. This time is counted from the moment a worker reserves this job. If the worker does not delete, release, or bury the job within `<ttr>` seconds, the job will time out and the server will release the job. The minimum ttr is 1. If the client sends 0, the server will silently increase the ttr to 1.
111 * `<bytes>` is an integer indicating the size of the job body, not including the trailing "\r\n". This value must be less than max-job-size (default: 2**16).
112 * `<data>` is the job body -- a sequence of bytes of length <bytes> from the previous line.
113
114
115 ##### `put` responses
116 After sending the command line and body, the client waits for a reply, which
117 may be:
118
119 * `INSERTED <id>\r\n` to indicate success. `<id>` is the integer id of the new job
120 * `BURIED <id>\r\n` if the server ran out of memory trying to grow the priority queue data structure. `<id>` is the integer id of the new job
121 * `EXPECTED_CRLF\r\n` The job body must be followed by a CR-LF pair, that is, `"\r\n"`. These two bytes are not counted in the job size given by the client in the put command line.
122 * `JOB_TOO_BIG\r\n` The client has requested to put a job with a body larger than max-job-size bytes.
123 * `DRAINING\r\n` This means that the server has been put into "drain mode" and is no longer accepting new jobs. The client should try another server or disconnect and try again later.
124
125 #### `use` command
126
127 The `use` command is for producers. Subsequent put commands will put jobs into the tube specified by this command. If no use command has been issued, jobs will be put into the tube named `default`.
128
129 ```
130 use <tube>\r\n
131 ```
132
133 ##### `use` options
134
135 * `<tube>` is a name at most 200 bytes. It specifies the tube to use. If the tube does not exist, it will be created.
136
137 ##### `use` responses
138
139 * `USING <tube>\r\n` -- `<tube>` is the name of the tube now being used.
140
141 #### Worker Commands
142
143 A process that wants to consume jobs from the queue uses those commands:
144
145 * `reserve`
146 * `delete`
147 * `release`
148 * `bury`
149
150 #### `reserve` command
151
152 ```
153 reserve\r\n
154 ```
155
156 Alternatively, you can specify a timeout as follows:
157
158 ```
159 reserve-with-timeout <seconds>\r\n
160 ```
161
162 This will return a newly-reserved job. If no job is available to be reserved, beanstalkd will wait to send a response until one becomes available. Once a job is reserved for the client, the client has limited time to run (TTR) the job before the job times out. When the job times out, the server will put the job back into the ready queue. Both the TTR and the actual time left can be found in response to the `stats-job` command.
163
164 A timeout value of `0` will cause the server to immediately return either a response or `TIMED_OUT`. A positive value of timeout will limit the amount of time the client will block on the reserve request until a job becomes available.
165
166 ##### `reserve` responses
167
168 ###### Non-succesful responses
169
170 * `DEADLINE_SOON\r\n` During the TTR of a reserved job, the last second is kept by the server as a safety margin, during which the client will not be made to wait for another job. If the client issues a reserve command during the safety margin, or if the safety margin arrives while the client is waiting on a reserve command.
171 * `TIMED_OUT\r\n` If a non-negative timeout was specified and the timeout exceeded before a job became available, the server will respond with TIMED_OUT.
172
173 Otherwise, the only other response to this command is a successful reservation
174 in the form of a text line followed by the job body:
175
176 ####### Succesful response
177
178
179 ```
180 RESERVED <id> <bytes>\r\n
181 <data>\r\n
182 ```
183
184 * `<id>` is the job id -- an integer unique to this job in this instance of beanstalkd.
185 * `<bytes>` is an integer indicating the size of the job body, not including the trailing `\r\n"`.
186 * `<data>` is the job body -- a sequence of bytes of length <bytes> from the previous line. This is a verbatim copy of the bytes that were originally sent to the server in the put command for this job.
187
188 #### `delete` command
189
190 The delete command removes a job from the server entirely. It is normally used by the client when the job has successfully run to completion. A client can delete jobs that it has `reserved`, `ready` jobs, `delayed` jobs, and jobs that are
191 `buried`. The delete command looks like this:
192
193 ```
194 delete <id>\r\n
195 ```
196
197 ##### `delete` options
198
199 * `<id>` is the job id to delete.
200
201 ##### `delete` responses
202
203 The client then waits for one line of response, which may be:
204
205 * `DELETED\r\n` to indicate success.
206 * `NOT_FOUND\r\n` if the job does not exist or is not either reserved by the client, ready, or buried. This could happen if the job timed out before the client sent the delete command.
207
208 #### `release` command
209
210 The release command puts a `reserved` job back into the ready queue (and marks its state as `ready`) to be run by any client. It is normally used when the job fails because of a transitory error. It looks like this:
211
212 ```
213 release <id> <pri> <delay>\r\n
214 ```
215
216 ##### `release` options
217
218 * `<id>` is the job id to release.
219 * `<pri>` is a new priority to assign to the job.
220 * `<delay>` is an integer number of seconds to wait before putting the job in the ready queue. The job will be in the "delayed" state during this time.
221
222 ##### `release` responses
223
224 The client expects one line of response, which may be:
225
226 * `RELEASED\r\n` to indicate success.
227 * `BURIED\r\n` if the server ran out of memory trying to grow the priority queue data structure.
228 * `NOT_FOUND\r\n` if the job does not exist or is not reserved by the client.
229
230 #### `touch` command
231
232 The `touch` command allows a worker to request more time to work on a job. This is useful for jobs that potentially take a long time, but you still want the benefits of a TTR pulling a job away from an unresponsive worker. A worker may periodically tell the server that it's still alive and processing a job (e.g. it may do this on `DEADLINE_SOON`).
233
234 The touch command looks like this:
235
236 ```
237 touch <id>\r\n
238 ```
239
240 ##### `touch` options
241
242 * `<id>` is the ID of a job reserved by the current connection.
243
244 ##### `touch` responses
245
246 There are two possible responses:
247
248 * `TOUCHED\r\n` to indicate success.
249 * `NOT_FOUND\r\n` if the job does not exist or is not reserved by the client.
250
251 #### `watch` command
252
253 The `watch` command adds the named tube to the watch list for the current connection. A reserve command will take a job from any of the tubes in the watch list. For each new connection, the watch list initially consists of one tube, named `default`.
254
255 ```
256 watch <tube>\r\n
257 ```
258
259 ##### `watch` options
260
261 * `<tube>` is a name at most 200 bytes. It specifies a tube to add to the watch list. If the tube doesn't exist, it will be created.
262
263 ##### `watch` responses
264
265 The reply is:
266
267 * `WATCHING <count>\r\n` `<count>` is the integer number of tubes currently in the watch list.
268
269 ##### `ignore` command
270
271 The `ignore` command is for consumers. It removes the named tube from the watch list for the current connection.
272
273 ```
274 ignore <tube>\r\n
275 ```
276
277 ##### `ignore` options
278
279 * `<tube>` is a name at most 200 bytes. It specifies a tube to add to the watch list. If the tube doesn't exist, it will be created.
280
281 ##### `ignore` command
282
283 The reply is one of:
284
285 * `WATCHING <count>\r\n` to indicate success. `<count>` is the integer number of tubes currently in the watch list.
286 * `NOT_IGNORED\r\n` if the client attempts to ignore the only tube in its watch list.
287
288 ### Other Commands
289
290 #### `peek` command
291
292 The peek commands let the client inspect a job in the system. There are four variations. All but the first operate only on the currently used tube.
293
294 * `peek <id>\r\n` - return `job <id>`.
295 * `peek-ready\r\n` - return the next ready job.
296 * `peek-delayed\r\n` - return the delayed job with the shortest delay left.
297 * `peek-buried\r\n` - return the next job in the list of buried jobs.
298
299 ##### `peek` responses
300
301 There are two possible responses, either a single line:
302
303 * `NOT_FOUND\r\n` if the requested job doesn't exist or there are no jobs in the requested state.
304 * `FOUND <id> <bytes>\r\n <data>\r\n`
305 * `<id>` is the job id.
306 * `<bytes>` is an integer indicating the size of the job body, not including the trailing `\r\n`.
307 * `<data>` is the job body -- a sequence of bytes of length <bytes> from the previous line.
308
309 #### `kick` command
310
311 The kick command applies only to the currently used tube. It moves jobs into the ready queue. If there are any buried jobs, it will only kick buried jobs. Otherwise it will kick delayed jobs. It looks like:
312
313 ```
314 kick <bound>\r\n
315 ```
316
317 ##### `kick` options
318
319 * `<bound>` is an integer upper bound on the number of jobs to kick. The server will kick no more than <bound> jobs.
320
321 ##### `kick` responses
322
323 The response is of the form:
324
325 * `KICKED <count>\r\n`
326 * <count> is an integer indicating the number of jobs actually kicked.
327
328 #### `kick-job` command
329
330 The kick-job command is a variant of kick that operates with a single job identified by its job id. If the given job id exists and is in a buried or delayed state, it will be moved to the ready queue of the the same tube where it currently belongs. The syntax is:
331
332 ```
333 kick-job <id>\r\n
334 ```
335
336 ##### `kick-job` options
337
338 * <id> is the job id to kick.
339
340 ##### `kick-job` responses
341
342 The response is one of:
343
344 * `NOT_FOUND\r\n` if the job does not exist or is not in a kickable state. This can also happen upon internal errors.
345 * `KICKED\r\n` when the operation succeeded.
346
347 #### `stats-jobs` command
348
349 The stats-job command gives statistical information about the specified job if it exists. Its form is:
350
351 ```
352 stats-job <id>\r\n
353 ```
354
355 ##### `stats-jobs` options
356
357 * `<id>` is a job id.
358
359 ##### `stats-jobs` responses
360
361 The response is one of:
362
363 * `NOT_FOUND\r\n` if the job does not exist.
364 * `OK <bytes>\r\n<data>\r\n`
365 * `<bytes>` is the size of the following data section in bytes.
366 * `<data>` is a sequence of bytes of length `<bytes>` from the previous line. It is a YAML file with statistical information represented a dictionary.
367
368 The `stats-job` data is a YAML file representing a single dictionary of strings to scalars. It contains these keys:
369
370 * `id` is the job id
371 * `tube` is the name of the tube that contains this job
372 * `state` is `ready` or `delayed` or `reserved` or `buried`
373 * `pri` is the priority value set by the put, release, or bury commands.
374 * `age` is the time in seconds since the put command that created this job.
375 * `time-left` is the number of seconds left until the server puts this job into the ready queue. This number is only meaningful if the job is reserved or delayed. If the job is reserved and this amount of time elapses before its state changes, it is considered to have timed out.
376 * `file` is the number of the earliest binlog file containing this job. If -b wasn't used, this will be 0.
377 * `reserves` is the number of times this job has been reserved.
378 * `timeouts` is the number of times this job has timed out during a reservation.
379 * `releases` is the number of times a client has released this job from a reservation.
380 * `buries` is the number of times this job has been buried.
381 * `kicks` is the number of times this job has been kicked.
382
383 #### `stats-tube` command
384
385 The stats-tube command gives statistical information about the specified tube if it exists. Its form is:
386
387 ```
388 stats-tube <tube>\r\n
389 ```
390
391 ##### `stats-tube` options
392
393 * `<tube>` is a name at most 200 bytes. Stats will be returned for this tube.
394
395 ##### `stats-tube` responses
396
397 The response is one of:
398
399 * `NOT_FOUND\r\n` if the tube does not exist.
400 * `OK <bytes>\r\n<data>\r\n`
401 * `<bytes>` is the size of the following data section in bytes.
402 * `<data>` is a sequence of bytes of length `<bytes>` from the previous line. It is a YAML file with statistical information represented a dictionary.
403
404 The stats-tube data is a YAML file representing a single dictionary of strings to scalars. It contains these keys:
405
406 * `name` is the tube's name.
407 * `current-jobs-urgent` is the number of ready jobs with priority < 1024 in this tube.
408 * `current-jobs-ready` is the number of jobs in the ready queue in this tube.
409 * `current-jobs-reserved` is the number of jobs reserved by all clients in this tube.
410 * `current-jobs-delayed` is the number of delayed jobs in this tube.
411 * `current-jobs-buried` is the number of buried jobs in this tube.
412 * `total-jobs` is the cumulative count of jobs created in this tube in the current beanstalkd process.
413 * `current-using` is the number of open connections that are currently using this tube.
414 * `current-waiting` is the number of open connections that have issued a reserve command while watching this tube but not yet received a response.
415 * `current-watching` is the number of open connections that are currently watching this tube.
416 * `pause` is the number of seconds the tube has been paused for.
417 * `cmd-delete` is the cumulative number of delete commands for this tube
418 * `cmd-pause-tube` is the cumulative number of pause-tube commands for this tube.
419 * `pause-time-left` is the number of seconds until the tube is un-paused.
420
421 #### `stats` command
422
423 The stats command gives statistical information about the system as a whole. Its form is:
424
425 ```
426 stats\r\n
427 ```
428
429 ##### `stats` responses
430
431 The server will respond:
432
433 ```
434 OK <bytes>\r\n
435 <data>\r\n
436 ```
437
438 * `<bytes>` is the size of the following data section in bytes.
439 * `<data>` is a sequence of bytes of length <bytes> from the previous line. It is a YAML file with statistical information represented a dictionary.
440
441 The stats data for the system is a YAML file representing a single dictionary of strings to scalars. Entries described as "cumulative" are reset when the beanstalkd process starts; they are not stored on disk with the -b flag.
442
443 * `current-jobs-urgent` is the number of ready jobs with priority < 1024.
444 * `current-jobs-ready` is the number of jobs in the ready queue.
445 * `current-jobs-reserved` is the number of jobs reserved by all clients.
446 * `current-jobs-delayed` is the number of delayed jobs.
447 * `current-jobs-buried` is the number of buried jobs.
448 * `cmd-put` is the cumulative number of put commands.
449 * `cmd-peek` is the cumulative number of peek commands.
450 * `cmd-peek-ready` is the cumulative number of peek-ready commands.
451 * `cmd-peek-delayed` is the cumulative number of peek-delayed commands.
452 * `cmd-peek-buried` is the cumulative number of peek-buried commands.
453 * `cmd-reserve` is the cumulative number of reserve commands.
454 * `cmd-use` is the cumulative number of use commands.
455 * `cmd-watch` is the cumulative number of watch commands.
456 * `cmd-ignore` is the cumulative number of ignore commands.
457 * `cmd-delete` is the cumulative number of delete commands.
458 * `cmd-release` is the cumulative number of release commands.
459 * `cmd-bury` is the cumulative number of bury commands.
460 * `cmd-kick` is the cumulative number of kick commands.
461 * `cmd-stats` is the cumulative number of stats commands.
462 * `cmd-stats-job` is the cumulative number of stats-job commands.
463 * `cmd-stats-tube` is the cumulative number of stats-tube commands.
464 * `cmd-list-tubes` is the cumulative number of list-tubes commands.
465 * `cmd-list-tube-used` is the cumulative number of list-tube-used commands.
466 * `cmd-list-tubes-watched` is the cumulative number of list-tubes-watched commands.
467 * `cmd-pause-tube` is the cumulative number of pause-tube commands
468 * `job-timeouts` is the cumulative count of times a job has timed out.
469 * `total-jobs` is the cumulative count of jobs created.
470 * `max-job-size` is the maximum number of bytes in a job.
471 * `current-tubes` is the number of currently-existing tubes.
472 * `current-connections` is the number of currently open connections.
473 * `current-producers` is the number of open connections that have each issued at least one put command.
474 * `current-workers` is the number of open connections that have each issued at least one reserve command.
475 * `current-waiting` is the number of open connections that have issued a reserve command but not yet received a response.
476 * `total-connections` is the cumulative count of connections.
477 * `pid` is the process id of the server.
478 * `version` is the version string of the server.
479 * `rusage-utime` is the cumulative user CPU time of this process in seconds and microseconds.
480 * `rusage-stime` is the cumulative system CPU time of this process in seconds and microseconds.
481 * `uptime` is the number of seconds since this server process started running.
482 * `binlog-oldest-index` is the index of the oldest binlog file needed to store the current jobs
483 * `binlog-current-index` is the index of the current binlog file being written to. If binlog is not active this value will be 0
484 * `binlog-max-size` is the maximum size in bytes a binlog file is allowed to get before a new binlog file is opened
485 * `binlog-records-written` is the cumulative number of records written to the binlog
486 * `binlog-records-migrated` is the cumulative number of records written as part of compaction
487
488 #### `list-tubes` command
489
490 The list-tubes command returns a list of all existing tubes. Its form is:
491
492 ```
493 list-tubes\r\n
494 ```
495
496 ##### `list-tubes` responses
497
498 The response is:
499
500 ```
501 OK <bytes>\r\n
502 <data>\r\n
503 ```
504
505 * `<bytes>` is the size of the following data section in bytes.
506 * `<data>` is a sequence of bytes of length <bytes> from the previous line. It is a YAML file containing all tube names as a list of strings.
507
508 #### `list-tube-used` command
509
510 The list-tube-used command returns the tube currently being used by the client. Its form is:
511
512 ```
513 list-tube-used\r\n
514 ```
515
516 ##### `list-tube-used` responses
517 The response is:
518
519 ```
520 USING <tube>\r\n
521 ```
522
523 * `<tube>` is the name of the tube being used.
524
525 #### `list-tubes-watched` command
526
527 The list-tubes-watched command returns a list tubes currently being watched by the client. Its form is:
528
529 ```
530 list-tubes-watched\r\n
531 ```
532
533 ##### `list-tubes-watched` responses
534
535 The response is:
536
537 ```
538 OK <bytes>\r\n
539 <data>\r\n
540 ```
541
542 * `<bytes>` is the size of the following data section in bytes.
543 * `<data>` is a sequence of bytes of length <bytes> from the previous line. It is a YAML file containing watched tube names as a list of strings.
544
545 #### `quit` command
546
547 The quit command simply closes the connection. Its form is:
548
549 ```
550 quit\r\n
551 ```
552
553 #### `pause-tube` command
554
555 The pause-tube command can delay any new job being reserved for a given time. Its form is:
556
557 ```
558 pause-tube <tube-name> <delay>\r\n
559 ```
560
561 ##### `pause-tube` options
562
563 * `<tube>` is the tube to pause
564 * `<delay>` is an integer number of seconds to wait before reserving any more jobs from the queue
565
566 ##### `pause-tube` responses
567
568 There are two possible responses:
569
570 * `PAUSED\r\n` to indicate success.
571 * `NOT_FOUND\r\n` if the tube does not exist.
Something went wrong with that request. Please try again.