Documentation for cf-request-id/cf-ray

Cloudflare requests sent into the workers has two very useful headers:

  • cf-request-id (made up of a 10 digit timeSince1jan2020 + 8 digit worker instance id/hash + 5 digit request id (looks like a random number, but could also be an internal cloudflare counter) + 000000001 (9 digit padding??))
  • cf-ray (request id hash + ‘-’ + POP id)

demo of how to extract info from cf-request-id/cf-ray

Could you please provide some documentation about these headers?

To test that digits 11-18 in cf-request-id is the worker instance, you simply make 15 requests in very high frequency to the same worker, and then see how the workerid numbers in the cf-request-id correlate with the count variable.

function parseCFHeaders(cfRequestId, cfRay){
  console.log(cfRequestId)
  const timeSince_jan1_2020 = parseInt(cfRequestId.substr(0,10), 16);
  const jan1_2020 = 1577836800166;
  const timestamp = timeSince_jan1_2020 + jan1_2020;

  const workerId = cfRequestId.substr(10,8);
  
  const reqIdRandom = cfRequestId.substr(18,5);

  //const padding = cfRequestId.substr(23); //always 000000001

  const [ray, colo] = cfRay.split('-');
  return {timestamp, workerId, reqIdRandom, colo, ray};
}

let count = 0;

async function handleRequest(req) {
  const cfi = req.headers.get('cf-request-id');
  const cfr = req.headers.get('cf-ray');
  const info = parseCFHeaders(cfi, cfr);

  //return new Response(JSON.stringify([count++, info]));
  //return new Response(Date.now() +' is just a little bit more than ' + info.timestamp);
  return new Response(' '+ info.workerId +':'+ count++);
}

addEventListener('fetch', e => e.respondWith(handleRequest(e.request)));
for i in {1..15}; do curl https://royal-snowflake.2js-no.workers.dev/; done

Thanks to judge for illustrating the way to test counters in workers using curl!

Is “cf-request-id” discontinued?