Redis是一个使用客户端-服务器模式也叫请求/响应协议的TCP服务器。
FastoRedis (fork of FastoNoSQL) - is a cross-platform open source Redis management tool (i.e. It put the same engine that powers Redis's redis-cli shell. Everything you can write in redis-cli shell - you can write in Fastoredis! Our program works on the most amount of Linux systems, also on Windows, Mac OS X, FreeBSD and Android. On a mac, how to I find the older version of Redis and uninstall it completely? I'm using OSX and installed Redis using the following command brew install redis. The version installed by brew states redis-3.0.7. However, when I run the command: redis-server the output states it's I'm running Redis 2.6.9 (00000000/0) 64 bit. I have no idea where. Redis Server simplifies development by providing a ready-to-run version of Redis, now you can manage the Redis service easily (start, stop, restart, launch at login, etc.). Redis is a data structure server.
这意味着一个请求通常由以下几个步骤完成:
- 客户端向服务器发送请求,并读取套接字, 通常以阻塞的方式等待服务器响应。
- 服务器处理请求并将响应结果返回给客户端。
So for instance a four commands sequence is something like this:
- Client: INCR X
- Server: 1
- Client: INCR X
- Server: 2
- Client: INCR X
- Server: 3
- Client: INCR X
- Server: 4
客户端群和服务器群之间通过网络连接。 Such a link can be very fast (a loopback interface) or very slow (a connection established over the Internet with many hops between the two hosts). Whatever the network latency is, there is a time for the packets to travel from the client to the server, and back from the server to the client to carry the reply.
这个时间叫往返时延。显而易见, how this can affect the performances when a client needs to perform many requests in a row (for instance adding many elements to the same list, or populating a database with many keys). For instance if the RTT time is 250 milliseconds (in the case of a very slow link over the Internet), even if the server is able to process 100k requests per second, we'll be able to process at max four requests per second.
If the interface used is a loopback interface, the RTT is much shorter (for instance my host reports 0,044 milliseconds pinging 127.0.0.1), but it is still a lot if you need to perform many writes in a row.
Fortunately there is a way to improve this use cases.
Redis Pipelining
A Request/Response server can be implemented so that it is able to process new requests even if the client didn't already read the old responses. This way it is possible to send multiple commands to the server without waiting for the replies at all, and finally read the replies in a single step.
This is called pipelining, and is a technique widely in use since many decades. For instance many POP3 protocol implementations already supported this feature, dramatically speeding up the process of downloading new emails from the server.
Redis supports pipelining since the very early days, so whatever version you are running, you can use pipelining with Redis. This is an example using the raw netcat utility:
This time we are not paying the cost of RTT for every call, but just one time for the three commands.
To be very explicit, with pipelining the order of operations of our very first example will be the following:
- Client: INCR X
- Client: INCR X
- Client: INCR X
- Client: INCR X
- Server: 1
- Server: 2
- Server: 3
- Server: 4
IMPORTANT NOTE: while the client sends commands using pipelining, the server will be forced to queue the replies, using memory. So if you need to send many many commands with pipelining it's better to send this commands up to a given reasonable number, for instance 10k commands, read the replies, and send again other 10k commands and so forth. The speed will be nearly the same, but the additional memory used will be at max the amount needed to queue the replies for this 10k commands.
Some benchmark
In the following benchmark we'll use the Redis Ruby client, supporting pipelining, to test the speed improvement due to pipelining:
Running the above simple script will provide this figures in my Mac OS X system, running over the loopback interface, where pipelining will provide the smallest improvement as the RTT is already pretty low:
As you can see using pipelining we improved the transfer by a factor of five.
Pipelining VS Scripting
Using Redis scripting (available in Redis version 2.6 or greater) a number of use cases for pipelining can be addressed more efficiently using scripts that perform a lot of the work needed server side. A big advantage of scripting is that it is able to both read and write data with minimal latency, making operations like read, compute, write very fast (pipelining can't help in this scenario since the client needs the reply of the read command before it can call the write command).
Sometimes the application may also want to send EVAL or EVALSHA commands in a pipeline. This is entirely possible and Redis explicitly supports it with the SCRIPT LOAD command (it guarantees that EVALSHA can be called without the risk of failing).
Introduction
In this article I will explain how you can:
- Install and Configure Memcached on Mac OS X
- Use Memcached in your Java Application
I won’t go in too much detail about the benefits of using a distributed cache in your applications, but let’s at least provide some use cases for applications that are running in the context of an enterprise portal, eXo Platform in my case - surprising isn’t? And I will show this in another post.
We have many reasons to use a cache (distributed or not), in the context of enterprise portal, let’s take a look to some of these reasons:
- A portal is used to aggregate data in a single page. These data could come from different sources : Web Services, Database, ERP, ….. and accessing the data in real time could be costly. So it will be quite interesting to cache the result of the call when possible.
- If the portal is used to aggregate many data from many sources, it is sometime necessary to jump into another application to continue some operation. A distributed and shared cache could be used to manage some context between different applications running in different processes (JVM or even technologies)These are two example where a shared cache could be interesting for your portal based applications, we can find many other reason.
Note that the Portlet API (JSR-286) contains already a cache mechanism that cache the HTML fragment, and that eXo Platform also provide a low level cache, based on JBoss Cache.
Installation and Configuration
Installing Memcached from sources
You can find some information about Memcached installation on the Memcached Wiki. The following steps are the steps that I have used on my environment.
As far as I know, Memached is not available as package for Mac OS X. I am still on Snow Leopard (10.6.8), and I have installed XCode and all development tools. I have use the article “Installing memcached 1.4.1 on Mac OS X 10.6 Snow Leopard” from wincent.com. For simplicity reason I have duplicate the content and updated to the latest releases.
1- Create a working directory :
2- Install libevent that is mandatory for memcached
3- Install memcached
Go back to your install directory (memcachedbuild)
You are now ready to use memcached that is available at /usr/local/bin/memcached
.
This allows you to avoid changing to the pre-installed memcached located in /usr/bin, if you want to replace it instead of having you own install, just run the configure command with the following parameter: ./configure --prefix=/usr
Starting and testing Memcached
Start the memcached server, using the following command line:
This command starts the memcached server as demon (-d parameter), on the TCP port 11211 (this is the default value). You can find more about the memcached command using man memcached
.
It is possible to connect and test your server using a telnet connection. Once connected you can set and get object in the cache, take a look to the following paragraph.
The set
command allows you to put a new value in the cache using the following syntax:
Mac Os Redis Client
- key : the key used to store the data in the cache
- flags : a 32 bits unsigned integer that memcached stored with the data
- expiration_time : expiration time in seconds, if you put 0 this means no delay
- number_if_bytes : number of bytes in the data block
- noreply : option to tell the server to not return any value
- value : the value to store and associate to the key.
This is a short view of the documentation located in your source directory /memcachedbuild/memcached-1.4.10/doc/protocol.txt
.
The get
command allows you to access the value that is associated with the key.
You can check the version of memcahed you are running by calling the stats
command in your telnet session.
Your memcached server is up and running, you can now start to use it inside your applications.
Simple Java Application with Memcached
The easiest way to use memcached from your Java applications is to use a client library. You can find many client libraries. In this example I am using spymemcached developped by the people from Couchbase.
1- Adding SpyMemcached to your Maven project
Add the repository to you pom.xml (or you setting.xml)
then the dependency to your pom.xml
2- Use SpyMemcache client in your application
The following code is a simple Java class that allows you to enter the key and the value and set it in the cache.
So when executing the application you will see something like :
Redis Client Mac Os
You can also access the object from a Telnet session:
You can use any Java class in your application, the only thing to do is to make this class serializable.
Redis Mac Os
This is it for the first post about memcached and Java, I am currently working on a small example integrating Web Services call, Portlets and memcached.