高性能缓存服务器 Nuster

简介: Nuster 是一个基于 HAProxy 的高性能缓存服务器。Nuster 完全兼容 HAProxy,并且利用 HAProxy 的 ACL 功能来提供非常细致的缓存规则,比如 请求地址为某某时缓存 请求参数中的 X 为 Y 时缓存 响应头中的 X 为 Y 时缓存 请求速率超过多少时缓存 等等 Nuster 性能评测 非常快, 单进程模式下是 nginx 的 3 倍,多进程下 nginx 的 2 倍,varnish 的 3 倍。

Nuster 是一个基于 HAProxy 的高性能缓存服务器。Nuster 完全兼容 HAProxy,并且利用 HAProxy 的 ACL 功能来提供非常细致的缓存规则,比如

  • 请求地址为某某时缓存
  • 请求参数中的 X 为 Y 时缓存
  • 响应头中的 X 为 Y 时缓存
  • 请求速率超过多少时缓存
  • 等等

Nuster 性能评测

非常快, 单进程模式下是 nginx 的 3 倍,多进程下 nginx 的 2 倍,varnish 的 3 倍。详见benchmark

安装 Nuster

git clone https://github.com/jiangwenyuan/nuster.git
cd nuster
make TARGET=linux2628
make install

详情请参照 HAProxy README

Naster 使用方法

global中添加cache on, 然后在backendlisten中添加 cache filter 和 cache rule

Naster 指令

cache

syntax: cache on|off [data-size size]

default: none

context: global

控制是否开启缓存。 可以设置data-size来控制缓存数据的内存使用量。可以使用mMg 和 G. 默认是 1MB,同时也是最小使用量。只有 http 内容计算在内,并不包括使用缓存带来的内存开销。

filter cache

syntax: filter cache [on|off]

default: on

context: backendlisten

定义一个 cache filter, 另外cache-rule也需要添加。 可以为多个代理添加,并单独设置某个代理的缓存是否开启。 如果定义了多个 filter,需要把 cache filter 放在最后。

cache-rule

syntax: cache-rule name [key KEY] [ttl TTL] [code CODE] [if|unless condition]

default: none

context: backendlisten

定义缓存规则。可以同时定义多个,但是需要注意顺序,匹配则会停止测试。

acl pathA path /a.html
filter cache
cache-rule all ttl 3600
cache-rule path01 ttl 60 if pathA

path01这条规则永远不会执行,因为 all 会匹配所有的规则。

name

定义一个名字。

key KEY

定义 key,由以下关键字组成:

  • method: http method, GET/POST…
  • scheme: http or https
  • host: the host in the request
  • path: the URL path of the request
  • query: the whole query string of the request
  • header_NAME: the value of header NAME
  • cookie_NAME: the value of cookie NAME
  • param_NAME: the value of query NAME
  • body: the body of the request

默认 key 是method.scheme.host.path.query.body

Example

GET http://www.example.com/q?name=X&type=Y

http header:
GET /q?name=X&type=Y HTTP/1.1
Host: www.example.com
ASDF: Z
Cookie: logged_in=yes; user=nuster;

会得到:

  • method: GET
  • scheme: http
  • host: www.example.com
  • path: /q
  • query: name=X&type=Y
  • header_ASDF: Z
  • cookie_user: nuster
  • param_type: Y
  • body: (empty)

所以默认的 key 就会得到GEThttpwww.example.com/qname=X&type=Y, 而 key method.scheme.host.path.header_ASDF.cookie_user.param_type则会生成 GEThttpwww.example.com/qZnusterY

一个请求的 key 能在缓存中找到则返回缓存内容。

ttl TTL

定义 key 的失效时间,可以使用 dhm and s。默认3600秒. 如果不希望失效则设为 0

code CODE1,CODE2…

默认只缓存 200 的响应,如果需要缓存其他的则可以添加,all会缓存任何状态码。

cache-rule only200
cache-rule 200and404 code 200,404
cache-rule all code all

if|unless condition

定义 ACL 条件 详见HAProxy configuration7. Using ACLs and fetching samples

常见问题

如何调试?

global添加debug, 或者带-d启动haproxy

缓存相关的调试信息以[CACHE]开头

如何缓存 POST 请求?

添加option http-buffer-request

如果自定义了 key 的话需要使用body关键字

请求 body 可能不完整,详见HAProxy configuration 的 option http-buffer-request小节

另外可以为 post 请求单独设置一个后端

配置样例

global
    cache on data-size 100m
    #daemon
    ## to debug cache
    #debug
defaults
    retries 3
    option redispatch
    timeout client  30s
    timeout connect 30s
    timeout server  30s
frontend web1
    bind *:8080
    mode http
    acl pathPost path /search
    use_backend app1a if pathPost
    default_backend app1b
backend app1a
    balance roundrobin
    # mode must be http
    mode http

    # http-buffer-request must be enabled to cache post request
    option http-buffer-request

    acl pathPost path /search

    # enable cache for this proxy
    filter cache

    # cache /search for 120 seconds. Only works when POST/PUT
    cache-rule rpost ttl 120 if pathPost

    server s1 10.0.0.10:8080
