|
| 1 | += Best Practices |
| 2 | +:description: Speed up your application development, with some best practices for using Couchbase SDKs. |
| 3 | +:page-toclevels: 2 |
| 4 | +:page-aliases: concept-docs:errors.adoc,concept-docs:rbac.adoc |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +// Note to editors |
| 9 | +// |
| 10 | +// This page pulls in content from -sdk-common- |
| 11 | +// and code samples from -example-dir- |
| 12 | +// |
| 13 | +// It can be seen built at wwww. |
| 14 | + |
| 15 | + |
| 16 | +[abstract] |
| 17 | +{description} |
| 18 | + |
| 19 | + |
| 20 | +// see .NET doc on this |
| 21 | + |
| 22 | + |
| 23 | +From batching and reactive APIs, to unit tests and handling errors. |
| 24 | +There's plenty that can be done to remove bottlenecks in development and in performance, and this page can be your checklist of areas not to neglect as you develop your app. |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +== Security |
| 29 | + |
| 30 | +But before worrying about bottlenecks, let's put security concerns first. |
| 31 | + |
| 32 | +Security is a process, and not just a set of checkboxes -- but there are key areas you must check off as you move from development, through testing, to deploying in production. |
| 33 | +// |
| 34 | + |
| 35 | +=== Roles and RBAC |
| 36 | + |
| 37 | +Self-managed Couchbase Server uses Role-Based Access Control (RBAC), which is reflected in our managed service (Capella) as a nuanced set of Roles. |
| 38 | +Data security depends in part on only giving access to the minimum amount of data and permissions required to do the job. |
| 39 | +// |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +// concept snippet |
| 44 | + |
| 45 | +// include::{version-common}@sdk:pages:partial$rbac.adoc[] |
| 46 | + |
| 47 | +RBAC restrict resources on a Couchbase cluster to an identified user, allocated by role. |
| 48 | + |
| 49 | +*TODO* update and add Capella here |
| 50 | + |
| 51 | + |
| 52 | +==== Users, Resources, Roles, and Privileges |
| 53 | + |
| 54 | +Couchbase Server Enterprise Edition uses _Role-Based Access Control_ for applications to restrict _resources_ on a Couchbase cluster to an identified _user_. |
| 55 | + |
| 56 | +Each user who attempts resource-access is identified by means of the _credentials_ they pass to Couchbase Server, for purposes of _authentication_: these consist of a _username_ and (typically) a _password_. |
| 57 | +Once the user is authenticated, an _authorization_ process checks the _roles_ with which the user is associated. |
| 58 | +If one or more of these roles correspond to _privileges_ that permit the user-requested level of resource-access, access is duly granted; otherwise, it is denied. |
| 59 | + |
| 60 | +Users who have been assigned the *Admin* role for the cluster are able to create, edit, and remove users. |
| 61 | +The SDK provides APIs to support these activities. |
| 62 | + |
| 63 | +NOTE: Introductory examples in the SDK documentation use the _Administrator_ user to ensure that developers can quickly get up and running; this _should not be used in production_. |
| 64 | +Elsewhere we use a general "user" which represents whichever permission levels are appropriate to your application. |
| 65 | + |
| 66 | + |
| 67 | +== Performance |
| 68 | + |
| 69 | +Couchbase's Data Service uses a fast binary protocol, which will always outperform JSON streamed over HTTP from {sqlpp} queries. |
| 70 | +If you know the key (ID) of a document, then use the xref:howtos:kv-operations.adoc[Data Service]. |
| 71 | + |
| 72 | +If you need pessimistic logging, in particular if you need to lock documents for multi-document ACID transactions, then anything you can do at the schema level to reduce the number of documents locked simultaneously wil remvoe a bottleneck to updating the affected documents. |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +=== Dealing with Timeout Errors |
| 77 | + |
| 78 | + |
| 79 | +// see also |
| 80 | +// https://couchbasecloud.atlassian.net/wiki/spaces/CST/pages/1993769712/Java+-+Debugging+KV+Timeouts |
| 81 | +// in Support wiki |
| 82 | + |
| 83 | + |
| 84 | +* `waitUntilReady` should be the default setting for `Cluster.connect` and `Cluster.bucket` in most cases, so that resources are fully loaded before the client proceeds with CRUD calls to the cluster. |
| 85 | +* LAN-type connection of client and server is recommended in production, but WAN development is a reality pre-production. |
| 86 | +Ensure that you're familiar with the |
| 87 | +xref:ref:client-settings.adoc#constrained-network-environments[best timeout options for WAN environments], |
| 88 | +or at least set a xref:ref:client-settings.adoc#wan-development[WAN development Configuration Profile]. |
| 89 | + |
| 90 | + |
| 91 | +// write after more LZA added |
| 92 | +//// |
| 93 | +=== Read From Replica |
| 94 | +
|
| 95 | +Advanced zone aware.... |
| 96 | +//// |
| 97 | + |
| 98 | + |
| 99 | +=== Concurrency and Async APIs |
| 100 | + |
| 101 | +// ....this paragraph different for each SDK. |
| 102 | + |
| 103 | +Choosing between the blocking, asynchronous, and reactive APIs for the Scala SDK is partly bound up with how (and where) you want to handle exceptions. |
| 104 | + |
| 105 | +* Synchronous operations are blocking, and return a Scala `Try` object. |
| 106 | +This contains either the result or a _Throwable_ exception, |
| 107 | +which can be pattern matched over (using `flatMap` in more complex cases). |
| 108 | +* The asynchronous API returns Scala `Future`, representing the execution of an asynchronous task and the promise of a future result. |
| 109 | +An `ExecutionContext` must be provided, to give a thread pool for handling whatever is returned. |
| 110 | +* The reactive API is a more natural fit for network-aware, fault tolerant programs, |
| 111 | +and will provide full back pressure for streaming results from large {sqlpp} or Search queries. |
| 112 | + |
| 113 | +See the xref:howtos:concurrent-async-apis.adoc[Async & Reactive APIs page] for further discussion and practical examples. |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +== Error Handling |
| 118 | + |
| 119 | +Best practices for error handling in Scala depend somewhat upon your choice of API: |
| 120 | +blocking, asynchronous, or reactive, as covered in the xref:concurrent-async-apis.adoc[async and reactive API guide]. |
| 121 | +That guide also covers how errors are actually returned (e.g. via `Try`, `Future`, or `Mono`) and handled. |
| 122 | +See also the xref:howtos:error-handling.adoc[error handling guide], |
| 123 | +which covers specific errors, along with a broader look at error handling strategies. |
| 124 | + |
| 125 | + |
| 126 | +== Testing |
| 127 | + |
| 128 | +Integrate developing with the {name-sdk} into your accustomed test framework. |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +//// |
| 134 | +== General Approach to Scala Exceptions |
| 135 | +
|
| 136 | + When the unexpected happens, take a step-by-step approach. |
| 137 | +
|
| 138 | +include::{version-common}@sdk:shared:partial$errors.adoc[tag=exception] |
| 139 | +
|
| 140 | +// include::{version-common}@sdk:shared:partial$errors.adoc[tag=ref] |
| 141 | +
|
| 142 | +include::{version-common}@sdk:shared:partial$errors.adoc[tag=durability] |
| 143 | +
|
| 144 | +include::{version-common}@sdk:shared:partial$errors.adoc[tag=diag] |
| 145 | +//// |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +// Slow Operations Logging |
| 151 | +include::{version-common}@sdk:shared:partial$errors.adoc[tag=observability] |
| 152 | + |
| 153 | +// until opentelemetry release for link below, could add note on API to expose own tracing features? |
| 154 | +// include::{version-common}@sdk:shared:partial$errors.adoc[tag=rto] |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +// Missing APIs |
| 161 | +// For anything that is not covered by the SDK, |
| 162 | +// for example |
| 163 | +// a raw FTS query from the SDK... |
| 164 | +// use the HTTP client: |
| 165 | + |
| 166 | +//// |
| 167 | +public class CouchbaseHttpClient |
| 168 | +extends Object |
| 169 | +A specialized HTTP client for making requests to Couchbase Server. |
| 170 | +Get an instance by calling Cluster.httpClient(). |
| 171 | +
|
| 172 | +Example usage: |
| 173 | +
|
| 174 | + public static String getAutoFailoverSettings(Cluster cluster) { |
| 175 | + HttpResponse response = cluster.httpClient().get( |
| 176 | + HttpTarget.manager(), |
| 177 | + HttpPath.of("/settings/autoFailover")); |
| 178 | +
|
| 179 | + if (!response.success()) { |
| 180 | + throw new RuntimeException( |
| 181 | + "Failed to get auto-failover settings. HTTP status code " + |
| 182 | + response.statusCode() + "; " + response.contentAsString()); |
| 183 | + } |
| 184 | +
|
| 185 | + return response.contentAsString(); |
| 186 | + } |
| 187 | +
|
| 188 | + public static void disableAutoFailover(Cluster cluster) { |
| 189 | + HttpResponse response = cluster.httpClient().post( |
| 190 | + HttpTarget.manager(), |
| 191 | + HttpPath.of("/settings/autoFailover"), |
| 192 | + HttpPostOptions.httpPostOptions() |
| 193 | + .body(HttpBody.form(Map.of("enabled", "false")))); |
| 194 | +
|
| 195 | + if (!response.success()) { |
| 196 | + throw new RuntimeException( |
| 197 | + "Failed to disable auto-failover. HTTP status code " + |
| 198 | + response.statusCode() + "; " + response.contentAsString()); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +//// |
| 203 | +// https://docs.couchbase.com/sdk-api/couchbase-java-client/com/couchbase/client/java/http/CouchbaseHttpClient.html |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | +== Additional Information |
| 209 | + |
| 210 | +SDKs are client to Couchbase Server -- whether Capella Database-As-A-Service, or self-managed -- and in some areas it would be wise to take a fully rounded approach. |
| 211 | +Read up on security and performance considerations relevant to your use case. |
| 212 | + |
| 213 | +=== Couchbase Security Best Practices |
| 214 | + |
| 215 | +* xref:cloud:security:security.adoc[Security Best Practices in Capella] |
| 216 | +* xref:server:learn:security/security-overview.adoc[Security for self-managed Couchbase Server] |
| 217 | + |
| 218 | +=== Role-Based Access Control |
| 219 | + |
| 220 | +*TODO* add Capella info |
| 221 | + |
| 222 | +All aspects of the Couchbase RBAC system are covered in the section xref:{version-server}@server:learn:security/authorization-overview.adoc[Authorization]. |
| 223 | +Specifically, for information on: |
| 224 | + |
| 225 | +* Adding _Users_ and assigning _roles_, by means of the Couchbase Web Console, see xref:{version-server}@server:manage:manage-security/manage-users-and-roles.adoc[Manage Users and Roles]. |
| 226 | +* _Roles_ required for resource-access, and the privileges they entail, see xref:{version-server}@server:learn:security/roles.adoc[Roles]. |
| 227 | +* _Resources_ controlled by Couchbase RBAC, see xref:{version-server}@server:learn:security/resources-under-access-control.adoc[Resources Under Access Control]. |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | +// other SDK's docs |
| 233 | +// |
| 234 | +// add in Python |
| 235 | +// https://docs.couchbase.com/sdk-api/couchbase-python-client/couchbase_api/parallelism.html |
| 236 | +// |
| 237 | +// |
0 commit comments