From 39dec124001bfe13712da8c4618e322e61d5deeb Mon Sep 17 00:00:00 2001 From: Andrii Havryliuk Date: Tue, 8 Oct 2024 16:45:11 +0200 Subject: [PATCH] feat: updated documentation with a new way to configure redis connections --- guides/hosting/infrastructure/redis.md | 19 +++++ guides/hosting/performance/cart-storage.md | 22 ++++++ guides/hosting/performance/increment.md | 31 +++++++- guides/hosting/performance/number-ranges.md | 26 ++++++- .../hosting/performance/performance-tweaks.md | 30 +------- guides/plugins/plugins/redis.md | 77 +++++++++++++++++++ 6 files changed, 173 insertions(+), 32 deletions(-) create mode 100644 guides/plugins/plugins/redis.md diff --git a/guides/hosting/infrastructure/redis.md b/guides/hosting/infrastructure/redis.md index 7deeb82aff..e5ff51cad0 100644 --- a/guides/hosting/infrastructure/redis.md +++ b/guides/hosting/infrastructure/redis.md @@ -47,6 +47,23 @@ As the data is critical, it is important to use a key eviction policy that will The cart, number range, lock store and increment data is what should be stored in this instance. +## Configuration + +Starting with v6.6.8.0 Shopware supports configuring different reusable Redis connections in the`config/packages/shopware.yaml` file under the `shopware` section: + +```yaml +shopware: + # ... + redis: + connections: + ephemeral: + dsn: 'redis://host1:port/dbindex' + persistent: + dsn: 'redis://host2:port/dbindex?persistent=1' +``` + +Connection names should reflect the actual connection purpose/type and be unique. Also, the names are used as part of the service names in the container, so they should follow the service naming conventions. After defining connections, you can reference them by name in the configuration of different subsystems. + @@ -54,3 +71,5 @@ The cart, number range, lock store and increment data is what should be stored i + + diff --git a/guides/hosting/performance/cart-storage.md b/guides/hosting/performance/cart-storage.md index 3ab93edd4b..6bb0bd6938 100644 --- a/guides/hosting/performance/cart-storage.md +++ b/guides/hosting/performance/cart-storage.md @@ -16,11 +16,33 @@ Redis is better suited in high-throughput scenarios, therefore you should use Re To use Redis, create a `config/packages/shopware.yml` file with the following content: + + + ```yaml shopware: cart: redis_url: 'redis://host:port/dbindex?persistent=1' ``` + + + + +```yaml +shopware: + redis: + connections: + persistent: + dsn: 'redis://host:port/dbindex?persistent=1' + cart: + storage: + type: 'redis' + config: + connection: 'persistent' +``` + + + It is recommended to use a persistent Redis connection to avoid connection issues in high-load scenarios. ## Migrating between storages diff --git a/guides/hosting/performance/increment.md b/guides/hosting/performance/increment.md index 56b94ddffa..06d5f6977c 100644 --- a/guides/hosting/performance/increment.md +++ b/guides/hosting/performance/increment.md @@ -20,6 +20,9 @@ Shopware uses the `increment` table to store such information by default. When m To use Redis, create a `config/packages/shopware.yml` file with the following content + + + ```yaml shopware: increment: @@ -33,6 +36,30 @@ shopware: config: url: 'redis://host:port/dbindex' ``` + + + + +```yaml +shopware: + redis: + connections: + persistent: + dsn: 'redis://host:port/dbindex?persistent=1' + + increment: + user_activity: + type: 'redis' + config: + connection: 'persistent' + + message_queue: + type: 'redis' + config: + connection: 'persistent' +``` + + ### Redis configuration @@ -50,8 +77,8 @@ To disable it, create a `config/packages/shopware.yml` file with the following c shopware: increment: user_activity: - type: 'array' + type: 'array' message_queue: - type: 'array' + type: 'array' ``` diff --git a/guides/hosting/performance/number-ranges.md b/guides/hosting/performance/number-ranges.md index 88d31558af..6faf44a185 100644 --- a/guides/hosting/performance/number-ranges.md +++ b/guides/hosting/performance/number-ranges.md @@ -18,12 +18,32 @@ Redis offers better support for atomic increments than the database. Therefore t To use Redis, create a `config/packages/shopware.yml` file with the following content: + + + +```yaml +shopware: + number_range: + increment_storage: "Redis" + redis_url: 'redis://host:port/dbindex' +``` + + + + ```yaml shopware: - number_range: - increment_storage: "Redis" - redis_url: 'redis://host:port/dbindex' + redis: + connections: + persistent: + dsn: 'redis://host:port/dbindex?persistent=1' + number_range: + increment_storage: 'redis' + config: + connection: 'persistent' ``` + + ### Redis configuration diff --git a/guides/hosting/performance/performance-tweaks.md b/guides/hosting/performance/performance-tweaks.md index 4daede194a..315eb17d5d 100644 --- a/guides/hosting/performance/performance-tweaks.md +++ b/guides/hosting/performance/performance-tweaks.md @@ -45,7 +45,7 @@ shopware: delay: 1 delay_options: storage: redis - dsn: 'redis://host:port/dbindex' + connection: 'ephemeral' # connection name from redis configuration ``` ## MySQL configuration @@ -92,23 +92,7 @@ If you ever wonder why it is in `prod`, take a look into the [Symfony configurat ## Increment storage The [Increment storage](../performance/increment) is used to store the state and display it in the Administration. -This storage increments or decrements a given key in a transaction-safe way, which causes locks upon the storage. Therefore, we recommend moving this source of server load to a separate Redis: - -```yaml -# config/packages/prod/shopware.yaml -shopware: - increment: - user_activity: - type: 'redis' - config: - url: 'redis://host:port/dbindex' - - message_queue: - type: 'redis' - config: - url: 'redis://host:port/dbindex' -``` - +This storage increments or decrements a given key in a transaction-safe way, which causes locks upon the storage. Therefore, we recommend moving this source of server load to a separate Redis, as described in [Increment storage Redis configuration](./increment#redis-configuration). If you don't need such functionality, it is highly recommended that you disable this behavior by using `array` as a type. ## Lock storage @@ -129,15 +113,7 @@ The generation of the number ranges is an **atomic** operation, which guarantees By default, the number range states are stored in the database. In scenarios where high throughput is required (e.g., thousands of orders per minute), the database can become a performance bottleneck because of the requirement for atomicity. -Redis offers better support for atomic increments than the database. Therefore, the number ranges should be stored in Redis in such scenarios. - -```yaml -# config/packages/prod/shopware.yaml -shopware: - number_range: - increment_storage: "Redis" - redis_url: 'redis://host:port/dbindex' -``` +Redis offers better support for atomic increments than the database. Therefore, the number ranges should be stored in Redis in such scenarios, see [Number Ranges - using Redis as a storage](./number-ranges#using-redis-as-storage). ## Sending mails with the Queue diff --git a/guides/plugins/plugins/redis.md b/guides/plugins/plugins/redis.md new file mode 100644 index 0000000000..1d421c8190 --- /dev/null +++ b/guides/plugins/plugins/redis.md @@ -0,0 +1,77 @@ +--- +nav: + title: Redis + position: 45 + +--- + +# Redis + +Starting with Shopware v6.6.8.0, Redis support has been improved, giving you more flexibility in how you use it in your projects and plugins. + +Once you've set up your Redis connections as explained in the [Redis configuration](../../hosting/infrastructure/redis) guide, you can access them in your code using the following methods: + +1. Inject `Shopware\Core\Framework\Adapter\Redis\RedisConnectionProvider` and retrieve connections by name: + + ```xml + + + %myservice.redis_connection_name% + + ``` + + ```php + class MyCustomService + { + public function __construct ( + private RedisConnectionProvider $redisConnectionProvider, + string $connectionName, + ) { } + + public function doSomething() + { + if ($this->redisConnectionProvider->hasConnection($this->connectionName)) { + $connection = $this->redisConnectionProvider->getConnection($this->connectionName); + // use connection + } + } + } + ``` + +2. Use `Shopware\Core\Framework\Adapter\Redis\RedisConnectionProvider` as factory to define custom services: + + ```xml + + + %myservice.redis_connection_name% + + + + + + ``` + + ```php + class MyCustomService + { + public function __construct ( + private Redis $redisConnection, + ) { } + + public function doSomething() + { + // use connection + } + } + ``` + This approach is especially useful when you want multiple services to share the same Redis connection. + +3. Inject connection directly by name: + ```xml + + + + ``` + Be cautious with this approach! If you change the Redis connection names in your configuration, it will cause container build errors. + +Keep in mind that Redis is an optional dependency in Shopware and might not be available in all installations.