backend app1b
    balance     roundrobin
    mode http

    filter cache on

    # cache /a.jpg, not expire
    acl pathA path /a.jpg
    cache-rule r1 ttl 0 if pathA

    # cache /mypage, key contains cookie[userId], so it will be cached per user
    acl pathB path /mypage
    cache-rule r2 key method.scheme.host.path.query.cookie_userId ttl 60 if pathB

    # cache /a.html if response's header[cache] is yes
    http-request set-var(txn.pathC) path
    acl pathC var(txn.pathC) -m str /a.html
    acl resHdrCache1 res.hdr(cache) yes
    cache-rule r3 if pathC resHdrCache1

    # cache /heavy for 100 seconds if be_conn greater than 10
    acl heavypage path /heavy
    acl tooFast be_conn ge 100
    cache-rule heavy ttl 100 if heavypage tooFast 

    # cache all if response's header[asdf] is fdsa
    acl resHdrCache2 res.hdr(asdf)  fdsa
    cache-rule resCache ttl 0 if resHdrCache1

    server s1 10.0.0.10:8080

frontend web2
    bind *:8081
    mode http
    default_backend app2
backend app2
    balance     roundrobin
    mode http

    # disable cache on this proxy
    filter cache off
    cache-rule all

    server s2 10.0.0.11:8080

listen web3
    bind *:8082
    mode http

    filter cache
    cache-rule everything

    server s3 10.0.0.12:8080

原文发布时间:2017-12-15
本文来自云栖社区合作伙伴“ Debian社区”,了解相关信息可以关注“ Debian社区”。
相关文章
|
28天前
|
缓存 运维 监控
Redis 7.0 高性能缓存架构设计与优化
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Redis 7.0高性能缓存架构,探索函数化编程、多层缓存、集群优化与分片消息系统,用代码在二进制星河中谱写极客诗篇。
|
5月前
|
缓存 NoSQL Java
Redis+Caffeine构建高性能二级缓存
大家好,我是摘星。今天为大家带来的是Redis+Caffeine构建高性能二级缓存,废话不多说直接开始~
837 0
|
3月前
|
安全
基于Reactor模式的高性能服务器之Acceptor组件(处理连接)
本节介绍了对底层 Socket 进行封装的设计与实现,通过 `Socket` 类隐藏系统调用细节,提供简洁、安全、可读性强的接口。重点包括 `Socket` 类的核心作用(管理 `sockfd_`)、成员函数的功能(如绑定地址、监听、接受连接等),以及 `Acceptor` 组件的职责:监听连接、接收新客户端连接并分发给上层处理。同时说明了 `Acceptor` 与 `EventLoop` 和 `TcpServer` 的协作关系,并展示了其成员变量和关键函数的工作机制。
79 2
|
11月前
|
缓存 监控 定位技术
|
消息中间件 缓存 NoSQL
Redis 是一个高性能的键值对存储系统,常用于缓存、消息队列和会话管理等场景。
【10月更文挑战第4天】Redis 是一个高性能的键值对存储系统,常用于缓存、消息队列和会话管理等场景。随着数据增长,有时需要将 Redis 数据导出以进行分析、备份或迁移。本文详细介绍几种导出方法:1)使用 Redis 命令与重定向;2)利用 Redis 的 RDB 和 AOF 持久化功能;3)借助第三方工具如 `redis-dump`。每种方法均附有示例代码,帮助你轻松完成数据导出任务。无论数据量大小,总有一款适合你。
213 6
|
7月前
|
人工智能 安全 大数据
【限时特惠】阿里云服务器7折抢购!高性能+高性价比,开发者与企业必备攻略
阿里云服务器限时7折特惠,高性能、高性价比,为开发者和企业量身打造!新老用户均可参与,灵活配置满足多种需求,全球节点低延迟覆盖。自研神龙架构保障稳定性,安全防护全面,操作便捷,生态丰富。适用于个人开发、企业部署、跨境业务及AI计算等场景。点击专属链接立即抢购,活动名额有限,速来享受云端算力带来的高效体验!
164 0
|
8月前
|
存储 人工智能 弹性计算
2025年阿里云企业高性能云服务器租用价格与选型详解
随着企业数字化转型,阿里云于2025年推出多款高性能云服务器实例,涵盖计算、通用和内存密集型场景。文章分析了企业选择云服务器的核心要点,包括明确业务需求(如计算密集型任务推荐计算型实例)、性能与架构升级(如第八代实例性能提升20%),以及第九代实例支持AI等高算力需求。同时提供了配置价格参考和成本优化策略,助力企业实现效率与成本的最优平衡。
|
11月前
|
缓存 NoSQL PHP
Redis作为PHP缓存解决方案的优势、实现方式及注意事项。Redis凭借其高性能、丰富的数据结构、数据持久化和分布式支持等特点,在提升应用响应速度和处理能力方面表现突出
本文深入探讨了Redis作为PHP缓存解决方案的优势、实现方式及注意事项。Redis凭借其高性能、丰富的数据结构、数据持久化和分布式支持等特点,在提升应用响应速度和处理能力方面表现突出。文章还介绍了Redis在页面缓存、数据缓存和会话缓存等应用场景中的使用,并强调了缓存数据一致性、过期时间设置、容量控制和安全问题的重要性。
216 5
|
缓存 NoSQL Ubuntu
大数据-39 Redis 高并发分布式缓存 Ubuntu源码编译安装 云服务器 启动并测试 redis-server redis-cli
大数据-39 Redis 高并发分布式缓存 Ubuntu源码编译安装 云服务器 启动并测试 redis-server redis-cli
183 3
|
缓存 监控 Java
造轮子能力大提升:基于SpringBoot打造高性能缓存组件
在快节奏的软件开发领域,"不重复造轮子" 常常被视为提高效率的金科玉律。然而,在某些特定场景下,定制化的高性能缓存组件却是提升系统性能、优化用户体验的关键。今天,我们将深入探讨如何利用SpringBoot框架,从零开始打造一款符合项目需求的高性能缓存组件,分享我在这一过程中的技术心得与学习体会。
176 6

热门文章

最新文章