capauwels
03/04/2021, 8:45 AM2021-03-04
Value:
{
"opens": 104,
"devices": {
"mobile": 84,
"desktop": 20
}
}
Option 2:
Key: opens/2021-03-04
Value: 104
Key: devices/2021-03-04
Value: {
"mobile": 84,
"desktop": 20
}
Both options would allow me to fetch the data for a given timespan, with state.storage.list()
and the appropriate start
and end
. I'm leaning towards option 2, since it would avoid the transaction issues I've had with concurrent requests failing (Transaction failed due to conflict while processing a put()
), but I wanted to ask which method is the best, most performant and most future-proof way to go about it.jed
03/04/2021, 9:34 AMcapauwels
03/04/2021, 10:38 AMopens
is just one of the metrics I want to track, there will be quite a few more (thinking of clicks, scroll position, duration, countries, etc.). So the question is more, should I create many metric + time
keys, or just a time
key with a big object containing all the metrics?Robin
03/04/2021, 1:07 PMRobin
03/04/2021, 1:12 PMjed
03/04/2021, 2:42 PMhaneefmubarak
03/04/2021, 6:58 PMhaneefmubarak
03/04/2021, 7:00 PMRobin
03/04/2021, 7:04 PMhaneefmubarak
03/04/2021, 7:07 PM[date]-[random number]
for each incoming request that you want to log metadata about and then schedule a daily cron trigger worker (https://developers.cloudflare.com/workers/platform/cron-triggers) that could call out to your DO(s) and tell it/them to collate the previous day's entries into a single object with a key like collated-[date]
etc and then delete the previous day's entries. This would let you ensure you received zero-contention writes, thus maintaining high performance while still getting you the analytics behavior you want.Robin
03/04/2021, 7:08 PMRobin
03/04/2021, 7:10 PMRobin
03/04/2021, 7:10 PMRobin
03/04/2021, 7:11 PMhaneefmubarak
03/04/2021, 7:11 PMhaneefmubarak
03/04/2021, 7:12 PMhaneefmubarak
03/04/2021, 7:12 PMhaneefmubarak
03/04/2021, 7:12 PMRobin
03/04/2021, 7:12 PMRobin
03/04/2021, 7:13 PMhaneefmubarak
03/04/2021, 7:13 PMRobin
03/04/2021, 7:13 PMhaneefmubarak
03/04/2021, 7:14 PMhaneefmubarak
03/04/2021, 7:22 PMstate.storage
) and an in-memory state (this.XYZ
) limited-length buffer (ie: ring/circular - https://stackoverflow.com/questions/1583123/circular-buffer-in-javascript presents some ideas). Then you can serve read-requests from in-memory state and only reload from persistent storage upon (re)initialization of your DO.Robin
03/04/2021, 7:33 PMhaneefmubarak
03/04/2021, 7:35 PMPhilipA
03/04/2021, 10:58 PMkenton
03/05/2021, 2:59 AMeidam | SuperSaaS
03/06/2021, 2:04 PM