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

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.

  1. What does this program print? attempt = 2 wait = 0.2 * (2 ** (attempt - 1)) puts wait

    Answer

    0.4

    The second attempt waits 0.2 times 2, which is the doubling backoff the client uses.

  2. 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 end

    Answer

    ok after 3

    retry runs the begin block again, so the third attempt reaches the success line.

  3. 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.code

    Answer

    false / 503

    A 5xx is a server error rather than a success, and code is the string "503" printed without quotes.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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