Recommend this page to a friend! |
Classes of Duong Huynh Nghia | Lego PHP | Library/vendor/predis/predis/FAQ.md | Download |
|
DownloadSome frequently asked questions about Predis________________________________________________ What is the point of Predis?The main point of Predis is about offering a highly customizable and extensible client for Redis, that can be easily extended by developers while still being reasonabily fast. With Predis you can swap almost any class with your own custom implementation: you can have custom connection classes, new distribution strategies for client-side sharding, or handlers to replace or add Redis commands. All of this can be achieved without messing with the source code of the library and directly in your own application. Given the fast pace at which Redis is developed and adds new features, this can be a great asset since it allows developers to add new and still missing features or commands or change the standard behaviour of the library without the need to break dependencies in production code (at least to some degree). Does Predis support UNIX domain sockets and persistent connections?Yes. Obviously persistent connections actually work only when using PHP configured as a persistent process reused by the web server (see PHP-FPM). Does Predis support SSL-encrypted connections?Yes. Encrypted connections are mostly useful when connecting to Redis instances exposed by various cloud hosting providers without the need to configure an SSL proxy, but you should also take into account the general performances degradation especially during the connect() operation when the TLS handshake must be performed to secure the connection. Persistent SSL-encrypted connections may help in that respect, but they are supported only when running on PHP >= 7.0.0. Does Predis support transparent (de)serialization of values?No and it will not ever do that by default. The reason behind this decision is that serialization is usually something that developers prefer to customize depending on their needs and can not be easily generalized when using Redis because of the many possible access patterns for your data. This does not mean that it is impossible to have such a feature since you can leverage the extensibility of this library to define your own serialization-aware commands. You can find more details about how to do that on this issue. How can I force Predis to connect to Redis before sending any command?Explicitly connecting to Redis is usually not needed since the client initializes connections lazily
only when they are needed. Admittedly, this behavior can be inconvenient in certain scenarios when
you absolutely need to perform an upfront check to determine if the server is up and running and
eventually catch exceptions on failures. Forcing the client to open the underlying connection can be
done by invoking
How Predis abstracts Redis commands?The approach used to implement Redis commands is quite simple: by default each command follows the
same signature as defined on the Redis documentation which makes things
pretty easy if you already know how Redis works or you need to look up how to use certain commands.
Alternatively, variadic commands can accept an array for keys or values (depending on the command)
instead of a list of arguments. Commands such as
An exception to this rule is Speaking about performances..._________________________________________________ Predis is a pure-PHP implementation: it can not be fast enough!It really depends, but most of the times the answer is: _yes, it is fast enough_. I will give you a couple of easy numbers with a simple test that uses a single client and is executed by PHP 5.5.6 against a local instance of Redis 2.8 that runs under Ubuntu 13.10 on a Intel Q6600:
How does it compare with __phpredis__, a nice C extension providing an efficient client for Redis?
Wow __phpredis__ seems much faster! Well, we are comparing a C extension with a pure-PHP library so
lower numbers are quite expected but there is a fundamental flaw in them: is this really how you are
going to use Redis in your application? Are you really going to send thousands of commands using a
for-loop on each page request using a single client instance? If so... well I guess you are probably
doing something wrong. Also, if you need to There is one more thing: we have tested the overhead of Predis by connecting on a localhost instance of Redis but how these numbers change when we hit the physical network by connecting to remote Redis instances?
There you go, you get almost the same average numbers and the reason is simple: network latency is a real performance killer and you cannot do (almost) anything about that. As a disclaimer, remember that we are measuring the overhead of client libraries implementations and the effects of network round-trip times, so we are not really measuring how fast Redis is. Redis shines best with thousands of concurrent clients doing requests! Also, actual performances should be measured according to how your application will use Redis. I am convinced, but performances for multi-bulk responses are still worseFair enough, but there is an option available if you need even more speed and consists on installing __phpiredis__ (note the additional _i_ in the name) and let the client use it. __phpiredis__ is another C extension that wraps __hiredis__ (the official C client library for Redis) with a thin layer exposing its features to PHP. You can then choose between two different connection classes: - You will now get the benefits of a faster protocol serializer and parser just by adding a couple of lines of code:
Dead simple. Nothing changes in the way you use the library in your application. So how fast is it
our basic benchmark script now? There are not much improvements for inline or short bulk responses
like the ones returned by
If I need an extension to get better performances, why not using phpredis?Good question. Generically speaking if you need absolute uber-speed using Redis on the localhost and you do not care about abstractions built around some Redis features such as MULTI / EXEC, or if you do not need any kind of extensibility or guaranteed backwards compatibility with different versions of Redis (Predis currently supports from 1.2 up to 2.8 and the current development version), then using __phpredis__ makes absolutely sense. Otherwise, Predis is perfect for the job and by adding __phpiredis__ you can get a nice speed bump almost for free. |