Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Cơ Sở Dữ Liệu

Làm chủ Redis Performance: Deep Dive vào Command Latency

Previous installment discussed topics và approaches preventing Redis instance becoming slow. Bây giờ examine ways measuring performance.

What To Measure

Installment này look at command latency và components. Tại sao? Vì number of commands push through Redis server/library là result của speed của each command.

Quick and Easy: The CLI

First way check command latency: dùng command line client redis-cli. Quick và gives immediate checkpoint start from. Examples dùng localhost cho simplicity, nhưng setup bạn nên dùng -h <host> options và nếu cần -a <auth> và -p <port>.

Basic Latency

Get basic latency results: run redis-cli --latency. Output set of numbers: min, max, avg, number of iterations. Iterations consist of running Redis command PING against open connection. Consequently không include time to connect to server. Nếu code connects on every command, bạn sẽ thấy much worse performance than this command indicates.

Command runs until kill it - easily via CTRL-C. Latency numbers in milliseconds. Result:

min: 0, max: 1, avg: 0.11 (11369 samples)

Nghĩa là minimum latency < 1ms, maximum 1ms, average per-PING latency 0.11ms. Yes, localhost connection. Numbers từ 11,369 samples (PING commands).

Alternatively, dùng --latency-history. Option này break out temporal slices. Running redis-cli --latency-history dùng default window 15 seconds. Result:

min: 0, max: 1, avg: 0.11 (1475 samples) -- 15.01 seconds range
min: 0, max: 1, avg: 0.10 (1474 samples) -- 15.01 seconds range

Like --latency, runs until kill it. Muốn different interval, pass -i. Ví dụ:

redis-cli --latency-history -i 10
min: 0, max: 1, avg: 0.11 (984 samples) -- 10.01 seconds range
min: 0, max: 1, avg: 0.11 (983 samples) -- 10.01 seconds range
min: 0, max: 1, avg: 0.11 (983 samples) -- 10.01 seconds range

Vì operations chỉ dùng PING command, should work against any Redis version.

Intrinsic Latency

At first glance, name might lead believe intrinsic latency mode measure intrinsic latency của server. It isn't. Nếu look at redis-cli source GitHub, find it doesn't even talk to server:

start = ustime(); compute_something_fast(); end = ustime(); latency = end-start;

According to source comments on intrinsic latency mode:

Measure max latency of a running process that does not result from syscalls. Basically this software should provide a hint about how much time the kernel leaves the process without a chance to run.

Cái này test intrinsic latency on your client host. Useful knowing nếu problem actually lies on client side rather than server itself.

So while useful probably not in first steps checking latency. Update: After speaking with Salvatore, confirmed intention run this command from server. Means useful nếu có shell access Redis server, nhưng not useful otherwise - commonly case nếu dùng Redis service provider.

Using Commissar

Commissar: small suite I'm working on for tracking và testing Redis latency và overall performance. Warning: early development, much will likely change. Only use nếu comfortable using non-production quality software. Commissar in The Github Commissar Repository.

Specifically for this article: latency tool/directory.

Tool này, configured via environment variables, connect to given Redis instance và run same "time the PING command" mechanism as redis-cli. Did this maintain parity of test. Where it differs: ability output more information và (currently) store in MongoDB - more stores to come.

Run of latency might look like this (output likely changed since article written):

./latency
Connected to 
100000 iterations over 53698us, average 536us/operation
Percentile breakout:
====================
99.00%: 3,876.99us
95.00%: 640.00us
90.00%: 514.00us
75.00%: 452.00us
50.00%: 414.00us
Min: 243us
Max: 44,686us
Mean: 536.98us
Jitter: 764.37us

Notice temporal unit 'us' (microseconds). Also percentile breakout. Lets you get better picture latency looks like compared to simple min/max/avg. In this context 'Jitter' là standard deviation of sample.

Output này tells us overall looking pretty good. Example này over private network. Peak much higher than mean, nhưng 95% iterations at or below 640 microseconds. What does this tell us?

Could tell us: sporadic network issue, sporadic server-side delay, hoặc garbage control in test client affecting output. This is why distribution of requests made available: so you can see how common "high" result is.

Để determine where problem is, first look at how long commands on server taking. This tells us if server is problem.

Case này, check redis info output, specifically Commandstats section:

redis-cli info commandstats
# Commandstats
cmdstat_ping:calls=32497193,usec=22213723,usec_per_call=0.68

Average time execute PING on server: 0.68 microseconds. Clearly ping command itself not likely culprit. However, could be other commands executed during test backed Redis process up causing delay.

Recent addition latency tool: ability run concurrent latency tests. Set environment variable LATENCY_CLIENTCOUNT, run multiple connections to Redis server và see how much concurrency affects latency results. Example: see difference simple Redis PING command sees khi 10 clients vs 100, hoặc even 1000 concurrent clients. Useful see difference giữa development/staging và production.

Testing from Containers

Prefer run tests from Docker containers? I push built Docker containers có just golatency tool to public Docker registry. Docker pull therealbill/golatency get image, ready run in seconds (currently < 4MB image size).

Simply call docker run with environment variables connect và configure as ReadMe indicates. Pull output from stdout hoặc, nếu run as daemon, via docker logs, don't have to build anything. Whenever significant changes to tool, Docker repo updated with new image. Alternatively, Dockerfile in repo, allowing customize as desired.

Final Thoughts

Analyzing performance với Redis can be tricky. Fortunately, với understanding latency và throughput và how they affect application, bạn can focus more on analyzing application's use of Redis rather than Redis itself. You'll improve usage và understanding how best use Redis by keeping in mind Zen of Redis's phrase "The key to performance is not slowing Redis down," while produce smarter, more efficient applications by isolating network issues và data structure alternatives, resulting in better performance.