Thrift Java Servers Compared

原文转载于:https://github.com/

Thrift Java Servers Compared

This article talks only about Java servers. See this page if you are interested in C++ servers.

Thrift is a cross-language serialization/RPC framework with three major components: protocol, transport, and server. Protocol defines how messages are serialized. Transport defines how messages are communicated between client and server. Server receives serialized messages from the transport, deserializes them according to the protocol and invokes user-defined message handlers, and serializes the responses from the handlers and writes them back to the transport. The modular architecture of Thrift allows it to offer various choices of servers. Here are the list of server available for Java:

Having choices is great, but which server is right for you? In this article, I'll describe the differences among all those servers and show benchmark results to illustrate performance characteristics (the details of the benchmark is explained in Appendix B). Let's start with the simplest one: TSimpleServer.

TSimpleServer

TSimpleServer accepts a connection, processes requests from the connection until the client closes the connection, and goes back to accept a new connection. Since it is all done in a single thread with blocking I/O, it can only serve one client connection, and all the other clients will have to wait until they get accepted. TSimpleServer is mainly used for testing purpose. Don't use it in production!

TNonblockingServer vs. THsHaServer

TNonblockingServer solves the problem with TSimpleServer of one client blocking all the other clients by using non-blocking I/O. It uses java.nio.channels.Selector, which allows you to get blocked on multiple connections instead of a single connection by calling select(). The select() call returns when one ore more connections are ready to be accepted/read/written. TNonblockingServer handles those connections either by accepting it, reading data from it, or writing data to it, and calls select() again to wait for the next available connections. This way, multiple clients can be served without one client starving others.

There is a catch, however. Messages are processed by the same thread that calls select(). Let's say there are 10 clients, and each message takes 100 ms to process. What would be the latency and throughput? While a message is being processed, 9 clients are waiting to be selected, so it takes 1 second for the clients to get the response back from the server, and throughput will be 10 requests / second. Wouldn't it be great if multiple messages can be processed simultaneously?

This is where THsHaServer (Half-Sync/Half-Async server) comes into picture. It uses a single thread for network I/O, and a separate pool of worker threads to handle message processing. This way messages will get processed immediately if there is an idle worker threads, and multiple messages can be processed concurrently. Using the example above, now the latency is 100 ms and throughput will be 100 requests / sec.

To demonstrate this, I ran a benchmark with 10 clients and a modified message handler that simply sleeps for 100 ms before returning. I used THsHaServer with 10 worker threads. The handler looks something like this:

public ResponseCode sleep() throws TException
{   
    try {
        Thread.sleep(100);
    } catch (Exception ex) {
    }
    return ResponseCode.Success;
}

The results are as expected. THsHaServer is able to process all the requests concurrently, while TNonblockingServer processes requests one at a time.

THsHaServer vs. TThreadedSelectorServer

Thrift 0.8 introduced yet another server, TThreadedSelectorServer. The main difference between TThreadedSelectorServer and THsHaServer is that TThreadedSelectorServer allows you to have multiple threads for network I/O. It maintains 2 thread pools, one for handling network I/O, and one for handling request processing. TThreadedSelectorServer performs better than THsHaServer when the network io is the bottleneck. To show the difference, I ran a benchmark with a handler that returns immediately without doing anything, and measured the average latency and throughput with varying number of clients. I used 32 worker threads for THsHaServer, and 16 worker threads/16 selector threads for TThreadedSelectorServer.

The result shows that TThreadedSelectorServer has much higher throughput than THsHaServer while maintaining lower latency.

TThreadedSelectorServer vs. TThreadPoolServer

Finally, there is TThreadPoolServer. TThreadPoolServer is different from the other 3 servers in that:

  • There is a dedicated thread for accepting connections.
  • Once a connection is accepted, it gets scheduled to be processed by a worker thread in ThreadPoolExecutor.
  • The worker thread is tied to the specific client connection until it's closed. Once the connection is closed, the worker thread goes back to the thread pool.
  • You can configure both minimum and maximum number of threads in the thread pool. Default values are 5 and Integer.MAX_VALUE, respectively.

