DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Develop a Reverse Proxy With Caching in Go
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Enhanced Query Caching Mechanism in Hibernate 6.3.0
  • Dive Into Tokenization, Attention, and Key-Value Caching

Trending

  • Java’s Next Act: Native Speed for a Cloud-Native World
  • A Guide to Container Runtimes
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  1. DZone
  2. Data Engineering
  3. Data
  4. Web Resource Caching: Server-Side

Web Resource Caching: Server-Side

Learn more about server-side web resource caching.

By 
Nicolas Fränkel user avatar
Nicolas Fränkel
DZone Core CORE ·
Dec. 07, 22 · Analysis
Likes (6)
Comment
Save
Tweet
Share
4.5K Views

Join the DZone community and get the full member experience.

Join For Free

The subject of Web resource caching is as old as the World Wide Web itself. However, I'd like to offer an as-exhaustive-as-possible catalogue of how one can improve performance by caching. Web resource caching can happen in two different places: client-side - on the browser and server-side. In the previous post, I explained the former; this post focuses on the latter.

While client-side caching works well, it has one central issue: to serve the resource locally, it must first have it in the cache. Thus, each client needs its cached resource. If the requested resource is intensive to compute, it doesn't scale. The idea behind server-side caching is to compute the resource once and serve it from the cache to all clients.

server-side caching


A couple of dedicated server-side resource caching solutions have emerged over the years: Memcached, Varnish, Squid, etc. Other solutions are less focused on web resource caching and more generic, e.g., Redis or Hazelcast.

If you want to dive deeper into generic caching solutions, please check these two posts on the subject.

To continue with the sample from last week, I'll use Apache APISIX to demo server-side caching. APISIX relies on the proxy-cache plugin for caching. Unfortunately, at the moment, APISIX doesn't integrate with any third-party caching solution. It offers two options: memory-based and disk-based.

In general, the former is faster, but memory is expensive, while the latter is slower, but disk storage is cheap. Within OpenResty, however, the disk option may be faster because of how LuaJIT handles memory. You should probably start with the disk, and if it's not fast enough, mount /dev/shm.

Here are my new routes:

YAML
 
routes:
  - uri: /cache
    upstream_id: 1
    plugins:
      proxy-rewrite:
        regex_uri: ["/cache(.*)", "/$1"]
      proxy-cache: ~


Note that the default cache key is the host and the request URI, which includes query parameters.

The default proxy-cache configuration uses the default disk-based configuration:

YAML
 
proxy_cache:                      # Proxy Caching configuration
  cache_ttl: 10s                  # The default caching time in disk if the upstream does not specify the cache time
  zones:                          # The parameters of a cache
    - name: disk_cache_one        # The name of the cache, administrator can specify
                                  # which cache to use by name in the admin api (disk|memory)
      memory_size: 50m            # The size of shared memory, it's used to store the cache index for
                                  # disk strategy, store cache content for memory strategy (disk|memory)
      disk_size: 1G               # The size of disk, it's used to store the cache data (disk)
      disk_path: /tmp/disk_cache_one  # The path to store the cache data (disk)
      cache_levels: 1:2           # The hierarchy levels of a cache (disk)
    - name: memory_cache
      memory_size: 50m


We can test the setup with curl:

Shell
 
curl -v localhost:9080/cache


The response is interesting:

Plain Text
 
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 147
< Connection: keep-alive
< Date: Tue, 29 Nov 2022 13:17:00 GMT
< Last-Modified: Wed, 23 Nov 2022 13:58:55 GMT
< ETag: "637e271f-93"
< Server: APISIX/3.0.0
< Apisix-Cache-Status: MISS                      #1
< Accept-Ranges: bytes
  1. Because the cache is empty, APISIX has a cache miss. Hence, the response is from the upstream

If we curl again before the default cache expiration period (300 seconds), the response is from the cache:


Plain Text
 
< HTTP/1.1 200 OK
...
< Apisix-Cache-Status: HIT


After the expiration period, the response is from the upstream, but the header is explicit:

Plain Text
 
< HTTP/1.1 200 OK
...
< Apisix-Cache-Status: EXPIRED


Note that we can explicitly purge the entire cache by using the custom PURGE HTTP method:

Shell
 
curl localhost:9080/cache -X PURGE


After purging the cache, the above cycle starts anew.

Note that it's also possible to bypass the cache, e.g., for testing purposes. We can configure the plugin accordingly:

YAML
 
routes:
  - uri: /cache*
    upstream_id: 1
      proxy-cache:
        cache_bypass: ["$arg_bypass"]       #1
  1. Bypass the cache if you send a bypass query parameter with a non-0 value
Shell
 
curl -v localhost:9080/cache?bypass=please


It serves the resource from the upstream regardless of the cache status:

Plain Text
 
< HTTP/1.1 200 OK
...
< Apisix-Cache-Status: BYPASS


For more details on all available configuration parameters, check the proxy-cache plugin.

Conclusion

This post was relatively straightforward. The most challenging issue with server-side caching is the configuration: what to cache, for how long, etc. Unfortunately, it depends significantly on your context, problems, and available resources. You probably need to apply PDCA: guesstimate a relevant configuration, apply it, measure the performance, and rinse and repeat until you find your sweet spot.

I hope that with an understanding of both client-side and server-side caching, you'll be able to improve the performance of your applications.

The source code is available on GitHub.

To go further:

  • Cache API responses
  • proxy-cache plugin
Cache (computing)

Published at DZone with permission of Nicolas Fränkel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Develop a Reverse Proxy With Caching in Go
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Enhanced Query Caching Mechanism in Hibernate 6.3.0
  • Dive Into Tokenization, Attention, and Key-Value Caching

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!