為什么批量請求要盡可能的合并操作
前言
線上情況:
- 線上Redis集群,多個Twemproxy代理(nutcracker),LVS DR路由均衡調度
- 客戶端使用Jedis操作Redis集群,一個程序進程實例使用原先1024個工作線程處理請求,若干個進程實例
- 一天超過22億次請求,網絡一般情況下,一天超過上萬個連接失敗異常
- 運維同學告知,LVS壓力較大
改進工作:
- 工作線程由原先1024改用16個
- 每個線程每次最多操作1000個Redis命令批量提交
實際效果:
- 一天不到一億次的請求量
- LVS壓力大減
- CPU壓力降低到原先1/3以下
- 單個請求抽樣調研平均減少1-90毫秒時間(尤其是跨機房處理)
Redis支持批量提交
原生支持批量操作方式
一般命令前綴若添加上m字符串,表示支持多個、批量命令提交了。
顯式的...
MSET key value [key value ...]
MSETNX key value [key value ...]
HMGET key field [field ...]
HMSET key field value [field value ...]
一般方式的...
HDEL key field [field ...]
SREM key member [member ...]
RPUSH key value [value ...]
......
更多,請參考:http://redis.cn/commands.html
pipeline管道方式
官方文檔:http://redis.io/topics/pipelining
- Redis Client把所有命令一起打包發送到Redis Server,然后阻塞等待處理結果
- Redis Server必須在處理完所有命令前先緩存起所有命令的處理結果
- 打包的命令越多,緩存消耗內存也越多
- 不是打包的命令越多越好
- 實際環境需要根據命令執行時間等各種因素選擇合并命令的個數,以及測試效果等
Java隊列支持
一般業務、接入前端請求量過大,生產者速度過快,這時候使用隊列暫時緩存會比較好一些,消費者直接直接從隊列獲取任務,通過隊列讓生產者和消費者進行分離這也是業界普通采用的方式。
監控隊列
有的時候,若可以監控一下隊列消費情況,可以監控一下,就很直觀。同事為隊列添加了一個監控線程,清晰明了了解隊列消費情況。
示范
示范使用了Redis Pipeline,線程池,準備數據,生產者-消費者隊列,隊列監控等,消費完畢,程序關閉。
/**
* 以下測試在Jedis 2.6下測試通過
*
* @author nieyong
*
*/
public class TestJedisPipeline {
private static final int NUM = 512;
private static final int MAX = 1000000; // 100W
private static JedisPool redisPool;
private static final ExecutorService pool = Executors.newCachedThreadPool();
protected static final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(
MAX); // 100W
private static boolean finished = false;
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(64);
config.setMaxIdle(64);
try {
redisPool = new JedisPool(config, "192.168.192.8", 6379, 10000,
null, 0);
} catch (Exception e) {
System.err.println("Init msg redis factory error! " + e.toString());
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("prepare test data 100W");
prepareTestData();
System.out.println("prepare test data done!");
// 生產者,模擬請求100W次
pool.execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < MAX; i++) {
if (i % 3 == 0) {
queue.offer("del_key key_" + i);
} else {
queue.offer("get_key key_" + i);
}
}
}
});
// CPU核數*2 個工作者線程
int threadNum = 2 * Runtime.getRuntime().availableProcessors();
for (int i = 0; i < threadNum; i++)
pool.execute(new ConsumerTask());
pool.execute(new MonitorTask());
Thread.sleep(10 * 1000);// 10sec
System.out.println("going to shutdown server ...");
setFinished(true);
pool.shutdown();
pool.awaitTermination(1, TimeUnit.MILLISECONDS);
System.out.println("colse!");
}
private static void prepareTestData() {
Jedis redis = redisPool.getResource();
Pipeline pipeline = redis.pipelined();
for (int i = 0; i < MAX; i++) {
pipeline.set("key_" + i, (i * 2 + 1) + "");
if (i % (NUM * 2) == 0) {
pipeline.sync();
}
}
pipeline.sync();
redisPool.returnResource(redis);
}
// queue monitor,生產者-消費隊列監控
private static class MonitorTask implements Runnable {
@Override
public void run() {
while (!Thread.interrupted() && !isFinished()) {
System.out.println("queue.size = " + queue.size());
try {
Thread.sleep(500); // 0.5 second
} catch (InterruptedException e) {
break;
}
}
}
}
// consumer,消費者
private static class ConsumerTask implements Runnable {
@Override
public void run() {
while (!Thread.interrupted() && !isFinished()) {
if (queue.isEmpty()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
continue;
}
List<String> tasks = new ArrayList<String>(NUM);
queue.drainTo(tasks, NUM);
if (tasks.isEmpty()) {
continue;
}
Jedis jedis = redisPool.getResource();
Pipeline pipeline = jedis.pipelined();
try {
List<Response<String>> resultList = new ArrayList<Response<String>>(
tasks.size());
List<String> waitDeleteList = new ArrayList<String>(
tasks.size());
for (String task : tasks) {
String key = task.split(" ")[1];
if (task.startsWith("get_key")) {
resultList.add(pipeline.get(key));
waitDeleteList.add(key);
} else if (task.startsWith("del_key")) {
pipeline.del(key);
}
}
pipeline.sync();
// 處理返回列表
for (int i = 0; i < resultList.size(); i++) {
resultList.get(i).get();
// handle value here ...
// System.out.println("get value " + value);
}
// 讀取完畢,直接刪除之
for (String key : waitDeleteList) {
pipeline.del(key);
}
pipeline.sync();
} catch (Exception e) {
redisPool.returnBrokenResource(jedis);
} finally {
redisPool.returnResource(jedis);
}
}
}
}
private static boolean isFinished(){
return finished;
}
private static void setFinished(boolean bool){
finished = bool;
}
}
代碼作為示范。若線上則需要處理一些異常等。
小結
若能夠批量請求進行合并操作,自然可以節省很多的網絡帶寬、CPU等資源。有類似問題的同學,不妨考慮一下。
posted on 2014-11-09 22:08 nieyong 閱讀(16190) 評論(17) 編輯 收藏 所屬分類: Socket