This means that if there are 10000 concurrent client connections, you need to run 10000 threads. As such, it is not as resource friendly as other servers. Also, if the number of clients exceeds the maximum number of threads in the thread pool, requests will be blocked until a worker thread becomes available.

Having said that, TThreadPoolServer performs very well; on the box I'm using it's able to support 10000 concurrent clients without any problem. If you know the number of clients that will be connecting to your server in advance and you don't mind running a lot of threads, TThreadPoolServer might be a good choice for you.

Conclusion

I hope this article helps you decide which Thrift server is right for you. I think TThreadedSelectorServer would be a safe choice for most of the use cases. You might also want to consider TThreadPoolServer if you can afford to run lots of concurrent threads. Feel free to send me email at mapkeeper-users@googlegroups.com or post your comments here if you have any questions/comments.

Appendix A: Hardware Configuration

Processors:     2 x Xeon E5620 2.40GHz (HT enabled, 8 cores, 16 threads)
Memory:         8GB
Network:        1Gb/s <full-duplex>
OS:             RHEL Server 5.4 Linux 2.6.18-164.2.1.el5 x86_64

Appendix B: Benchmark Details

It's pretty straightforward to run the benchmark yourself. First clone the MapKeeper repository and compile the stub java server:

git clone git://github.com/m1ch1/mapkeeper.git
cd mapkeeper/stubjava
make

Then, start the server you like to benchmark:

make run mode=threadpool    # run TThreadPoolServer
make run mode=nonblocking   # run TNonblockingServer
make run mode=hsha          # run THsHaServer
make run mode=selector      # run TThreadedSelectorServer

Then, clone YCSB repository and compile:

git clone git://github.com/brianfrankcooper/YCSB.git
cd YCSB
mvn clean package

Once the compilation finishes, you can run YCSB against the stub server:

./bin/ycsb load mapkeeper -P ./workloads/workloada

For more detailed information about how to use YCSB, check out their wiki page.

Java 后端 & AI 应用面试题精讲(含流程图解说) 本文系统梳理了JVM、Redis、MySQL、Spring Boot、Kafka、微服务、Linux及AI后端等核心面试知识点,涵盖内存管理、垃圾回收、数据结构、缓存策略、高可用架构等关键技术。重点解析了JVM内存模型与GC机制、Redis高性能原理与持久化方案、缓存三大问题应对策略,以及分布式场景下的实战设计思路,适用于中高级研发岗位技术面试准备。 阅读详情

相关推荐

VGG16

VGG16学习笔记

一去不复返的通信er 1186

迭代模式的最终总结(10)where you are?(转)

其实做一个项目经理真不是一个好的职业,它需要太多的千锤百炼才能修炼出来。这不仅需要反复经历“失败-总结-再失败”的轮回,而且需要有一颗无比坚强的心,能够在无数次经历无比艰难并且令人沮丧的时刻而能坚持不懈、毫不气馁。一个项目经理就像一位将军。将军百战死,而项目经理呢,经历无数项目以后沉淀下来的,更多的是疲惫与沧桑。 但凡一个好的项目经理都是要经过一次又一次项目失败的教训,似乎只有失败才能留给他们...

jayluns的专栏 205

解决 PyInstaller 与 `pathlib` 包不兼容的问题

为了避免 PyInstaller 和pathlib包的兼容性问题,最简单的解决办法就是卸载不必要的pathlib包。只需在命令行中运行卸载命令,然后重新打包即可。

Winfredzhang的博客 3234

Java版的各种Thrift server实现的比较

