A REST client that survives
The client
A request that fails once is normal: a connection reset, a 502 from a proxy, a timeout. The client below retries with a doubling wait, stops at a retry limit, and stops earlier when the total budget is gone:
require "net/http" require "json" class Client def initialize(url, retries: 3, budget: 5) @uri = URI(url) @retries = retries @budget = budget end def get deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @budget attempt = 0 begin attempt += 1 res = Net::HTTP.start(@uri.host, @uri.port, use_ssl: @uri.scheme == "https", open_timeout: 2, read_timeout: 2) do |http| http.get(@uri.request_uri) end raise "HTTP #{res.code}" unless res.is_a?(Net::HTTPSuccess) JSON.parse(res.body) rescue StandardError => e left = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) raise if attempt > @retries || left <= 0 wait = 0.2 * (2 ** (attempt - 1)) puts "attempt #{attempt} failed (#{e.message}), retrying in #{wait}s" sleep wait retry end end end data = Client.new(ARGV.fetch(0)).get puts "ok: #{data["url"]}"
The pieces that matter: the deadline is computed once with the monotonic clock, the retry condition tests both the count and the clock, and the waiting time doubles so a struggling server is not hit in a tight loop.
A real request
Against a service that answers on the first try, the retry code is invisible and the JSON has the shape the endpoint documents:
ruby client.rb https://httpbin.org/get
ok: https://httpbin.org/get
Retries against a flaky server
To see the retries, a local server answers 500 twice and 200 on the third request. The client logs each attempt:
require "webrick" count = 0 server = WEBrick::HTTPServer.new(Port: 9292, Logger: WEBrick::Log.new(File::NULL), AccessLog: []) server.mount_proc("/flaky") do |_req, res| count += 1 if count < 3 res.status = 500 res.body = "not yet" else res.status = 200 res.body = '{"url":"/flaky"}' end end trap("INT") { server.shutdown } server.start
ruby flaky.rb &
ruby client.rb http://127.0.0.1:9292/flaky
attempt 1 failed (HTTP 500), retrying in 0.2s attempt 2 failed (HTTP 500), retrying in 0.4s ok: /flaky
The third attempt succeeds, and the caller sees one value. The waits add 0.6 seconds, which is the price of not failing the whole job for a transient error.
The rules that keep it safe
- Retry only idempotent requests. A GET, a PUT and a DELETE can be repeated; a POST that creates a row may create two. A retried POST needs an idempotency key the server honours.
- Retry the errors that go away. A 500, a 502, a timeout and a reset are worth another try; a 400 or a 401 will fail the same way forever.
- Bound both the count and the time. A count alone is not enough: three attempts with a growing wait can outlast a user's patience, and a deadline stops the next one.
- Log each attempt. The line above is what makes a slow request explainable after the fact.
What to watch for
rescue StandardError also catches the JSON parse
error, so a malformed body is retried as if the network failed. Parse
after the success check and let a parse error be a parse error, or rescue
the network classes by name.
A retry inside a request that already has a retry (a proxy, a load
balancer, a framework middleware) multiplies. Know how many layers retry,
and give the outer one the larger budget.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
attempt = 2 wait = 0.2 * (2 ** (attempt - 1)) puts waitAnswer
0.4
The second attempt waits 0.2 times 2, which is the doubling backoff the client uses.
-
What does this program print?
attempts = 0 begin attempts += 1 raise "HTTP 500" if attempts < 3 puts "ok after #{attempts}" rescue StandardError retry if attempts < 3 raise endAnswer
ok after 3
retry runs the begin block again, so the third attempt reaches the success line.
-
What does this program print?
require "net/http" res = Net::HTTPResponse::CODE_TO_OBJ["503"].new("1.1", "503", "Service Unavailable") puts res.is_a?(Net::HTTPSuccess) puts res.codeAnswer
false / 503
A 5xx is a server error rather than a success, and code is the string "503" printed without quotes.
-
Which requests may be retried without an idempotency key?
Answer
A GET, a PUT and a DELETE
Repeating an idempotent request gives the same result, while a repeated POST can create a second row.
-
Which failure is worth retrying?
Answer
A 502 from a proxy
A transient gateway error may pass on the next try, while a 400, 401 or 404 fails the same way forever.
-
Why does the client compute the deadline once, before the first attempt?
Answer
So the total time budget covers all the attempts together
A single deadline makes the budget total rather than per attempt, which is what stops a long chain of retries.
-
Why is the monotonic clock used instead of
Time.now?Answer
It cannot be moved by a system clock adjustment, so a time budget stays honest
A wall clock can jump backwards or forwards; a monotonic one only moves ahead.
-
What does this program print?
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 5 puts(Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline)Answer
true
The second reading is a moment after the first, so it is still before the deadline five seconds ahead.
-
What does the client log line add to a slow request?
Answer
It explains after the fact which attempt failed and why
The attempt number and the error message are what make a slow request explainable later.
-
What happens when a retrying client sits behind a proxy that also retries?
Answer
The retries multiply, so the outer layer needs the larger budget
Each layer retries what the layer below already retried, so the counts and the waits compound.
Article author: Arthur Isaev