Introducing mcrouter: A memcached protocol router for scaling memcached deployments

简介: Mcrouter 是一个基于Memcached 协议的路由器,它是 Facebook缓存架构的核心组件,在峰值的时候,它能够处理每秒50亿次的请求。近日,Facebook开放了Mcrouter的源代码,且遵从BSD协议,希望能够帮助更多的网站使用Mcrouter并扩大其系统规模。

Most web-based services begin as a collection of front-end application servers paired with databases used to manage data storage. As they grow, the databases are augmented with caches to store frequently-read pieces of data and improve site performance. Often, the ability to quickly access data moves from being an optimization to a requirement for a site. This evolution of cache from neat optimization to necessity is a common path that has been followed by many large web scale companies, including Facebook, Twitter[1], Instagram, Reddit, and many others.

Last year, at the Data@Scale event and at the USENIX Networked Systems Design and Implementation conference , we spoke about turning caches into distributed systems using software we developed called mcrouter (pronounced “mick-router”). Mcrouter is a memcached protocol router that is used at Facebook to handle all traffic to, from, and between thousands of cache servers across dozens of clusters distributed in our data centers around the world. It is proven at massive scale — at peak, mcrouter handles close to 5 billion requests per second. Mcrouter was also proven to work as a standalone binary in an Amazon Web Services setup when Instagram used it last year before fully transitioning to Facebook's infrastructure.

Today, we are excited to announce that we are releasing mcrouter’s code under an open-source BSD license. We believe it will help many sites scale more easily by leveraging Facebook’s knowledge about large-scale systems in an easy-to-understand and easy-to-deploy package.

Features

Since any client that wants to talk to memcached can already speak the standard ASCII memcached protocol, we use that as the common API and enter the picture silently. To a client, mcrouter looks like a memcached server. To a server, mcrouter looks like a normal memcached client. But mcrouter's feature-rich configurability makes it more than a simple proxy.

