On this page
Developing ten SmartDoctor products meant working on the costs hidden behind ordinary-looking endpoints. A staff lookup could pull image data into memory. An async task could lose the context that selected its hospital. A waiting-room connection could leave a listener registered after the display disconnected.
Three implementations capture that work particularly well. Each gives a resource an explicit boundary: when to load it, where it belongs, and when to release it.
The Kotlin examples use neutral names and a simplified schema. Imports, unrelated fields and application wiring are omitted; the examples retain the behavior relevant to each decision.
Load the image when the caller asks for the image
Hospital tables mixed small metadata columns with large binary values. Staff pictures lived alongside staff details, and consent-form templates had a similar arrangement.
I separated those image columns into dedicated entity mappings. Both mappings address the same row, but the ordinary metadata entity omits the picture. This illustrative schema shows the split:
@Embeddable
data class ProfileKey(
var organization: String = "",
var person: String = "",
) : Serializable
@Entity
@Table(name = "profiles")
class ProfileMetadata(
@EmbeddedId var id: ProfileKey = ProfileKey(),
var displayName: String = "",
var changedOn: LocalDate = LocalDate.MIN,
var changedAt: LocalTime = LocalTime.MIN,
)
@Entity
@Table(name = "profiles")
class ProfilePicture(
@EmbeddedId var id: ProfileKey = ProfileKey(),
@Column(name = "picture") var content: ByteArray? = null,
)
The ordinary entity read has no mapped picture column to hydrate. The image-only entity is used when a caller explicitly requests the bytes. This is a targeted split: the full staff metadata model still contained a separate signature field, so it would be inaccurate to call all metadata access binary-free.
The image service’s cache key includes the organization, record identity and metadata modification timestamp. Spring’s default key includes every method argument, even though the database lookup only needs the identity:
@Service
class Pictures(private val images: PictureRepository) {
@Cacheable(cacheManager = "rawImages", cacheNames = ["portraits"])
fun bytes(
organization: String,
person: String,
revision: LocalDateTime,
): ByteArray? = images.findById(ProfileKey(organization, person))
.orElseThrow()
.content
}
val revision = LocalDateTime.of(metadata.changedOn, metadata.changedAt)
val bytes = pictures.bytes(organization, person, revision)
The caller obtains the revision from metadata before calling the cached service through its Spring proxy. A changed timestamp produces a new key. The byte-cache manager stores the value with a raw byte-array serializer:
val defaults = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
RedisSerializer.byteArray()
)
)
| Request | Relevant path |
|---|---|
| Load staff metadata | Read the ordinary entity without its picture column |
| Fetch a staff picture | Read the modification timestamp, then retrieve cached or stored bytes |
| Fetch a consent-form template | Reuse the base image through the image cache |
| Render a personalized consent form | Retrieve the reusable base, then apply customer-specific text |
For consent forms, the shared cached value is the template. Patient-specific rendering happens afterward, keeping one patient’s personalized output out of that reusable cache.
This byte-cache configuration has no explicit TTL. Revision-based keys need an expiry or cleanup policy to bound Redis storage over time; changing the key alone does not provide one. This image path is separate from the reservation API’s timing result.
Carry context across the async boundary—and clear it
Async work uses both custom request context and Spring Security context. The custom context holds request identity, user, platform and IP address. Hospital datasource selection derives hospital identity from the authenticated principal.
A thread-local value on the request thread does not automatically appear on a reusable worker. I captured the custom context when work was submitted, installed it on the worker and removed it in a finally block:
class ContextExecutor(
private val delegate: TaskExecutor,
private val local: ThreadLocal<CallContext>,
) : AsyncTaskExecutor {
override fun execute(task: Runnable) {
val captured = local.get()
delegate.execute {
if (captured != null) local.set(captured)
try {
task.run()
} finally {
local.remove()
}
}
}
}
pool.setTaskDecorator { task ->
ContextSnapshot.captureAll().wrap(task)
}
val executor = DelegatingSecurityContextAsyncTaskExecutor(
ContextExecutor(pool, requestLocal)
)
The custom wrapper carries request context. The outer Spring Security wrapper carries the authenticated context, while the task decorator captures the available Micrometer context snapshot. Cleanup still runs when a task throws, which matters when a later request reuses the worker.
The reservation flow uses this configured executor for automatic no-show and completion reconciliation. The implementation concern is context lifetime, not simply adding an async annotation to make the request return sooner.
Capacity remains a separate tradeoff. The executor’s queue was configured with the maximum integer capacity. Its maximum thread count therefore does not bound queued work or queued memory. Context propagation solves one correctness problem; backpressure needs its own design.
Make a live subscription own its listener
Smart Waiting displays could connect to different server instances. Clinical updates are published to a Redis channel keyed by hospital. Each instance forwards received messages to its local GraphQL/Flux subscribers.
Trying Streams, then returning to Pub/Sub
The first Pub/Sub attempt did not work as expected. I tried Redis Streams, then returned to Pub/Sub and completed the revised implementation. The exact failure behind that experiment is not preserved; the tradeoff here is an architectural reading of the final code.
A waiting-room update belongs on every server with a connected display for that hospital. Redis Streams can support this: independent XREAD consumers can each receive the entries, but must manage their read positions. A shared XREADGROUP instead divides messages among its consumers; it does not by itself broadcast every update to every application instance. Those are different consumption models, not a general inability of Streams to broadcast. See the Redis documentation for XREAD and consumer groups.
The final design uses hospital-keyed Pub/Sub for live fan-out and handles the local connection lifecycle explicitly. It avoids adding retained history and replay management to this notification path. The difficult boundary remains at each server: the last display can disconnect while another connects, so listener removal must be coordinated with registration.
Coordinate registration with the last disconnect
The subscriber registry makes the Redis listener’s lifetime follow the local connections. The listener in this excerpt is the existing message handler; it deserializes a received update and forwards it to that hospital’s local sinks:
private val sinks =
ConcurrentHashMap<String, CopyOnWriteArrayList<FluxSink<Update>>>()
private val topics = ConcurrentHashMap<String, ChannelTopic>()
fun stream(hospital: String): Flux<Update> = Flux.create { sink ->
synchronized(sinks) {
topics.computeIfAbsent(hospital) {
ChannelTopic(it).also { topic ->
listeners.addMessageListener(listener, topic)
}
}
sinks.computeIfAbsent(hospital) {
CopyOnWriteArrayList()
}.add(sink)
}
sink.onDispose {
synchronized(sinks) {
val active = sinks[hospital]
active?.remove(sink)
if (active.isNullOrEmpty()) {
sinks.remove(hospital)
topics.remove(hospital)?.let { topic ->
listeners.removeMessageListener(listener, topic)
}
}
}
}
}
| Event | Local state transition |
|---|---|
| First subscriber for a hospital | Register the Redis topic/listener and add the Flux sink |
| Additional subscriber | Reuse the topic and add another local sink |
| Redis message arrives | Deserialize once for that listener and send to the local sinks |
| A connection is disposed | Remove its sink |
| Last local sink is removed | Remove the topic entry and detach the Redis listener |
Registration and disposal use the same synchronization boundary. Concurrent maps and copy-on-write lists support access to the registry and subscriber lists, while the disposal callback ties cleanup to the actual connection. A hospital that no longer has a connected display does not keep its local Redis listener registered indefinitely.
This is Redis Pub/Sub delivery to currently connected consumers. It does not provide replay, durable delivery or exactly-once handling. The stream creation also does not define a custom bounded slow-consumer policy. Listener cleanup addresses resource lifetime without solving every streaming failure mode.
What these choices demonstrate
These are small pieces of code with consequences beyond their size: database-to-JVM transfer, identity on pooled threads, and the lifetime of a cross-instance subscription. They required following a feature across persistence, cache configuration, executors and connection disposal.
The same approach informed the selected-date reservation API, where I combined projected reads, keyed assembly and database-driven invalidation. The common thread is deciding which work must happen for this request, which work can be reused, and what remains after the request finishes.