Skip to content

Commit

Permalink
feat: updated documentation with a new way to configure redis connect…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
h1k3r committed Oct 9, 2024
1 parent d212614 commit 2dedd22
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 32 deletions.
19 changes: 19 additions & 0 deletions guides/hosting/infrastructure/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,29 @@ 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

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'

Check warning on line 62 in guides/hosting/infrastructure/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/hosting/infrastructure/redis.md#L62

Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/hosting/infrastructure/redis.md:62:21: Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION
```
Connection names should reflect the actual connection purpose/type and be unique. Also the names are used as a part of service names in the container, so they should follow the service naming conventions. After defining connections, you can reference them by name in configuration of different subsystems.

Check warning on line 65 in guides/hosting/infrastructure/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/hosting/infrastructure/redis.md#L65

A comma may be missing after the conjunctive/linking adverb ‘Also’. (SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA[1]) Suggestions: `Also,` URL: https://languagetool.org/insights/post/linking-words/ Rule: https://community.languagetool.org/rule/show/SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA?lang=en-US&subId=1 Category: PUNCTUATION
Raw output
guides/hosting/infrastructure/redis.md:65:82: A comma may be missing after the conjunctive/linking adverb ‘Also’. (SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA[1])
 Suggestions: `Also,`
 URL: https://languagetool.org/insights/post/linking-words/ 
 Rule: https://community.languagetool.org/rule/show/SENT_START_CONJUNCTIVE_LINKING_ADVERB_COMMA?lang=en-US&subId=1
 Category: PUNCTUATION
<PageRef page="../performance/cart-storage" />
<PageRef page="../performance/number-ranges" />
<PageRef page="../performance/lock-store" />
<PageRef page="../performance/increment" />
<PageRef page="../performance/performance-tweaks#delayed-invalidation" />

Check warning on line 75 in guides/hosting/infrastructure/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/hosting/infrastructure/redis.md#L75

Unpaired symbol: ‘"’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/hosting/infrastructure/redis.md:75:14: Unpaired symbol: ‘"’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION

Check warning on line 75 in guides/hosting/infrastructure/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/hosting/infrastructure/redis.md#L75

Unpaired symbol: ‘"’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/hosting/infrastructure/redis.md:75:69: Unpaired symbol: ‘"’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION
22 changes: 22 additions & 0 deletions guides/hosting/performance/cart-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

<Tabs>
<Tab title="Before v.6.6.7">

```yaml
shopware:
cart:
redis_url: 'redis://host:port/dbindex?persistent=1'
```
</Tab>
<Tab title="Since v.6.6.7">
```yaml
shopware:
redis:
connections:
persistent:
dsn: 'redis://host:port/dbindex?persistent=1'

Check warning on line 36 in guides/hosting/performance/cart-storage.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/hosting/performance/cart-storage.md#L36

Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/hosting/performance/cart-storage.md:36:21: Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION
cart:
storage:
type: 'redis'
config:
connection: 'persistent'
```
</Tab>
</Tabs>
It is recommended to use a persistent Redis connection to avoid connection issues in high-load scenarios.
## Migrating between storages
Expand Down
31 changes: 29 additions & 2 deletions guides/hosting/performance/increment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<Tabs>
<Tab title="Before v.6.6.7">

```yaml
shopware:
increment:
Expand All @@ -33,6 +36,30 @@ shopware:
config:
url: 'redis://host:port/dbindex'
```
</Tab>
<Tab title="Since v.6.6.7">
```yaml
shopware:
redis:
connections:
persistent:
dsn: 'redis://host:port/dbindex?persistent=1'

Check warning on line 48 in guides/hosting/performance/increment.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/hosting/performance/increment.md#L48

Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/hosting/performance/increment.md:48:21: Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION

increment:
user_activity:
type: 'redis'
config:
connection: 'persistent'

message_queue:
type: 'redis'
config:
connection: 'persistent'
```
</Tab>
</Tabs>
### Redis configuration
Expand All @@ -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'
```
26 changes: 23 additions & 3 deletions guides/hosting/performance/number-ranges.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

<Tabs>
<Tab title="Before v.6.6.7">

```yaml
shopware:
number_range:
increment_storage: "Redis"
redis_url: 'redis://host:port/dbindex'
```
</Tab>
<Tab title="Since v.6.6.7">
```yaml
shopware:
number_range:
increment_storage: "Redis"
redis_url: 'redis://host:port/dbindex'
redis:
connections:
persistent:
dsn: 'redis://host:port/dbindex?persistent=1'

Check warning on line 39 in guides/hosting/performance/number-ranges.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/hosting/performance/number-ranges.md#L39

Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES) URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US Category: PUNCTUATION
Raw output
guides/hosting/performance/number-ranges.md:39:21: Unpaired symbol: ‘'’ seems to be missing (EN_UNPAIRED_QUOTES)
 URL: https://languagetool.org/insights/post/punctuation-guide/#what-are-parentheses 
 Rule: https://community.languagetool.org/rule/show/EN_UNPAIRED_QUOTES?lang=en-US
 Category: PUNCTUATION
number_range:
increment_storage: 'redis'
config:
connection: 'persistent'
```
</Tab>
</Tabs>
### Redis configuration
Expand Down
30 changes: 3 additions & 27 deletions guides/hosting/performance/performance-tweaks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
77 changes: 77 additions & 0 deletions guides/plugins/plugins/redis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
nav:
title: Redis
position: 45

---

# Redis

Starting with Shopware v6.6.7, 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

Check warning on line 16 in guides/plugins/plugins/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/redis.md#L16

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `XML` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/redis.md:16:7: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `XML`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
<service id="MyCustomService">
<argument type="service" id="Shopware\Core\Framework\Adapter\Redis\RedisConnectionProvider" />
<argument>%myservice.redis_connection_name%</argument>
</service>
```

```php

Check warning on line 23 in guides/plugins/plugins/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/redis.md#L23

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/redis.md:23:7: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
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

Check warning on line 43 in guides/plugins/plugins/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/redis.md#L43

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `XML` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/redis.md:43:7: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `XML`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
<service id="my.custom.redis_connection" class="Redis">
<factory service="Shopware\Core\Framework\Adapter\Redis\RedisConnectionProvider" method="getConnection" />
<argument>%myservice.redis_connection_name%</argument>
</service>

<service id="MyCustomService">
<argument type="service" id="my.custom.redis_connection" />
</service>
```

```php

Check warning on line 54 in guides/plugins/plugins/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/redis.md#L54

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/redis.md:54:7: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
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

Check warning on line 70 in guides/plugins/plugins/redis.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/redis.md#L70

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `XML` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/redis.md:70:7: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `XML`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
<service id="MyCustomService">
<argument type="service" id="shopware.redis.connection.connection_name" />
</service>
```
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.

0 comments on commit 2dedd22

Please sign in to comment.