Some features of mcrouter are listed below. In the following, a “destination” is a memcached host (or some other cache service that understands the memcached protocol) and “pool” is a set of destinations configured for some workload — e.g., a sharded pool with a specified hashing function, or a replicated pool with multiple copies of the data on separate hosts. Finally, pools can be organized into multiple clusters.

  • Standard open source memcached ASCII protocol support: Any client that can talk the memcached protocol can already talk to mcrouter — no changes are needed. Mcrouter can simply be simply dropped in between clients and memcached boxes to take advantage of its functionality.
  • Connection pooling: Multiple clients can connect to a single mcrouter instance and share the outgoing connections, reducing the number of open connections to memcached instances.
  • Multiple hashing schemes: Mcrouter provides a proven consistent hashing algorithm (furc_hash) that allows distribution of keys across many memcached instances. Hostname hashing is useful for selecting a unique replica per client. There are a number of other hashes useful in specialized applications.
  • Prefix routing: Mcrouter can route keys according to common key prefixes. For example, you can send all keys starting with “foo” to one pool, “bar” prefix to another pool, and everything else to a “wildcard” pool. This is a simple way to separate different workloads.
  • Replicated pools: A replicated pool has the same data on multiple hosts. Writes are replicated to all hosts in the pool, while reads are routed to a single replica chosen separately for each client. This could be done either due to per-host packet limitations where a sharded pool would not be able to handle the read rate; or for increased availability of the data (one replica going down doesn't affect availability due to automatic failover).
  • Production traffic shadowing: When testing new cache hardware, we found it extremely useful to be able to route a complete copy of production traffic from clients. Mcrouter supports flexible shadowing configuration. It's possible to shadow test a different pool size (re-hashing the key space), shadow only a fraction of the key space, or vary shadowing settings dynamically at runtime.
  • Online reconfiguration: Mcrouter monitors its configuration files and automatically reloads them on any file change; this loading and parsing is done on a background thread and new requests are routed according to the new configuration as soon as it's ready. There's no extra latency from client's point of view.
  • Flexible routing: Configuration is specified as a graph of small routing modules called “route handles,” which share a common interface (route a request and return a reply) and which can be composed freely. Route handles are easy to understand, create, and test individually, allowing for arbitrarily complex logic when used together. For example: An “all-sync” route handle will be set up with multiple child route handles (which themselves could be arbitrary route handles). It will pass a request to all of its children and wait for all of the replies to come back before returning one of these replies. Other examples include, among many others, “all-async” (send to all but don't wait for replies), “all-majority” (for consensus polling), and “failover” (send to every child in order until an non-error reply is returned). Expanding a pool can be done quickly by using a “cold cache warmup” route handle on the pool (with the old set of servers as the warm pool). Moving this handle handle up the stack will allow for an entire cluster to be warmed up from a warm cluster.
  • Destination health monitoring and automatic failover: Mcrouter keeps track of the health status of each destination. If mcrouter marks a destination as unresponsive, it will fail over incoming requests to an alternate destination automatically (fast failover) without attempting to send them to the original destination. At the same time health check requests will be sent in the background, and as soon as a health check is successful, mcrouter will revert to using the original destination. We distinguish between “soft errors” (e.g., data timeouts) that are allowed to happen a few times in a row and “hard errors” (e.g., connection refused) that cause a host to be marked unresponsive immediately. Needless to say, all of this is completely transparent to the client.
  • Cold cache warm up: Mcrouter can smooth the performance impact of starting a brand new empty cache host or set of hosts (as large as an entire cluster) by automatically refilling it from a designated “warm” cache.
  • Broadcast operations: By adding a special prefix to a key in a request, it's easy to replicate the same request into multiple pools and/or clusters.
  • Reliable delete stream: In a demand-filled look-aside cache, it's important to ensure all deletes are eventually delivered to guarantee consistency. Mcrouter supports logging delete commands to disk in cases when the destination is not accessible (due to a network outage or other failure). A separate process then replays those deletes asynchronously. This is done transparently to the client — the original delete command is always reported as successful.
  • Multi-cluster support: Configuration management for large multi-cluster setups is easy. A single config can be distributed to all clusters and, depending on command line options, mcrouter will interpret the config based on its location.
  • Rich stats and debug commands: Mcrouter exports many internal counters (via a “stats” command; also to a JSON file on disk). Introspection debug commands are also available, which can answer questions like “Which host would a particular request go to?” at runtime.
  • Quality of service: Mcrouter allows throttling the rate of any type of request (e.g., get/set/delete) at any level (per-host, per-pool, per-cluster), rejecting requests over a specified limit. We also support rate limit requests to slow delivery.
  • Large values: Mcrouter can automatically split/re-stitch large values that would not normally fit in a memcached slab.
  • Multi-level caches: Mcrouter supports local/remote cache setup, where values would be looked up locally first and automatically set in a local cache from remote after fetching.
  • IPv6 support: We have strong support internally for IPv6 at Facebook, so mcrouter is IPv6 compatible out of the box.
  • SSL support: Mcrouter supports SSL connections (incoming or outgoing), as long as the client or the destination hosts support it as well. It is also possible to set up multiple mcrouters in series, in which case the middle connection between mcrouters can be over SSL out of the box.
  • Multi-threaded architecture: Mcrouter can take full advantage of multicore systems by starting one thread per core.

Implementation

Mcrouter is written mostly in C++ (with heavy use of C++11 features), with some library code written in C and protocol parsing code written in Ragel. It uses Facebook's open source libraries Folly and fbthrift (for async networking code).

A mcrouter process starts up multiple independent threads, each running an event loop that processes all network events asynchronously (using libevent). Once each request or reply is parsed, it's processed inside its own lightweight thread or “fiber”; we have a custom fiber library implementation built on top of boost::context.

Mcrouter configuration is written in JSON format and allows specifying an arbitrary route handle scheme to easily adapt to any routing task. We have presented some common use cases in depth on our wiki.

What's next

We invite software engineers using memcached everywhere to evaluate mcrouter and see if it helps to simplify the site administration while providing the new capabilities listed above (shadow testing, cold cache warmup, and so on). Instagram used mcrouter for the last year, before transitioning to Facebook's infrastructure, so mcrouter is proven in an Amazon Web Services setup. Prior to open sourcing, we partnered with Reddit for a limited beta test, and they are currently running mcrouter in production for some of their caches.

We would also love to see patches come back that will make mcrouter more helpful to you and to others in the memcached community.

Mcrouter source code has been open sourced at https://github.com/facebook/mcrouter. We're always looking for ways to improve mcrouter's performance, fix bugs, and add new features. We will continuously update the external Github repo with our internal changes, so you can benefit from this work as well. We maintain mcrouter documentation on the Github wiki. We have also set up a Facebook discussion group.

Footnotes:

[1] https://blog.twitter.com/2012/twemproxy
相关文章
|
2月前
|
存储 人工智能 安全
Agent Harness 到底是什么:模型之外的那层控制系统
AI Agent Harness 是包裹大模型的“运行支架”,提供工具调用、记忆管理、权限控制、安全护栏、可观测性与故障恢复等能力,将聪明但无约束的模型,转化为安全、可控、可审计的生产级智能体。
700 1
Agent Harness 到底是什么:模型之外的那层控制系统
|
调度
关于定时任务,看着一系列就够了——2.Cron表达式
软件开发定时任务基础—— Cron 表达式介绍
2134 0
关于定时任务,看着一系列就够了——2.Cron表达式
|
弹性计算 负载均衡 定位技术
阿里云服务器ECS【地域】如何选择?不同地域区别及优缺点对比
阿里云服务器地域选择需综合考虑用户地理位置、网络延迟、备案要求、内网互通、价格差异及产品功能等因素。建议根据用户所在地区就近选择地域,以降低延迟、提升访问速度。同时注意地域一旦选定不可更改,需谨慎选择。
2383 155
|
开发工具 git
解决:‘git‘ 不是内部或外部命令,也不是可运行的程序
解决:‘git‘ 不是内部或外部命令,也不是可运行的程序
解决:‘git‘ 不是内部或外部命令,也不是可运行的程序
|
机器学习/深度学习
小尺度信道建模 | 带你读《大规模天线波束赋形技术原理与设计 》之二十六
小尺度衰落是指无线电信号在短时间或短距离(若干波长)传播后其幅度、 相位或多径时延的快速变化。这种衰落是由于同一传输信号沿不同的路径传播, 由不同时刻(或相位)到达接收机的信号互相叠加所引起的,这些不同路径到 达的信号称为多径信号,接收机的信号强度取决于多径信号的强度、相对到达 时延以及传输信号的带宽。
10798 1
 小尺度信道建模  | 带你读《大规模天线波束赋形技术原理与设计 》之二十六
|
9月前
|
弹性计算 固态存储 应用服务中间件
阿里云服务器租用价格全解析:从入门到企业级云服务器配置收费标准与价格参考
阿里云服务器分为云服务器ECS和轻量应用服务器,轻量应用服务器2核2G峰值200M带宽40G ESSD云盘38元1年,经济型e云服务器2核2G3M带宽40G ESSD Entry云盘99元1年,通用算力型u1云服务器2核4G5M带宽80G ESSD Entry云盘199元1年。本文将详细解析阿里云服务器的价格体系,包括不同配置、不同实例类型的价格,以供参考和选择。
978 2
|
算法 安全 测试技术
GPS北斗信号放大转发器应用特点分析
GPS 北斗信号放大转发器是一种用于增强和转发GPS/北斗卫星信号的设备,以下是关于它的详细介绍: GPS 北斗信号放大转发器主要应用于以下场景 生产测试 卫星导航产品生产线:在智能手机、平板电脑、车载 DVD、导航仪、天线、模块、行车记录仪等产品的生产过程中,需要对其GPS或北斗定位功能进行测试。信号放大转发器可将室外卫星信号引入室内生产线,让产品在室内就能接收到稳定的卫星信号,从而完成各项定位功能测试,提高生产效率和产品质量。 科研院校实验室:科研人员在进行卫星导航相关的研究和实验时,需要在实验室环境中模拟真实的卫星信号。SYN2308型GNSS卫星信号转发器能够为实验室提供可调整的 GP
|
人工智能 并行计算 编译器
【AI系统】SIMD & SIMT 与 CUDA 关系
本文深入解析了AI芯片中SIMD和SIMT的计算本质,基于NVIDIA CUDA实现的对比,探讨了不同并行编程模型,包括串行(SISD)、数据并行(SIMD)和多线程(MIMD/SPMD)。文章详细介绍了各模型的特点及应用场景,特别强调了英伟达GPU中的SIMT机制如何通过SPMD编程模型实现高效并行计算,以及SIMD、SIMT、SPMD之间的关系和区别。
1301 13
|
消息中间件 存储 缓存
kafka 的数据是放在磁盘上还是内存上,为什么速度会快?
Kafka的数据存储机制通过将数据同时写入磁盘和内存,确保高吞吐量与持久性。其日志文件按主题和分区组织,使用预写日志(WAL)保证数据持久性,并借助操作系统的页缓存加速读取。Kafka采用顺序I/O、零拷贝技术和批量处理优化性能,支持分区分段以实现并行处理。示例代码展示了如何使用KafkaProducer发送消息。
|
消息中间件 XML 前端开发
springBoot集成websocket实时消息推送
本文介绍了如何在Spring Boot项目中集成WebSocket实现实时消息推送。首先,通过引入`spring-boot-starter-websocket`依赖,配置`WebSocketConfig`类来启用WebSocket支持。接着,创建`WebSocketTest`服务器类,处理连接、消息收发及错误等事件,并使用`ConcurrentHashMap`管理用户连接。最后,前端通过JavaScript建立WebSocket连接,监听消息并进行相应处理。此方案适用于需要实时通信的应用场景,如聊天室、通知系统等。
2777 2