(转载文章,点击查看原文http://www.codelast.com/) 本文是我对这篇文章的翻译:Thrift Java Servers Compared,为了便于阅读,我将原文附于此处,翻译穿插在其中。此外,为了防止原链接在未来某一天失效后,文中的图片再也看不到的问题,我将原文中的图片也保存到了本站的服务器上,我不知道github或原作者是否允许这样做,但我翻译本文仅在于传播知识的

竹叶青 的专栏 1万+

如何在RT-DETR-R18中修改ultralytics的yaml文件(附完整代码)

本文详细指导如何在RT-DETR模型中,通过修改Ultralytics框架的yaml配置文件,将Backbone从ResNet50轻量化替换为ResNet18。核心步骤包括自定义ResNet18基础模块、调整HybridEncoder的expansion参数至0.5,并协同修改检测头Decoder层数,最终提供完整的配置文件代码,助力开发者在资源受限环境下部署轻量级实时目标检测模型。

weixin_29091445的博客 296

手把手学计科--web网页设计

网页的组成:文字、图像和超链接等元素。还可以包括哦音频、视频和动画。分类:静态(固定信息)和动态(与服务器数据库进行实时的数据交换。HTML和CSS 是一种静态网页搭建技术。

Yhvaely的博客 1756

SAP CO 初级(一)——学 CO 之前要补的三块基础:管理会计、FI 与后勤(附 ACDOCA 实操)

SAP CO 技能树系列第 1 篇:用算例讲清固定与变动成本、价差与量差、边际贡献;梳理 FI 的字段状态、凭证拆分,MM 的标准价与移动平均价、OBYC,PP 的数量结构与工单成本流,SD 的收入确定;动手过 5 张凭证,逐字段看 ACDOCA 里 FI 和 CO 怎样落在同一行。附排错表与自测题,S/4HANA 2022 核对。

缘起缘灭皆为因果,看到就是缘分,点赞收藏一键三连吧! 302

LVGL实战 透视卡盘

八张卡立在一个圆柱面上,摄像机在柱心前方。每帧对屏幕上的每个像素发一条射线,与卡片所在平面求交,得到三维交点,再投影回卡片自己的二维坐标系取样。透视、倾斜、近大远小、相互遮挡 —— 全部是这一步的副产物。

摸鱼专家 81

java高频实战50题

只保留面试常问、业务代码天天用的点。标 ✍️ 的必须写代码跑通。规则:先凭记忆写,卡住查 JDK 源码或官方文档,不搜博客答案;不会的标❓并写明卡在哪。做完发我,我按组批注并追问「为什么」。

qq_26889291的博客 162

四非人的保研之路,2024(2025届)四非计算机的保研经验分享(西南交通、苏大nlp、西电、北邮、山软、山计、电科、厦大等)

本身我就是福建人,xmu 3小时就能到家,然后导这边毕业要求容易,不想读博,达到要求就能放实习,每周打卡35h,比较自由,遂考虑。北邮弱com,联系了蛮多老师,几乎都给了考核机会,面试的老师好几位都考核人工智能基础,会问你卷积的计算,问的还是蛮细的。当时结果是暂定,以为是稳了,遂没联系其他老师,但是之后询问发现只是候补,懒得联系其他老师,928候补到,放弃。东北大学,2000人大机试,入营点送,全国巡回面试,每个人就面不到10分钟,我就被问了3个问题就结束了,候补。科研组,导师有人脉,想读博出成果可以去。

专注于AI领域前沿知识,业余爱好开发软件 4372

stable diffusioncontrolNet 预处理器/模型的下载及安装使用教程

首先说明,controlNet 的模型 不同于stable diffusion 模型,这一点一定要区分清楚。下面提供多个ControlNet 预处理器+ 专用模型的百度网盘下载链接,都是由群友整理贡献,但各自包含的版本不同,大家可自行选择。推荐下载紫色链接内的模型,fp16版文件更小,稍微新一些的controlnet插件还添加了对control-lora的支持(注意,要求controlnet插件的版本比12月16号新(1.1.423以上)才能正常识别),推荐下载;

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 3401

MQ工作模式

RabbitMQ 的工作模式,本质是。

2401_88415955的博客 187

下载igv报错:Error loading genome hg38raw.githubusercontent.com...如何解决?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1010

《阿里巴巴 Java 开发手册》的 MySQL 核心规范

既然你平时经常自己练习表结构设计,那这份提炼自《阿里巴巴 Java 开发手册》的 MySQL 核心规范,绝对是你日常建表和写 SQL 时的“避坑宝典”。我帮你把最核心的【强制】级别规范,按和。

qq_26889291的博客 390

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

CMIIT、IMSI、ICCID 与设备认证:一文理清四者的关系

CMIIT ID(China Ministry of Industry and Information Technology ID)是无线电发射设备型号核准代码,由工业和信息化部(MIIT)下属的无线电管理机构颁发。凡是在中国境内生产、销售、使用的无线电发射设备(手机、蜂窝模组、Wi-Fi 路由器、蓝牙设备、对讲机等),原则上都需要取得无线电发射设备型号核准,并标注对应的 CMIIT ID。

Alan_Ashley的博客 476

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

成本告警 39 天 0 行:13 位时间戳在 SQLite 里算出 NULL

日预算告警 39 天 0 行:13 位毫秒时间戳被 SQLite 的 unixepoch 当秒,date() 返回 NULL。用 12,008 条请求复现,再查时区、聚合字段、cost 归零三个盲区。

撸码猿的博客 234

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

mybatis-plus初次使用

由于mybatis-plus框架spring框架没有集成进来,所以需要另外导入依赖 相关依赖可以在MyBatis-Plus🚀为简化开发而生官网中导入 MybatisPlus提供了⼀个基础的BaseMapper 接口,已经实现了单表的CRUD,我们自定义的 Mapper只需要继承这个BaseMapper,就无需自己实现单表CRUD了 在Mapper中没有定义任何接口,测试继承了< BaseMapper > 后的UserInfoMapper 是否可以操作数据库在上面的程序中,MyBatis-Plus是如何知

2302_81707171的博客 265

笔面试复盘总结

看jdtoC高并发项目会拷打锁、并发容器、缓存中间件纯业务类会问Spring和Java基础,JVM等大厂额外会问语言以外的计算机基础知识。

写代码的小阿帆的博客 245

周报-12-4

在简历准备方面,本周梳理了个人基本信息、教育背景、校园经历、专业技能与自我评价,核对了联系方式、邮箱等基础信息;对照目标岗位要求,将网上图书商店系统等项目经历改写为动词开头、带量化表述的简历条目,使经历描述更贴合岗位的需求。此前梳理的登录注册、图书、购物车、订单等接口本周均未联调测试,接口规范文档也未继续补充完善。在课程学习与作业方面,本周本阶段课程作业已告一段落,下一阶段作业将跟随老师安排的节奏推进。下周计划:在完善简历的同时,跟随老师节奏开启下一阶段作业,把网上图书商店系统已梳理的接口逐个落地。

WMY2006的博客 222

Java 基础|面向对象核心知识点笔记

反射允许程序运行时拿到类的全部信息:字段、方法、构造器,可以动态创建对象、调用方法。javac 一次性编译成字节码,JVM 再解释执行,现代 JVM 带有 JIT 即时编译,热点代码会直接编译成本地机器码提升速度。不是简单写 get、set,而是控制数据的合法性,在 set 里面增加校验逻辑。作用:编译期类型校验,省去大量强制类型转换,减少类型转换异常。一句话区分:JVM 跑字节码,JRE 用来跑程序,JDK 用来写程序。超出范围会新建对象。多态好处:代码解耦,方便扩展,新增子类不用大量修改原有业务代码。

2602_95753785的博客 168

基于SpringBoot的费用报销审批管理系统-毕业设计

浅色财务单据风,SpringBoot 3.2.5 + Vue3 真前后端分离:费用标准取自财政部 财行[2016]71 号公开附件(XLS 50,176 字节,清洗得 36 地区 / 81 条标准 / 146 城市索引,含三级人员限额与旺季上浮价),外币按 Frankfurter 公开参考汇率(USD/EUR/JPY/GBP/HKD→CNY,基准日 2026-12-03)换算。

csbysj2020 234

Spring Cloud 微服务分布式深度实践:从注册发现到分布式事务

本文基于JDK 21、Spring Boot 3.2.12与Spring Cloud 2023.0.6,实测验证微服务七大核心组件:服务注册发现(Nacos AP模式)、配置中心、API网关、服务调用与负载均衡、熔断限流(Sentinel/R4j)、分布式事务(Seata)。通过真实环境部署与命令行输出,深入剖析CAP定理在注册中心设计中的应用,强调“分区时选A而非放弃P”,并结合PACELC理论说明正常场景下延迟与一致性的权衡,为微服务架构选型提供可复现的实践依据。

一只懂代码不懂人情世故的源码爱好猿 535
上一篇: TThreadedServer vs. TNonblockingServer
kernelcsx
博客等级 码龄14年 0粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址