Shared services
Each workspace runs a separate copy of each service. Some services are heavy or slow to start, and they use nothing that is different in each workspace. For these services, you can run one shared instance. All workspaces that select it use that one instance. This guide tells how to share a service, how to see the instances, and how to remove one.
When you can share a service
Section titled “When you can share a service”A shared instance serves more than one workspace. It must use nothing that is different in each workspace. The author of the template decides this, and marks the service in the template:
services: customer: repo: customer-service port: 8080 shareable: true env: DATABASE_URL: ${customer-db.url}| Field | Effect |
|---|---|
shareable: true |
A workspace can share the service when you select it with --share. |
shared: true |
A workspace shares the service by default. This value implies shareable: true. |
lo new refuses to share a service in these cases:
- The template does not mark the service
shareableorshared. - The service has an environment value with a reference such as
${workspace.slug}, which is different in each workspace. - The service refers to a service that is not shared.
- The service uses a resource or a volume that a different service uses too.
A resource that only the shared service uses belongs to the shared instance. For example, the database customer-db above is part of the shared instance of customer. The workspaces do not get a separate copy of it.
Share a service
Section titled “Share a service”-
Select the service when you make the workspace:
Terminal window lo new shop-a --template shopping --share customer -
Run
lo up shop-a.lo upalso starts the shared instance. -
Run
lo new shop-b --template shopping --share customer, thenlo up shop-b. The workspaceshop-buses the same instance asshop-a.
The services of the workspace use the shared service with their usual references, for example ${customer.internal_url}. If the service is exposed, its public URL has the reserved workspace name shared, for example https://customer-main.shared.lo. The first part of the host name has the service and the ref.
An instance has a template, a service and a ref. The ref is the branch or commit of the source that the service runs. For a service with an image, the image is the ref. Workspaces of one template that use the same service at the same ref use one instance. A workspace with a different ref makes a different instance.
Run a separate copy of a shared service
Section titled “Run a separate copy of a shared service”If the template marks a service shared: true, each new workspace uses the shared instance. To run a separate copy in one workspace, use one of these options:
- Add
--own <service>tolo new. - Name the service, or its repository, with a ref, for example
lo new shop-c --template shopping customer=feature/x.
You cannot name the same service in --share and --own.
See and remove the instances
Section titled “See and remove the instances”Run lo shared ls to list the instances. The table shows the instance as <service>@<ref>, its template, its status and the workspaces that use it.
lo shared lslo down stops the shared instances that no other running workspace uses. lo rm does the same. The instance and its data stay on the machine. The next lo up of a workspace that uses it starts it again.
To delete an instance, remove the workspaces that use it. Then run lo shared rm <service>@<ref>.
lo shared rm stops the instance and drops its resources. It refuses while a workspace uses the instance. It also refuses if the worktrees of the instance contain unsaved work.