Are you using Redis as your data structure storage and enjoying its high performance and high availability features? If so, then you might be interested in KeyDB.
What is KeyDB?
KeyDB An open source database powered by Snap and built for scale. It is a high-performance fork of Redis that focuses on multi-threading, memory efficiency, and high throughput. KeyDB uses an MVCC architecture that allows you to perform queries such as KEYS and SCAN without blocking the database and degrading performance. KeyDB maintains full compatibility with the Redis protocol, modules, and scripts, including atomicity guarantees for scripts and transactions. Because KeyDB keeps pace with Redis development, KeyDB is a superset of Redis functionality, making KeyDB an ideal replacement for existing Redis.
Project address:https://docs.keydb.dev/Source code address:https://github.com/Snapchat/ KeyDB
KeyDB unique features
In addition to the same functionality as Redis, KeyDB also provides some unique features, such as:
- Non-blocking architecture: Adopts an MVCC architecture that allows you to query a single snapshot of the database without blocking calls such as SCAN and KEYS. This allows such queries to be invoked concurrently at scale without degrading the overall performance of existing workloads.
- Cross-region active replication: This is a new replication mode that allows you to perform bidirectional asynchronous replication between multiple master nodes. This enables multi-master support across regions, improves data availability and consistency, and eliminates the need for sentinel monitoring nodes for failover.
- Child Expiration: This is a new expiration mechanism that allows you to set expiration time for members in the collection. This enables more fine-grained data management and cleaning.
- TLS Encryption: This is a new security mechanism that provides TLS (Transport Layer Security) support for your data. KeyDB’s TLS performance is 7x better than Redis + TLS and does not require any additional configuration or certificates.
- JavaScript modules: This is a new extension mechanism that allows you to write custom commands and logic using JavaScript. JavaScript modules are built on the V8 engine, which is faster than Lua and supports many Node.js modules.
KeyDB Performance Test
KeyDB can achieve much higher throughput than Redis on the same hardware. Active replication simplifies hot standby failover, allowing you to easily distribute write operations across replicas and use simple TCP-based load balancing/failover. KeyDB’s higher performance allows you to do more with less hardware, reducing operational costs and complexity.
The figure below shows a comparison of Redis, TLS and multi-threaded KeyDB benchmarks:
Testing showed a 36% drop in measurements with TLS enabled on Redis 6 (single-threaded). However, if you have used the I/O threads feature before, you may see a 61% performance drop because I/O threads are not supported using TLS.
The figure below shows a comparison of Redis, TLS and multi-threaded KeyDB latency benchmarks (the lower the value, the better):
As you can see, the latency under these loads is significantly higher when using TLS. Not only does KeyDB serve at very high volumes, but the latency is also 7x lower than Redis using TLS.
The following figure shows a throughput test comparison of Redis 6 multi-threaded I/O with Elasticache and KeyDB using YCSB:
It’s clear that KeyDB achieved the highest throughput under every workload tested. Elasticache is in second place, followed by Redis 6. It can be noticed that without I/O threads, Redis 6 is still faster than version 5.
The following figure shows a latency test comparison of Redis 6 multi-threaded I/O with Elasticache and KeyDB using YCSB (in microseconds, lower values are better):
The trends shown above indicate that latency increases significantly as an instance approaches its capacity throughput. KeyDB can handle much higher throughput than Redis 6 and slightly higher than Elasticache, allowing it to maintain lower latency under higher loads.
KeyDB, Elasticache, and Redis 6 all have similar latencies under light to moderate traffic. You won’t see the difference until capacity starts reaching higher loads.
For more test content, please go here to view: https://docs.keydb.dev/blog
How to use KeyDB?
Since KeyDB is fully compatible with Redis, it is very easy to use and we can use it like Redis. Let’s do a simple example showing how to connect to KeyDB, set and get a string value, and use the EXPIREMEMBER command to set subkey expiration.
KeyDB command documentation:https://docs.keydb.dev/docs/commands/#expiremember
First, pull the KeyDB image from Docker Hub and run:
# Pull the KeyDB image docker pull eqalpha/keydb #Container running KeyDB docker run -p 6379:6379 -d eqalpha/keydb
Then, create a console application and install the StackExchange.Redis library:
# Create a console application dotnet new console -o KeyDBDemo #Install StackExchange.Redis library nuget install StackExchange.Redis
Then, edit the Program.cs file and add the following code:
using StackExchange.Redis; public class KeyDBDemo { private static void Main(string[] args) { // Create a connector to the local Redis instance ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost"); // Get a database object IDatabase db = redis.GetDatabase(); // Set a string value, the key is "name", the value is "Bing" db.StringSet("name", "Bing"); // Get the string value with key "name" var name = db.StringGet("name"); // print results Console.WriteLine($"The name is {name}"); //Set a collection value, the key is "fruits", the value is ["apple", "banana", "orange"] db.SetAdd("fruits", new RedisValue[] { "apple", "banana", "orange" }); //Set the expiration time to 5 seconds for the member "apple" in the collection db.Execute("EXPIREMEMBER", "fruits", "apple", 5); // Get the collection value with key "fruits" var fruits = db.SetMembers("fruits"); // print results Console.WriteLine($"The fruits are {string.Join(", ", fruits)}"); // After waiting for 5 seconds, get the collection value with the key "fruits" again Thread.Sleep(5100); fruits = db.SetMembers("fruits"); // print results Console.WriteLine($"The fruits are {string.Join(", ", fruits)}"); // close connection redis.Close(); } }
Finally, when you run the program, you can get the following results:
The name is Bing The fruits are banana, apple, orange The fruits are banana, orange
Writing is not easy, please indicate the source of the article when reprinting, otherwise reprinting is prohibited! ! !
Thanks for reading, like + share + favorite + follow
The article comes from the WeChat official account of Yuan Huohuo