Welcome to Centmin Mod Community
Register Now

Cloudflare What CF Worker Usage Model to use?

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

  1. rdan

    rdan Premium Member Premium Member

    5,327
    1,337
    113
    May 25, 2014
    Ratings:
    +2,065
    Local Time:
    10:46 PM
    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

    49,234
    11,291
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,567
    Local Time:
    12:46 AM
    Nginx 1.21.x
    MariaDB 10.x
    I just use Bundled as Worker based caching isn't going to use alot of CPU.
     
  3. eva2000

    eva2000 Administrator Staff Member

    49,234
    11,291
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,567
    Local Time:
    12:46 AM
    Nginx 1.21.x
    MariaDB 10.x
    Just checked my top 4 CF Workers' CPU Time medians :)

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

    deltahf Premium Member Premium Member

    534
    237
    43
    Jun 8, 2014
    Ratings:
    +431
    Local Time:
    10:46 AM
    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

    49,234
    11,291
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,567
    Local Time:
    12:46 AM
    Nginx 1.21.x
    MariaDB 10.x
    Yeah it's relatively cheap for what CF Workers can do :D
     
  6. rdan

    rdan Premium Member Premium Member

    5,327
    1,337
    113
    May 25, 2014
    Ratings:
    +2,065
    Local Time:
    10:46 PM
    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

    103
    31
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +73
    Local Time:
    4:46 PM
    1.21.x
    10.4
    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 Premium Member Premium Member

    5,327
    1,337
    113
    May 25, 2014
    Ratings:
    +2,065
    Local Time:
    10:46 PM
    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

    103
    31
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +73
    Local Time:
    4:46 PM
    1.21.x
    10.4
    That optional plugin only works when running the Litespeed webserver I presume?
     
  10. eva2000

    eva2000 Administrator Staff Member

    49,234
    11,291
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,567
    Local Time:
    12:46 AM
    Nginx 1.21.x
    MariaDB 10.x
  11. rdan

    rdan Premium Member Premium Member

    5,327
    1,337
    113
    May 25, 2014
    Ratings:
    +2,065
    Local Time:
    10:46 PM
    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

    103
    31
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +73
    Local Time:
    4:46 PM
    1.21.x
    10.4
    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 Premium Member Premium Member

    5,327
    1,337
    113
    May 25, 2014
    Ratings:
    +2,065
    Local Time:
    10:46 PM
    Mainline
    10.2
  14. duderuud

    duderuud Premium Member Premium Member

    103
    31
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +73
    Local Time:
    4:46 PM
    1.21.x
    10.4
    Code:
    cf-cache-status HIT
    
    Code:
    x-xf-cache-status HIT
    
    :)
     
  15. rdan

    rdan Premium Member Premium Member

    5,327
    1,337
    113
    May 25, 2014
    Ratings:
    +2,065
    Local Time:
    10:46 PM
    Mainline
    10.2
    This is from XenForo :)
     
  16. duderuud

    duderuud Premium Member Premium Member

    103
    31
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +73
    Local Time:
    4:46 PM
    1.21.x
    10.4
    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 Premium Member Premium Member

    5,327
    1,337
    113
    May 25, 2014
    Ratings:
    +2,065
    Local Time:
    10:46 PM
    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

    103
    31
    28
    Dec 5, 2020
    The Netherlands
    Ratings:
    +73
    Local Time:
    4:46 PM
    1.21.x
    10.4
    I use the same as you
     
  19. rdan

    rdan Premium Member Premium Member

    5,327
    1,337
    113
    May 25, 2014
    Ratings:
    +2,065
    Local Time:
    10:46 PM
    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

    49,234
    11,291
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +17,567
    Local Time:
    12:46 AM
    Nginx 1.21.x
    MariaDB 10.x
    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.