Supporting Multiple Redis Databases With Infinispan Cache Aliases Enhancement
Learn how Infinispan cache aliases will help you replace your Redis Server with Infinispan for multiple Redis databases.
Join the DZone community and get the full member experience.
Join For FreeIn Infinispan 15, we provided a large set of commands to make it possible to replace your Redis Server with Infinispan without changing your code. In this tutorial, you will learn how Infinispan cache aliases will help you replace your Redis Server with Infinispan for multiple Redis databases.
- Key takeaways:
- What are cache aliases and how to create caches with aliases or update existing ones
- Learn how Infinispan and Redis differ in data organization
- Support multiple databases in Infinispan with cache aliases when using the RESP protocol
Supporting multiple Redis databases has been available since Infinispan 15.0 (the latest stable release at the time of this writing). However, Hot Rod, CLI, and Infinispan Console support is Tech Preview in Infinispan 15.1 (in development right now).
Redis Hot Replacement for Infinispan
Since Infinispan 15, you can use Infinispan as a hot replacement for Redis because it supports most Redis commands through the RESP protocol. This works because Infinispan Server has the RESP endpoint enabled by default. Redis clients will automatically connect and be routed to Infinispan’s internal connector.
Running the Infinispan Server and Using a Redis Client
Testing a Redis client with Infinispan Server is very easy.
First, run the Infinispan Server as explained in the "Getting Started tutorial."
- Important: Caches aliases fully work from 15.1.0.Dev04 release. Make sure you pull the latest 15.1 image locally.
Command line with Docker or Podman:
docker run -it -p 11222:11222 -e USER="admin" -e PASS="password" quay.io/infinispan/server:15.1 podman run -it -p 11222:11222 -e USER="admin" -e PASS="password" --net=host quay.io/infinispan/server:15.1
Next, connect to Infinispan using Redis-CLI.
Use port 11222
instead of the default 6379
. Since Infinispan is secured by default, make sure to provide the admin credentials.
> redis-cli -p 11222 --user admin --pass password 127.0.0.1:11222> set hello world OK 127.0.0.1:11222> get hello "world"
That’s all!
If you're wondering where the data is stored, it’s in the "respCache". This is the default cache used by the Infinispan RESP connector, and it's pre-configured with sensible defaults. It’s ready to use and serves as a good replacement for Redis. Please note that starting with Infinispan 15.1, the data container cache list includes a new column called "Aliases." We'll cover that later.
Infinispan Server Console in http://localhost:11222 (admin/password) credentials:
Redis Databases Versus Infinispan Caches
In Redis, databases are essentially separate, isolated namespaces within a single Redis server. Each database can store its own set of key-value pairs independently from the others.
By default, Redis provides 16 databases, numbered from 0 to 15. You can switch between these databases using the SELECT
command. This feature helps organize data and isolate different applications or use cases within the same Redis instance, though it's important to note that all databases share the same memory space and configuration settings.
Infinispan, on the other hand, employs a distributed cache model where data is spread across multiple nodes. It doesn't use the concept of separate databases; instead, it organizes data using caches, which can be configured with different settings and partitioned across a cluster. Data is distributed and replicated across multiple nodes, offering high availability and scalability. There isn’t a direct equivalent to Redis’s databases, but data can be segmented using different caches and configurations.
Here is a table that resumes the main differences between Redis Databases and Infinispan caches:
If the Infinispan connector uses a single cache named "respCache" by default, you can support multiple Redis databases — by using cache aliases.
Cache Aliases
In Infinispan, cache aliases are alternative names you can assign to a cache. They allow you to refer to the same underlying cache configuration using different names. Cache aliases in Infinispan allow for efficient switching between different versions or states of cached data, without having to modify or reload your application logic. This makes cache aliases especially useful in scenarios where data needs to be updated, but you want to ensure high availability and minimal impact on application performance.
Use Cases for Cache Aliases
Cache aliases in Infinispan are great for managing changing data without disrupting your application. It allows you to switch between data snapshots easily. You can keep using an old data version while loading a new one. When the new data is ready, you just switch the alias to point to it, without downtime. There is better performance and high availability since your app doesn’t touch the cache that’s being updated, it runs smoothly without slowdowns or errors. If something goes wrong, you can quickly rollback and switch to the previous data version with the alias.
For example, imagine an online shop that needs to update its catalog:
- The shop keeps showing products using the current data (
current_catalog
pointing tocatalog_snapshot_1
). - While customers browse, new product data is loaded into
catalog_snapshot_2
in the background. - Once
catalog_snapshot_2
is fully updated, the alias (current_catalog
) is switched to point tocatalog_snapshot_2
. - The old
catalog_snapshot_1
cache is now free to be cleared and used for the next update.
The website updates its catalog data without causing big delays or downtime for users.
Creating a Cache With an Alias
Before learning how to use cache aliases for the RESP protocol and multiple databases, let’s first learn how to create and update cache aliases. There are several ways to create a cache or cache configuration in Infinispan, but my favorite is using the Infinispan Server Console.
Run the Infinispan Server and access the Console as explained in the "Getting Started tutorial."
To create a cache, use the cache creation wizard by clicking the "Create Cache" button.
In the cache tuning step, you'll find the "Aliases" option, where you can add as many aliases as you want.
In the final step, you'll be able to review the configuration in JSON, XML, or YAML formats.
When you create a cache with aliases, the list will show the cache's aliases. You can filter caches by name or alias using the “search by” field.
Adding an Alias at Runtime
For existing caches, good news! The aliases attribute in a cache configuration can be changed at runtime. You can do this in several ways:
- Using the administration API in Hotrod
- Using the Infinispan Server Command Line Interface (CLI)
- Using the Server Console or REST API
To perform this operation, you need ADMIN access in Infinispan.
Using the Hotrod Client
To modify an alias at runtime, use the administration API. Below is an example for client/server mode. If you're using Infinispan Embedded in your application, a similar API is available.
RemoteCacheManager remoteCacheManager = // created or injected if using Quarkus or Spring Boot
remoteCacheManager.administration().updateConfigurationAttribute("myCache", "aliases", "alias alias2");
RemoteCache<String, String> cacheFromAlias = cacheManager.getCache("alias");
Using the Command Line Tool
The Command Line Tool (CLI) of Infinispan provides a way to change cache aliases at runtime.
First, run the CLI with the following command:
podman or docker run -it --net=host infinispan/cli
From the command line, connect to the running server:
[disconnected]> connect
Username: admin
Password: ********
[6b0130c153e3-50183@cluster//containers/default]>
Then, use the alter cache
command to update the aliases
attribute:
alter cache myCache2 --attribute=aliases --value=current_catalog
Finally, describe the configuration of the cache and verify the change:
[6b0130c153e3-50183@cluster//containers/default]> describe caches/myCache2
{
"myCache2" : {
"distributed-cache" : {
"aliases" : [ "current_catalog" ],
"owners" : "2",
"mode" : "SYNC",
"statistics" : true,
"encoding" : {
"media-type" : "application/x-protostream"
}
}
}
}
Tip: Use the help
command.
[6b0130c153e3-50183@cluster//containers/default]> alter cache -h
Usage: alter cache [<options>] <name>
Alters a cache configuration
Options:
--attribute The configuration attribute
--value The value for the configuration attribute. If the attribute supports multiple values, separate them with commas
-f, --file
-h, --help
Argument:
The cache name
Using the Server Console
From the list of caches, select the Edit aliases action.
A modal dialog will open. You can add or remove aliases from there.
Supporting Multiple Databases
Let’s try selecting databases 0
and 1
using the Redis CLI. To switch databases in Redis, use the SELECT
command followed by the database number. Let's try over Infinispan again.
First, use SELECT 0
to start in database 0
. Then, use SELECT 1
to switch to database 1
.
> redis-cli --user admin --pass password
127.0.0.1:11222[1]> select 0
OK
127.0.0.1:11222[1]> select 1
(error) ERR DB index is out of range
Database 0
works, but database 1
does not. On closer inspection of the respCache
configuration, we see the default respCache
with alias "0"
is defined.
To select database "1" you need to create a new cache. Let's use the Infinispan Console again to do that. Go to the cache creation wizard and choose the "add cache configuration" option this time.
Choose the RESP.DIST template and create the cache. This template is specifically designed for RESP caches.
Finally, add alias "1" to the new cache as described in the section on adding an alias at runtime. Alternatively, you can copy and paste the configuration from respCache
changing the alias 0
to alias 1
.
Now that we have a cache with alias 1
, we can select and add the following data:
> redis-cli --user admin --pass password
127.0.0.1:11222[1]> select 0
OK
127.0.0.1:11222[1]> select 1
OK
127.0.0.1:11222[1]> set hello word
OK
It is important to highlight that, unlike Redis Databases, each cache can be set up differently based on your application's needs. This lets you take advantage of Infinispan's flexible configuration (for example, you can add backups using Cross-Site Replication for some “databases” and not all of them) while still keeping the simplicity of using your Redis client in your app.
Conclusion
In this tutorial, you’ve learned how to use multiple databases with the RESP protocol and how to use Infinispan caches as a replacement for Redis databases. By using different caches instead of Redis databases, you gain several advantages, as discussed. You can now approach your data needs in a more flexible and effective way, tailored to your specific scenarios. You have also learned what cache aliases are and how helpful they can be in different situations, not just Redis databases.
Published at DZone with permission of Katia Aresti. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments