Join the community today
Become a Member

Cloudflare What CF Worker Usage Model to use?

Discussion in 'Web Development & Web Performance' started by rdan, Jun 1, 2022.

  1. rdan

    rdan Well-Known Member

    5,443
    1,402
    113
    May 25, 2014
    Ratings:
    +2,194
    Local Time:
    2:16 AM
    Mainline
    10.2
    I'm just using this CF worker script for caching guest view:
    Cloudflare Workers: Edge caching anonymous requests - @mmaton


    What model should I use?
    Bundled or Unbound?

    After some reading here:
    https://developers.cloudflare.com/workers/platform/pricing

    It seems Bundled is the best model to choose specially if monthly requests don't reach 10 million.
    Not so sure if I can really hit the 50 ms CPU time / invocation limit.

    My workers created 1 hour ago:
    upload_2022-6-1_6-26-1.png
     
  2. eva2000

    eva2000 Administrator Staff Member

    54,107
    12,179
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,738
    Local Time:
    4:16 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    I just use Bundled as Worker based caching isn't going to use alot of CPU.
     
  3. eva2000

    eva2000 Administrator Staff Member

    54,107
    12,179
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,738
    Local Time:
    4:16 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Just checked my top 4 CF Workers' CPU Time medians :)

    010622-cf-workers-top-4-01.png
     
  4. deltahf

    deltahf Premium Member Premium Member

    585
    264
    63
    Jun 8, 2014
    Ratings:
    +486
    Local Time:
    1:16 PM
    Another vote for Bundled from me. :)

    My Workers handled just over 31 million requests last month and it cost me $9 (plus $2 for KV read operations).
     
  5. eva2000

    eva2000 Administrator Staff Member

    54,107
    12,179
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,738
    Local Time:
    4:16 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Yeah it's relatively cheap for what CF Workers can do :D
     
  6. rdan

    rdan Well-Known Member

    5,443
    1,402
    113
    May 25, 2014
    Ratings:
    +2,194
    Local Time:
    2:16 AM
    Mainline
    10.2
    I only targeted most visited and cacheable pages:
    upload_2022-6-5_17-0-13.png

    And I already have 4 million request for the past 4 days.
    upload_2022-6-5_17-1-13.png

    So I guess I'll be paying $15+ for workers plan.
     
  7. duderuud

    duderuud Premium Member Premium Member

    238
    78
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +170
    Local Time:
    7:16 PM
    1.25 x
    10.6
    Hi @rdan, could you give me some pointers to get this properly set up?
    I'm using the Pro plan but haven't done anything with workers yet. I doubt you can just copy/paste that script.
     
  8. rdan

    rdan Well-Known Member

    5,443
    1,402
    113
    May 25, 2014
    Ratings:
    +2,194
    Local Time:
    2:16 AM
    Mainline
    10.2
    For XF 2.2, this should work and cache your site for 1 hour on CF Edge (with minor edit from the code above).

    HTML:
    addEventListener('fetch', event => {
      event.respondWith(noCacheOnCookie(event.request))
    })
    
    async function noCacheOnCookie(request) {
      const cookie = request.headers.get('Cookie')
      // Cache for 1 hour
      const cacheSeconds = 3600
      if (cookie
        && (
          cookie.includes(`xf_session`)
          || cookie.includes(`xf_user`)
          || cookie.includes(`xf_lscxf_logged_in`)
        )) {
        const bustedRequest = new Request(request, { cf: { cacheTtl: -1 } })
        const response = await fetch(bustedRequest)
    
        const newHeaders = new Headers(response.headers)
        newHeaders.append('xf-cache-busted', `true`)
        return new Response(response.body, {
          status: response.status,
          statusText: response.statusText,
          headers: newHeaders
        })
      } else {
        return fetch(new Request(request, { cf: { cacheTtl: cacheSeconds } }))
      }
    }
    
    This addon is optional:
    LiteSpeed Cache for XF2 - Community

    But if someone can teach us how to bypass cache if some http response header is present, that addon is a must have.
    I just don't know javascript coding.
     
  9. duderuud

    duderuud Premium Member Premium Member

    238
    78
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +170
    Local Time:
    7:16 PM
    1.25 x
    10.6
    That optional plugin only works when running the Litespeed webserver I presume?
     
  10. eva2000

    eva2000 Administrator Staff Member

    54,107
    12,179
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,738
    Local Time:
    4:16 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  11. rdan

    rdan Well-Known Member

    5,443
    1,402
    113
    May 25, 2014
    Ratings:
    +2,194
    Local Time:
    2:16 AM
    Mainline
    10.2
    No, it works with Nginx too.
    The persistent cookie and header of that plugin are great for caching purposes.
     
  12. duderuud

    duderuud Premium Member Premium Member

    238
    78
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +170
    Local Time:
    7:16 PM
    1.25 x
    10.6
    Thanks @rdan, got a worker running now. I'll let it run for a few hours and gonna check some stats and run some benchmarks.
     
  13. rdan

    rdan Well-Known Member

    5,443
    1,402
    113
    May 25, 2014
    Ratings:
    +2,194
    Local Time:
    2:16 AM
    Mainline
    10.2
  14. duderuud

    duderuud Premium Member Premium Member

    238
    78
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +170
    Local Time:
    7:16 PM
    1.25 x
    10.6
    Code:
    cf-cache-status HIT
    
    Code:
    x-xf-cache-status HIT
    
    :)
     
  15. rdan

    rdan Well-Known Member

    5,443
    1,402
    113
    May 25, 2014
    Ratings:
    +2,194
    Local Time:
    2:16 AM
    Mainline
    10.2
    This is from XenForo :)
     
  16. duderuud

    duderuud Premium Member Premium Member

    238
    78
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +170
    Local Time:
    7:16 PM
    1.25 x
    10.6
    So I have to disable the local server caching?

    Edit:
    Holy crap, already burned through the 100.000 free requests, that was fast :nailbiting:

    So upgraded to the paid plan, let's see how much that is gonna cost on a monthly basis..
     
    Last edited: Jun 6, 2022
  17. rdan

    rdan Well-Known Member

    5,443
    1,402
    113
    May 25, 2014
    Ratings:
    +2,194
    Local Time:
    2:16 AM
    Mainline
    10.2
    It's up to you :)

    What is your HTTP routes looks like?
    Try targeting those cacheable pages only.
    Like what i did above.
     
  18. duderuud

    duderuud Premium Member Premium Member

    238
    78
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +170
    Local Time:
    7:16 PM
    1.25 x
    10.6
    I use the same as you
     
  19. rdan

    rdan Well-Known Member

    5,443
    1,402
    113
    May 25, 2014
    Ratings:
    +2,194
    Local Time:
    2:16 AM
    Mainline
    10.2
    CF Worker can be abused also, during DDOS attack some requests will pass through, be counted and billable :|

    upload_2022-6-21_8-45-44.png
     
  20. eva2000

    eva2000 Administrator Staff Member

    54,107
    12,179
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,738
    Local Time:
    4:16 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    That is normal if you configured CF Worker for caching and the attack is HTTP layer 7 application level attack which Cloudflare doesn't see as a DDOS attack.